@enonic/js-utils
Version:
Enonic XP JavaScript Utils
316 lines (315 loc) • 9.59 kB
JavaScript
;
function _instanceof(left, right) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return !!right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = function(target, all) {
for(var name in all)__defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = function(to, from, except, desc) {
if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") {
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
try {
var _loop = function() {
var key = _step.value;
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: function() {
return from[key];
},
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
};
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally{
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally{
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
return to;
};
var __toCommonJS = function(mod) {
return __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
};
// object/index.ts
var object_exports = {};
__export(object_exports, {
deleteIn: function() {
return deleteIn_default;
},
entries: function() {
return entries;
},
getIn: function() {
return getIn;
},
hasOwnProperty: function() {
return hasOwnProperty;
},
lcKeys: function() {
return lcKeys;
},
mapKeys: function() {
return mapKeys;
},
setIn: function() {
return setIn;
},
sortKeys: function() {
return sortKeys;
},
sortKeysRec: function() {
return sortKeysRec;
},
values: function() {
return values;
}
});
module.exports = __toCommonJS(object_exports);
// object/deleteIn.ts
function deleteIn(obj) {
for(var _len = arguments.length, paths = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){
paths[_key - 1] = arguments[_key];
}
if (!obj || !paths) {
return;
}
var uniformPath = [];
for(var i = 0; i < paths.length; i++){
var path = paths[i];
if (typeof path === "string") {
path.split(".").forEach(function(p) {
return uniformPath.push(p);
});
} else if (Array.isArray(path)) {
path.forEach(function(p) {
return uniformPath.push(p);
});
} else {
uniformPath.push(path);
}
}
for(var i1 = 0; i1 < uniformPath.length - 1; i1++){
obj = obj[uniformPath[i1]];
if (typeof obj === "undefined") {
return;
}
}
delete obj[uniformPath.pop()];
}
var deleteIn_default = deleteIn;
// value/isObject.ts
var isObject = function(value) {
return Object.prototype.toString.call(value).slice(8, -1) === "Object";
};
// value/toStr.ts
function toStr(value, replacer) {
var space = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 4;
return JSON.stringify(value, replacer, space);
}
// object/mapKeys.ts
function mapKeys(obj, fn) {
if (!isObject(obj)) {
throw new TypeError("mapKeys: First param must be an object! got:".concat(toStr(obj)));
}
var result = {};
var keys3 = Object.keys(obj);
for(var i = 0; i < keys3.length; i++){
var key = keys3[i];
fn({
key: key,
result: result,
value: obj[key]
});
}
return result;
}
// object/lcKeys.ts
function lcKeys(obj) {
return mapKeys(obj, function(param) {
var key = param.key, result = param.result, value = param.value;
result[String(key).toLowerCase()] = value;
});
}
// object/entries.ts
function entries(o) {
return Object.keys(o).map(function(key) {
return [
key,
o[key]
];
});
}
// value/isBasicObject.ts
var isBasicObject = function(value) {
return (typeof value === "undefined" ? "undefined" : _type_of(value)) === "object";
};
// value/isNumber.ts
function isNumber(value) {
return typeof value === "number" && isFinite(value);
}
// value/isStringLiteral.ts
var isStringLiteral = function(value) {
return typeof value === "string";
};
// value/isStringObject.ts
var isStringObject = function(value) {
return _instanceof(value, String);
};
// value/isString.ts
var isString = function(value) {
return isStringLiteral(value) || isStringObject(value);
};
// value/isSymbol.ts
var isSymbol = function(value) {
return (typeof value === "undefined" ? "undefined" : _type_of(value)) === "symbol";
};
// value/isPropertyKey.ts
var isPropertyKey = function(value) {
return isString(value) || isNumber(value) || isSymbol(value);
};
// object/hasOwnProperty.ts
function hasOwnProperty(obj, propKey) {
if (!isBasicObject(obj)) {
throw new Error("First parameter to hasOwnProperty must be a basic Object! ".concat(toStr(obj)));
}
if (!isPropertyKey(propKey)) {
throw new Error("Second parameter to hasOwnProperty must be a PropertyKey (string|number|symbol)! ".concat(toStr(propKey)));
}
return obj.hasOwnProperty(propKey);
}
// object/getIn.ts
function getIn(source, path, def) {
if (!Array.isArray(path)) {
if (isNumber(path)) {
path = [
path
];
} else {
path = path.split(".");
}
}
var leafKey = path[0];
var obj = source;
if (path.length > 1) {
var pathLength = path.length;
leafKey = path[pathLength - 1];
for(var i = 0; i < pathLength - 1; i++){
var branchKey = path[i];
if (!isBasicObject(obj) || !hasOwnProperty(obj, branchKey) || typeof obj[branchKey] === "undefined") {
return def;
}
obj = obj[branchKey];
}
}
if (!isBasicObject(obj) || !hasOwnProperty(obj, leafKey) || typeof obj[leafKey] === "undefined") {
return def;
}
return obj[leafKey];
}
// object/setIn.ts
function isUnsafeKey(key) {
return key === "__proto__" || key === "constructor" || key === "prototype";
}
function setIn(target, path, value) {
if (!path || !isObject(target)) return target;
if (!Array.isArray(path)) {
path = path.split(".");
}
var leafKey = path[0];
if (isUnsafeKey(leafKey)) {
throw new Error('setIn: unsafe root key: "'.concat(String(leafKey), '"'));
}
var obj = target;
if (path.length > 1) {
var pathLength = path.length;
leafKey = path[pathLength - 1];
if (isUnsafeKey(leafKey)) {
throw new Error('setIn: unsafe leaf key: "'.concat(String(leafKey), '"'));
}
for(var i = 0; i < pathLength - 1; i++){
var branchKey = path[i];
if (isUnsafeKey(branchKey)) {
throw new Error('setIn: unsafe branch key: "'.concat(String(branchKey), '"'));
}
if (!hasOwnProperty(obj, branchKey)) {
obj[branchKey] = {};
}
obj = obj[branchKey];
}
}
if (typeof value !== "undefined") {
obj[leafKey] = value;
} else {
delete obj[leafKey];
}
return target;
}
// object/sortKeys.ts
var isArray = Array.isArray;
var keys = Object.keys;
function sortKeys(obj) {
if ((typeof obj === "undefined" ? "undefined" : _type_of(obj)) !== "object" || isArray(obj)) {
throw new Error("sortKeys");
}
var newObject = {};
var sortedKeys = keys(obj).sort();
for(var i = 0, l = sortedKeys.length; i < l; i++){
var k = sortedKeys[i];
newObject[k] = obj[k];
}
return newObject;
}
// object/sortKeysRec.ts
var isArray2 = Array.isArray;
var keys2 = Object.keys;
function sortKeysRec(obj) {
if (isArray2(obj)) {
var newArray = [];
for(var i = 0, l = obj.length; i < l; i++){
newArray[i] = sortKeysRec(obj[i]);
}
return newArray;
}
if ((typeof obj === "undefined" ? "undefined" : _type_of(obj)) !== "object" || obj === null) {
return obj;
}
var sortedKeys = keys2(obj).sort();
var newObject = {};
for(var i1 = 0, l1 = sortedKeys.length; i1 < l1; i1++){
var k = sortedKeys[i1];
newObject[k] = sortKeysRec(obj[k]);
}
return newObject;
}
// object/values.ts
function values(o) {
return Object.keys(o).map(function(key) {
return o[key];
});
}