UNPKG

abolish

Version:

A javascript object validator.

115 lines (114 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.abolish_UpperFirst = abolish_UpperFirst; exports.abolish_StartCase = abolish_StartCase; exports.abolish_Pick = abolish_Pick; exports.abolish_Omit = abolish_Omit; exports.abolish_Set = abolish_Set; exports.abolish_Get = abolish_Get; exports.hasDotNotation = hasDotNotation; exports.InstanceOf = InstanceOf; const lodash_1 = require("lodash"); /** * Change to string to upperFirst * @param str * @constructor */ function abolish_UpperFirst(str) { return str[0].toUpperCase() + str.substring(1); } /** * abolish_StartCase * @param str * @param abolishInstance * @constructor */ function abolish_StartCase(str, abolishInstance) { return abolishInstance ? abolishInstance.config.useStartCaseInErrors ? (0, lodash_1.startCase)(str) : str : (0, lodash_1.startCase)(str); } /** * Pick keys from object * @param obj * @param keys * @param $hasDotFields */ function abolish_Pick(obj, keys, $hasDotFields) { // Create new object const picked = {}; const hasDotKeys = $hasDotFields === undefined ? keys.some(hasDotNotation) : $hasDotFields; // Loop through props and push to new object if (hasDotKeys) { for (let prop of keys) { picked[prop] = abolish_Get(obj, prop); } } else { for (let prop of keys) { picked[prop] = obj[prop]; } } // Return new object return picked; } function abolish_Omit(obj, keys) { // Create new object const picked = {}; // Loop through props and push to new object for (let prop in obj) { if (keys.includes(prop)) continue; picked[prop] = obj[prop]; } // Return new object return picked; } /** * Abolish_Set * Because lodash is slow, we will only include it when there is a dot notation in the path. * @param obj * @param path * @param value * @param $hasDotNotation */ function abolish_Set(obj, path, value, $hasDotNotation) { if ($hasDotNotation === undefined && hasDotNotation(path)) { return (0, lodash_1.set)(obj, path, value); } else { obj[path] = value; return obj; } } /** * Abolish_Get * Because lodash is slow, we will only include it when there is a dot notation in the path. * @param obj * @param path * @param $hasDotNotation */ function abolish_Get(obj, path, $hasDotNotation) { if ($hasDotNotation === undefined && hasDotNotation(path)) { return (0, lodash_1.get)(obj, path); } else { return obj[path]; } } /** * Check if a string has a dot notation * @param path */ function hasDotNotation(path) { return path.indexOf(".") !== -1; } /** * Instanceof * After running some tests with instanceof, we decided to use this instead. */ function InstanceOf(type, obj) { return typeof obj === "object" && obj instanceof type; }