mirror of
https://github.com/flynx/types.js.git
synced 2025-10-29 02:20:07 +00:00
addes stoppable iterator functions to Array...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
4fd5fdc993
commit
bf8c2c8cdc
25
Array.js
25
Array.js
@ -187,6 +187,30 @@ Array.prototype.inplaceSortAs = function(other){
|
||||
return this }
|
||||
|
||||
|
||||
// Equivalent to .map(..) / .filter(..) / .reduce(..) / .. with support for
|
||||
// StopIteration...
|
||||
//
|
||||
// NOTE: these add almost no overhead to the iteration.
|
||||
//
|
||||
// XXX should these return a partial result on StopIteration?
|
||||
var wrapIterFunc = function(iter){
|
||||
return function(func){
|
||||
try {
|
||||
return this[iter](...arguments)
|
||||
// handle StopIteration...
|
||||
} catch(err){
|
||||
if(err === StopIteration){
|
||||
return
|
||||
} else if( err instanceof StopIteration){
|
||||
return err.msg }
|
||||
throw err } } }
|
||||
|
||||
Array.prototype.smap = wrapIterFunc('map')
|
||||
Array.prototype.sfilter = wrapIterFunc('filter')
|
||||
Array.prototype.sreduce = wrapIterFunc('reduce')
|
||||
Array.prototype.sforEach = wrapIterFunc('forEach')
|
||||
|
||||
|
||||
// Equivalent to .map(..) / .filter(..) / .reduce(..) that process the
|
||||
// contents in chunks asynchronously...
|
||||
//
|
||||
@ -225,6 +249,7 @@ Array.prototype.inplaceSortAs = function(other){
|
||||
// The main goal of this is to not block the runtime while processing a
|
||||
// very long array by interrupting the processing with a timeout...
|
||||
//
|
||||
// XXX should these return a partial result on StopIteration?
|
||||
var makeChunkIter = function(iter, wrapper){
|
||||
wrapper = wrapper
|
||||
|| function(res, func, array, e){
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-types",
|
||||
"version": "3.1.0",
|
||||
"version": "3.2.0",
|
||||
"description": "Generic JavaScript types and type extensions...",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user