Data

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

Methods

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

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.

You can call the method via the monster namespace Monster.Data.buildMap().

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/monster.js';
console.log(Monster.Data.buildMap())
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {buildMap} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/data/buildmap.js';
console.log(buildMap())
</script>

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:
Name Type Attributes Description
subject *
selector string | Monster.Data~exampleSelectorCallback
valueTemplate string <optional>
keyTemplate string <optional>
filter Monster.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 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/data/buildmap.js';
// 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, id, parentID, filteropt) → {*}

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

You can call the method via the monster namespace Monster.Data.buildTree().

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/buildtree.js';
console.log(Monster.Data.buildTree())
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {buildTree} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/data/buildtree.js';
console.log(buildTree())
</script>
Parameters:
Name Type Attributes Description
subject *
selector string | Monster.Data~exampleSelectorCallback
id string
parentID string
filter Monster.Data~exampleFilterCallback <optional>
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. 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.

You can call the method via the monster namespace Monster.Data.Diff().

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/monster.js';
Monster.Data.Diff(a, b)
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {Diff} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/data/diff.js';
Diff(a, b)
</script>
Parameters:
Name Type Description
first *
second *
Since:
  • 1.6.0
Returns:
Type
array
Example
import {Diff} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/data/diff.js';

// 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. It returns the modified target object.

You can call the method via the monster namespace Monster.Data.extend().

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/monster.js';
Monster.Data.extend(a, b)
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {extend} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/data/extend.js';
extend(a, b)
</script>
Parameters:
Name Type Description
target object
object
Since:
  • 1.10.0
Throws:
  • unsupported argument

    Type
    Error
  • type mismatch

    Type
    Error
Returns:
Type
object

Type Definitions

# exampleFilterCallback(value, key)

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:
Name Type Description
value *

Value

key string

Key

# exampleSelectorCallback(subject)

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

Parameters:
Name Type Description
subject *

subject

Since:
  • 1.17.0
Returns:

Map

Example
import {buildMap} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/data/buildmap.js';

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 €"
// }