Skip to content

AlgorithmTest.js

Alexis Rico edited this page Sep 13, 2018 · 1 revision

AlgorithmTest.js

import * as traverse from "traverse";

let _ = require('lodash');

let object = {
    "lastUpdated": "2018-06-08T09:38:20.163",
    "id": "zq27hSb6gXo",
    "created": "2016-02-11T14:27:22.820",
    "name": "Data type",
    "code": "TI_IN_PRATT_DATA-TYPE",
    "indicatorAttribute": false,
    "indicatorGroupAttribute": false,
    "publicAccess": "--------",
    "userGroupAttribute": false,
    "dataElementAttribute": false,
    "constantAttribute": false,
    "valueType": "TEXT",
    "categoryOptionAttribute": false,
    "optionSetAttribute": false,
    "sqlViewAttribute": false,
    "legendSetAttribute": false,
    "trackedEntityAttributeAttribute": false,
    "organisationUnitAttribute": false,
    "dataSetAttribute": false,
    "documentAttribute": false,
    "unique": false,
    "dataElementGroupAttribute": false,
    "sectionAttribute": false,
    "userAttribute": false,
    "displayName": "Data type",
    "mandatory": false,
    "categoryOptionGroupAttribute": false,
    "trackedEntityAttribute": false,
    "externalAccess": false,
    "programStageAttribute": false,
    "programAttribute": true,
    "categoryOptionComboAttribute": false,
    "programIndicatorAttribute": false,
    "organisationUnitGroupAttribute": false,
    "organisationUnitGroupSetAttribute": false,
    "optionAttribute": false,
    "lastUpdatedBy": {
        "id": "q9wZgtaDxWn"
    },
    "access": {
        "read": true,
        "update": true,
        "externalize": true,
        "delete": true,
        "write": true,
        "manage": true
    },
    "optionSet": {
        "id": "BQRcggdAep9"
    },
    "user": {
        "id": "q9wZgtaDxWn"
    },
    "translations": [{
        "id": "1"
    },
        {
            "id": "2"
        }],
    "userGroupAccesses": [],
    "attributeValues": [],
    "userAccesses": []
};

function recursiveTraverse(element) {
    let references = [];
    for (let propertyName in element) {
        if (element.hasOwnProperty(propertyName)) {
            let property = element[propertyName];
            if (Array.isArray(property)) {
                _.forEach(property, arrayElement => {
                    if (typeof arrayElement === 'object' && arrayElement.id !== undefined) {
                        references.push(arrayElement.id);
                    }
                });
            } else if (typeof property === 'object' && property.id !== undefined) {
                references.push(property.id);
            }
        }
    }
    return references;
}

function recursiveParse(builder) {
    return new Promise(function (resolve, reject) {
        let references = [];
        traverse(builder.element).forEach(function (item) {
            let context = this;
            if (context.isLeaf && context.key === 'id' && item !== '') {
                let parent = context.parent;
                while (parent.level > 1) parent = parent.parent;
                if (parent !== undefined && parent.key !== undefined && builder.d2.models[parent.key] !== undefined) {
                    let key = parent.key === 'children' ? builder.type : parent.key;
                    if (shouldDeepCopy(builder.type, parent.key)) {
                        if (!fetchedItems.has(item)) references.push(item);
                    } else if (DEBUG) console.log('recursiveParse: Shallow copy of ' + item + ' (' + key + ')');
                }
            }
        });
        resolve(traverseForIds(builder.element));
    });
}

console.log(recursiveTraverse(object));
Clone this wiki locally