Monster. Data

In this namespace you will find classes and methods for handling data.

Author
  • schukai GmbH

Classes

Datasource

The datasource class encapsulates the access to data objects.

Pathfinder
Pipe
Transformer

Namespaces

Datasource

Namespace for datasources

.

Members

(static, constant) PARENT :string

Type:
  • string

Methods

(static) buildMap(subject, selector, valueTemplateopt, keyTemplateopt, filteropt) → {*}

With the help of the function buildMap(), maps can be easily created from data objects.

With the help of the function buildMap(), maps can be easily created from data objects.

Either a simple definition a.b.c or a template ${a.b.c} can be specified as the path. Key and value can be either a definition or a template. The key does not have to be defined.

The templates determine the appearance of the keys and the value of the map. Either a single value id can be taken or a composite key ${id} ${name} can be used.

If you want to access values of the parent data set, you have to use the ^ character ${id} ${^.name}.

Parameters:
NameTypeAttributesDescription
subject*
selectorstring | Monster.Data~exampleSelectorCallback
valueTemplatestring<optional>
keyTemplatestring<optional>
filterMonster.Data~exampleFilterCallback<optional>
Throws:
  • value is neither a string nor a function

    Type
    TypeError
  • the selector callback must return a map

    Type
    TypeError
Returns:
Type: 
*
Example
import {buildMap} from '@schukai/monster/source/data/buildmap.mjs';
// a typical data structure as reported by an api

let map;
let obj = {
    "data": [
        {
            "id": 10,
            "name": "Cassandra",
            "address": {
                "street": "493-4105 Vulputate Street",
                "city": "Saumur",
                "zip": "52628"
            }
        },
        {
            "id": 20,
            "name": "Holly",
            "address": {
                "street": "1762 Eget Rd.",
                "city": "Schwalbach",
                "zip": "952340"
            }
        },
        {
            "id": 30,
            "name": "Guy",
            "address": {
                "street": "957-388 Sollicitudin Avenue",
                "city": "Panchià",
                "zip": "420729"
            }
        }
    ]
};

// The function is passed this data structure and with the help of the selector `'data.*'` the data to be considered are selected.
// The key is given by a simple definition `'id'` and the value is given by a template `'${name} (${address.zip} ${address.city})'`.
map = buildMap(obj, 'data.*', '${name} (${address.zip} ${address.city})', 'id');
console.log(map);

// ↦ Map(3) {
//  '10' => 'Cassandra (52628 Saumur)',
//  '20' => 'Holly (952340 Schwalbach)',
//  '30' => 'Guy (420729 Panchià)'
// }

// If no key is specified, the key from the selection, here the array index, is taken.
map = buildMap(obj, 'data.*', '${name} (${address.zip} ${address.city})');
console.log(map);

// ↦ Map(3) {
//  '0' => 'Cassandra (52628 Saumur)',
//  '1' => 'Holly (952340 Schwalbach)',
//  '2' => 'Guy (420729 Panchià)'
// }

// a filter (function(value, key) {}) can be specified to accept only defined entries.
map = buildMap(obj, 'data.*', '${name} (${address.zip} ${address.city})', 'id', function (value, key) {
    return (value['id'] >= 20) ? true : false
});
console.log(map);

// ↦ Map(2) {
//  20 => 'Holly (952340 Schwalbach)',
//  30 => 'Guy (420729 Panchià)'
// }

(static) buildTree(subject, selector, idKey, parentIDKey, optionsopt) → {*}

With the help of the function buildTree(), nodes can be easily created from data objects.

With the help of the function buildTree(), nodes can be easily created from data objects.

Parameters:
NameTypeAttributesDescription
subject*
selectorstring | Monster.Data~exampleSelectorCallback
idKeystring
parentIDKeystring
optionsbuildTreeOptions<optional>
Since
  • 1.26.0
License
  • AGPLv3
Throws:
  • value is neither a string nor a function

    Type
    TypeError
  • the selector callback must return a map

    Type
    TypeError
  • the object has no value for the specified id

    Type
    Error
Returns:
Type: 
*

(static) diff(first, second) → {array}

With the diff function you can perform the change of one object to another.

With the diff function you can perform the change of one object to another. The result shows the changes of the second object to the first object.

The operator add means that something has been added to the second object. delete means that something has been deleted from the second object compared to the first object.

Parameters:
NameTypeDescription
first*
second*
Since
  • 1.6.0
License
  • AGPLv3
Returns:
Type: 
array
Example
import {Diff} from '@schukai/monster/source/data/diff.mjs';

// given are two objects x and y.

let x = {
    a: 1,
    b: "Hello!"
}

let y = {
    a: 2,
    c: true
}

// These two objects can be compared with each other.

console.log(Diff(x, y));

// the result is then the following

