Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "types/document"

Index

Type aliases

Name

Name: "document"

Variables

Const DocumentType

DocumentType: DocumentTypeConstructor = <any> class<T> implements IoType<T>,DocumentIoTypeOptions<T> {readonly name: Name = name;readonly noExtraKeys?: boolean;readonly properties!: {readonly [P in keyof T]: PropertyDescriptor<T[P], any>};readonly rename?: {readonly [P in keyof T]?: string};readonly changeCase?: CaseStyle;private _options: Lazy<DocumentTypeOptions<T>>;private _outKeys: Map<string, keyof T> | undefined;constructor(options: Lazy<DocumentTypeOptions<T>>) {this._options = options;if (typeof options !== "function") {this._applyOptions();} else {lazyProperties(this, this._applyOptions, ["noExtraKeys", "properties", "changeCase", "rename" as keyof this]);}}/*** Map from serialized keys to the document keys*/get outKeys(): Map<string, keyof T> {if (this._outKeys === undefined) {this._outKeys = new Map();for (const key of Object.keys(this.properties) as (keyof T)[]) {this._outKeys.set(this.getOutKey(key), key);}}return this._outKeys;}getOutKey(key: keyof T): string {if (typeof key !== "string") {throw new Error(`NonStringKey: ${key}`);}const descriptor: PropertyDescriptor<any> = this.properties[key];if (descriptor.rename !== undefined) {return descriptor.rename;} else if (descriptor.changeCase !== undefined) {return rename(key as string, descriptor.changeCase);}if (this.rename !== undefined && this.rename[key] !== undefined) {return this.rename[key]!;} else if (this.changeCase !== undefined) {return rename(key as string, this.changeCase);}return key;}// TODO: Dynamically add with prototype?read<R>(reader: Reader<R>, raw: R): T {return reader.readDocument(raw, readVisitor({fromMap: <RK, RV>(input: Map<RK, RV>, keyReader: Reader<RK>, valueReader: Reader<RV>): T => {const extra: Set<string> = new Set();const missing: Set<string> = new Set();for (const key in this.properties) {const descriptor: PropertyDescriptor<any> = this.properties[key];if (!descriptor.optional) {missing.add(key);}}const invalid: Map<string, Error> = new Map();const result: Partial<T> = {}; // Object.create(null);for (const [rawKey, rawValue] of input) {const outKey: string = keyReader.readString(rawKey,readVisitor({fromString: (input: string): string => input}),);const key: keyof T | undefined = this.outKeys.get(outKey);if (key === undefined) {// Extra keyextra.add(outKey);continue;}missing.delete(key as string);const descriptor: PropertyDescriptor<any> = this.properties[key];// TODO: Update readers so `undefined` is impossible/not handled hereif (rawValue === undefined) {if (descriptor.optional) {result[key] = undefined;} else {missing.add(key as string);}continue;}try {result[key] = descriptor.type.read!(valueReader, rawValue);} catch (err) {invalid.set(key as string, err);}}if (this.noExtraKeys && extra.size > 0 || missing.size > 0 || invalid.size > 0) {throw createInvalidDocumentError({extra, missing, invalid});}return result as T;},}));}// TODO: Dynamically add with prototype?write<W>(writer: Writer<W>, value: T): W {const outKeys: Map<string, keyof T> = new Map(this.outKeys);for (const [outKey, jskey] of outKeys) {if (value[jskey] === undefined) {outKeys.delete(outKey);}}return writer.writeDocument(outKeys.keys(), <FW>(outKey: string, fieldWriter: Writer<FW>): FW => {const jsKey: keyof T = this.outKeys.get(outKey)!;const descriptor: PropertyDescriptor<any> = this.properties[jsKey];if (descriptor.type.write === undefined) {throw new Incident("NotWritable", {type: descriptor.type});}return descriptor.type.write(fieldWriter, value[jsKey]);});}testError(val: T): Error | undefined {if (typeof val !== "object" || val === null) {return createInvalidTypeError("object", val);}const extra: Set<string> | undefined = this.noExtraKeys ? new Set(Object.keys(val)) : undefined;const missing: Set<string> = new Set();const invalid: Map<string, Error> = new Map();for (const key in this.properties) {if (extra !== undefined) {extra.delete(key);}const descriptor: PropertyDescriptor<any> = this.properties[key];const propertyValue: any = val[key];if (propertyValue === undefined) {if (!descriptor.optional) {missing.add(key);}continue;}const error: Error | undefined = descriptor.type.testError!(propertyValue);if (error !== undefined) {invalid.set(key as string, error);}}if (extra !== undefined && extra.size > 0 || missing.size > 0 || invalid.size > 0) {return createInvalidDocumentError({extra, missing, invalid});}return undefined;}test(val: T): val is T {if (typeof val !== "object" || val === null) {return false;}const extra: Set<string> | undefined = this.noExtraKeys ? new Set(Object.keys(val)) : undefined;for (const key in this.properties) {if (extra !== undefined) {extra.delete(key);}const descriptor: PropertyDescriptor<any> = this.properties[key];const propertyValue: any = val[key];if (propertyValue === undefined) {if (!descriptor.optional) {return false;}} else if (!descriptor.type.test(propertyValue)) {return false;}}return extra === undefined || extra.size === 0;}equals(val1: T, val2: T): boolean {for (const key in this.properties) {const descriptor: PropertyDescriptor<any> = this.properties[key];if (!descriptor.optional) {if (!descriptor.type.equals(val1[key], val2[key])) {return false;}continue;}if (val1[key] === undefined && val2[key] === undefined) {continue;}if (val1[key] === undefined || val2[key] === undefined || !descriptor.type.equals(val1[key], val2[key])) {return false;}}return true;}clone(val: T): T {const result: Partial<T> = {}; // Object.create(null);for (const key in this.properties) {result[key] = val[key] === undefined ? undefined : this.properties[key].type.clone(val[key]);}return result as T;}diff(oldVal: T, newVal: T): Diff<T> | undefined {let equal: boolean = true;const result: Diff<T> = {set: {}, unset: {}, update: {}};for (const key in this.properties) {// TODO: Remove castconst descriptor: PropertyDescriptor<any, VersionedType<any, any>> = <any> this.properties[key];const oldMember: any = (<any> oldVal)[key];const newMember: any = (<any> newVal)[key];if (oldMember !== undefined) {if (newMember !== undefined) {const diff: any = descriptor.type.diff(oldMember, newMember);if (diff !== undefined) {result.update[key] = diff;equal = false;}} else {result.unset[key] = descriptor.type.clone(oldMember);equal = false;}} else {if (newMember === undefined) {result.set[key] = descriptor.type.clone(newMember);equal = false;}}}return equal ? undefined : result;}patch(oldVal: T, diff: Diff<T> | undefined): T {const result: T = this.clone(oldVal);if (diff === undefined) {return result;}for (const key in diff.set) {result[key] = this.properties[key].type.clone(diff.set[key]);}for (const key in diff.unset) {Reflect.deleteProperty(result as any as object, key);}for (const key in diff.update) {// TODO: Remove castresult[key] = (this.properties[key].type as any).patch(result[key], diff.update[key]);}return result;}reverseDiff(diff: Diff<T> | undefined): Diff<T> | undefined {if (diff === undefined) {return undefined;}const result: Diff<T> = {set: {}, unset: {}, update: {}};for (const key in diff.unset) {result.set[key] = this.properties[key].type.clone(diff.unset[key]);}for (const key in diff.set) {result.unset[key] = this.properties[key].type.clone(diff.set[key]);}for (const key in diff.update) {// TODO: Remove castresult.update[key] = (this.properties[key].type as any).reverseDiff(diff.update[key]);}return result;}squash(_diff1: Diff<T> | undefined, _diff2: Diff<T> | undefined): Diff<T> | undefined {throw createNotImplementedError("DocumentType#squash");}private _applyOptions(): void {if (this._options === undefined) {throw createLazyOptionsError(this);}const options: DocumentTypeOptions<T> = typeof this._options === "function" ?this._options() :this._options;const noExtraKeys: boolean | undefined = options.noExtraKeys;const properties: {[P in keyof T]: PropertyDescriptor<any>} = options.properties;const rename: {[P in keyof T]?: string} | undefined = options.rename;const changeCase: CaseStyle | undefined = options.changeCase;Object.assign(this, {noExtraKeys, properties, rename, changeCase});}}

Const name

name: Name = "document"

Functions

renameKeys

  • renameKeys<T>(obj: T, renameAll?: CaseStyle): Map<keyof T, string>

Generated using TypeDoc