Socket
Socket
Sign inDemoInstall

filter-obj

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter-obj - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

index.d.ts

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

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