export = ObjectUtils; /** * @module shared/objectUtils */ /** * @callback ObjectUtilsShouldFlatFn * @param {string} key The key for the property object that is being parsed. * @param {Object} value The value of the property object that is being parsed. * @returns {boolean} Whether or not the method should flat a sub object. * @parent module:shared/objectUtils */ /** * @typedef {Object.} ObjectUtilsExtractPathsDictionary * @parent module:shared/objectUtils */ /** * @typedef {ObjectUtilsExtractPathsDictionary | string} ObjectUtilsExtractPath * @parent module:shared/objectUtils */ /** * A small collection of utility methods to work with objects. * * @parent module:shared/objectUtils * @tutorial objectUtils */ declare class ObjectUtils { /** * Creates a deep copy of a given object. * * @param {Object} target The object to copy. * @returns {Object} */ static copy(target: any): any; /** * A shorthand method for {@link ObjectUtils.formatKeys} that transforms the keys from * `dash-case` to `lowerCamelCase`. * * @param {Object} target The object for format. * @param {string[]} [include=[]] A list of keys or paths where the * transformation will be made. If not specified, * the method will use all the keys from the * object. * @param {string[]} [exclude=[]] A list of keys or paths where the * transformation won't be made. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components for both `include` and `exclude`. * @returns {Object} */ static dashToLowerCamelKeys(target: any, include?: string[], exclude?: string[], pathDelimiter?: string): any; /** * A shorthand method for {@link ObjectUtils.formatKeys} that transforms the keys from * `dash-case` to `snake_case`. * * @param {Object} target The object for format. * @param {string[]} [include=[]] A list of keys or paths where the * transformation will be made. If not specified, * the method will use all the keys from the * object. * @param {string[]} [exclude=[]] A list of keys or paths where the * transformation won't be made. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components for both `include` and `exclude`. * @returns {Object} */ static dashToSnakeKeys(target: any, include?: string[], exclude?: string[], pathDelimiter?: string): any; /** * Deletes a property of an object using a path. * * @param {Object} target The object from where the property will * be removed. * @param {string} objPath The path to the property. * @param {string} [pathDelimiter='.'] The delimiter that will separate the * path components. * @param {boolean} [cleanEmptyProperties=true] If this flag is `true` and after * removing the property the parent object * is empty, it will remove it recursively * until a non empty parent object is * found. * @param {boolean} [failWithError=false] Whether or not to throw an error when * the path is invalid. If this is * `false`, the method will silently fail. * @returns {Object} A copy of the original object with the removed property/properties. * @example * * const target = { * propOne: { * propOneSub: 'Charito!', * }, * propTwo: '!!!', * }; * console.log(ObjectUtils.delete(target, 'propOne.propOneSub')); * // Will output { propTwo: '!!!' } * */ static delete(target: any, objPath: string, pathDelimiter?: string, cleanEmptyProperties?: boolean, failWithError?: boolean): any; /** * Extracts a property or properties from an object in order to create a new one. * * @param {Object} target * The object from where the property/properties will be extracted. * @param {ObjectUtilsExtractPath | ObjectUtilsExtractPath[]} objPaths * This can be a single path or a list of them. And for this method, the paths are not * only strings but can also be an object with a single key, the would be the path to * where to "do the extraction", and the value the path on the target object. * @param {string} [pathDelimiter='.'] * The delimiter that will separate the path components. * @param {boolean} [failWithError=false] * Whether or not to throw an error when the path is invalid. If this is `false`, the * method will silently fail an empty object. * @returns {Object} * @example * * const target = { * name: { * first: 'Rosario', * }, * age: 3, * address: { * planet: 'earth', * something: 'else', * }, * }; * console.log( * ObjectUtils.set(obj, [{ name: 'name.first' }, 'age', 'address.planet']), * ); * // Will output { name: 'Rosario', age: 3, address: { planet: 'earth' } } * */ static extract(target: any, objPaths: ObjectUtilsExtractPath | ObjectUtilsExtractPath[], pathDelimiter?: string, failWithError?: boolean): any; /** * Flatterns an object properties into a single level dictionary. * * @param {Object} target * The object to transform. * @param {string} [pathDelimiter='.'] * The delimiter that will separate the path components. * @param {string} [prefix=''] * A custom prefix to be added before the name of the properties. This can be used on * custom cases and it's also used when the method calls itself in order to flattern a * sub object. * @param {?ObjectUtilsShouldFlatFn} [shouldFlattern=null] * A custom function that can be used in order to tell the method whether an Object or * an Array property should be flattern or not. It will receive the key for the property * and the Object/Array itself. * @returns {Object} * @example * * const target = { * propOne: { * propOneSub: 'Charito!', * }, * propTwo: '!!!', * }; * console.log(ObjectUtils.flat(target); * // Will output { 'propOne.propOneSub': 'Charito!', propTwo: '!!!' } * */ static flat(target: any, pathDelimiter?: string, prefix?: string, shouldFlattern?: ObjectUtilsShouldFlatFn | null): any; /** * Formats all the keys on an object using a way similar to `.replace(regexp, ...)` but * that also works recursively and with _"object paths"_. * * @param {Object} target The object for format. * @param {RegExp} searchExpression The regular expression the method will use * "match" the keys. * @param {Function} replaceWith The callback the method will call when * formatting a replacement. Think of * `searchExpression` and `replaceWith` as the * parameters of a `.replace` call, * where the object is the key. * @param {string[]} [include=[]] A list of keys or paths where the * transformation will be made. If not specified, * the method will use all the keys from the * object. * @param {string[]} [exclude=[]] A list of keys or paths where the * transformation won't be made. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components for both `include` and `exclude`. * @returns {Object} * @example * * const target = { * prop_one: 'Charito!', * }; * console.log( * ObjectUtils.formatKeys( * target, * // Find all the keys with snake case. * /([a-z])_([a-z])/g, * // Using the same .replace style callback, replace it with lower camel case. * (fullMatch, firstLetter, secondLetter) => { * const newSecondLetter = secondLetter.toUpperCase(); * return `${firstLetter}${newSecondLetter}`; * }, * ), * ); * // Will output { propOne: 'Charito!}. * */ static formatKeys(target: any, searchExpression: RegExp, replaceWith: Function, include?: string[], exclude?: string[], pathDelimiter?: string): any; /** * Returns the value of an object property using a path. * * @param {Object} target The object from where the property will be * read. * @param {string} objPath The path to the property. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components. * @param {boolean} [failWithError=false] Whether or not to throw an error when the * path is invalid. If this is `false`, the * method will silently fail and return * `undefined`. * @returns {*} * @throws {Error} If the path is invalid and `failWithError` is set to `true`. * @example * * const obj = { * propOne: { * propOneSub: 'Charito!', * }, * propTwo: '!!!', * }; * console.log(ObjectUtils.get(obj, 'propOne.propOneSub')); * // Will output 'Charito!' * */ static get(target: any, objPath: string, pathDelimiter?: string, failWithError?: boolean): any; /** * A shorthand method for {@link ObjectUtils.formatKeys} that transforms the keys from * `lowerCamelCase` to `dash-case`. * * @param {Object} target The object for format. * @param {string[]} [include=[]] A list of keys or paths where the * transformation will be made. If not specified, * the method will use all the keys from the * object. * @param {string[]} [exclude=[]] A list of keys or paths where the * transformation won't be made. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components for both `include` and `exclude`. * @returns {Object} */ static lowerCamelToDashKeys(target: any, include?: string[], exclude?: string[], pathDelimiter?: string): any; /** * A shorthand method for {@link ObjectUtils.formatKeys} that transforms the keys from * `lowerCamelCase` to `snake_case`. * * @param {Object} target The object for format. * @param {string[]} [include=[]] A list of keys or paths where the * transformation will be made. If not specified, * the method will use all the keys from the * object. * @param {string[]} [exclude=[]] A list of keys or paths where the * transformation won't be made. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components for both `include` and `exclude`. * @returns {Object} */ static lowerCamelToSnakeKeys(target: any, include?: string[], exclude?: string[], pathDelimiter?: string): any; /** * This method makes a deep merge of a list of objects into a new one. The method also * supports arrays. * * @param {...Object} targets The objects to merge. * @returns {Object} * @example * * const objA = { a: 'first' }; * const objB = { b: 'second' }; * console.log(ObjectUtils.merge(objA, objB)); * // Will output { a: 'first', b: 'second' } * * @example * * const arrA = [{ a: 'first' }]; * const arrB = [{ b: 'second' }]; * console.log(ObjectUtils.merge(objA, objB)); * // Will output [{ a: 'first', b: 'second' }] * */ static merge(...targets: any[]): any; /** * Sets a property on an object using a path. If the path doesn't exist, it will be * created. * * @param {Object} target The object where the property will be set. * @param {string} objPath The path for the property. * @param {*} value The value to set on the property. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components. * @param {boolean} [failWithError=false] Whether or not to throw an error when the * path is invalid. If this is `false`, the * method will silently fail and return * `undefined`. * @returns {Object} A copy of the original object with the added property/properties. * @throws {Error} If one of the path components is for a non-object property and * `failWithError` is set to `true`. * @example * * const target = {}; * console.log(ObjectUtils.set(target, 'some.prop.path', 'some-value')); * // Will output { some: { prop: { path: 'some-value' } } } * */ static set(target: any, objPath: string, value: any, pathDelimiter?: string, failWithError?: boolean): any; /** * A shorthand method for {@link ObjectUtils.formatKeys} that transforms the keys from * `snake_case` to `dash-case`. * * @param {Object} target The object for format. * @param {string[]} [include=[]] A list of keys or paths where the * transformation will be made. If not specified, * the method will use all the keys from the * object. * @param {string[]} [exclude=[]] A list of keys or paths where the * transformation won't be made. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components for both `include` and `exclude`. * @returns {Object} */ static snakeToDashKeys(target: any, include?: string[], exclude?: string[], pathDelimiter?: string): any; /** * A shorthand method for {@link ObjectUtils.formatKeys} that transforms the keys from * `snake_case` to `lowerCamelCase`. * * @param {Object} target The object for format. * @param {string[]} [include=[]] A list of keys or paths where the * transformation will be made. If not specified, * the method will use all the keys from the * object. * @param {string[]} [exclude=[]] A list of keys or paths where the * transformation won't be made. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components for both `include` and `exclude`. * @returns {Object} */ static snakeToLowerCamelKeys(target: any, include?: string[], exclude?: string[], pathDelimiter?: string): any; /** * This method does the exact opposite from `flat`: It takes an already flattern object * and restores it structure. * * @param {Object} target The object to transform. * @param {string} [pathDelimiter='.'] The delimiter that will separate the path * components. * @returns {Object} * @example * * const target = { * 'propOne.propOneSub': 'Charito!', * propTwo: '!!!', * }; * console.log(ObjectUtils.unflat(target)); * // Will output { propOne: { propOneSub: 'Charito!' }, 'propTwo': '!!!' } * */ static unflat(target: any, pathDelimiter?: string): any; } declare namespace ObjectUtils { export { ObjectUtilsShouldFlatFn, ObjectUtilsExtractPathsDictionary, ObjectUtilsExtractPath }; } type ObjectUtilsExtractPath = ObjectUtilsExtractPathsDictionary | string; type ObjectUtilsShouldFlatFn = (key: string, value: any) => boolean; type ObjectUtilsExtractPathsDictionary = { [x: string]: string; };