object-array-utils
Advanced tools
+2
-2
| { | ||
| "name": "object-array-utils", | ||
| "version": "1.3.0", | ||
| "version": "1.4.0", | ||
| "description": "Utilities for working with arrays and objects", | ||
@@ -10,3 +10,3 @@ "scripts": { | ||
| "devDependencies": { | ||
| "@babel/core": "^7.17.5", | ||
| "@babel/core": "^7.17.8", | ||
| "@babel/preset-env": "^7.16.11", | ||
@@ -13,0 +13,0 @@ "jest": "^27.5.1" |
+4
-0
@@ -58,2 +58,6 @@ # `object-array-utils` | ||
| import { filterProps } from 'object-array-utils'; | ||
| filterProps({ prop1: 1, prop2: 2 }, ['prop1', 'prop3']) // { prop1: 1 } | ||
| import { isObjectSubset } from 'object-array-utils'; | ||
@@ -60,0 +64,0 @@ |
+23
-0
@@ -169,2 +169,24 @@ function isNullOrUndefined(v) { | ||
| function filterProps(o, props) { | ||
| return props.reduce((newObject, prop) => { | ||
| return (prop in o) | ||
| ? { ...newObject, [prop]: o[prop] } | ||
| : newObject; | ||
| }, {}); | ||
| } | ||
| // https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/groupBy | ||
| // function groupArrayElementsBy(arrayOfObjects, getKey) { | ||
| // return arrayOfObjects.reduce(function (resultingObject, object) { | ||
| // const key = getKey(object); | ||
| // if (!resultingObject[key]) { | ||
| // resultingObject[key] = []; | ||
| // } | ||
| // resultingObject[key].push(object); | ||
| // return resultingObject; | ||
| // }, {}); | ||
| // } | ||
| function isObjectSubset(superObject, subObject, options = {}) { | ||
@@ -271,2 +293,3 @@ if (!isObject(superObject) || !isObject(subObject)) { | ||
| deepFreeze, | ||
| filterProps, | ||
| hasObjectProp, | ||
@@ -273,0 +296,0 @@ hasObjectProps, |
+10
-2
| import { | ||
| areArraysEqual, | ||
| areObjectsEqual, | ||
| isEmptyArray, | ||
| deepFreeze, | ||
| hasObjectProps | ||
| filterProps, | ||
| hasObjectProps, | ||
| isEmptyArray | ||
| } from './index'; | ||
@@ -75,2 +76,9 @@ | ||
| test('filterProps', () => { | ||
| expect(filterProps({ foo: 1, bar: 2 }, ['foo'])).toEqual({ foo: 1 }); | ||
| expect(filterProps({ foo: 1, bar: 2 }, ['bar'])).toEqual({ bar: 2 }); | ||
| expect(filterProps({ foo: 1, bar: 2 }, ['bar', 'foo'])).toEqual({ foo: 1, bar: 2 }); | ||
| expect(filterProps({ foo: 1, bar: 2 }, ['bar', 'foo', 'baz'])).toEqual({ foo: 1, bar: 2 }); | ||
| }); | ||
| test('isEmptyArray', () => { | ||
@@ -77,0 +85,0 @@ expect(isEmptyArray([])).toBeTruthy(); |
24851
4.87%306
9.29%102
4.08%