all files / utils/general/ general.js

62.69% Statements 42/67
57.89% Branches 22/38
66.67% Functions 8/12
62.69% Lines 42/67
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 100 101 102 103 104  1249×   1249×       20× 20× 15×     32× 38×   13×     178× 129× 49×   49×                                                                                             143×   143×   143×      
"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(/(^|[_\-]+)\w/g, function (s) {
        return s.replace(/[_\-]+/, ' ').toUpperCase();
    }).replace(/[a-z][A-Z]/g, function (s) {
        return s[0] + ' ' + s[1];
    }).trim();
}
exports.makeTitle = makeTitle;
function listsEqual(listA, listB) {
    if (listA === listB)
        return true;
    Iif (!listA || !listB)
        return false;
    return immutable_class_1.arraysEqual(listA.toArray(), listB.toArray());
}
exports.listsEqual = listsEqual;
function calculateDragPosition(offset, numItems, itemWidth, itemGap) {
    if (!numItems) {
        return {
            dragInsertPosition: null,
            dragReplacePosition: 0
        };
    }
    if (offset < 0) {
        return {
            dragInsertPosition: 0,
            dragReplacePosition: null
        };
    }
    var sectionWidth = itemWidth + itemGap;
    var sectionNumber = Math.floor(offset / sectionWidth);
    if (sectionNumber > numItems) {
        return {
            dragInsertPosition: null,
            dragReplacePosition: numItems
        };
    }
    var offsetWithinSection = offset - sectionWidth * sectionNumber;
    if (offsetWithinSection < itemWidth) {
        return {
            dragInsertPosition: null,
            dragReplacePosition: sectionNumber
        };
    }
    else {
        return {
            dragInsertPosition: sectionNumber + 1,
            dragReplacePosition: null
        };
    }
}
exports.calculateDragPosition = calculateDragPosition;
function collect(wait, func) {
    var timeout;
    var later = function () {
        timeout = null;
        func();
    };
    return function () {
        if (!timeout) {
            timeout = setTimeout(later, wait);
        }
    };
}
exports.collect = collect;
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');
    if (!/^[\w.~\-]*$/.test(name)) {
        throw new Error("'" + name + "' is not a URL safe name. Try '" + name.replace(/[^\w~.~\-]+/g, '_') + "' instead?");
    }
}
exports.verifyUrlSafeName = verifyUrlSafeName;