@enonic/js-utils
Version:
Enonic XP JavaScript Utils
225 lines (206 loc) • 5.55 kB
JavaScript
// object/deleteIn.ts
function deleteIn(obj, ...paths) {
if (!obj || !paths) {
return;
}
const uniformPath = [];
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (typeof path === "string") {
path.split(".").forEach((p) => uniformPath.push(p));
} else if (Array.isArray(path)) {
path.forEach((p) => uniformPath.push(p));
} else {
uniformPath.push(path);
}
}
for (let i = 0; i < uniformPath.length - 1; i++) {
obj = obj[uniformPath[i]];
if (typeof obj === "undefined") {
return;
}
}
delete obj[uniformPath.pop()];
}
var deleteIn_default = deleteIn;
// value/isObject.ts
var isObject = (value) => Object.prototype.toString.call(value).slice(8, -1) === "Object";
// value/toStr.ts
function toStr(value, replacer, space = 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:${toStr(obj)}`);
}
const result = {};
const keys3 = Object.keys(obj);
for (let i = 0; i < keys3.length; i++) {
const key = keys3[i];
fn({
key,
result,
value: obj[key]
});
}
return result;
}
// object/lcKeys.ts
function lcKeys(obj) {
return mapKeys(obj, ({
key,
result,
value
}) => {
result[String(key).toLowerCase()] = value;
});
}
// object/entries.ts
function entries(o) {
return Object.keys(o).map((key) => [key, o[key]]);
}
// value/isBasicObject.ts
var isBasicObject = (value) => typeof value === "object";
// value/isNumber.ts
function isNumber(value) {
return typeof value === "number" && isFinite(value);
}
// value/isStringLiteral.ts
var isStringLiteral = (value) => typeof value === "string";
// value/isStringObject.ts
var isStringObject = (value) => value instanceof String;
// value/isString.ts
var isString = (value) => isStringLiteral(value) || isStringObject(value);
// value/isSymbol.ts
var isSymbol = (value) => typeof value === "symbol";
// value/isPropertyKey.ts
var isPropertyKey = (value) => 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! ${toStr(obj)}`);
}
if (!isPropertyKey(propKey)) {
throw new Error(`Second parameter to hasOwnProperty must be a PropertyKey (string|number|symbol)! ${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(".");
}
}
let leafKey = path[0];
let obj = source;
if (path.length > 1) {
const pathLength = path.length;
leafKey = path[pathLength - 1];
for (let i = 0; i < pathLength - 1; i++) {
const 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(".");
}
let leafKey = path[0];
if (isUnsafeKey(leafKey)) {
throw new Error(`setIn: unsafe root key: "${String(leafKey)}"`);
}
let obj = target;
if (path.length > 1) {
const pathLength = path.length;
leafKey = path[pathLength - 1];
if (isUnsafeKey(leafKey)) {
throw new Error(`setIn: unsafe leaf key: "${String(leafKey)}"`);
}
for (let i = 0; i < pathLength - 1; i++) {
const branchKey = path[i];
if (isUnsafeKey(branchKey)) {
throw new Error(`setIn: unsafe branch key: "${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;
var { keys } = Object;
function sortKeys(obj) {
if (typeof obj !== "object" || isArray(obj)) {
throw new Error("sortKeys");
}
const newObject = {};
const sortedKeys = keys(obj).sort();
for (let i = 0, l = sortedKeys.length; i < l; i++) {
const k = sortedKeys[i];
newObject[k] = obj[k];
}
return newObject;
}
// object/sortKeysRec.ts
var { isArray: isArray2 } = Array;
var { keys: keys2 } = Object;
function sortKeysRec(obj) {
if (isArray2(obj)) {
const newArray = [];
for (let i = 0, l = obj.length; i < l; i++) {
newArray[i] = sortKeysRec(obj[i]);
}
return newArray;
}
if (typeof obj !== "object" || obj === null) {
return obj;
}
const sortedKeys = keys2(obj).sort();
const newObject = {};
for (let i = 0, l = sortedKeys.length; i < l; i++) {
const k = sortedKeys[i];
newObject[k] = sortKeysRec(obj[k]);
}
return newObject;
}
// object/values.ts
function values(o) {
return Object.keys(o).map((key) => o[key]);
}
export {
deleteIn_default as deleteIn,
entries,
getIn,
hasOwnProperty,
lcKeys,
mapKeys,
setIn,
sortKeys,
sortKeysRec,
values
};