fix APBase toPlain

pull/4/head
SiranWeb 2022-11-14 19:22:12 +03:00
rodzic a3e2588365
commit 277bfcd2ca
2 zmienionych plików z 9 dodań i 16 usunięć

Wyświetl plik

@ -6,20 +6,8 @@ export class APBase<T extends WithContext> {
public fields: T; public fields: T;
protected constructor(fields: T) { protected constructor(fields: T) {
// TODO move ordering to toPlain
const obj = cloneObjDeep<T>(fields);
if ('@context' in obj) {
this.fields = {
'@context': obj['@context'],
} as T;
delete obj["@context"];
} else {
this.fields = {} as T;
}
this.fields = { this.fields = {
...this.fields, ...cloneObjDeep<T>(fields),
...obj
}; };
} }
@ -37,12 +25,17 @@ export class APBase<T extends WithContext> {
} }
protected parseValue(value: any): any { protected parseValue(value: any): any {
return value instanceof APBase ? value.toPlain() : value; return value instanceof APBase ? value.toPlain() : value.toValue();
} }
public toPlain(): Record<string, any> { public toPlain(): Record<string, any> {
const result = {} as Record<string, any>; const result = {} as Record<string, any>;
if ('@context' in this.fields) {
result['@context'] = this.fields['@context'];
}
for (const [key, value] of Object.entries(this.fields)) { for (const [key, value] of Object.entries(this.fields)) {
if (key === '@context') continue;
const isFunction = typeof value === 'function'; const isFunction = typeof value === 'function';
const isArray = Array.isArray(value); const isArray = Array.isArray(value);
const isNull = value === null; const isNull = value === null;
@ -69,4 +62,4 @@ export class APBase<T extends WithContext> {
public toJSON(): string { public toJSON(): string {
return JSON.stringify(this.toPlain()); return JSON.stringify(this.toPlain());
} }
} }

Wyświetl plik

@ -2,7 +2,7 @@
"compilerOptions": { "compilerOptions": {
"outDir": "./dist", "outDir": "./dist",
"declaration": true, "declaration": true,
"target": "es2016", "target": "es2017",
"module": "ES6", "module": "ES6",
"moduleResolution": "Node", "moduleResolution": "Node",
"esModuleInterop": true, "esModuleInterop": true,