Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

object-array-utils

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-array-utils - npm Package Compare versions

Comparing version
2.0.0
to
2.1.0
+1
-1
package.json
{
"name": "object-array-utils",
"version": "2.0.0",
"version": "2.1.0",
"description": "Utilities for working with arrays and objects",

@@ -5,0 +5,0 @@ "scripts": {

@@ -61,3 +61,9 @@ # `object-array-utils`

filterProperties({ prop1: 1, prop2: 2 }, ['prop1', 'prop3']) // { prop1: 1 }
filterProperties({ prop1: 1, prop2: 2 }, (_key, val) => val < 2) // { prop1: 1 }
import { takeProperties } from 'object-array-utils';
takeProperties({ prop1: 1, prop2: 2 }, ['prop1', 'prop3']) // { filtered: { prop1: 1 }, rejected: { prop2: 2 } }
takeProperties({ prop1: 1, prop2: 2 }, (_key, val) => val < 2) // { filtered: { prop1: 1 }, rejected: { prop2: 2 } }
import { isObjectSubset } from 'object-array-utils';

@@ -64,0 +70,0 @@

@@ -169,3 +169,9 @@ function isNullOrUndefined(v) {

function filterProperties(o, props) {
function filterProperties(o, arg) {
return isArray(arg)
? filterPropsByWhitelist(o, arg)
: filterPropsByFun(o, arg);
}
function filterPropsByWhitelist(o, props) {
return props.reduce((newObject, prop) => {

@@ -178,2 +184,30 @@ return (prop in o)

function filterPropsByFun(o, fun) {
const filteredEntries = Object.entries(o).filter(([key, val]) => fun(key, val));
return Object.fromEntries(filteredEntries);
}
function takeProperties(o, arg) {
return isArray(arg)
? takePropsByWhitelist(o, arg)
: takePropsByFun(o, arg);
}
function takePropsByWhitelist(o, props) {
return Object.keys(o).reduce(({ filtered, rejected }, prop) => {
return (props.includes(prop))
? { filtered: { ...filtered, [prop]: o[prop] }, rejected }
: { filtered, rejected: { ...rejected, [prop]: o[prop] } }
}, { filtered: {}, rejected: {} });
}
function takePropsByFun(o, fun) {
const filteredKeys =
Object.entries(o)
.filter(([key, val]) => fun(key, val))
.map(([key, _]) => key);
return takePropsByWhitelist(o, filteredKeys);
}
// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/groupBy

@@ -306,3 +340,4 @@ // function groupArrayElementsBy(arrayOfObjects, getKey) {

isObjectLiteral,
isObjectSubset
isObjectSubset,
takeProperties
}

@@ -7,3 +7,4 @@ import {

hasObjectProperties,
isEmptyArray
isEmptyArray,
takeProperties
} from './index';

@@ -77,3 +78,3 @@

test('filterProperties', () => {
test('filterProperties using whitelist of props', () => {
expect(filterProperties({ foo: 1, bar: 2 }, ['foo'])).toEqual({ foo: 1 });

@@ -85,2 +86,20 @@ expect(filterProperties({ foo: 1, bar: 2 }, ['bar'])).toEqual({ bar: 2 });

test('filterProperties using function', () => {
expect(filterProperties({ foo: 1, bar: 2 }, (_key, val) => val < 2)).toEqual({ foo: 1 });
expect(filterProperties({ foo: 3, bar: 2 }, (_key, val) => val < 2)).toEqual({});
});
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 } });
});
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 } });
});
test('isEmptyArray', () => {

@@ -87,0 +106,0 @@ expect(isEmptyArray([])).toBeTruthy();