Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pickBy #402

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,14 @@ const pick = (obj, keys2) => {
return acc;
}, {});
};
const pickBy = (obj, predicate = () => true) => {
if (!obj)
return {};
return pick(
obj,
Object.keys(obj).filter((key) => predicate(obj[key]))
);
};
const omit = (obj, keys2) => {
if (!obj)
return {};
Expand Down Expand Up @@ -937,4 +945,4 @@ const trim = (str, charsToTrim = " ") => {
return str.replace(regex, "");
};

export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, pickBy, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
9 changes: 9 additions & 0 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ var radash = (function (exports) {
return acc;
}, {});
};
const pickBy = (obj, predicate = () => true) => {
if (!obj)
return {};
return pick(
obj,
Object.keys(obj).filter((key) => predicate(obj[key]))
);
};
const omit = (obj, keys2) => {
if (!obj)
return {};
Expand Down Expand Up @@ -1002,6 +1010,7 @@ var radash = (function (exports) {
exports.partob = partob;
exports.pascal = pascal;
exports.pick = pick;
exports.pickBy = pickBy;
exports.proxied = proxied;
exports.random = random;
exports.range = range;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions docs/object/pick-by.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: pickBy
description: Pick only the desired attributes from an object
group: Object
---

## Basic usage

Given an object and an identity function, returns a new object with only the values which makes identity returns true.

```ts
import { pickBy } from 'radash'

const fish = {
name: 'Bass',
weight: 8,
source: 'lake',
barckish: false
}

pick(fish, v => v === 8) // => { weight }
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "12.1.0",
"version": "12.2.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export {
mapValues,
omit,
pick,
pickBy,
set,
shake,
upperize
Expand Down
18 changes: 18 additions & 0 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ export const pick = <T extends object, TKeys extends keyof T>(
}, {} as Pick<T, TKeys>)
}

/**
* Picks properties of an object based on a predicate function.
*
* @param {T} obj - The object from which to pick properties.
* @param {function} predicate - The function used to filter the object's properties.
* @return {object} A new object with properties picked based on the predicate.
*/
export const pickBy = <T extends object, TKeys extends keyof T>(
obj: T,
predicate: (value: T[TKeys]) => boolean = () => true
): Pick<T, TKeys> => {
if (!obj) return {} as Pick<T, TKeys>
return pick(
obj,
(Object.keys(obj) as TKeys[]).filter(key => predicate(obj[key]))
)
}

/**
* Omit a list of properties from an object
* returning a new object with the properties
Expand Down
5 changes: 5 additions & 0 deletions src/tests/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { assert } from 'chai'
import * as _ from '..'
import { AggregateError } from '../async'

// fix error of calling useFakeTimers on nodejs version v20.12.1 https://stackoverflow.com/a/77694958/5131623
// Object.defineProperty(global, 'performance', {
// writable: true
// })

describe('async module', () => {
beforeEach(() => jest.useFakeTimers({ advanceTimers: true }))

Expand Down
17 changes: 17 additions & 0 deletions src/tests/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ describe('object module', () => {
})
})

describe('pickBy function', () => {
test('handles null input', () => {
const result = _.pickBy(null as unknown as Record<string, unknown>)
assert.deepEqual(result, {})
})

test('handles no identity function', () => {
const result = _.pickBy({ a: 2 })
assert.deepEqual(result, { a: 2 })
})

test('returns picked properties only', () => {
const result = _.pickBy({ a: 1, b: 2, c: 3, d: 4 }, v => v % 2 === 0)
assert.deepEqual(result, { b: 2, d: 4 } as any)
})
})

describe('omit function', () => {
const person = {
name: 'jay',
Expand Down
Loading