array-reject

1.0.1 • Public • Published

NPM version Build Status codecov MIT License

array-reject

⚠️ This module extend the global Array.prototype.
Array.prototype.reject(callback) return new array which removed value matched condition.
This is the opposite of Array.prototype.filter!

install

$ npm install array-reject

usage

import 'array-reject';

example

const arr = [1, 2, 3, 4, 5];
 
function over(n) {
  return function(x) {
    return x > n;
  };
}
 
arr.reject( over(3) );
// => [1, 2, 3]
arr.filter( over(3) );
// => [4, 5]
const cities = [
  'Tokyo',
  'Kyoto',
  'Osaka',
  'NewYork',
  'Helsinki',
  'Tampere',
  'Espoo'
];
 
cities.reject(function(val) {
  return val.match(/o/i);
});
// => ['Helsinki', 'Tampere']
const idols = [
  { name: 'Hoshimiya Ichigo', type: 'cute' },
  { name: 'Kiriya Aoi',       type: 'cool' },
  { name: 'Shibuki Ran',      type: 'sexy' },
  { name: 'Arisugawa Otome',  type: 'pop'  },
  { name: 'Todo Yurika',      type: 'cool' },
  { name: 'Kamiya Shion',     type: 'cool' },
  { name: 'Ichinose Kaede',   type: 'pop'  },
  { name: 'Minowa Hikari',    type: 'sexy' },
  { name: 'Kanzaki Mizuki',   type: 'sexy' },
  { name: 'Natsuki Mikuru',   type: 'pop'  },
  { name: 'Kitaoji Sakura',   type: 'cute' },
  { name: 'Ozora Akari',      type: 'cute' },
  { name: 'Hattori Yu',       type: 'cool' },
  { name: 'Hikami Sumire',    type: 'cool' },
  { name: 'Shinjo Hinaki',    type: 'pop'  },
  { name: 'Kurebayashi Juri', type: 'sexy' },
  { name: 'Kurosawa Rin',     type: 'cool' },
  { name: 'Amahane Madoka',   type: 'cute' },
];
 
function idolType(type) {
  return function(idol) {
    return idol.type === type
  };
}
 
const notCoolIdols = idols.reject( idolType('cool') );
/* [
  {name: "Hoshimiya Ichigo", type: "cute"},
  {name: "Shibuki Ran", type: "sexy"},
  {name: "Arisugawa Otome", type: "pop"},
  {name: "Ichinose Kaede", type: "pop"},
  {name: "Minowa Hikari", type: "sexy"},
  {name: "Kanzaki Mizuki", type: "sexy"},
  {name: "Natsuki Mikuru", type: "pop"},
  {name: "Kitaoji Sakura", type: "cute"},
  {name: "Ozora Akari", type: "cute"},
  {name: "Shinjo Hinaki", type: "pop"},
  {name: "Kurebayashi Juri", type: "sexy"},
  {name: "Amahane Madoka", type: "cute"},
*/

When already has Own Array.reject ?

Old Array.prototype.reject be renamed to Array.prototype.__reject.
And run old reject function before this Array.prototype.reject.

👌

import './my-array-reject';
import 'array-reject';

😇

// This override `array-reject` 
Array.prototype.reject = function() {
  // ...
}
 
import 'array-reject';

example

// my-array-reject.js
Array.prototype.reject = function(index) {
  if( typeof(index) !== 'number' ) return this;
 
  const res = [];
  for(let i = 0, l = this.length; i < l; i += 1) {
    if( i !== index ) res.push(this[i]);
  }
  return res;
};
import './my-array-reject';
import 'array-reject';
 
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 
function over(n) {
  return function(x) {
    return x > n;
  };
}
 
// run my Array.reject
arr.__reject(0);
// => [2, 3, 4, 5, 6, 7, 8, 9, 10]
 
arr.reject(over(3));
// => [1, 2, 3]
 
// run array-reject after my Array.reject
arr.reject(0, over(3));
// => [2, 3]
 
// array-reject use last argument as a callback
arr.reject(0, 1, 2, over(3));
// => [2, 3]

test

$ npm run test

Dependents (0)

Package Sidebar

Install

npm i array-reject

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

10.9 kB

Total Files

11

Last publish

Collaborators

  • kikiki_kiki