Comparing version 3.0.0 to 3.1.0
12
index.js
'use strict' | ||
const filter = require('filter-iterator') | ||
const pf = require('possible-function') | ||
const {entries, reconstruct} = require('m-o') | ||
const filterIterable = require('filter-iter') | ||
const isPlainObject = require('is-plain-object') | ||
const PossibleFunction = require('possible-function') | ||
module.exports = (map, test) => new (map.constructor)(filter(map.entries(), e => pf(test, (k, v) => !!v)(...e))) | ||
module.exports = (map, test, options) => { | ||
if (isPlainObject(test) && !isPlainObject(options)) options = test | ||
test = PossibleFunction(test, (k, v) => !!v) | ||
return reconstruct(map, filterIterable(entries(map), entry => test(...entry), options)) | ||
} |
{ | ||
"name": "filter-map", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Returns a copy of a Map object containing only those entries which pass a test function.", | ||
@@ -19,3 +19,5 @@ "keywords": [ | ||
"dependencies": { | ||
"filter-iterator": "0.0.1", | ||
"filter-iter": "^1.0.0", | ||
"is-plain-object": "^2.0.4", | ||
"m-o": "^2.1.0", | ||
"possible-function": "^1.0.1" | ||
@@ -22,0 +24,0 @@ }, |
# filter-map | ||
Returns a copy of a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object containing only those entries which pass a test function. | ||
Returns a copy of a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) or Object containing only those entries which pass a test function. | ||
Supports a numeric limit on the number of entries in the resulting Map or Object. | ||
## Installation | ||
@@ -15,5 +17,10 @@ | ||
const filterMap = require('filter-map') | ||
const map = new Map([['a', 1], ['b', 2], ['c', 3]]) | ||
const filteredMap = filterMap(map, (key, value) => value > 1) | ||
filteredMap.size // 2 | ||
// Works on objects too | ||
const obj = {a: 1, b: 2, c: 3} | ||
filterMap(obj, (key, value) => value > 1) // {b: 2, c: 3} | ||
``` | ||
@@ -25,4 +32,15 @@ | ||
const filterMap = require('filter-map') | ||
const map = new Map([[1, true], [2, false], [3, true]]) | ||
filterMap(map).size // 2 | ||
filterMap({1: true, 2: false, 3: true}) // {1: true, 3: true} | ||
``` | ||
If you only want a certain number of entries in the filtered Map or Object, you can specify a numeric limit parameter: | ||
```javascript | ||
const obj = {a: 0, b: 1, c: 2, d: 3} | ||
filterMap(obj, (k, v) => v > 1, {limit: 1}) // {c: 2} | ||
filterMap(obj, {limit: 1}) // {b: 1} | ||
``` |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
3511
10
45
1
4
+ Addedfilter-iter@^1.0.0
+ Addedis-plain-object@^2.0.4
+ Addedm-o@^2.1.0
+ Added@lamansky/flatten@1.0.0(transitive)
+ Addedarray-pad@0.0.1(transitive)
+ Addedarrify@1.0.1(transitive)
+ Addedcase-insensitive@1.0.0(transitive)
+ Addedclass-chain@1.0.0(transitive)
+ Addedcopy-own@1.2.0(transitive)
+ Addedenglish-list@1.0.0(transitive)
+ Addederrate@1.3.0(transitive)
+ Addedffn@2.1.0(transitive)
+ Addedfilter-iter@1.1.0(transitive)
+ Addedget-own-property@1.0.0(transitive)
+ Addedhas-duplicates@1.0.0(transitive)
+ Addedif-else-throw@1.0.0(transitive)
+ Addedis-class-of@1.0.1(transitive)
+ Addedis-global-object@1.0.0(transitive)
+ Addedis-instance-of@1.0.2(transitive)
+ Addedis-nil@1.0.1(transitive)
+ Addedis-obj@1.0.1(transitive)
+ Addedis-object@1.0.2(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedlodash.set@4.3.2(transitive)
+ Addedm-o@2.2.0(transitive)
+ Addednew-object@3.1.0(transitive)
+ Addedofn@1.0.0(transitive)
+ Addedpfn@1.1.0(transitive)
+ Addedplainify@1.0.0(transitive)
+ Addedqfn@1.0.1(transitive)
+ Addedrtrim-array@1.1.0(transitive)
+ Addedsbo@1.1.3(transitive)
+ Addedsorp@1.0.0(transitive)
+ Addedtrim-call@1.1.0(transitive)
+ Addedwfn@1.0.0(transitive)
- Removedfilter-iterator@0.0.1
- Removedfilter-iterator@0.0.1(transitive)