allow for arrays
This commit is contained in:
parent
432adc5b58
commit
b9381c2824
@ -1,7 +1,9 @@
|
|||||||
|
import { copy_object } from '@sapphirecode/utilities';
|
||||||
import { Assignable, Serializable } from './interfaces';
|
import { Assignable, Serializable } from './interfaces';
|
||||||
|
|
||||||
type PersistentTypeString = 'string'|'number'|'boolean';
|
type PersistentTypeString = 'string'|'number'|'boolean'|'array';
|
||||||
type PersistentType = string|number|boolean;
|
type PersistentPrimitive = string|number|boolean;
|
||||||
|
type PersistentType = PersistentPrimitive|PersistentPrimitive[];
|
||||||
|
|
||||||
export abstract class Persistent implements Assignable, Serializable {
|
export abstract class Persistent implements Assignable, Serializable {
|
||||||
private _data: Record<string, PersistentType> = {};
|
private _data: Record<string, PersistentType> = {};
|
||||||
@ -24,7 +26,7 @@ export abstract class Persistent implements Assignable, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public to_object (): Record<string, PersistentType> {
|
public to_object (): Record<string, PersistentType> {
|
||||||
return this._data;
|
return copy_object (this._data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public serialize (formatted = false): string {
|
public serialize (formatted = false): string {
|
||||||
@ -33,9 +35,18 @@ export abstract class Persistent implements Assignable, Serializable {
|
|||||||
return JSON.stringify (this.to_object (), null, 2);
|
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 {
|
public set (key: string, value: unknown): void {
|
||||||
const prop = this.properties[key];
|
const prop = this.properties[key];
|
||||||
if (typeof prop !== 'undefined' && typeof value === prop)
|
if (this.check_type (value, prop))
|
||||||
this._data[key] = value as PersistentType;
|
this._data[key] = value as PersistentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,5 +18,8 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"/dist/",
|
"/dist/",
|
||||||
"LICENSE"
|
"LICENSE"
|
||||||
]
|
],
|
||||||
|
"dependencies": {
|
||||||
|
"@sapphirecode/utilities": "^1.0.39"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,6 +64,11 @@
|
|||||||
eslint-plugin-node "^11.0.0"
|
eslint-plugin-node "^11.0.0"
|
||||||
eslint-plugin-sort-requires-by-path "^1.0.2"
|
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":
|
"@types/color-name@^1.1.1":
|
||||||
version "1.1.1"
|
version "1.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
|
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user