From b9381c2824ff66e96b09df1e4831e2241745948e Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Sat, 9 May 2020 18:06:13 +0200 Subject: [PATCH] allow for arrays --- lib/Persistent.ts | 19 +++++++++++++++---- package.json | 5 ++++- yarn.lock | 5 +++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/Persistent.ts b/lib/Persistent.ts index 2340b02..1afad0f 100644 --- a/lib/Persistent.ts +++ b/lib/Persistent.ts @@ -1,7 +1,9 @@ +import { copy_object } from '@sapphirecode/utilities'; import { Assignable, Serializable } from './interfaces'; -type PersistentTypeString = 'string'|'number'|'boolean'; -type PersistentType = string|number|boolean; +type PersistentTypeString = 'string'|'number'|'boolean'|'array'; +type PersistentPrimitive = string|number|boolean; +type PersistentType = PersistentPrimitive|PersistentPrimitive[]; export abstract class Persistent implements Assignable, Serializable { private _data: Record = {}; @@ -24,7 +26,7 @@ export abstract class Persistent implements Assignable, Serializable { } public to_object (): Record { - return this._data; + return copy_object (this._data); } public serialize (formatted = false): string { @@ -33,9 +35,18 @@ export abstract class Persistent implements Assignable, Serializable { return JSON.stringify (this.to_object (), null, 2); } + private check_type (value: unknown, prop: PersistentTypeString): boolean { + if (typeof prop === 'undefined') + return false; + if (prop === 'array') + return Array.isArray (value); + + return typeof value === prop; + } + public set (key: string, value: unknown): void { const prop = this.properties[key]; - if (typeof prop !== 'undefined' && typeof value === prop) + if (this.check_type (value, prop)) this._data[key] = value as PersistentType; } diff --git a/package.json b/package.json index 81f4b40..ae01bd0 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,8 @@ "files": [ "/dist/", "LICENSE" - ] + ], + "dependencies": { + "@sapphirecode/utilities": "^1.0.39" + } } diff --git a/yarn.lock b/yarn.lock index b6157d5..d900c52 100644 --- a/yarn.lock +++ b/yarn.lock @@ -64,6 +64,11 @@ eslint-plugin-node "^11.0.0" eslint-plugin-sort-requires-by-path "^1.0.2" +"@sapphirecode/utilities@^1.0.39": + version "1.0.39" + resolved "https://registry.yarnpkg.com/@sapphirecode/utilities/-/utilities-1.0.39.tgz#12fdc564065102ea156188541941f92fbb126238" + integrity sha512-8IgL9Cw43ipUOIqyqXvgmvyR/s+4Mf1h75jIBwOniNM3x7EU2jJiE01ePDrdsNraaGNn6o4GnFMx6ymp07U2SQ== + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"