From 84eea14a86265ce0585342d9f3a3c4107c02df17 Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Wed, 6 Feb 2019 01:43:09 -0500 Subject: Simplified function names in Document class --- src/fields/Document.ts | 67 +++++++++++++++++++++++++++-------------- src/fields/DocumentReference.ts | 2 +- 2 files changed, 46 insertions(+), 23 deletions(-) (limited to 'src/fields') diff --git a/src/fields/Document.ts b/src/fields/Document.ts index ef759615b..742149a03 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -10,9 +10,11 @@ export class Document extends Field { static _untitledDocName = ""; @computed - public get Title() { return this.GetFieldValue(KeyStore.Title, TextField, Document._untitledDocName); } + public get Title() { + return this.GetData(KeyStore.Title, TextField, Document._untitledDocName); + } - GetField(key: Key, ignoreProto: boolean = false): Opt { + Get(key: Key, ignoreProto: boolean = false): Opt { let field: Opt; if (ignoreProto) { if (this.fields.has(key)) { @@ -32,39 +34,39 @@ export class Document extends Field { return field; } - GetFieldT(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): Opt { - return Cast(this.GetField(key, ignoreProto), ctor); + GetT(key: Key, ctor: { new(...args: any[]): T }, ignoreProto: boolean = false): Opt { + return Cast(this.Get(key, ignoreProto), ctor); } - GetFieldOrCreate(key: Key, ctor: { new(): T }, ignoreProto: boolean = false): T { - const field = this.GetFieldT(key, ctor, ignoreProto); + GetOrCreate(key: Key, ctor: { new(): T }, ignoreProto: boolean = false): T { + const field = this.GetT(key, ctor, ignoreProto); if (field) { return field; } const newField = new ctor(); - this.SetField(key, newField); + this.Set(key, newField); return newField; } - GetFieldValue(key: Key, ctor: { new(): U }, defaultVal: T): T { - let val = this.GetField(key); + GetData(key: Key, ctor: { new(): U }, defaultVal: T): T { + let val = this.Get(key); let vval = (val && val instanceof ctor) ? val.Data : defaultVal; return vval; } - GetNumberField(key: Key, defaultVal: number): number { - return this.GetFieldValue(key, NumberField, defaultVal); + GetNumber(key: Key, defaultVal: number): number { + return this.GetData(key, NumberField, defaultVal); } - GetTextField(key: Key, defaultVal: string): string { - return this.GetFieldValue(key, TextField, defaultVal); + GetText(key: Key, defaultVal: string): string { + return this.GetData(key, TextField, defaultVal); } - GetListField(key: Key, defaultVal: T[]): T[] { - return this.GetFieldValue>(key, ListField, defaultVal) + GetList(key: Key, defaultVal: T[]): T[] { + return this.GetData>(key, ListField, defaultVal) } - SetField(key: Key, field: Field | undefined): void { + Set(key: Key, field: Field | undefined): void { if (field) { this.fields.set(key, field); } else { @@ -72,23 +74,44 @@ export class Document extends Field { } } - SetFieldValue(key: Key, value: any, ctor: { new(): T }): boolean { - let field = this.GetField(key, true); + SetData(key: Key, value: T, ctor: { new(): U }, replaceWrongType = true) { + let field = this.Get(key, true); + if (field instanceof ctor) { + field.Data = value; + } else if (!field || replaceWrongType) { + let newField = new ctor(); + newField.Data = value; + this.Set(key, newField); + } + } + + SetVal(key: Key, value: any, ctor: { new(): T }, replaceWrongType = true): boolean { + let field = this.Get(key, true); if (field != null) { return field.TrySetValue(value); - } else { + } else if (field && replaceWrongType) { field = new ctor(); if (field.TrySetValue(value)) { - this.SetField(key, field); + this.Set(key, field); return true; } else { return false; } + } else { + return false; } } + SetText(key: Key, value: string, replaceWrongType = true) { + this.SetData(key, value, TextField, replaceWrongType); + } + + SetNumber(key: Key, value: number, replaceWrongType = true) { + this.SetData(key, value, NumberField, replaceWrongType); + } + GetPrototype(): Opt { - return this.GetFieldT(KeyStore.Prototype, Document, true); + return this.GetT(KeyStore.Prototype, Document, true); } GetAllPrototypes(): Document[] { @@ -104,7 +127,7 @@ export class Document extends Field { MakeDelegate(): Document { let delegate = new Document(); - delegate.SetField(KeyStore.Prototype, this); + delegate.Set(KeyStore.Prototype, this); return delegate; } diff --git a/src/fields/DocumentReference.ts b/src/fields/DocumentReference.ts index f4f933848..10dac9f92 100644 --- a/src/fields/DocumentReference.ts +++ b/src/fields/DocumentReference.ts @@ -16,7 +16,7 @@ export class DocumentReference extends Field { } Dereference(): Opt { - return this.document.GetField(this.key); + return this.document.Get(this.key); } DereferenceToRoot(): Opt { -- cgit v1.2.3-70-g09d2