UNPKG

@leapfrogtechnology/db-model

Version:

Low-footprint database abstraction layer and model built on top of Knex.

118 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var snakecaseKeys = require("snakecase-keys"); var camelcaseKeys = require("camelcase-keys"); var ramda_1 = require("ramda"); /** * Recursively convert the object keys into camelCase. * * @param {any} obj * @returns {T} */ function toCamelCase(obj) { return camelcaseKeys(obj, { deep: true }); } exports.toCamelCase = toCamelCase; /** * Recursively convert the object keys into snake_case. * * @param {any} obj * @returns {T} */ function toSnakeCase(obj) { return snakecaseKeys(obj, { deep: true }); } exports.toSnakeCase = toSnakeCase; /** * Get the copy of list of objects without attributes. * * @param {any[]} obj * @param {any[]} attrsToExclude * @returns {T[]} */ function listWithoutAttrs(obj, attrsToExclude) { return obj.map(function (item) { return withoutAttrs(item, attrsToExclude); }); } exports.listWithoutAttrs = listWithoutAttrs; /** * Get the copy of object without attributes. * * @param {any} obj * @param {any[]} attrsToExclude * @returns {T} */ function withoutAttrs(obj, attrsToExclude) { if (Array.isArray(obj)) { // It is recommended to use listWithoutAttrs() function instead for arrays. throw new TypeError('withoutAttrs() expects first argument to be a plain object, array given.'); } var result = {}; Object.keys(obj).forEach(function (key) { if (!attrsToExclude.includes(key)) { result[key] = obj[key]; } }); return result; } exports.withoutAttrs = withoutAttrs; /** * Get the copy of object with only specified attributes. * * @param {any} obj * @param {any[]} attrs * @returns {T} */ function withOnlyAttrs(obj, attrs) { var result = {}; Object.keys(obj).forEach(function (key) { if (attrs.includes(key)) { result[key] = obj[key]; } }); return result; } exports.withOnlyAttrs = withOnlyAttrs; /** * Parse JSON encoded string and return object with camelcase keys. * * @param {string} encoded * @returns {T} */ function fromJson(encoded) { var parsedObject = JSON.parse(encoded); return toCamelCase(parsedObject); } exports.fromJson = fromJson; /** * Get number of keys from given value. * * @param {any} value * @returns {number} */ function getKeysLength(value) { if (!(value instanceof Object)) { return 0; } return Object.keys(value).length; } exports.getKeysLength = getKeysLength; /** * Checks whether two objects are partially equal. Two objects * are partially equal if one object is subset of another * objects. * E.g. a = { id: 1 } and b = { id: 1, name: 'John Doe' } * are partially equal. * * @param {object} a={} * @param {object} b={} * @returns {boolean} */ function isPartiallyEqual(a, b) { if (!(a instanceof Object) || !(b instanceof Object)) { return false; } var intersectObj = function (x, y) { return ramda_1.pick(Object.keys(x), y); }; return ramda_1.equals(a, intersectObj(a, b)); } exports.isPartiallyEqual = isPartiallyEqual; //# sourceMappingURL=object.js.map