farr-this
v1.0.2
Published
Arrays of _bound_ functions with shared `this` parameters.
Downloads
1
Readme
— farr-this
—
Documentation
Please see: Documentation
Arrays of bound functions with shared this
parameters.
This module exports a class FarrThis
, that extends the native Array class (via its parent class Farr).
Anytime an array-index-valued property of a FarrThis
is set, the provided value gets filtered:
- If the value is a
function
, it's bound. - If the value is not a
function
, it's replaced by a new bound function that produces the value when called.
This means that FarrThis
arrays only store bound function
elements or empty slots.
this
Binding
Each function element has a bound this
property corresponding to a plain object with some common parameters:
a
: the containing instance (FarrThis
)i
: the index of this element in the containing instance (integer
)f
: a reference to this element (function
)o
: any user-specified object, also accessible as theo
property of the containing instance ('object')
Farr
For convienience, this module also exports its parent class, Farr).
Installation
Run npm install farr-this
Usage
import { FarrThis as Ft } from 'farr'
const n = 5
function e () {
return this.i
}
const a = new Array(n).fill(e)
const f = new Ft({ a })
f.push(e)
const result = await f.all() // [ 0, 1, 2, 3, 4, 5 ]
Testing
Run npm test
. This will also trigger a download of the tests from the farr
dependency. These tests will be stored and run in the test/farrtest
directory, so don't put anything there.
Incompatibilities
The following features of farr
are incompatible with farr-this
:
generated
: all elements undergo mutationgivenFunc
: all elements undergo mutation