Socket
Socket
Sign inDemoInstall

@empathyco/x-utils

Package Overview
Dependencies
Maintainers
5
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@empathyco/x-utils - npm Package Compare versions

Comparing version 0.1.0-alpha.4 to 0.1.0-alpha.5

121

dist/cjs/object.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isObject = void 0;
exports.isObject = exports.every = exports.getNewAndUpdatedKeys = exports.objectFilter = exports.cleanUndefined = exports.map = exports.reduce = exports.forEach = void 0;
/**
* Iterates over every non-undefined property of the object calling the callback passed as
* parameter.
*
* @param obj - The object to iterate through each property.
* @param callbackFn - The callback function to call for each property.
* @public
*/
function forEach(obj, callbackFn) {
if (obj == null) {
return;
}
let index = 0;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined) {
callbackFn(key, obj[key], index++);
}
}
}
exports.forEach = forEach;
/**
* Iterates through the obj properties calling the reducer function.
*
* @param obj - The object to iterate through each property.
* @param reducer - A function that will be called for each property, modifying the initialValue
* object.
* @param initialValue - The initial value of the accumulator property of the reducer function.
* @returns Result of the reducer function.
* @public
*/
function reduce(obj, reducer, initialValue) {
let accumulator = initialValue;
forEach(obj, (key, value, index) => {
accumulator = reducer(accumulator, key, value, index);
});
return accumulator;
}
exports.reduce = reduce;
/**
* Creates an object from another object transforming each property value.
*
* @param obj - The object to transform each property value.
* @param mapper - The mapper function which will transform each value.
* @returns A record with the result of the mapper.
* @public
*/
function map(obj, mapper) {
return reduce(obj, (accumulator, key, value, index) => {
accumulator[key] = mapper(key, value, index);
return accumulator;
}, {});
}
exports.map = map;
/**
* Creates an object picking only the not undefined properties.
*
* @param obj - The object from whom pick the values.
* @returns A new object with the not undefined properties of the source object.
* @public
*/
function cleanUndefined(obj) {
return typeof obj !== 'object' || obj === null || Array.isArray(obj)
? obj
: reduce(obj, (pickedObject, key, value) => {
pickedObject[key] = cleanUndefined(value);
return pickedObject;
}, {});
}
exports.cleanUndefined = cleanUndefined;
/**
* Creates an object picking only the ones that pass the test implemented by the
* provided function isIncluded.
*
* @param obj - T object to be filtered.
* @param isIncluded - Test function that every obj item must pass.
* @returns A filtered object.
* @public
*/
function objectFilter(obj, isIncluded) {
return reduce(obj, (accumulator, key, value, index) => {
if (isIncluded(key, value, index)) {
accumulator[key] = value;
}
return accumulator;
}, {});
}
exports.objectFilter = objectFilter;
/**
* Compares two objects of the same type, checking the values of their keys and retrieving
* those that were not present in the old value and/or those whose value has changed.
*
* @param newValue - The new object value.
* @param oldValue - The old object value.
*
* @returns An array of keys.
* @public
*/
function getNewAndUpdatedKeys(newValue, oldValue) {
if (newValue === oldValue || !newValue || !oldValue) {
return [];
}
return Object.keys(newValue).filter(key => !(key in oldValue) || newValue[key] !== oldValue[key]);
}
exports.getNewAndUpdatedKeys = getNewAndUpdatedKeys;
/**
* Ensures that the given condition is met in all the non-undefined entries of the object.
*
* @param object - The object to check if every item meets the given condition.
* @param condition - The condition to check in each one of the entries of the object.
*
* @returns True when all the entries pass the condition. False otherwise.
* @public
*/
function every(object, condition) {
return Object.entries(object)
.filter(([, value]) => value !== undefined)
.every(([key, value], index) => condition(key, value, index));
}
exports.every = every;
/**
* Returns true if the retrieved parameter is an object.

@@ -6,0 +125,0 @@ *

/**
* Iterates over every non-undefined property of the object calling the callback passed as
* parameter.
*
* @param obj - The object to iterate through each property.
* @param callbackFn - The callback function to call for each property.
* @public
*/
export function forEach(obj, callbackFn) {
if (obj == null) {
return;
}
let index = 0;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined) {
callbackFn(key, obj[key], index++);
}
}
}
/**
* Iterates through the obj properties calling the reducer function.
*
* @param obj - The object to iterate through each property.
* @param reducer - A function that will be called for each property, modifying the initialValue
* object.
* @param initialValue - The initial value of the accumulator property of the reducer function.
* @returns Result of the reducer function.
* @public
*/
export function reduce(obj, reducer, initialValue) {
let accumulator = initialValue;
forEach(obj, (key, value, index) => {
accumulator = reducer(accumulator, key, value, index);
});
return accumulator;
}
/**
* Creates an object from another object transforming each property value.
*
* @param obj - The object to transform each property value.
* @param mapper - The mapper function which will transform each value.
* @returns A record with the result of the mapper.
* @public
*/
export function map(obj, mapper) {
return reduce(obj, (accumulator, key, value, index) => {
accumulator[key] = mapper(key, value, index);
return accumulator;
}, {});
}
/**
* Creates an object picking only the not undefined properties.
*
* @param obj - The object from whom pick the values.
* @returns A new object with the not undefined properties of the source object.
* @public
*/
export function cleanUndefined(obj) {
return typeof obj !== 'object' || obj === null || Array.isArray(obj)
? obj
: reduce(obj, (pickedObject, key, value) => {
pickedObject[key] = cleanUndefined(value);
return pickedObject;
}, {});
}
/**
* Creates an object picking only the ones that pass the test implemented by the
* provided function isIncluded.
*
* @param obj - T object to be filtered.
* @param isIncluded - Test function that every obj item must pass.
* @returns A filtered object.
* @public
*/
export function objectFilter(obj, isIncluded) {
return reduce(obj, (accumulator, key, value, index) => {
if (isIncluded(key, value, index)) {
accumulator[key] = value;
}
return accumulator;
}, {});
}
/**
* Compares two objects of the same type, checking the values of their keys and retrieving
* those that were not present in the old value and/or those whose value has changed.
*
* @param newValue - The new object value.
* @param oldValue - The old object value.
*
* @returns An array of keys.
* @public
*/
export function getNewAndUpdatedKeys(newValue, oldValue) {
if (newValue === oldValue || !newValue || !oldValue) {
return [];
}
return Object.keys(newValue).filter(key => !(key in oldValue) || newValue[key] !== oldValue[key]);
}
/**
* Ensures that the given condition is met in all the non-undefined entries of the object.
*
* @param object - The object to check if every item meets the given condition.
* @param condition - The condition to check in each one of the entries of the object.
*
* @returns True when all the entries pass the condition. False otherwise.
* @public
*/
export function every(object, condition) {
return Object.entries(object)
.filter(([, value]) => value !== undefined)
.every(([key, value], index) => condition(key, value, index));
}
/**
* Returns true if the retrieved parameter is an object.

@@ -3,0 +115,0 @@ *

4

package.json
{
"name": "@empathyco/x-utils",
"version": "0.1.0-alpha.4",
"version": "0.1.0-alpha.5",
"description": "A utility package for Empathy search",

@@ -44,3 +44,3 @@ "author": "Empathy Systems Corporation S.L.",

},
"gitHead": "8efe7a9f47c1aad2fdfe6fc796a46ad0e2dc987d"
"gitHead": "e6bed958716d0fe7219030fa25e870f6cdaaf68a"
}

@@ -0,2 +1,71 @@

import { Dictionary } from './types';
/**
* Iterates over every non-undefined property of the object calling the callback passed as
* parameter.
*
* @param obj - The object to iterate through each property.
* @param callbackFn - The callback function to call for each property.
* @public
*/
export declare function forEach<T extends Dictionary>(obj: T | undefined | null, callbackFn: (key: keyof T, value: Exclude<T[keyof T], undefined>, index: number) => void): void;
/**
* Iterates through the obj properties calling the reducer function.
*
* @param obj - The object to iterate through each property.
* @param reducer - A function that will be called for each property, modifying the initialValue
* object.
* @param initialValue - The initial value of the accumulator property of the reducer function.
* @returns Result of the reducer function.
* @public
*/
export declare function reduce<T extends Dictionary, V>(obj: T | undefined | null, reducer: (accumulator: V, key: keyof T, value: Exclude<T[keyof T], undefined>, index: number) => V, initialValue: V): V;
/**
* Creates an object from another object transforming each property value.
*
* @param obj - The object to transform each property value.
* @param mapper - The mapper function which will transform each value.
* @returns A record with the result of the mapper.
* @public
*/
export declare function map<T extends Dictionary, W>(obj: T | undefined | null, mapper: (key: keyof T, value: Exclude<T[keyof T], undefined>, index: number) => W): Record<keyof T, W>;
/**
* Creates an object picking only the not undefined properties.
*
* @param obj - The object from whom pick the values.
* @returns A new object with the not undefined properties of the source object.
* @public
*/
export declare function cleanUndefined<T>(obj: T): T;
/**
* Creates an object picking only the ones that pass the test implemented by the
* provided function isIncluded.
*
* @param obj - T object to be filtered.
* @param isIncluded - Test function that every obj item must pass.
* @returns A filtered object.
* @public
*/
export declare function objectFilter<T extends Dictionary>(obj: T | undefined | null, isIncluded: (key: keyof T, value: Exclude<T[keyof T], undefined>, index: number) => boolean): T;
/**
* Compares two objects of the same type, checking the values of their keys and retrieving
* those that were not present in the old value and/or those whose value has changed.
*
* @param newValue - The new object value.
* @param oldValue - The old object value.
*
* @returns An array of keys.
* @public
*/
export declare function getNewAndUpdatedKeys<ObjectType extends Dictionary>(newValue: ObjectType | undefined, oldValue: ObjectType | undefined): (keyof ObjectType)[];
/**
* Ensures that the given condition is met in all the non-undefined entries of the object.
*
* @param object - The object to check if every item meets the given condition.
* @param condition - The condition to check in each one of the entries of the object.
*
* @returns True when all the entries pass the condition. False otherwise.
* @public
*/
export declare function every<ObjectType extends Dictionary>(object: ObjectType, condition: (key: keyof ObjectType, value: Exclude<ObjectType[keyof ObjectType], undefined>, index: number) => boolean): boolean;
/**
* Returns true if the retrieved parameter is an object.

@@ -3,0 +72,0 @@ *

@@ -8,2 +8,14 @@ /**

/**
* TypeScript type non-primitives. Array or Record with all possible types.
*
* @public
*/
export declare type NonPrimitive = Array<any> | Record<any, any>;
/**
* TypeScript type primitives. Basically every type possible except objects or arrays.
*
* @public
*/
export declare type Primitive = string | number | boolean | bigint | undefined | null | symbol | AnyFunction;
/**
* Retrieves the keys of a determinate type from a provided interface.

@@ -18,12 +30,8 @@ *

/**
* TypeScript type non-primitives. Array or Record with all possible types.
* Object where all its properties are strings, and the value of them is defined by the type of
* the T property.
*
* @param T - The type of the properties of the object.
* @public
*/
export declare type NonPrimitive = Array<any> | Record<any, any>;
/**
* TypeScript type primitives. Basically every type possible except objects or arrays.
*
* @public
*/
export declare type Primitive = string | number | boolean | bigint | undefined | null | symbol | AnyFunction;
export declare type Dictionary<T = any> = Record<string, T>;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc