Skip to content

Filters an array based on the provided expression.

License

Notifications You must be signed in to change notification settings

mcabreradev/filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm (scoped) npm bundle size (minified)

Filter

Filter the array to a subset of it based on provided criteria.

Install

$ npm i @mcabreradev/filter
$ yarn add @mcabreradev/filter

Usage

const filter = require("@mcabreradev/filter");

const customers = [
  { "name" : "Alfreds Futterkiste", "city" : "Berlin" },
  { "name" : "Around the Horn", "city" : "London" },
  { "name" : "B's Beverages", "city" : "London" },
  { "name" : "Bolido Comidas preparadas", "city" : "Madrid" },
  { "name" : "Bon app", "city" : "Marseille" },
  { "name" : "Bottom-Dollar Marketse" ,"city" : "Tsawassen" },
  { "name" : "Cactus Comidas para llevar", "city" : "Buenos Aires" }
];

Filter customers with a specific city

filter(customers, 'Berlin' );

// [ { name: 'Alfreds Futterkiste', city: 'Berlin' } ]

Filter customer with a specific city and name containing 'o' letter

filter(customers, {'name' : 'O', 'city' : 'London'});

// [ { name: 'Around the Horn', city: 'London' } ]

Filter customers with a half name of city

filter(customers, {'city' : 'Mars'} );

// [ { name: 'Bon app', city: 'Marseille' } ]

Filter customers with a single letter

filter(customers, {'city' : 's'} );

// [
//   { name: 'Bon app', city: 'Marseille' },
//   { name: 'Bottom-Dollar Marketse', city: 'Tsawassen' },
//   { name: 'Cactus Comidas para llevar', city: 'Buenos Aires' }
// ]

Filter customers with two single letters

filter(customers, {'city' : 'B', 'city' : 'L'} );

// [
//   { name: 'Bon app', city: 'Marseille' },
//   { name: 'Bottom-Dollar Marketse', city: 'Tsawassen' },
//   { name: 'Cactus Comidas para llevar', city: 'Buenos Aires' }
// ]

Tests

$ npm test

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.