Skip to content

Add custom array methods that can chainable, like subclass Array, but not modify Array.prototype, works in ES3+

Notifications You must be signed in to change notification settings

futurist/add-array-methods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

add-array-methods

Add custom array methods that can chainable, like subclass Array, but not modify Array.prototype, works in ES3+

npm Build Status JavaScript Style Guide

Install

  • NPM
npm install -S add-array-methods

Usage

var myArr = []
addArrayMethods({
  last: function() {
    return this[this.length-1]
  },
  // ... more here
})(myArr)

myArr.sort().last()

The result is, the last methods will always available, even after all native methods that returns array, like below:

assert.equal(Array.prototype.last, undefined)

myArr.filter(v=>v > 2).sort().last()

API

addArrayMethods(yourMethods, options) -> (arr) -> arr

options.es3 = true|false

default: false

When set to true, using direct methods assign instead of Object.defineProperty

options.natives = string[]

default: [ 'filter', 'slice', 'concat', 'reverse', 'sort', 'splice', 'map', 'fill', 'copyWithin' ]

This default value holds in addArrayMethods.natives

Native methods will copy from Array.prototype, to arr, when create new array wrapper

yourMethods = object

default: undefined

The object format is:

{
  method1: func1,
  method2: func2,
  ...
}

yourMethods can overwrite native functions with same method name

addArrayMethods.natives

This property hold the default natives array values, change it will affect all upcoming instance of addArrayMethods

About

Add custom array methods that can chainable, like subclass Array, but not modify Array.prototype, works in ES3+

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published