mirror of
https://github.com/flynx/types.js.git
synced 2025-10-29 02:20:07 +00:00
fixed a logical design error...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
80d851a071
commit
ea6fbc1ff5
12
Array.js
12
Array.js
@ -19,7 +19,7 @@ var generator = require('./generator')
|
||||
|
||||
/*********************************************************************/
|
||||
|
||||
// NOTE: this is used in a similar fasion to Python's StopIteration...
|
||||
// NOTE: this is used in a similar fashion to Python's StopIteration...
|
||||
var STOP =
|
||||
module.STOP =
|
||||
object.STOP
|
||||
@ -80,7 +80,7 @@ var wrapIterFunc = function(iter){
|
||||
//
|
||||
//
|
||||
// STOP can be thrown in func or chunk_handler at any time to
|
||||
// abort iteration, this will reject the promise.
|
||||
// abort iteration, this will resolve the promise.
|
||||
//
|
||||
//
|
||||
// The main goal of this is to not block the runtime while processing a
|
||||
@ -128,9 +128,9 @@ var makeChunkIter = function(iter, wrapper){
|
||||
// handle STOP...
|
||||
} catch(err){
|
||||
if(err === STOP){
|
||||
return Promise.reject()
|
||||
return Promise.resolve()
|
||||
} else if( err instanceof STOP){
|
||||
return Promise.reject(err.value) }
|
||||
return Promise.resolve(err.value) }
|
||||
throw err } }
|
||||
|
||||
var res = []
|
||||
@ -153,9 +153,9 @@ var makeChunkIter = function(iter, wrapper){
|
||||
// handle STOP...
|
||||
} catch(err){
|
||||
if(err === STOP){
|
||||
return reject()
|
||||
return resolve()
|
||||
} else if( err instanceof STOP){
|
||||
return reject(err.value) }
|
||||
return resolve(err.value) }
|
||||
throw err }
|
||||
|
||||
// stop condition...
|
||||
|
||||
36
README.md
36
README.md
@ -28,10 +28,10 @@ A library of JavaScript type extensions, types and type utilities.
|
||||
- [`Array.zip(..)` / `<array>.zip(..)`](#arrayzip--arrayzip)
|
||||
- [`Array.iter(..)` / `<array>.iter()`](#arrayiter--arrayiter)
|
||||
- [Abortable `Array` iteration](#abortable-array-iteration)
|
||||
- [`array.StopIteration`](#arraystopiteration)
|
||||
- [`array.STOP` / `array.STOP(..)`](#arraystop--arraystop)
|
||||
- [`<array>.smap(..)` / `<array>.sfilter(..)` / `<array>.sreduce(..)` / `<array>.sforEach(..)`](#arraysmap--arraysfilter--arraysreduce--arraysforeach)
|
||||
- [Large `Array` iteration (chunked)](#large-array-iteration-chunked)
|
||||
- [`array.StopIteration`](#arraystopiteration-1)
|
||||
- [`array.STOP` / `array.STOP(..)`](#arraystop--arraystop-1)
|
||||
- [`<array>.CHUNK_SIZE`](#arraychunk_size)
|
||||
- [`<array>.mapChunks(..)`](#arraymapchunks)
|
||||
- [`<array>.filterChunks(..)`](#arrayfilterchunks)
|
||||
@ -417,10 +417,10 @@ This is mostly useful in combination with the [Generator extensions and utilitie
|
||||
### Abortable `Array` iteration
|
||||
|
||||
A an alternative to `Array`'s `.map(..)` / `.filter(..)` / .. methods with ability to
|
||||
stop the iteration process by `throw`ing `StopIteration`.
|
||||
stop the iteration process by `throw`ing `STOP` or `STOP(<value>)`.
|
||||
|
||||
```javascript
|
||||
var {StopIteration} = require('ig-types/Array')
|
||||
var {STOP} = require('ig-types/Array')
|
||||
```
|
||||
|
||||
This can be used in two ways:
|
||||
@ -430,35 +430,35 @@ This can be used in two ways:
|
||||
;[1,2,3,4,5]
|
||||
.smap(function(e){
|
||||
// simply abort here and now...
|
||||
throw StopIteration })
|
||||
throw STOP })
|
||||
```
|
||||
Since we aborted the iteration without passing any arguments to `StopIteration`,
|
||||
Since we aborted the iteration without passing any arguments to `STOP`,
|
||||
`.smap(..)` will return `undefined`.
|
||||
|
||||
2) `throw` an instance and return the argument...
|
||||
```javascript
|
||||
// this will print "4" -- the value passed to StopIteration...
|
||||
// this will print "4" -- the value passed to STOP...
|
||||
console.log([1,2,3,4,5]
|
||||
.smap(function(e){
|
||||
if(e > 3){
|
||||
// NOTE: new is optional here...
|
||||
// ...StopIteratiom is an object.js constructor.
|
||||
throw new StopIteration(e) } }))
|
||||
throw new STOP(e) } }))
|
||||
```
|
||||
|
||||
Note that no partial result is returned unless passed through `StopIteration(..)`.
|
||||
Note that no partial result is returned unless passed through `STOP(..)`.
|
||||
|
||||
|
||||
#### `array.StopIteration`
|
||||
#### `array.STOP` / `array.STOP(..)`
|
||||
|
||||
An exception that if raised while iterating via a supporting iterator method
|
||||
will abort further execution and correctly exit.
|
||||
An _object/constructor_ that if raised (as an exception) while iterating via
|
||||
a supporting iterator method will abort further execution and correctly exit.
|
||||
|
||||
|
||||
#### `<array>.smap(..)` / `<array>.sfilter(..)` / `<array>.sreduce(..)` / `<array>.sforEach(..)`
|
||||
|
||||
Like `Array`'s `.map(..)`, `.filter(..)`, `.reduce(..)` and `.forEach(..)` but
|
||||
with added support for aborting iteration by throwing `StopIteration`.
|
||||
with added support for aborting iteration by throwing `STOP` or `STOP(<value>)`.
|
||||
|
||||
|
||||
### Large `Array` iteration (chunked)
|
||||
@ -495,18 +495,18 @@ var c = await [1,2,3,4,5]
|
||||
return e*2 })
|
||||
```
|
||||
|
||||
#### `array.StopIteration`
|
||||
#### `array.STOP` / `array.STOP(..)`
|
||||
|
||||
Like for [`<array>.smap(..)` and friends](#abortable-array-iteration) iteration
|
||||
can be stopped by throwing a `array.StopIteration` and as before there are
|
||||
two ways to go:
|
||||
can be stopped by throwing a `array.STOP` / `array.STOP(<value>)` and as before
|
||||
there are two ways to go:
|
||||
|
||||
1) `throw` as-is to simply stop
|
||||
```javascript
|
||||
;[1,2,3,4,5]
|
||||
.mapChunks(function(e){
|
||||
// simply abort here and now...
|
||||
throw StopIteration })
|
||||
throw STOP })
|
||||
.catch(function(){
|
||||
console.log('done.') })
|
||||
```
|
||||
@ -518,7 +518,7 @@ two ways to go:
|
||||
if(e > 3){
|
||||
// NOTE: new is optional here...
|
||||
// ...StopIteratiom is an object.js constructor.
|
||||
throw new StopIteration(e) } })
|
||||
throw new STOP(e) } })
|
||||
.catch(function(e){
|
||||
console.log('first value greater than 3:', e) })
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-types",
|
||||
"version": "4.1.3",
|
||||
"version": "5.0.0",
|
||||
"description": "Generic JavaScript types and type extensions...",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user