UNPKG

@korbiniankuhn/validator

Version:

Validate object schemas for js, expressjs, angular and mongoose.

122 lines 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isObjectObject = (value) => { return (value != null && typeof value === 'object' && Array.isArray(value) === false); }; exports.isPlainObject = (value) => { let ctor, prot; if (exports.isObjectObject(value) === false) { return false; } ctor = value.constructor; if (typeof ctor !== 'function') { return false; } prot = ctor.prototype; if (exports.isObjectObject(prot) === false) { return false; } if (prot.hasOwnProperty('isPrototypeOf') === false) { return false; } return true; }; exports.get = (object, path, defaultValue) => { let temp = object; try { path.split('.').map(key => { temp = temp[key]; }); } catch (err) { return defaultValue; } return temp === undefined ? defaultValue : temp; }; exports.set = (object, path, value) => { let temp = object; const keys = path.split('.'); const length = keys.length - 1; for (let i = 0; i < length; i++) { const key = keys[i]; if (temp[key] === undefined) { temp[key] = {}; } temp = temp[key]; } temp[keys[length]] = value; }; exports.has = (object, path) => { return exports.get(object, path) !== undefined; }; exports.isEqual = (value, other) => { try { return JSON.stringify(value) === JSON.stringify(other); } catch (err) { return false; } }; exports.uniqWith = (array, comparator) => { const unique = []; array.map(v => { if (unique.find(o => comparator(o, v)) === undefined) { unique.push(v); } }); return unique; }; exports.isNull = (value) => value === null; exports.isNil = (value) => value == null; exports.isNotNil = (value) => !exports.isNil(value); exports.isUndefined = (value) => value === undefined; exports.isNotUndefined = (value) => !exports.isUndefined(value); exports.keys = Object.keys; exports.isString = (value) => Object.prototype.toString.call(value) === '[object String]'; exports.isBoolean = (value) => Object.prototype.toString.call(value) === '[object Boolean]'; exports.isInteger = (value) => Number.isInteger(value); exports.isNumber = (value) => Object.prototype.toString.call(value) === '[object Number]'; exports.isSyncFunction = (value) => Object.prototype.toString.call(value) === '[object Function]'; exports.isAsyncFunction = (value) => value.constructor.name === 'AsyncFunction'; exports.isFunction = (value) => exports.isSyncFunction(value) || exports.isAsyncFunction(value); exports.isRegExp = (value) => Object.prototype.toString.call(value) === '[object RegExp]'; exports.isArray = (value) => Array.isArray(value); exports.isObject = (value) => { const type = typeof value; return value != null && (type == 'object' || type == 'function'); }; exports.defaultToAny = (...values) => { for (const value of values) { if (exports.isNotNil(value)) return value; } return undefined; }; exports.removeUndefinedProperties = (object) => { Object.keys(object).forEach(key => exports.isUndefined(object[key]) && delete object[key]); return object; }; exports.removeNilProperties = (object) => { Object.keys(object).forEach(key => exports.isNil(object[key]) && delete object[key]); return object; }; exports.clone = (object) => { return JSON.parse(JSON.stringify(object)); }; const REGEX_FLAGS = { global: 'g', ignoreCase: 'i', multiline: 'm', dotAll: 's', sticky: 'y', unicode: 'u' }; exports.cloneRegex = (regex) => { const flags = Object.keys(REGEX_FLAGS) .map(flag => (regex[flag] ? REGEX_FLAGS[flag] : '')) .join(''); const clonedRegexp = new RegExp(regex.source, flags); clonedRegexp.lastIndex = regex.lastIndex; return clonedRegexp; }; //# sourceMappingURL=lodash.js.map