137 lines
3.2 KiB
JavaScript
137 lines
3.2 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.defer = defer;
|
|
exports.makeString = makeString;
|
|
exports.copy = copy;
|
|
exports.setPath = setPath;
|
|
exports.pushPath = pushPath;
|
|
exports.getPath = getPath;
|
|
exports.deepExtend = deepExtend;
|
|
exports.regexEscape = regexEscape;
|
|
exports.escape = escape;
|
|
|
|
// http://lea.verou.me/2016/12/resolve-promises-externally-with-this-one-weird-trick/
|
|
function defer() {
|
|
var res;
|
|
var rej;
|
|
var promise = new Promise(function (resolve, reject) {
|
|
res = resolve;
|
|
rej = reject;
|
|
});
|
|
promise.resolve = res;
|
|
promise.reject = rej;
|
|
return promise;
|
|
}
|
|
|
|
function makeString(object) {
|
|
if (object == null) return '';
|
|
/* eslint prefer-template: 0 */
|
|
|
|
return '' + object;
|
|
}
|
|
|
|
function copy(a, s, t) {
|
|
a.forEach(function (m) {
|
|
if (s[m]) t[m] = s[m];
|
|
});
|
|
}
|
|
|
|
function getLastOfPath(object, path, Empty) {
|
|
function cleanKey(key) {
|
|
return key && key.indexOf('###') > -1 ? key.replace(/###/g, '.') : key;
|
|
}
|
|
|
|
function canNotTraverseDeeper() {
|
|
return !object || typeof object === 'string';
|
|
}
|
|
|
|
var stack = typeof path !== 'string' ? [].concat(path) : path.split('.');
|
|
|
|
while (stack.length > 1) {
|
|
if (canNotTraverseDeeper()) return {};
|
|
var key = cleanKey(stack.shift());
|
|
if (!object[key] && Empty) object[key] = new Empty();
|
|
object = object[key];
|
|
}
|
|
|
|
if (canNotTraverseDeeper()) return {};
|
|
return {
|
|
obj: object,
|
|
k: cleanKey(stack.shift())
|
|
};
|
|
}
|
|
|
|
function setPath(object, path, newValue) {
|
|
var _getLastOfPath = getLastOfPath(object, path, Object),
|
|
obj = _getLastOfPath.obj,
|
|
k = _getLastOfPath.k;
|
|
|
|
obj[k] = newValue;
|
|
}
|
|
|
|
function pushPath(object, path, newValue, concat) {
|
|
var _getLastOfPath2 = getLastOfPath(object, path, Object),
|
|
obj = _getLastOfPath2.obj,
|
|
k = _getLastOfPath2.k;
|
|
|
|
obj[k] = obj[k] || [];
|
|
if (concat) obj[k] = obj[k].concat(newValue);
|
|
if (!concat) obj[k].push(newValue);
|
|
}
|
|
|
|
function getPath(object, path) {
|
|
var _getLastOfPath3 = getLastOfPath(object, path),
|
|
obj = _getLastOfPath3.obj,
|
|
k = _getLastOfPath3.k;
|
|
|
|
if (!obj) return undefined;
|
|
return obj[k];
|
|
}
|
|
|
|
function deepExtend(target, source, overwrite) {
|
|
/* eslint no-restricted-syntax: 0 */
|
|
for (var prop in source) {
|
|
if (prop in target) {
|
|
// If we reached a leaf string in target or source then replace with source or skip depending on the 'overwrite' switch
|
|
if (typeof target[prop] === 'string' || target[prop] instanceof String || typeof source[prop] === 'string' || source[prop] instanceof String) {
|
|
if (overwrite) target[prop] = source[prop];
|
|
} else {
|
|
deepExtend(target[prop], source[prop], overwrite);
|
|
}
|
|
} else {
|
|
target[prop] = source[prop];
|
|
}
|
|
}
|
|
|
|
return target;
|
|
}
|
|
|
|
function regexEscape(str) {
|
|
/* eslint no-useless-escape: 0 */
|
|
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
|
|
}
|
|
/* eslint-disable */
|
|
|
|
|
|
var _entityMap = {
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
'"': '"',
|
|
"'": ''',
|
|
'/': '/'
|
|
};
|
|
/* eslint-enable */
|
|
|
|
function escape(data) {
|
|
if (typeof data === 'string') {
|
|
return data.replace(/[&<>"'\/]/g, function (s) {
|
|
return _entityMap[s];
|
|
});
|
|
}
|
|
|
|
return data;
|
|
} |