object-array-utils
Advanced tools
+1
-1
| { | ||
| "name": "object-array-utils", | ||
| "version": "2.10.0", | ||
| "version": "2.11.0", | ||
| "description": "Utilities for working with arrays and objects", | ||
@@ -5,0 +5,0 @@ "scripts": { |
+5
-0
@@ -69,2 +69,7 @@ # `object-array-utils` | ||
| import { rejectProperties } from 'object-array-utils'; | ||
| rejectProperties({ prop1: 1, prop2: 2 }, ['prop1', 'prop3']) // { prop2: 2 } | ||
| rejectProperties({ prop1: 1, prop2: 2 }, (_key, val) => val < 2) // { prop2: 2 } | ||
| import { takeProperties } from 'object-array-utils'; | ||
@@ -71,0 +76,0 @@ |
+21
-1
@@ -248,2 +248,21 @@ function isNullOrUndefined(v) { | ||
| function rejectProperties(o, arg) { | ||
| return isArray(arg) | ||
| ? rejectPropsByWhitelist(o, arg) | ||
| : rejectPropsByFun(o, arg); | ||
| } | ||
| function rejectPropsByWhitelist(o, props) { | ||
| return Object.keys(o).reduce((newObject, prop) => { | ||
| return (props.includes(prop)) | ||
| ? newObject | ||
| : { ...newObject, [prop]: o[prop] }; | ||
| }, {}); | ||
| } | ||
| function rejectPropsByFun(o, fun) { | ||
| const filteredEntries = Object.entries(o).filter(([key, val]) => !fun(key, val)); | ||
| return Object.fromEntries(filteredEntries); | ||
| } | ||
| function takeProperties(o, arg) { | ||
@@ -341,3 +360,3 @@ return isArray(arg) | ||
| // https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/groupBy | ||
| // https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/group | ||
| // function groupArrayElementsBy(arrayOfObjects, getKey) { | ||
@@ -485,2 +504,3 @@ // return arrayOfObjects.reduce(function (resultingObject, object) { | ||
| isPrimitive, | ||
| rejectProperties, | ||
| removeArrayElement, | ||
@@ -487,0 +507,0 @@ removeArrayElementByIndex, |
+18
-0
@@ -16,2 +16,3 @@ import { | ||
| isPrimitive, | ||
| rejectProperties, | ||
| removeArrayElement, | ||
@@ -116,2 +117,19 @@ removeArrayElementByIndex, | ||
| test('rejectProperties using whitelist of props', () => { | ||
| expect(rejectProperties({ foo: 1, bar: 2 }, ['foo'])).toEqual({ bar: 2 }); | ||
| expect(rejectProperties({ foo: 1, bar: 2 }, ['bar'])).toEqual({ foo: 1 }); | ||
| expect(rejectProperties({ foo: 1, bar: 2 }, ['bar', 'foo'])).toEqual({}); | ||
| expect(rejectProperties({ foo: 1, bar: 2 }, ['bar', 'foo', 'baz'])).toEqual({}); | ||
| expect(rejectProperties({ foo: 1, bar: 2 }, ['foo', 'foo'])).toEqual({ bar: 2 }); | ||
| expect(rejectProperties({ foo: 1, foo: 2, bar: 3 }, ['foo'])).toEqual({ bar: 3 }); | ||
| expect(rejectProperties({ foo: 1, foo: 2, bar: 3 }, ['bar'])).toEqual({ foo: 1, foo: 2 }); | ||
| expect(rejectProperties({ foo: 1, bar: 2 }, ['baz'])).toEqual({ foo: 1, bar: 2 }); | ||
| }); | ||
| test('rejectProperties using function', () => { | ||
| expect(rejectProperties({ foo: 1, bar: 2 }, (_key, val) => val < 2)).toEqual({ bar: 2 }); | ||
| expect(rejectProperties({ foo: 3, bar: 2 }, (_key, val) => val < 2)).toEqual({ foo: 3, bar: 2 }); | ||
| expect(rejectProperties({ foo: 3, bar: 2 }, (_key, val) => val > 1)).toEqual({}); | ||
| }); | ||
| test('takeProperties using whitelist of props', () => { | ||
@@ -118,0 +136,0 @@ expect(takeProperties({ foo: 1, bar: 2 }, ['foo'])).toEqual({ filtered: { foo: 1 }, rejected: { bar: 2 }, undefined: {} }); |
40126
4.84%601
5.81%175
2.94%