allow for arrays
This commit is contained in:
		@@ -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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user