Skip to content

Commit

Permalink
feat(typescript): cpt Serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
jinxin0112 committed Jan 21, 2020
1 parent 374545f commit 8599066
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions typescript/src/learnMixin.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
// ref: https://www.codewars.com/kata/597277844998c50c9a000051/train/typescript

export class Serializable {
stringfiyValue = (value: any): string => {
const t = typeof value;
if (t !== 'object' || t === null) {
return `"${String(value)}"`;
} else {
// todo 深度序列化
return '';
}
};

stringfiy = (pre: string, [key, value]: [string, string], index: number): string => {
const itemStr = `"${key}": ${this.stringfiyValue(value)}`;
pre += itemStr;
index === Object.keys(this).length - 1 && (pre += '}');
return pre;
};

serialize(): string {
return Object.entries(this).reduce(this.stringfiy, '{');
return JSON.stringify(this);
}

deserialize(source: string): void {
// TODO:
Object.assign(this, JSON.parse(source));
}
}

0 comments on commit 8599066

Please sign in to comment.