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