https://github.com/futurist/add-array-methods
Add custom array methods that can chainable, like subclass Array, but not modify Array.prototype, works in ES3+
https://github.com/futurist/add-array-methods
array custom extend inheritance methods prototype subclass
Last synced: 3 months ago
JSON representation
Add custom array methods that can chainable, like subclass Array, but not modify Array.prototype, works in ES3+
- Host: GitHub
- URL: https://github.com/futurist/add-array-methods
- Owner: futurist
- Created: 2017-04-15T02:46:00.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-15T08:46:29.000Z (almost 9 years ago)
- Last Synced: 2025-03-11T11:53:14.710Z (about 1 year ago)
- Topics: array, custom, extend, inheritance, methods, prototype, subclass
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# add-array-methods
Add custom array methods that can chainable, like subclass Array, but not modify Array.prototype, works in ES3+
[](https://www.npmjs.com/package/add-array-methods)
[](https://travis-ci.org/futurist/add-array-methods)
[](https://standardjs.com)
## Install
- NPM
``` bash
npm install -S add-array-methods
```
## Usage
``` javascript
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:
``` javascript
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:
``` javascript
{
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**