@voiceflow/common
Version:
Junk drawer of utility functions
91 lines (90 loc) • 3.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.shallowPartialEquals = exports.mapValue = exports.mapEntry = exports.filterEntries = exports.omitBy = exports.pickBy = exports.pick = exports.omit = exports.hasProperty = exports.isPlainObject = exports.isObject = exports.selectValue = exports.selectKey = exports.selectID = exports.selectField = exports.shallowEquals = void 0;
const isPlainObject_js_1 = __importDefault(require("lodash/isPlainObject.js"));
var shallowequal_1 = require("shallowequal");
Object.defineProperty(exports, "shallowEquals", { enumerable: true, get: function () { return __importDefault(shallowequal_1).default; } });
const selectField = (field) => (obj) => obj[field];
exports.selectField = selectField;
exports.selectID = (0, exports.selectField)('id');
exports.selectKey = (0, exports.selectField)('key');
exports.selectValue = (0, exports.selectField)('value');
const isObject = (obj) => obj !== null && typeof obj === 'object';
exports.isObject = isObject;
const isPlainObject = (obj) => (0, isPlainObject_js_1.default)(obj);
exports.isPlainObject = isPlainObject;
const hasProperty = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
exports.hasProperty = hasProperty;
const omit = (obj, keys) => {
const newObj = { ...obj };
if (keys.length > 3) {
return keys.reduce((acc, key) => {
delete acc[key];
return acc;
}, newObj);
}
delete newObj[keys[0]];
if (keys.length === 1)
return newObj;
delete newObj[keys[1]];
if (keys.length === 2)
return newObj;
delete newObj[keys[2]];
return newObj;
};
exports.omit = omit;
const pick = (obj, keys) => {
const newObj = {};
if (keys.length > 3) {
return keys.reduce((acc, key) => {
if ((0, exports.hasProperty)(obj, key)) {
acc[key] = obj[key];
}
return acc;
}, newObj);
}
if (keys.length === 0)
return newObj;
if ((0, exports.hasProperty)(obj, keys[0])) {
newObj[keys[0]] = obj[keys[0]];
}
if (keys.length === 1)
return newObj;
if ((0, exports.hasProperty)(obj, keys[1])) {
newObj[keys[1]] = obj[keys[1]];
}
if (keys.length === 2)
return newObj;
if ((0, exports.hasProperty)(obj, keys[2])) {
newObj[keys[2]] = obj[keys[2]];
}
return newObj;
};
exports.pick = pick;
const pickBy = (obj, predicate) => Object.entries(obj).reduce((acc, [key, value]) => {
if (predicate(key, value)) {
acc[key] = value;
}
return acc;
}, {});
exports.pickBy = pickBy;
const omitBy = (obj, predicate) => Object.entries(obj).reduce((acc, [key, value]) => {
if (predicate(key, value)) {
delete acc[key];
}
return acc;
}, { ...obj });
exports.omitBy = omitBy;
/**
* @deprecated use pickBy instead
*/
exports.filterEntries = exports.pickBy;
const mapEntry = (obj, callback) => Object.fromEntries(Object.entries(obj).map(callback));
exports.mapEntry = mapEntry;
const mapValue = (obj, callback) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, callback(value)]));
exports.mapValue = mapValue;
const shallowPartialEquals = (obj, partial) => Object.entries(partial).every(([key, partialValue]) => (0, exports.hasProperty)(obj, key) && partialValue === obj[key]);
exports.shallowPartialEquals = shallowPartialEquals;