all files / common/utils/general/ general.js

75.81% Statements 47/62
71.43% Branches 20/28
64.29% Functions 9/14
75.81% Lines 47/62
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  1396×   1396×       20× 20× 15×     67×     92×     21×     178× 129× 49×   49×                         208×   181×   181×   181× 181×              
"use strict";
var immutable_1 = require('immutable');
var immutable_class_1 = require('immutable-class');
var objectHasOwnProperty = Object.prototype.hasOwnProperty;
function hasOwnProperty(obj, key) {
    Iif (!obj)
        return false;
    return objectHasOwnProperty.call(obj, key);
}
exports.hasOwnProperty = hasOwnProperty;
function moveInList(list, itemIndex, insertPoint) {
    var n = list.size;
    Iif (itemIndex < 0 || itemIndex >= n)
        throw new Error('itemIndex out of range');
    Iif (insertPoint < 0 || insertPoint > n)
        throw new Error('insertPoint out of range');
    var newArray = [];
    list.forEach(function (value, i) {
        if (i === insertPoint)
            newArray.push(list.get(itemIndex));
        if (i !== itemIndex)
            newArray.push(value);
    });
    if (n === insertPoint)
        newArray.push(list.get(itemIndex));
    return immutable_1.List(newArray);
}
exports.moveInList = moveInList;
function makeTitle(name) {
    return name
        .replace(/^[ _\-]+|[ _\-]+$/g, '')
        .replace(/(^|[_\-]+)\w/g, function (s) {
        return s.replace(/[_\-]+/, ' ').toUpperCase();
    })
        .replace(/[a-z0-9][A-Z]/g, function (s) {
        return s[0] + ' ' + s[1];
    });
}
exports.makeTitle = makeTitle;
function immutableListsEqual(listA, listB) {
    if (listA === listB)
        return true;
    Iif (!listA !== !listB)
        return false;
    return immutable_class_1.immutableArraysEqual(listA.toArray(), listB.toArray());
}
exports.immutableListsEqual = immutableListsEqual;
function collect(wait, fn) {
    var timeout;
    var later = function () {
        timeout = null;
        fn();
    };
    return function () {
        if (!timeout) {
            timeout = setTimeout(later, wait);
        }
    };
}
exports.collect = collect;
var URL_UNSAFE_CHARS = /[^\w.~\-]+/g;
function makeUrlSafeName(name) {
    return name.replace(URL_UNSAFE_CHARS, '_');
}
exports.makeUrlSafeName = makeUrlSafeName;
function verifyUrlSafeName(name) {
    Iif (typeof name !== 'string')
        throw new TypeError('name must be a string');
    Iif (!name.length)
        throw new Error('can not have empty name');
    var urlSafeName = makeUrlSafeName(name);
    if (name !== urlSafeName) {
        throw new Error("'" + name + "' is not a URL safe name. Try '" + urlSafeName + "' instead?");
    }
}
exports.verifyUrlSafeName = verifyUrlSafeName;
function arraySum(inputArray) {
    return inputArray.reduce(function (pV, cV) {
        return pV + cV;
    }, 0);
}
exports.arraySum = arraySum;