Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

filter-anything

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter-anything - npm Package Compare versions

Comparing version 2.1.9 to 2.2.0

.vscode/settings.json

83

dist/index.cjs.js

@@ -20,3 +20,3 @@ 'use strict';

function recursiveFilter(obj, fillables, guarded, pathUntilNow) {
function recursiveOmit(obj, omittedKeys, pathUntilNow) {
if (pathUntilNow === void 0) { pathUntilNow = ''; }

@@ -26,3 +26,4 @@ if (!isWhat.isPlainObject(obj)) {

}
return Object.keys(obj).reduce(function (carry, key) {
return Object.entries(obj).reduce(function (carry, _a) {
var key = _a[0], value = _a[1];
var path = pathUntilNow;

@@ -32,29 +33,42 @@ if (path)

path += key;
// check guard regardless
if (guarded.some(function (guardPath) { return pathsAreEqual(path, guardPath); })) {
if (omittedKeys.some(function (guardPath) { return pathsAreEqual(path, guardPath); })) {
return carry;
}
var value = obj[key];
// check fillables up to this point
if (fillables.length) {
// no further recursion needed
if (!isWhat.isPlainObject(value)) {
carry[key] = value;
return carry;
}
carry[key] = recursiveOmit(obj[key], omittedKeys, path);
return carry;
}, {});
}
function recursivePick(obj, pickedKeys, pathUntilNow) {
if (pathUntilNow === void 0) { pathUntilNow = ''; }
if (!isWhat.isPlainObject(obj)) {
return obj;
}
return Object.entries(obj).reduce(function (carry, _a) {
var key = _a[0], value = _a[1];
var path = pathUntilNow;
if (path)
path += '.';
path += key;
// check pickedKeys up to this point
if (pickedKeys.length) {
var passed_1 = false;
fillables.forEach(function (fillable) {
pickedKeys.forEach(function (pickedKey) {
var pathDepth = path.split('.').length;
var fillableDepth = fillable.split('.').length;
var fillableUpToNow = fillable
.split('.')
.slice(0, pathDepth)
.join('.');
var pathUpToFillableDepth = path
.split('.')
.slice(0, fillableDepth)
.join('.');
if (pathsAreEqual(pathUpToFillableDepth, fillableUpToNow))
var pickedKeyDepth = pickedKey.split('.').length;
var pickedKeyUpToNow = pickedKey.split('.').slice(0, pathDepth).join('.');
var pathUpToPickedKeyDepth = path.split('.').slice(0, pickedKeyDepth).join('.');
if (pathsAreEqual(pathUpToPickedKeyDepth, pickedKeyUpToNow))
passed_1 = true;
});
// there's not one fillable that allows up to now
// there's not one pickedKey that allows up to now
if (!passed_1)
return carry;
}
// no fillables or fillables up to now allow it
// no further recursion needed
if (!isWhat.isPlainObject(value)) {

@@ -64,3 +78,3 @@ carry[key] = value;

}
carry[key] = recursiveFilter(obj[key], fillables, guarded, path);
carry[key] = recursivePick(obj[key], pickedKeys, path);
return carry;

@@ -85,3 +99,3 @@ }, {});

// @ts-ignore
return recursiveFilter(obj, keys, []);
return recursivePick(obj, keys);
}

@@ -101,28 +115,5 @@ var fillable = pick;

// @ts-ignore
return recursiveFilter(obj, [], keys);
return recursiveOmit(obj, keys);
}
var guard = omit;
// /**
// * Returns a new object but with only the props passed as fillables and/or without guarded props
// *
// * @export
// * @param {object} obj the target object to check
// * @param {string[]} fillables array of strings, with the props which should be allowed on returned object
// * @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
// */
// export function filter<
// T extends object,
// KeyToKeep extends string,
// KeyToDelete extends string,
// KeysToKeep extends KeyToKeep[],
// KeysToDelete extends KeyToDelete[]
// > (
// obj: T,
// fillables: KeysToKeep,
// guarded?: KeysToDelete
// ) {
// // @ts-ignore
// return recursiveFilter(obj, fillables, guarded)
// }

@@ -129,0 +120,0 @@ exports.fillable = fillable;

@@ -16,3 +16,3 @@ import { isPlainObject } from 'is-what';

function recursiveFilter(obj, fillables, guarded, pathUntilNow) {
function recursiveOmit(obj, omittedKeys, pathUntilNow) {
if (pathUntilNow === void 0) { pathUntilNow = ''; }

@@ -22,3 +22,4 @@ if (!isPlainObject(obj)) {

}
return Object.keys(obj).reduce(function (carry, key) {
return Object.entries(obj).reduce(function (carry, _a) {
var key = _a[0], value = _a[1];
var path = pathUntilNow;

@@ -28,29 +29,42 @@ if (path)

path += key;
// check guard regardless
if (guarded.some(function (guardPath) { return pathsAreEqual(path, guardPath); })) {
if (omittedKeys.some(function (guardPath) { return pathsAreEqual(path, guardPath); })) {
return carry;
}
var value = obj[key];
// check fillables up to this point
if (fillables.length) {
// no further recursion needed
if (!isPlainObject(value)) {
carry[key] = value;
return carry;
}
carry[key] = recursiveOmit(obj[key], omittedKeys, path);
return carry;
}, {});
}
function recursivePick(obj, pickedKeys, pathUntilNow) {
if (pathUntilNow === void 0) { pathUntilNow = ''; }
if (!isPlainObject(obj)) {
return obj;
}
return Object.entries(obj).reduce(function (carry, _a) {
var key = _a[0], value = _a[1];
var path = pathUntilNow;
if (path)
path += '.';
path += key;
// check pickedKeys up to this point
if (pickedKeys.length) {
var passed_1 = false;
fillables.forEach(function (fillable) {
pickedKeys.forEach(function (pickedKey) {
var pathDepth = path.split('.').length;
var fillableDepth = fillable.split('.').length;
var fillableUpToNow = fillable
.split('.')
.slice(0, pathDepth)
.join('.');
var pathUpToFillableDepth = path
.split('.')
.slice(0, fillableDepth)
.join('.');
if (pathsAreEqual(pathUpToFillableDepth, fillableUpToNow))
var pickedKeyDepth = pickedKey.split('.').length;
var pickedKeyUpToNow = pickedKey.split('.').slice(0, pathDepth).join('.');
var pathUpToPickedKeyDepth = path.split('.').slice(0, pickedKeyDepth).join('.');
if (pathsAreEqual(pathUpToPickedKeyDepth, pickedKeyUpToNow))
passed_1 = true;
});
// there's not one fillable that allows up to now
// there's not one pickedKey that allows up to now
if (!passed_1)
return carry;
}
// no fillables or fillables up to now allow it
// no further recursion needed
if (!isPlainObject(value)) {

@@ -60,3 +74,3 @@ carry[key] = value;

}
carry[key] = recursiveFilter(obj[key], fillables, guarded, path);
carry[key] = recursivePick(obj[key], pickedKeys, path);
return carry;

@@ -81,3 +95,3 @@ }, {});

// @ts-ignore
return recursiveFilter(obj, keys, []);
return recursivePick(obj, keys);
}

@@ -97,29 +111,6 @@ var fillable = pick;

// @ts-ignore
return recursiveFilter(obj, [], keys);
return recursiveOmit(obj, keys);
}
var guard = omit;
// /**
// * Returns a new object but with only the props passed as fillables and/or without guarded props
// *
// * @export
// * @param {object} obj the target object to check
// * @param {string[]} fillables array of strings, with the props which should be allowed on returned object
// * @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
// */
// export function filter<
// T extends object,
// KeyToKeep extends string,
// KeyToDelete extends string,
// KeysToKeep extends KeyToKeep[],
// KeysToDelete extends KeyToDelete[]
// > (
// obj: T,
// fillables: KeysToKeep,
// guarded?: KeysToDelete
// ) {
// // @ts-ignore
// return recursiveFilter(obj, fillables, guarded)
// }
export { fillable, guard, omit, pick };
{
"name": "filter-anything",
"version": "2.1.9",
"version": "2.2.0",
"sideEffects": false,

@@ -12,3 +12,3 @@ "description": "A simple (TypeScript) integration of \"pick\" and \"omit\" to filter props of an object",

"test": "ava",
"rollup": "rollup -c build/rollup.js",
"rollup": "rimraf types && rimraf dist && rollup -c build/rollup.js",
"build": "npm run lint && npm run test && npm run rollup",

@@ -24,2 +24,4 @@ "release": "npm run build && np"

"omit",
"picked-props",
"omited-props",
"typescript",

@@ -45,9 +47,9 @@ "fillables",

"is-what": "^3.14.1",
"ts-toolbelt": "9.3.12"
"ts-toolbelt": "^9.5.10"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.15.2",
"@typescript-eslint/parser": "^4.15.2",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"ava": "^3.15.0",
"eslint": "^7.20.0",
"eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0",

@@ -57,7 +59,8 @@ "eslint-plugin-tree-shaking": "^1.8.0",

"prettier": "^2.2.1",
"rollup": "^2.39.1",
"rimraf": "^3.0.2",
"rollup": "^2.40.0",
"rollup-plugin-typescript2": "^0.30.0",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.2.2"
"typescript": "^4.2.3"
},

@@ -64,0 +67,0 @@ "ava": {

@@ -1,3 +0,4 @@

import { O } from 'ts-toolbelt'
import { recursiveFilter } from './recursiveFilter'
import { O, S, F, U } from 'ts-toolbelt'
import { recursiveOmit } from './recursiveOmit'
import { recursivePick } from './recursivePick'

@@ -14,7 +15,10 @@ /**

*/
export function pick<T extends Record<string, any>, K extends string> (obj: T, keys: K[]): O.Pick<T, K> {
export function pick<T extends Record<string, any>, K extends string>(
obj: T,
keys: F.AutoPath<T, K>[]
): U.Merge<O.P.Pick<T, S.Split<K, '.'>>> {
// @ts-ignore
if (!keys.length) return {}
// @ts-ignore
return recursiveFilter(obj, keys, [])
return recursivePick(obj, keys)
}

@@ -34,31 +38,10 @@

*/
export function omit<T extends Record<string, any>, K extends string> (obj: T, keys: K[]): O.Omit<T, K> {
export function omit<T extends Record<string, any>, K extends string>(
obj: T,
keys: F.AutoPath<T, K>[]
): U.Merge<O.P.Omit<T, S.Split<K, '.'>>> {
// @ts-ignore
return recursiveFilter(obj, [], keys)
return recursiveOmit(obj, keys)
}
export const guard = omit
// /**
// * Returns a new object but with only the props passed as fillables and/or without guarded props
// *
// * @export
// * @param {object} obj the target object to check
// * @param {string[]} fillables array of strings, with the props which should be allowed on returned object
// * @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
// */
// export function filter<
// T extends object,
// KeyToKeep extends string,
// KeyToDelete extends string,
// KeysToKeep extends KeyToKeep[],
// KeysToDelete extends KeyToDelete[]
// > (
// obj: T,
// fillables: KeysToKeep,
// guarded?: KeysToDelete
// ) {
// // @ts-ignore
// return recursiveFilter(obj, fillables, guarded)
// }

@@ -1,142 +0,102 @@

import { fillable, guard } from '../src/index'
import { pick, omit } from '../src/index'
import pathsAreEqual from '../src/pathsAreEqual'
import test from 'ava'
test('check fillable FLAT', t => {
test('check pick FLAT', (t) => {
const doc = { name: 'n1', id: '1', filled: true, notfilled: false }
const res = fillable(doc, ['name', 'filled', 'id'])
const res = pick(doc, ['name', 'filled', 'id'])
t.deepEqual(res, { name: 'n1', id: '1', filled: true })
})
test('check fillable 0 props', t => {
test('check pick 0 props', (t) => {
const doc = { name: 'n1', id: '1', filled: true, notfilled: false }
const res = fillable(doc, [])
t.deepEqual(res, {})
const res = pick(doc, [] as any)
t.deepEqual(res, {} as any)
})
test('check guard FLAT', t => {
const doc = { name: 'n1', id: '1', filled: true, guarded: true }
const res = guard(doc, ['guarded'])
test('check omit FLAT', (t) => {
const doc = { name: 'n1', id: '1', filled: true, omited: true }
const res = omit(doc, ['omited'])
t.deepEqual(res, { name: 'n1', id: '1', filled: true })
})
// test('check filter FLAT', t => {
// const doc = { nested: { fillables: { yes: 0, no: 0 } }, guarded: true }
// const res = filter(doc, ['nested', 'guarded'])
// t.deepEqual(res, { nested: { fillables: { yes: 0, no: 0 } } })
// })
test('check fillables NESTED - single fillable', t => {
const doc = { nested: { fillables: { yes: 0, no: 0 } }, secondProp: true }
const res = fillable(doc, ['nested.fillables.yes'])
t.deepEqual(res, { nested: { fillables: { yes: 0 } } })
test('check picks NESTED - single pick', (t) => {
const doc = {
nested: { picks: { yes: 0, no: 0 } },
secondProp: true,
}
const res = pick(doc, ['nested.picks.yes'])
t.deepEqual(res, { nested: { picks: { yes: 0 } } })
})
test('check fillables NESTED - single fillable 2', t => {
const doc = { nested: { fillables: { yes: 0, no: 0 } }, secondProp: true }
const res = fillable(doc, ['nested.fillables'])
t.deepEqual(res, { nested: { fillables: { yes: 0, no: 0 } } })
test('check picks NESTED - single pick 2', (t) => {
const doc = {
nested: { picks: { yes: 0, no: 0 } },
secondProp: true,
}
const res = pick(doc, ['nested.picks.yes'])
t.deepEqual(res, { nested: { picks: { yes: 0 } } })
})
test('check fillables NESTED - single fillable 3', t => {
const doc = { nested: { fillables: { yes: 0, no: 0 } }, secondProp: true }
const res = fillable(doc, ['nested'])
t.deepEqual(res, { nested: { fillables: { yes: 0, no: 0 } } })
test('check picks NESTED - single pick 3', (t) => {
const doc = {
nested: { picks: { yes: 0, no: 0 } },
secondProp: true,
}
const res = pick(doc, ['nested'])
t.deepEqual(res, { nested: { picks: { yes: 0, no: 0 } } })
})
test('check fillables NESTED - multiple fillable', t => {
test('check picks NESTED - multiple pick', (t) => {
const doc = {
nested: { fillables: { yes: 0, no: 0 } },
nested: { picks: { yes: 0, no: 0 } },
secondProp: { yes: true, no: false },
}
const res1 = fillable(doc, ['nested.fillables.yes', 'secondProp.yes'])
t.deepEqual(res1, {
nested: { fillables: { yes: 0 } },
secondProp: { yes: true },
})
const res2 = fillable(doc, ['nested.fillables', 'secondProp.yes'])
t.deepEqual(res2, {
nested: { fillables: { yes: 0, no: 0 } },
secondProp: { yes: true },
})
const res3 = fillable(doc, ['nested', 'secondProp.yes'])
t.deepEqual(res3, {
nested: { fillables: { yes: 0, no: 0 } },
// @ts-ignore
secondProp: { yes: true },
})
const res1 = pick(doc, ['nested.picks.yes', 'secondProp.yes'])
t.deepEqual(res1, { nested: { picks: { yes: 0 } }, secondProp: { yes: true } })
const res2 = pick(doc, ['nested.picks', 'secondProp.yes'])
t.deepEqual(res2, { nested: { picks: { yes: 0, no: 0 } }, secondProp: { yes: true } })
const res3 = pick(doc, ['nested', 'secondProp.yes'])
t.deepEqual(res3, { nested: { picks: { yes: 0, no: 0 } }, secondProp: { yes: true } })
})
test('check guard NESTED', t => {
const doc = { nested: { guard: { yes: 0, no: 0 } }, secondProp: true }
const res1 = guard(doc, ['nested.guard.yes'])
// @ts-ignore
t.deepEqual(res1, { nested: { guard: { no: 0 } }, secondProp: true })
const res2 = guard(doc, ['nested.guard'])
// @ts-ignore
test('check omit NESTED', (t) => {
const doc = {
nested: { omit: { yes: 0, no: 0 } },
secondProp: true,
}
const res1 = omit(doc, ['nested.omit.yes'])
t.deepEqual(res1, { nested: { omit: { no: 0 } }, secondProp: true })
const res2 = omit(doc, ['nested.omit'])
t.deepEqual(res2, { nested: {}, secondProp: true })
const res3 = guard(doc, ['nested'])
const res3 = omit(doc, ['nested'])
t.deepEqual(res3, { secondProp: true })
const res4 = omit(doc, ['nested.omit.yes', 'secondProp'])
t.deepEqual(res4, { nested: { omit: { no: 0 } } } as any)
})
// test('check NESTED - multiple fillable & guard', t => {
// const doc = {
// nested: { fillables: { yes: 0, no: 0 } },
// secondProp: { yes: true, no: false },
// guardedTop: true,
// guardedDeep: { yes: true, no: true },
// }
// const res = filter(doc, ['nested.fillables.yes', 'secondProp.yes', 'guardedTop', 'guardedDeep'], ['guardedTop', 'guardedDeep.yes'])
// t.deepEqual(res, {
// nested: { fillables: { yes: 0 } },
// secondProp: { yes: true },
// guardedDeep: { no: true },
// })
// })
// test('check NESTED - multiple fillable & guard 2', t => {
// const doc = {
// nested: { fillables: { yes: 0, no: 0 } },
// secondProp: { yes: true, no: false },
// guardedTop: true,
// guardedDeep: { yes: true, no: true },
// }
// const res = filter(doc, ['nested.fillables', 'secondProp.yes', 'guardedTop', 'guardedDeep'], ['guardedTop', 'guardedDeep.yes'])
// t.deepEqual(res, {
// nested: { fillables: { yes: 0, no: 0 } },
// // @ts-ignore
// secondProp: { yes: true },
// // @ts-ignore
// guardedDeep: { no: true },
// })
// })
// test('check NESTED - multiple fillable & guard 3', t => {
// const doc = {
// nested: { fillables: { yes: 0, no: 0 } },
// secondProp: { yes: true, no: false },
// guardedTop: true,
// guardedDeep: { yes: true, no: true },
// }
// const res = filter(doc, ['nested', 'secondProp.yes', 'guardedTop', 'guardedDeep'], ['guardedTop', 'guardedDeep.yes'])
// t.deepEqual(res, {
// nested: { fillables: { yes: 0, no: 0 } },
// // @ts-ignore
// secondProp: { yes: true },
// // @ts-ignore
// guardedDeep: { no: true },
// })
// })
test('check NESTED wildcards - fillable', t => {
test('check NESTED wildcards - pick', (t) => {
const doc = {
fillables: {
picks: {
'123': { yes: true, no: false },
'456': { yes: true, no: false },
},
guarded: {
omited: {
'123': { yes: true, no: false },
},
}
const res = fillable(doc, ['fillables.*.yes'])
type Res = { picks: { '123': { yes: boolean }; '456': { yes: boolean } } }
const res: Res = pick(doc, ['picks.*.yes'] as any)
t.deepEqual(res, {
fillables: {
picks: {
'123': { yes: true },

@@ -147,99 +107,17 @@ '456': { yes: true },

})
test('check NESTED wildcards - guard', t => {
test('check NESTED wildcards - omit', (t) => {
const doc = {
fillables: {
'456': { yes: true, no: false },
},
guarded: {
'123': { yes: true, no: false },
'456': { yes: true, no: false },
},
picks: { '456': { yes: true, no: false } },
omited: { '123': { yes: true, no: false }, '456': { yes: true, no: false } },
}
const res = guard(doc, ['guarded.*.yes'])
const res = omit(doc, ['omited.*.yes'] as any)
t.deepEqual(res, {
fillables: {
'456': { yes: true, no: false },
},
guarded: {
// @ts-ignore
'123': { no: false },
// @ts-ignore
'456': { no: false },
},
})
picks: { '456': { yes: true, no: false } },
omited: { '123': { no: false }, '456': { no: false } },
} as any)
})
// test('check NESTED wildcards - filter', t => {
// let res: object, fillables: string[], guard: string[]
// const doc = {
// fillables: {
// '123': { yes: true, no: false },
// '456': { yes: true, no: false },
// },
// guarded: {
// '123': { yes: true, no: false },
// '456': { yes: true, no: false },
// },
// }
// fillables = ['fillables.*.yes', 'guarded']
// guard = ['guarded.*.yes']
// res = filter(doc, fillables, guard)
// t.deepEqual(res, {
// fillables: {
// '123': { yes: true },
// '456': { yes: true },
// },
// guarded: {
// '123': { no: false },
// '456': { no: false },
// },
// })
// })
// test('check custom class instances', t => {
// class A {
// isArrayHelper: boolean
// payload: any
// constructor (payload?: any) {
// this.isArrayHelper = true
// this.payload = payload
// }
// }
// let _1 = new A()
// let _2 = new A()
// t.is(_1 instanceof A, true)
// t.is(_2 instanceof A, true)
// const doc = {
// fillables: {
// '123': { yes: _1, no: false },
// '456': { yes: true, no: false },
// },
// guarded: {
// '123': { yes: true, no: false },
// '456': { yes: true, no: _2 },
// },
// }
// t.is(doc.fillables['123'].yes instanceof A, true)
// t.is(doc.guarded['456'].no instanceof A, true)
// const res = filter(doc, ['fillables.*.yes', 'guarded'], ['guarded.*.yes'])
// t.deepEqual(res, {
// fillables: {
// '123': { yes: _1 },
// '456': { yes: true },
// },
// guarded: {
// '123': { no: false },
// '456': { no: _2 },
// },
// })
// t.is(_1 instanceof A, true)
// t.is(_2 instanceof A, true)
// // @ts-ignore
// t.is(res.fillables['123'].yes instanceof A, true)
// // @ts-ignore
// t.is(res.guarded['456'].no instanceof A, true)
// })
test('pathsAreEqual', t => {
test('pathsAreEqual', (t) => {
t.is(pathsAreEqual('bob', '*'), true)
t.is(pathsAreEqual('bob.and.alice', 'bob.*.alice'), true)
})

@@ -1,2 +0,2 @@

import { O } from 'ts-toolbelt';
import { O, S, F, U } from 'ts-toolbelt';
/**

@@ -12,3 +12,3 @@ * pick returns a new object with only the props you pick

*/
export declare function pick<T extends Record<string, any>, K extends string>(obj: T, keys: K[]): O.Pick<T, K>;
export declare function pick<T extends Record<string, any>, K extends string>(obj: T, keys: F.AutoPath<T, K>[]): U.Merge<O.P.Pick<T, S.Split<K, '.'>>>;
export declare const fillable: typeof pick;

@@ -25,3 +25,3 @@ /**

*/
export declare function omit<T extends Record<string, any>, K extends string>(obj: T, keys: K[]): O.Omit<T, K>;
export declare function omit<T extends Record<string, any>, K extends string>(obj: T, keys: F.AutoPath<T, K>[]): U.Merge<O.P.Omit<T, S.Split<K, '.'>>>;
export declare const guard: typeof omit;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc