object-array-utils
Advanced tools
+1
-1
| { | ||
| "name": "object-array-utils", | ||
| "version": "2.6.0", | ||
| "version": "2.7.0", | ||
| "description": "Utilities for working with arrays and objects", | ||
@@ -5,0 +5,0 @@ "scripts": { |
+9
-0
@@ -74,2 +74,11 @@ # `object-array-utils` | ||
| import { removeArrayElement } from 'object-array-utils'; | ||
| removeArrayElement([1, 1, 2, 3], 1) // [1, 2, 3] | ||
| removeArrayElement([1, 1, 2, 3], (e) => e === 1) // [1, 2, 3] | ||
| import { removeArrayElementByIndex } from 'object-array-utils'; | ||
| removeArrayElementByIndex([1, 2, 3], 1) // [1, 3] | ||
| import { isObjectSubset } from 'object-array-utils'; | ||
@@ -76,0 +85,0 @@ |
+63
-4
@@ -231,7 +231,13 @@ function isNullOrUndefined(v) { | ||
| function takePropsByWhitelist(o, props) { | ||
| return Object.keys(o).reduce(({ filtered, rejected }, prop) => { | ||
| const keys = Object.keys(o); | ||
| const undefined_ = | ||
| differenceArraysOfPrimitives(props, keys) | ||
| .reduce((acc, key) => ({ ...acc, [key]: undefined }), {}); | ||
| return keys.reduce(({ filtered, rejected, undefined }, prop) => { | ||
| return (props.includes(prop)) | ||
| ? { filtered: { ...filtered, [prop]: o[prop] }, rejected } | ||
| : { filtered, rejected: { ...rejected, [prop]: o[prop] } } | ||
| }, { filtered: {}, rejected: {} }); | ||
| ? { filtered: { ...filtered, [prop]: o[prop] }, rejected, undefined } | ||
| : { filtered, rejected: { ...rejected, [prop]: o[prop] }, undefined } | ||
| }, { filtered: {}, rejected: {}, undefined: undefined_ }); | ||
| } | ||
@@ -248,2 +254,53 @@ | ||
| function removeArrayElement(array, valueOrFun) { | ||
| if (!isArray(array)) { | ||
| throw new Error('expected array'); | ||
| } | ||
| return (typeof valueOrFun === 'function') | ||
| ? removeArrayElementByFun(array, valueOrFun) | ||
| : removeArrayElementByValue(array, valueOrFun); | ||
| } | ||
| function removeArrayElementByValue(array, value) { | ||
| const indexToRemove = array.indexOf(value); | ||
| return (indexToRemove !== -1) | ||
| ? removeArrayElementByIndex(array, indexToRemove) | ||
| : array; | ||
| } | ||
| function removeArrayElementByFun(array, fun) { | ||
| let indexToRemove = null; | ||
| for (let i = 0; i < array.length; ++i) { | ||
| if (fun(array[i])) { | ||
| indexToRemove = i; | ||
| break; | ||
| } | ||
| } | ||
| if (indexToRemove === null) { | ||
| return array; | ||
| } | ||
| return removeArrayElementByIndex(array, indexToRemove); | ||
| } | ||
| function removeArrayElementByIndex(array, index) { | ||
| if (!isArray(array)) { | ||
| throw new Error('expected array'); | ||
| } | ||
| if (isNaN(index) || index < 0) { | ||
| throw new Error('expected positive number') | ||
| } | ||
| return [...array.slice(0, index), ...array.slice(index + 1)]; | ||
| } | ||
| function differenceArraysOfPrimitives(a1, a2) { | ||
| return a1.filter((e) => !a2.includes(e)); | ||
| } | ||
| // https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/groupBy | ||
@@ -387,3 +444,5 @@ // function groupArrayElementsBy(arrayOfObjects, getKey) { | ||
| isPrimitive, | ||
| removeArrayElement, | ||
| removeArrayElementByIndex, | ||
| takeProperties | ||
| } |
+28
-7
@@ -15,2 +15,4 @@ import { | ||
| isPrimitive, | ||
| removeArrayElement, | ||
| removeArrayElementByIndex, | ||
| takeProperties | ||
@@ -83,2 +85,3 @@ } from './index'; | ||
| expect(hasObjectProperties({ foo: 1, bar: 2 }, ['bar', 'foo', 'baz'])).toBeFalsy(); | ||
| expect(hasObjectProperties({ foo: 1, bar: 2 }, ['foo', 'foo'])).toBeTruthy(); | ||
| }); | ||
@@ -91,2 +94,4 @@ | ||
| expect(filterProperties({ foo: 1, bar: 2 }, ['bar', 'foo', 'baz'])).toEqual({ foo: 1, bar: 2 }); | ||
| expect(filterProperties({ foo: 1, bar: 2 }, ['foo', 'foo'])).toEqual({ foo: 1 }); | ||
| expect(filterProperties({ foo: 1, foo: 2, bar: 3 }, ['foo'])).toEqual({ foo: 1, foo: 2 }); | ||
| }); | ||
@@ -100,12 +105,13 @@ | ||
| test('takeProperties using whitelist of props', () => { | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['foo'])).toEqual({ filtered: { foo: 1 }, rejected: { bar: 2 } }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['bar'])).toEqual({ filtered: { bar: 2 }, rejected: { foo: 1 } }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['bar', 'foo'])).toEqual({ filtered: { foo: 1, bar: 2 }, rejected: {} }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['bar', 'foo', 'baz'])).toEqual({ filtered: { foo: 1, bar: 2 }, rejected: {} }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['baz'])).toEqual({ filtered: {}, rejected: { foo: 1, bar: 2 } }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['foo'])).toEqual({ filtered: { foo: 1 }, rejected: { bar: 2 }, undefined: {} }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['bar'])).toEqual({ filtered: { bar: 2 }, rejected: { foo: 1 }, undefined: {} }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['bar', 'foo'])).toEqual({ filtered: { foo: 1, bar: 2 }, rejected: {}, undefined: {} }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['bar', 'foo', 'baz'])).toEqual({ filtered: { foo: 1, bar: 2 }, rejected: {}, undefined: { baz: undefined } }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, ['baz'])).toEqual({ filtered: {}, rejected: { foo: 1, bar: 2 }, undefined: { baz: undefined } }); | ||
| expect(takeProperties({ foo: 1, foo: 2, bar: 3 }, ['foo'])).toEqual({ filtered: { foo: 1, foo: 2 }, rejected: { bar: 3 }, undefined: {} }); | ||
| }); | ||
| test('takeProperties using function', () => { | ||
| expect(takeProperties({ foo: 1, bar: 2 }, (_key, val) => val < 2)).toEqual({ filtered: { foo: 1 }, rejected: { bar: 2 } }); | ||
| expect(takeProperties({ foo: 3, bar: 2 }, (_key, val) => val < 2)).toEqual({filtered: {}, rejected: { foo: 3, bar: 2 } }); | ||
| expect(takeProperties({ foo: 1, bar: 2 }, (_key, val) => val < 2)).toEqual({ filtered: { foo: 1 }, rejected: { bar: 2 }, undefined: {} }); | ||
| expect(takeProperties({ foo: 3, bar: 2 }, (_key, val) => val < 2)).toEqual({filtered: {}, rejected: { foo: 3, bar: 2 }, undefined: {} }); | ||
| }); | ||
@@ -178,1 +184,16 @@ | ||
| }); | ||
| test('removeArrayElementByIndex', () => { | ||
| expect(removeArrayElementByIndex([1, 2, 3], 1)).toEqual([1, 3]); | ||
| expect(removeArrayElementByIndex([], 1)).toEqual([]); | ||
| expect(removeArrayElementByIndex([1, 2, 3], 5)).toEqual([1, 2, 3]); | ||
| }); | ||
| test('removeArrayElement', () => { | ||
| expect(removeArrayElement([1, 2, 3, 1], 1)).toEqual([2, 3, 1]); | ||
| expect(removeArrayElement([], 1)).toEqual([]); | ||
| expect(removeArrayElement([1, 2, 3], 5)).toEqual([1, 2, 3]); | ||
| expect(removeArrayElement([1, 2, 3, 1], (e) => e === 1)).toEqual([2, 3, 1]); | ||
| expect(removeArrayElement([], (e) => e === 1)).toEqual([]); | ||
| expect(removeArrayElement([1, 2, 3], (e) => e === 5)).toEqual([1, 2, 3]); | ||
| }); |
35797
9.29%510
14.35%161
5.92%