refactor(entity): JSON
This commit is contained in:
parent
a55c7141f3
commit
50cca7bb9f
|
@ -371,16 +371,35 @@ export default (flecks) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
const json = {};
|
let output;
|
||||||
const traits = Object.entries(this.#traits);
|
if (this.uri) {
|
||||||
for (let i = 0; i < traits.length; i++) {
|
const pristine = {};
|
||||||
const [type, trait] = traits[i];
|
const json = {};
|
||||||
json[type] = trait.toJSON();
|
const traits = Object.entries(this.#traits);
|
||||||
|
for (let i = 0; i < traits.length; i++) {
|
||||||
|
const [type, trait] = traits[i];
|
||||||
|
pristine.traits = pristine.traits || {};
|
||||||
|
pristine.traits[type] = trait.constructor.withDefaults(
|
||||||
|
this.#originalJson ? this.#originalJson.traits[type] : {},
|
||||||
|
);
|
||||||
|
json.traits = json.traits || {};
|
||||||
|
json.traits[type] = trait.toJSON();
|
||||||
|
}
|
||||||
|
output = {
|
||||||
|
extends: this.uri,
|
||||||
|
...(mergeDiff(pristine, json) || {}),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return {
|
else {
|
||||||
...(this.uri ? {extends: this.uri} : {}),
|
output = {};
|
||||||
traits: json,
|
const traits = Object.entries(this.#traits);
|
||||||
};
|
for (let i = 0; i < traits.length; i++) {
|
||||||
|
const [type, trait] = traits[i];
|
||||||
|
output.traits = {};
|
||||||
|
output.traits[type] = trait.toJSON();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
toNetwork(informed) {
|
toNetwork(informed) {
|
||||||
|
|
|
@ -25,9 +25,9 @@ afterEach(() => {
|
||||||
|
|
||||||
it('outputs JSON', () => {
|
it('outputs JSON', () => {
|
||||||
const entity = new Entity();
|
const entity = new Entity();
|
||||||
expect(entity.toJSON()).to.deep.equal({traits: {}});
|
expect(entity.toJSON()).to.deep.equal({});
|
||||||
entity.uri = '/foo/bar';
|
entity.uri = '/foo/bar';
|
||||||
expect(entity.toJSON()).to.deep.equal({extends: entity.uri, traits: {}});
|
expect(entity.toJSON()).to.deep.equal({extends: entity.uri});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can trim defaults from JSON', async () => {
|
it('can trim defaults from JSON', async () => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user