allow for arrays

This commit is contained in:
Timo Hocker 2020-05-09 18:06:13 +02:00
parent 432adc5b58
commit b9381c2824
3 changed files with 24 additions and 5 deletions

View File

@ -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<string, PersistentType> = {};
@ -24,7 +26,7 @@ export abstract class Persistent implements Assignable, Serializable {
}
public to_object (): Record<string, PersistentType> {
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;
}

View File

@ -18,5 +18,8 @@
"files": [
"/dist/",
"LICENSE"
]
],
"dependencies": {
"@sapphirecode/utilities": "^1.0.39"
}
}

View File

@ -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"