@softvar/util-array
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -21,1 +21,48 @@ /** | ||
export declare function copy(arr: any[], mapFn: any): unknown[]; | ||
/** | ||
* Given two array of objects, check whether object differs on the given key | ||
* @since 0.0.9 | ||
* @param {Object[]} arr1 - input arr1 | ||
* @param {Object[]} arr2 - inpur arr2, to be evaluated with | ||
* @param key - key of the the object on which difference needs to be checked | ||
* | ||
* @returns {Object[]} - array of objects that differ on the key | ||
*/ | ||
export declare function checkIfDifferOnKey(arr1: { | ||
[key: string]: any; | ||
}[], arr2: { | ||
[key: string]: any; | ||
}[], key: string): object[]; | ||
/** | ||
* Find a particular object based on key from an array of objects | ||
* @since 0.0.9 | ||
* @param {Object[]} array - Array of objects | ||
* @param {String} key - Key to be searched | ||
* @param {String/Number} value - value to be matched | ||
* | ||
* @returns {Object} - Matched object | ||
*/ | ||
export declare function findObjectByKey(arr: { | ||
[key: string]: any; | ||
}[], key: string, value: string | number): object | null; | ||
/** | ||
* Find a particular object index based on key from an array of objects | ||
* @since 0.0.9 | ||
* @param {Array} array - Array of objects | ||
* @param {String} key - Key to be searched | ||
* @param {String/Number} value - Value to be matched | ||
* | ||
* @returns {Object} - Matched object | ||
*/ | ||
export declare function findObjectIndexByKey(arr: { | ||
[key: string]: any; | ||
}[], key: string, value: string | number): number | null; | ||
/** | ||
* Sort Array of objects on the given key of object | ||
* @since 0.0.9 | ||
* @param arr - Array of objects to be sorted | ||
* @param key - key of the object on which sorting is required | ||
* | ||
* @returns - sorted array of objects | ||
*/ | ||
export declare function sortOnKey(arr: object[], key: string): object[] | []; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.copy = void 0; | ||
exports.sortOnKey = exports.findObjectIndexByKey = exports.findObjectByKey = exports.checkIfDifferOnKey = exports.copy = void 0; | ||
/** | ||
@@ -28,2 +28,79 @@ * @file All array related utilities | ||
exports.copy = copy; | ||
/** | ||
* Given two array of objects, check whether object differs on the given key | ||
* @since 0.0.9 | ||
* @param {Object[]} arr1 - input arr1 | ||
* @param {Object[]} arr2 - inpur arr2, to be evaluated with | ||
* @param key - key of the the object on which difference needs to be checked | ||
* | ||
* @returns {Object[]} - array of objects that differ on the key | ||
*/ | ||
function checkIfDifferOnKey(arr1, arr2, key) { | ||
return arr1.filter((obj1) => { | ||
return (arr2.filter((obj2) => { | ||
return obj1[key] === obj2[key]; | ||
}).length === 0); | ||
}); | ||
} | ||
exports.checkIfDifferOnKey = checkIfDifferOnKey; | ||
/** | ||
* Find a particular object based on key from an array of objects | ||
* @since 0.0.9 | ||
* @param {Object[]} array - Array of objects | ||
* @param {String} key - Key to be searched | ||
* @param {String/Number} value - value to be matched | ||
* | ||
* @returns {Object} - Matched object | ||
*/ | ||
function findObjectByKey(arr, key, value) { | ||
if (!arr) { | ||
return null; | ||
} | ||
for (const item of arr) { | ||
if (item[key] === value) { | ||
return item; | ||
} | ||
} | ||
return null; | ||
} | ||
exports.findObjectByKey = findObjectByKey; | ||
/** | ||
* Find a particular object index based on key from an array of objects | ||
* @since 0.0.9 | ||
* @param {Array} array - Array of objects | ||
* @param {String} key - Key to be searched | ||
* @param {String/Number} value - Value to be matched | ||
* | ||
* @returns {Object} - Matched object | ||
*/ | ||
function findObjectIndexByKey(arr, key, value) { | ||
if (!arr) { | ||
return null; | ||
} | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i][key] === value) { | ||
return i; | ||
} | ||
} | ||
return null; | ||
} | ||
exports.findObjectIndexByKey = findObjectIndexByKey; | ||
/** | ||
* Sort Array of objects on the given key of object | ||
* @since 0.0.9 | ||
* @param arr - Array of objects to be sorted | ||
* @param key - key of the object on which sorting is required | ||
* | ||
* @returns - sorted array of objects | ||
*/ | ||
function sortOnKey(arr, key) { | ||
if (!arr || !arr.length || !key) { | ||
return []; | ||
} | ||
arr.sort((a, b) => { | ||
return b[key] > a[key] ? -1 : a[key] > b[key] ? 1 : 0; | ||
}); | ||
return arr; | ||
} | ||
exports.sortOnKey = sortOnKey; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@softvar/util-array", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "array utility", | ||
@@ -32,3 +32,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "a3ac5b3d7a86a03e6c07cb83fa6a4359f63877f0" | ||
"gitHead": "673c4400598adac744246dce69c2f1d8c4eb24e9" | ||
} |
## util-array | ||
[![Maintained with Lerna](https://img.shields.io/badge/maintained%20with-lerna-blue?style=for-the-badge)](https://lerna.js.org/) | ||
[![License](https://img.shields.io/badge/License-MIT-green.svg?style=for-the-badge&logo=)](https://opensource.org/licenses/MIT) | ||
![TypeScript: Strict](https://img.shields.io/badge/typescript-strict-yellow?style=for-the-badge) | ||
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-e72163?style=for-the-badge)](https://prettier.io) | ||
[![npm version](https://badge.fury.io/js/%40softvar%2Futil-array.svg)](https://badge.fury.io/js/%40softvar%2Futil-array) | ||
### License | ||
MIT (c) Varun Malhotra 2020-present |
Sorry, the diff of this file is not supported yet
9235
170
14