//
// [
// {
//        operator: 'update',
//        path: [ 'a' ],
//        first: { value: 1, type: 'number' },
//        second: { value: 2, type: 'number' }
//    },
// {
//        operator: 'delete',
//        path: [ 'b' ],
//        first: { value: 'Hello!', type: 'string' }
//    },
// {
//        operator: 'add',
//        path: [ 'c' ],
//        second: { value: true, type: 'boolean' }
//    }
// ]

(static) extend(target) → {object}

Extend copies all enumerable own properties from one or more source objects to a target object.

Extend copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.

Parameters:
NameTypeDescription
targetobject
object
Since
  • 1.10.0
License
  • AGPLv3
Throws:
  • unsupported argument

    Type
    Error
  • type mismatch

    Type
    Error
Returns:
Type: 
object

Type Definitions

buildTreeOptions

Type:
  • Object
Properties
NameTypeDefaultDescription
options.rootReferencesarray[null,

undefined] defines the values for elements without parents

options.filterMonster.Data~exampleFilterCallback

filtering of the values

exampleFilterCallback(value, key)

With the help of this filter callback, values can be filtered out.

With the help of this filter callback, values can be filtered out. Only if the filter function returns true, the value is taken for the map.

Parameters:
NameTypeDescription
value*

Value

keystring

Key

exampleSelectorCallback(subject)

Alternatively to a string selector a callback can be specified.

Alternatively to a string selector a callback can be specified. this must return a map.

Parameters:
NameTypeDescription
subject*

subject

Since
  • 1.17.0
License
  • AGPLv3
Returns:

Map

Example
import {buildMap} from '@schukai/monster/source/data/buildmap.mjs';

let obj = {
               "data": [
                   {
                       "id": 10,
                       "name": "Cassandra",
                       "enrichment": {
                           variants: [
                               {
                                   sku: 1, label: "XXS", price: [
                                       {vk: '12.12 €'},
                                       {vk: '12.12 €'}
                                   ]
                               },
                               {
                                   sku: 2, label: "XS", price: [
                                       {vk: '22.12 €'},
                                       {vk: '22.12 €'}
                                   ]
                               },
                               {
                                   sku: 3, label: "S", price: [
                                       {vk: '32.12 €'},
                                       {vk: '32.12 €'}
                                   ]
                               },
                               {
                                   sku: 4, label: "L", price: [
                                       {vk: '42.12 €'},
                                       {vk: '42.12 €'}
                                   ]
                               }
                           ]

                       }
                   },
                   {
                       "id": 20,
                       "name": "Yessey!",
                       "enrichment": {
                           variants: [
                               {
                                   sku: 1, label: "XXS", price: [
                                       {vk: '12.12 €'},
                                       {vk: '12.12 €'}
                                   ]
                               },
                               {
                                   sku: 2, label: "XS", price: [
                                       {vk: '22.12 €'},
                                       {vk: '22.12 €'}
                                   ]
                               },
                               {
                                   sku: 3, label: "S", price: [
                                       {vk: '32.12 €'},
                                       {vk: '32.12 €'}
                                   ]
                               },
                               {
                                   sku: 4, label: "L", price: [
                                       {vk: '42.12 €'},
                                       {vk: '42.12 €'}
                                   ]
                               }
                           ]

                       }
                   }
               ]
           };

let callback = function (subject) {
               let m = new Map;

               for (const [i, b] of Object.entries(subject.data)) {

                   let key1 = i;

                   for (const [j, c] of Object.entries(b.enrichment.variants)) {
                       let key2 = j;

                       for (const [k, d] of Object.entries(c.price)) {

                           let key3 = k;

                           d.name = b.name;
                           d.label = c.label;
                           d.id = [key1, key2, key3].join('.');

                           m.set(d.id, d);
                       }

                   }
               }
               return m;
           }

let map = buildMap(obj, callback, '${name} ${vk}', '${id}')

// ↦ Map(3) {
//  "0.0.0":"Cassandra 12.12 €",
//  "0.0.1":"Cassandra 12.12 €",
//  "0.1.0":"Cassandra 22.12 €",
//  "0.1.1":"Cassandra 22.12 €",
//  "0.2.0":"Cassandra 32.12 €",
//  "0.2.1":"Cassandra 32.12 €",
//  "0.3.0":"Cassandra 42.12 €",
//  "0.3.1":"Cassandra 42.12 €",
//  "1.0.0":"Yessey! 12.12 €",
//  "1.0.1":"Yessey! 12.12 €",
//  "1.1.0":"Yessey! 22.12 €",
//  "1.1.1":"Yessey! 22.12 €",
//  "1.2.0":"Yessey! 32.12 €",
//  "1.2.1":"Yessey! 32.12 €",
//  "1.3.0":"Yessey! 42.12 €",
//  "1.3.1":"Yessey! 42.12 €"
// }