filter-anything
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -68,28 +68,34 @@ 'use strict'; | ||
/** | ||
* Returns a new object with only the props passed as fillables | ||
* pick returns a new object with only the props you pick | ||
* | ||
* @export | ||
* @param {object} obj the target object to check | ||
* @param {string[]} [fillables=[]] an array of strings, with the props which should be allowed on returned object | ||
* @returns {AnyObject} the cleaned object after deleting guard and non-fillables | ||
* @template T | ||
* @template K | ||
* @param {T} obj the target object to pick props from | ||
* @param {K[]} keys the prop names you want to keep | ||
* @returns {O.Pick<T, K>} a new object with just the picked props | ||
*/ | ||
function fillable(obj, fillables) { | ||
function pick(obj, keys) { | ||
// @ts-ignore | ||
if (!fillables.length) | ||
if (!keys.length) | ||
return {}; | ||
// @ts-ignore | ||
return recursiveFilter(obj, fillables, []); | ||
return recursiveFilter(obj, keys, []); | ||
} | ||
var fillable = pick; | ||
/** | ||
* Returns a new object without guarded props | ||
* omit returns a new object without the props you omit | ||
* | ||
* @export | ||
* @param {object} obj the target object to check | ||
* @param {string[]} [guarded=[]] an array of strings, with the props which should NOT be allowed on returned object | ||
* @returns {AnyObject} the cleaned object after deleting guard and non-fillables | ||
* @template T | ||
* @template K | ||
* @param {T} obj the target object to omit props from | ||
* @param {K[]} keys the prop names you want to omit | ||
* @returns {O.Omit<T, K>} a new object without the omitted props | ||
*/ | ||
function guard(obj, guarded) { | ||
function omit(obj, keys) { | ||
// @ts-ignore | ||
return recursiveFilter(obj, [], guarded); | ||
return recursiveFilter(obj, [], keys); | ||
} | ||
var guard = omit; | ||
// /** | ||
@@ -121,1 +127,3 @@ // * Returns a new object but with only the props passed as fillables and/or without guarded props | ||
exports.guard = guard; | ||
exports.omit = omit; | ||
exports.pick = pick; |
@@ -64,28 +64,34 @@ import { isPlainObject } from 'is-what'; | ||
/** | ||
* Returns a new object with only the props passed as fillables | ||
* pick returns a new object with only the props you pick | ||
* | ||
* @export | ||
* @param {object} obj the target object to check | ||
* @param {string[]} [fillables=[]] an array of strings, with the props which should be allowed on returned object | ||
* @returns {AnyObject} the cleaned object after deleting guard and non-fillables | ||
* @template T | ||
* @template K | ||
* @param {T} obj the target object to pick props from | ||
* @param {K[]} keys the prop names you want to keep | ||
* @returns {O.Pick<T, K>} a new object with just the picked props | ||
*/ | ||
function fillable(obj, fillables) { | ||
function pick(obj, keys) { | ||
// @ts-ignore | ||
if (!fillables.length) | ||
if (!keys.length) | ||
return {}; | ||
// @ts-ignore | ||
return recursiveFilter(obj, fillables, []); | ||
return recursiveFilter(obj, keys, []); | ||
} | ||
var fillable = pick; | ||
/** | ||
* Returns a new object without guarded props | ||
* omit returns a new object without the props you omit | ||
* | ||
* @export | ||
* @param {object} obj the target object to check | ||
* @param {string[]} [guarded=[]] an array of strings, with the props which should NOT be allowed on returned object | ||
* @returns {AnyObject} the cleaned object after deleting guard and non-fillables | ||
* @template T | ||
* @template K | ||
* @param {T} obj the target object to omit props from | ||
* @param {K[]} keys the prop names you want to omit | ||
* @returns {O.Omit<T, K>} a new object without the omitted props | ||
*/ | ||
function guard(obj, guarded) { | ||
function omit(obj, keys) { | ||
// @ts-ignore | ||
return recursiveFilter(obj, [], guarded); | ||
return recursiveFilter(obj, [], keys); | ||
} | ||
var guard = omit; | ||
// /** | ||
@@ -115,2 +121,2 @@ // * Returns a new object but with only the props passed as fillables and/or without guarded props | ||
export { fillable, guard }; | ||
export { fillable, guard, omit, pick }; |
{ | ||
"name": "filter-anything", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"sideEffects": false, | ||
"description": "A simple integration of \"fillables\" and \"guard\" filtering on the props of an object", | ||
"description": "A simple (TypeScript) integration of \"pick\" and \"omit\" to filter props of an object", | ||
"main": "dist/index.cjs.js", | ||
@@ -20,2 +20,5 @@ "module": "dist/index.esm.js", | ||
"keywords": [ | ||
"pick", | ||
"omit", | ||
"typescript", | ||
"fillables", | ||
@@ -22,0 +25,0 @@ "guard", |
@@ -7,3 +7,4 @@ # Filter anything ⚔️ | ||
Filter out object props based on "fillables" and "guard". A simple & small integration. | ||
An implementation that filters out object props like the TypeScript "pick" and "omit". | ||
In the Laravel world, this is also called "fillables" and "guard". | ||
@@ -14,6 +15,7 @@ ## Motivation | ||
- be able to filter out object props based on just what we need - aka "fillables" | ||
- be able to filter out object props based on what we don't need - aka "guarded" props | ||
- be able to filter out object props based on just what we need - aka "pick" props | ||
- be able to filter out object props based on what we don't need - aka "omit" props | ||
- **supports for nested properties** | ||
- supports wildcards `*` for nested properties | ||
- the return type must be TypeScript supported! (see screenshots below) | ||
@@ -32,5 +34,5 @@ ## Meet the family | ||
### Fillable | ||
### Pick | ||
With `fillable` pass an array of keys of an object - the props which may stay. | ||
With `pick` you pass an object and an array of keys of an object - **the props which may stay**. | ||
@@ -48,3 +50,3 @@ ```js | ||
With `guard` pass an array of keys of an object - the props which should be removed. | ||
With `guard` you pass an object and an array of keys of an object - the props which should be removed. | ||
@@ -60,2 +62,6 @@ ```js | ||
### Aliases | ||
`pick()` and `omit()` can also be imported with the names `fillable()` and `guard()`. This pays homage to my history with Laravel. 😉 | ||
### TypeScript | ||
@@ -65,4 +71,4 @@ | ||
![typescript example fillable](https://raw.githubusercontent.com/mesqueeb/filter-anything/master/.github/typescript-fillable.png) | ||
![typescript example guard](https://raw.githubusercontent.com/mesqueeb/filter-anything/master/.github/typescript-guard.png) | ||
![typescript example pick](https://raw.githubusercontent.com/mesqueeb/filter-anything/master/.github/typescript-pick.png) | ||
![typescript example omit](https://raw.githubusercontent.com/mesqueeb/filter-anything/master/.github/typescript-omit.png) | ||
@@ -76,6 +82,6 @@ ### Nested props | ||
fillable(doc, ['items.keep']) | ||
pick(doc, ['items.keep']) | ||
// returns {items: {keep: '📌'}} | ||
guard(doc, ['items.discard']) | ||
omit(doc, ['items.discard']) | ||
// returns {items: {keep: '📌'}} | ||
@@ -96,3 +102,3 @@ ``` | ||
// use wildcard * | ||
guard(doc, ['*.discard']) | ||
omit(doc, ['*.discard']) | ||
// returns { | ||
@@ -99,0 +105,0 @@ // '123': {keep: '📌'}, |
@@ -5,32 +5,37 @@ import { O } from 'ts-toolbelt' | ||
/** | ||
* Returns a new object with only the props passed as fillables | ||
* pick returns a new object with only the props you pick | ||
* | ||
* @export | ||
* @param {object} obj the target object to check | ||
* @param {string[]} [fillables=[]] an array of strings, with the props which should be allowed on returned object | ||
* @returns {AnyObject} the cleaned object after deleting guard and non-fillables | ||
* @template T | ||
* @template K | ||
* @param {T} obj the target object to pick props from | ||
* @param {K[]} keys the prop names you want to keep | ||
* @returns {O.Pick<T, K>} a new object with just the picked props | ||
*/ | ||
export function fillable<T extends object, K extends string> ( | ||
obj: T, | ||
fillables: K[] | ||
): O.Pick<T, K> { | ||
export function pick<T extends object, K extends string> (obj: T, keys: K[]): O.Pick<T, K> { | ||
// @ts-ignore | ||
if (!fillables.length) return {} | ||
if (!keys.length) return {} | ||
// @ts-ignore | ||
return recursiveFilter(obj, fillables, []) | ||
return recursiveFilter(obj, keys, []) | ||
} | ||
export const fillable = pick | ||
/** | ||
* Returns a new object without guarded props | ||
* omit returns a new object without the props you omit | ||
* | ||
* @export | ||
* @param {object} obj the target object to check | ||
* @param {string[]} [guarded=[]] an array of strings, with the props which should NOT be allowed on returned object | ||
* @returns {AnyObject} the cleaned object after deleting guard and non-fillables | ||
* @template T | ||
* @template K | ||
* @param {T} obj the target object to omit props from | ||
* @param {K[]} keys the prop names you want to omit | ||
* @returns {O.Omit<T, K>} a new object without the omitted props | ||
*/ | ||
export function guard<T extends object, K extends string> (obj: T, guarded: K[]): O.Omit<T, K> { | ||
export function omit<T extends object, K extends string> (obj: T, keys: K[]): O.Omit<T, K> { | ||
// @ts-ignore | ||
return recursiveFilter(obj, [], guarded) | ||
return recursiveFilter(obj, [], keys) | ||
} | ||
export const guard = omit | ||
// /** | ||
@@ -37,0 +42,0 @@ // * Returns a new object but with only the props passed as fillables and/or without guarded props |
@@ -1,2 +0,2 @@ | ||
import { fillable, guard } from '../src/index' | ||
import { pick, omit } from '../src/index' | ||
import test from 'ava' | ||
@@ -7,3 +7,3 @@ | ||
const withoutId = fillable(squirtle, ['name', 'type']) | ||
const withoutId = pick(squirtle, ['name', 'type']) | ||
@@ -16,5 +16,5 @@ t.deepEqual(withoutId, { name: 'Squirtle', type: 'water' }) | ||
const withoutId = guard(squirtle, ['id']) | ||
const withoutId = omit(squirtle, ['id']) | ||
t.deepEqual(withoutId, { name: 'Squirtle', type: 'water' }) | ||
}) |
import { O } from 'ts-toolbelt'; | ||
/** | ||
* Returns a new object with only the props passed as fillables | ||
* pick returns a new object with only the props you pick | ||
* | ||
* @export | ||
* @param {object} obj the target object to check | ||
* @param {string[]} [fillables=[]] an array of strings, with the props which should be allowed on returned object | ||
* @returns {AnyObject} the cleaned object after deleting guard and non-fillables | ||
* @template T | ||
* @template K | ||
* @param {T} obj the target object to pick props from | ||
* @param {K[]} keys the prop names you want to keep | ||
* @returns {O.Pick<T, K>} a new object with just the picked props | ||
*/ | ||
export declare function fillable<T extends object, K extends string>(obj: T, fillables: K[]): O.Pick<T, K>; | ||
export declare function pick<T extends object, K extends string>(obj: T, keys: K[]): O.Pick<T, K>; | ||
export declare const fillable: typeof pick; | ||
/** | ||
* Returns a new object without guarded props | ||
* omit returns a new object without the props you omit | ||
* | ||
* @export | ||
* @param {object} obj the target object to check | ||
* @param {string[]} [guarded=[]] an array of strings, with the props which should NOT be allowed on returned object | ||
* @returns {AnyObject} the cleaned object after deleting guard and non-fillables | ||
* @template T | ||
* @template K | ||
* @param {T} obj the target object to omit props from | ||
* @param {K[]} keys the prop names you want to omit | ||
* @returns {O.Omit<T, K>} a new object without the omitted props | ||
*/ | ||
export declare function guard<T extends object, K extends string>(obj: T, guarded: K[]): O.Omit<T, K>; | ||
export declare function omit<T extends object, K extends string>(obj: T, keys: K[]): O.Omit<T, K>; | ||
export declare const guard: typeof omit; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
704
105
0
156824