Skip to content

Commit

Permalink
[upd] Refactor + rename form modify structure function
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim committed Aug 18, 2020
1 parent 828a0db commit 0c60fef
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 28 deletions.
30 changes: 18 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@babel/preset-env": "^7.9.6",
"@babel/preset-react": "^7.9.4",
"@babel/register": "^7.10.3",
"@types/jsonld": "^1.5.1",
"babel-jest": "^26.0.1",
"babel-loader": "^8.1.0",
"clean-css-cli": "^4.3.0",
Expand Down
3 changes: 1 addition & 2 deletions src/model/WizardGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export default class WizardGenerator {
steps,
title
};

} catch (e) {
wizardProperties = WizardGenerator.createDefaultWizard(data, title);
}
Expand All @@ -66,7 +65,7 @@ export default class WizardGenerator {
let stepQuestions = [];

if (structure['@graph'][0]['@id'] !== undefined) {
id2ObjectMap = JsonLdFramingUtils.modifyStructure(structure); //TODO make as callback
id2ObjectMap = JsonLdFramingUtils.expandStructure(structure); //TODO make as callback

Object.keys(id2ObjectMap).map((key) => {
JsonLdObjectMap.putObject(key, id2ObjectMap[key]);
Expand Down
22 changes: 11 additions & 11 deletions src/util/JsonLdFramingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,20 @@ export default class JsonLdFramingUtils {
jsonld.flatten(input, null, null, flattenedCallback);
}

static modifyStructure(structure) {
const defs = structure['@graph'];
let item;
let form;
static expandStructure(structure) {
let root;
const id2objectMap = {}; // mapping @id -> object

for (let i = 0; i < defs.length; i++) {
item = defs[i];
structure['@graph'].forEach((item) => {
id2objectMap[item['@id']] = item;

if (FormUtils.isForm(item)) {
form = item;
root = item;
}
}
});

try {
this._expandGraph(form, formShape, id2objectMap);
this._expandGraph(root, formShape, id2objectMap);
} catch (e) {
console.error("Error '" + e + "' occured, while trying to apply frame-ing with custom shape.");
}
Expand All @@ -76,13 +74,15 @@ export default class JsonLdFramingUtils {
let child;
let childId;

for (const prop of shape.expandProperties) {
shape.expandProperties.forEach((prop) => {
if (parentNode.hasOwnProperty(prop)) {
parentNode[prop] = Utils.asArray(parentNode[prop]);
childArray = parentNode[prop];

for (let i = 0; i < childArray.length; i++) {
childId = childArray[i]['@id'];
child = id2ObjectMap[childId];

if (child !== undefined) {
childArray[i] = child;
//console.log(childId + " expanded.");
Expand All @@ -92,6 +92,6 @@ export default class JsonLdFramingUtils {
}
}
}
}
});
}
}
4 changes: 2 additions & 2 deletions test/__tests__/JsonLdFramingUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('JsonLd framing utils', () => {
};
});

it('returns jsonld object map from modifyStructure', () => {
const id2objectMap = JsonLdFramingUtils.modifyStructure(formDocument);
it('returns expanded jsonld object map from expandStructure', () => {
const id2objectMap = JsonLdFramingUtils.expandStructure(formDocument);

expect(id2objectMap[formQuestion['@id']]).toBe(formQuestion);
});
Expand Down
3 changes: 2 additions & 1 deletion types/s-forms.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { Modal } from 'react-bootstrap';
import { JsonLdObj } from 'jsonld/jsonld-spec';

export interface SOptions {
intl?: { locale: string }; // default 'en'
Expand Down Expand Up @@ -130,7 +131,7 @@ export class JsonLdObjectUtils {

export class JsonLdFramingUtils {
static customFrame(input, shape, callback): any;
static modifyStructure(structure): any;
static expandStructure(structure: JsonLdObj): JsonLdObj;
}

export class JsonLdObjectMap {
Expand Down

0 comments on commit 0c60fef

Please sign in to comment.