All files / dist utils.js

15.38% Statements 6/39
0% Branches 0/22
0% Functions 0/10
15.79% Lines 6/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99  1x                           1x                         1x                                                                                 1x 1x                                                     1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Iterate trough object keys
 *
 * @param {object} obj - Object that needs to be iterated
 * @param {Function} fn - Function that should be called for every iteration
 */
function objectForEach(obj, fn) {
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            fn(key);
        }
    }
}
exports.objectForEach = objectForEach;
/**
 * Iterate trough one item or array of items and call the defined function
 *
 * @export
 * @template T
 * @param {(object|Array<object>)} data - Data which needs to be iterated
 * @param {Function} fn - Function that needs to be callse
 * @returns {(T|Array<T>)} - The result of iteration
 */
function mapItems(data, fn) {
    return data instanceof Array ? data.map(function (item) { return fn(item); }) : fn(data);
}
exports.mapItems = mapItems;
/**
 * Flatten the JSON API record so it can be inserted into the model
 *
 * @export
 * @param {IJsonApiRecord} record - original JSON API record
 * @returns {IDictionary<any>} - Flattened object
 */
function flattenRecord(record) {
    var data = {
        __internal: {},
        id: record.id,
        type: record.type,
    };
    objectForEach(record.attributes, function (key) {
        data[key] = record.attributes[key];
    });
    objectForEach(record.relationships, function (key) {
        var rel = record.relationships[key];
        if (rel.meta) {
            data[key + "Meta"] = rel.meta;
        }
        if (rel.links) {
            data.__internal.relationships = data.__internal.relationships || {};
            data.__internal.relationships[key] = rel.links;
        }
    });
    objectForEach(record.links, function (key) {
        if (record.links[key]) {
            data.__internal.links = data.__internal.links || {};
            data.__internal.links[key] = record.links[key];
        }
    });
    objectForEach(record.meta, function (key) {
        if (record.meta[key]) {
            data.__internal.meta = data.__internal.meta || {};
            data.__internal.meta[key] = record.meta[key];
        }
    });
    return data;
}
exports.flattenRecord = flattenRecord;
exports.isBrowser = (typeof window !== 'undefined');
/**
 * Assign objects to the target object
 * Not a complete implementation (Object.assign)
 * Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign polyfill
 *
 * @private
 * @param {object} target - Target object
 * @param {Array<object>} args - Objects to be assigned
 * @returns
 */
function assign(target) {
    var args = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        args[_i - 1] = arguments[_i];
    }
    args.forEach(function (nextSource) {
        if (nextSource != null) {
            for (var nextKey in nextSource) {
                if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
                    target[nextKey] = nextSource[nextKey];
                }
            }
        }
    });
    return target;
}
exports.assign = assign;