filter-obj
Advanced tools
Comparing version 1.1.0 to 2.0.0
17
index.js
'use strict'; | ||
module.exports = function (obj, predicate) { | ||
var ret = {}; | ||
var keys = Object.keys(obj); | ||
var isArr = Array.isArray(predicate); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
var val = obj[key]; | ||
module.exports = (object, predicate) => { | ||
const result = {}; | ||
const isArray = Array.isArray(predicate); | ||
if (isArr ? predicate.indexOf(key) !== -1 : predicate(key, val, obj)) { | ||
ret[key] = val; | ||
for (const [key, value] of Object.entries(object)) { | ||
if (isArray ? predicate.includes(key) : predicate(key, value, object)) { | ||
result[key] = value; | ||
} | ||
} | ||
return ret; | ||
return result; | ||
}; |
{ | ||
"name": "filter-obj", | ||
"version": "1.1.0", | ||
"description": "Filter object keys and values into a new object", | ||
"license": "MIT", | ||
"repository": "sindresorhus/filter-obj", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && node test.js" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"filter", | ||
"obj", | ||
"object", | ||
"key", | ||
"keys", | ||
"value", | ||
"values", | ||
"val", | ||
"iterate", | ||
"iterator" | ||
], | ||
"devDependencies": { | ||
"ava": "0.0.4", | ||
"xo": "*" | ||
} | ||
"name": "filter-obj", | ||
"version": "2.0.0", | ||
"description": "Filter object keys and values into a new object", | ||
"license": "MIT", | ||
"repository": "sindresorhus/filter-obj", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"filter", | ||
"object", | ||
"key", | ||
"keys", | ||
"value", | ||
"values", | ||
"iterate", | ||
"iterator" | ||
], | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # filter-obj [![Build Status](https://travis-ci.org/sindresorhus/filter-obj.svg?branch=master)](https://travis-ci.org/sindresorhus/filter-obj) | ||
``` | ||
$ npm install --save filter-obj | ||
$ npm install filter-obj | ||
``` | ||
@@ -17,5 +17,5 @@ | ||
```js | ||
var filterObj = require('filter-obj'); | ||
const filterObject = require('filter-obj'); | ||
var obj = { | ||
const object = { | ||
foo: true, | ||
@@ -25,16 +25,37 @@ bar: false | ||
var newObject = filterObj(obj, function (key, value, object) { | ||
return value === true; | ||
}); | ||
const newObject = filterObject(object, (key, value) => value === true); | ||
//=> {foo: true} | ||
var newObject2 = filterObj(obj, ['bar']); | ||
//=> {bar: true} | ||
const newObject2 = filterObject(object, ['bar']); | ||
//=> {bar: false} | ||
``` | ||
## API | ||
### filterObject(source, filter) | ||
### filterObject(source, includeKeys) | ||
#### source | ||
Type: `object` | ||
Source object to filter properties from. | ||
#### filter | ||
Type: `Function` | ||
A predicate function that detemines whether a property should be assigned to the new object. The function has the signature `filterFunction(sourceKey, sourceValue, source)`. | ||
#### includeKeys | ||
Type: `string[]` | ||
Array of property names that should be assigned to the new object. | ||
## Related | ||
- [map-obj](https://github.com/sindresorhus/map-obj) - Map object keys and values into a new object | ||
- [object-assign](https://github.com/sindresorhus/object-assign) - Copy enumerable own properties from one or more source objects to a target object | ||
@@ -44,2 +65,2 @@ | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
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
4186
5
44
63
3