array-events

Events and more for arrays

MIT 13 个版本
安装
npm install array-events
yarn add array-events
pnpm add array-events
bun add array-events
README

array-events.js

NPM version npm Travis

Is an Array extension class which adds asynchronous functions to Array as well as firing events on 'remove', 'add' or 'change'

import { EventedArray } = from 'array-events/array-events.mjs';
// OR: const EventedArray = require('array-events');
const myArray = new EventedArray();

events

change : fired any time an element is added or removed

add : fired any time an element is added

remove : fired any time an element is removed

emitter functions

EventedArrays are also Emitters but have an expanded syntax from extended-emitter and the additional .when() call.

myArray.on('change', function(event){ });
myArray.once('change', function(event){ });
myArray.off('change', function(event){ });
myArray.emit('change'[, arguments]);

async functions

forEachEmission : execute serially

myArray.forEachEmission(function(item, index, done){
    somethingAsynchronous(function(){
        done();
    });
}, function(){
    //we're all done!
});

forAllEmissions : execute all jobs in parallel

myArray.forAllEmissions(function(item, index, done){
    somethingAsynchronous(function(){
        done();
    });
}, function(){
    //we're all done!
});

forAllEmissionsInPool : execute all jobs in parallel up to a maximum #, then queue for later

myArray.forAllEmissionsInPool(poolSize, function(item, index, done){
    somethingAsynchronous(function(){
        done();
    });
}, function(){
    //we're all done!
});

utility functions

contains : does the array contain this element?

new EventedArray(['dog', 'cat', 'mouse']).contains('cat') //returns true;

combine : generate a new array that is the union of the provided arrays

new EventedArray(['dog', 'cat']).combine(['mouse']) //returns a new array  ['dog', 'cat', 'mouse'];

erase : generate a new array without the member provided

new EventedArray(['dog', 'cat', 'mouse']).erase('cat') //returns a new array ['dog', 'mouse'];

EventedArray.is : is the provided object an instance of EventedArray

EventedArray.is(new EventedArray()); //returns true
EventedArray.is([]) //returns false

Testing

just run

mocha

Enjoy,

-Abbey Hawk Sparrow

版本列表
2.0.1 2023-06-20
2.0.0 2023-06-19
1.1.0 2023-05-31
1.0.0 2016-12-20
0.2.0 2015-03-04
0.1.4 2015-02-12
0.1.3-beta 2014-03-28
0.1.1-beta 2014-03-14
0.1.0-beta 2014-02-23
0.0.4-alpha 2014-02-21
0.0.3-alpha 2014-02-16
0.0.2-alpha 2014-02-16
0.0.1-alpha 2014-02-16