-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
374545f
commit 8599066
Showing
1 changed file
with
2 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |