@nsnanocat/util
Version:
Pure JS's util module for well-known iOS network tools
73 lines (64 loc) • 2.22 kB
JavaScript
/* https://www.lodashjs.com */
export class Lodash {
static escape(string) {
const map = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
return string.replace(/[&<>"']/g, m => map[m]);
}
static get(object = {}, path = "", defaultValue = undefined) {
// translate array case to dot case, then split with .
// a[0].b -> a.0.b -> ['a', '0', 'b']
if (!Array.isArray(path)) path = Lodash.toPath(path);
const result = path.reduce((previousValue, currentValue) => {
return Object(previousValue)[currentValue]; // null undefined get attribute will throwError, Object() can return a object
}, object);
return result === undefined ? defaultValue : result;
}
static omit(object = {}, paths = []) {
if (!Array.isArray(paths)) paths = [paths.toString()];
paths.forEach(path => Lodash.unset(object, path));
return object;
}
static pick(object = {}, paths = []) {
if (!Array.isArray(paths)) paths = [paths.toString()];
const filteredEntries = Object.entries(object).filter(([key, value]) => paths.includes(key));
return Object.fromEntries(filteredEntries);
}
static set(object, path, value) {
if (!Array.isArray(path)) path = Lodash.toPath(path);
path.slice(0, -1).reduce((previousValue, currentValue, currentIndex) => (Object(previousValue[currentValue]) === previousValue[currentValue] ? previousValue[currentValue] : (previousValue[currentValue] = /^\d+$/.test(path[currentIndex + 1]) ? [] : {})), object)[path[path.length - 1]] = value;
return object;
}
static toPath(value) {
return value
.replace(/\[(\d+)\]/g, ".$1")
.split(".")
.filter(Boolean);
}
static unescape(string) {
const map = {
"&": "&",
"<": "<",
">": ">",
""": '"',
"'": "'",
};
return string.replace(/&|<|>|"|'/g, m => map[m]);
}
static unset(object = {}, path = "") {
if (!Array.isArray(path)) path = Lodash.toPath(path);
const result = path.reduce((previousValue, currentValue, currentIndex) => {
if (currentIndex === path.length - 1) {
delete previousValue[currentValue];
return true;
}
return Object(previousValue)[currentValue];
}, object);
return result;
}
}