mirror of
https://github.com/flynx/types.js.git
synced 2025-10-29 02:20:07 +00:00
tweaking + some cleanup + some refactoring...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
711933113e
commit
c02e768240
34
Array.js
34
Array.js
@ -81,6 +81,27 @@ Array.prototype.last
|
||||
: this[this.length - 1]})
|
||||
|
||||
|
||||
// Roll left/right (in-place)...
|
||||
//
|
||||
// NOTE: to .rol(..) left just pass a negative n value...
|
||||
// NOTE: we can't use ...[..] for sparse arrays as the will expand undefined
|
||||
// inplace of empty positions, this is thereason the .splice(..)
|
||||
// implementation was replaced by a less clear (but faster)
|
||||
// .copyWithin(..) version...
|
||||
Array.prototype.rol
|
||||
|| (Array.prototype.rol = function(n=1){
|
||||
var l = this.length
|
||||
n = (n >= 0 ?
|
||||
n
|
||||
: l - n)
|
||||
% l
|
||||
if(n != 0){
|
||||
this.length += n
|
||||
this.copyWithin(l, 0, n)
|
||||
this.splice(0, n) }
|
||||
return this })
|
||||
|
||||
|
||||
// Compact a sparse array...
|
||||
//
|
||||
// NOTE: this will not compact in-place.
|
||||
@ -93,8 +114,9 @@ Array.prototype.compact = function(){
|
||||
'len' in Array.prototype
|
||||
|| Object.defineProperty(Array.prototype, 'len', {
|
||||
get : function () {
|
||||
return Object.keys(this).length
|
||||
},
|
||||
// NOTE: if we don't do .slice() here this can count array
|
||||
// instance attributes...
|
||||
return Object.keys(this.slice()).length },
|
||||
set : function(val){},
|
||||
})
|
||||
|
||||
@ -105,7 +127,9 @@ Array.prototype.compact = function(){
|
||||
Array.prototype.unique = function(normalize){
|
||||
return normalize ?
|
||||
[...new Map(this.map(function(e){ return [normalize(e), e] })).values()]
|
||||
: [...new Set(this)] }
|
||||
// NOTE: we are calling .compact() here to avoid creating undefined
|
||||
// items from empty slots in sparse arrays...
|
||||
: [...new Set(this.compact())] }
|
||||
Array.prototype.tailUnique = function(normalize){
|
||||
return this
|
||||
.slice()
|
||||
@ -202,20 +226,19 @@ Array.prototype.inplaceSortAs = function(other){
|
||||
// StopIteration...
|
||||
//
|
||||
// NOTE: these add almost no overhead to the iteration.
|
||||
// NOTE: these will not return a partial result if stopped.
|
||||
//
|
||||
// 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')
|
||||
@ -459,7 +482,6 @@ function(func, ...arrays){
|
||||
//
|
||||
//
|
||||
// XXX should this take an argument and be like map??
|
||||
// XXX revise name
|
||||
Array.prototype.iter = function*(){
|
||||
for(var e of this){
|
||||
yield e } }
|
||||
|
||||
30
README.md
30
README.md
@ -16,6 +16,7 @@ A library of JavaScript type extensions, types and type utilities.
|
||||
- [`Object.sort(..)`](#objectsort)
|
||||
- [`Array`](#array)
|
||||
- [`<array>.first(..)` / `<array>.last(..)`](#arrayfirst--arraylast)
|
||||
- [`<array>.rol(..)`](#arrayrol)
|
||||
- [`<array>.compact()`](#arraycompact)
|
||||
- [`<array>.len`](#arraylen)
|
||||
- [`<array>.unique(..)` / `<array>.tailUnique(..)`](#arrayunique--arraytailunique)
|
||||
@ -315,6 +316,21 @@ Note that these do not affect `<array>` length unless setting items on
|
||||
an empty `<array>`.
|
||||
|
||||
|
||||
#### `<array>.rol(..)`
|
||||
|
||||
Roll `<array>` in-place left.
|
||||
```
|
||||
<array>.rol()
|
||||
<array>.rol(1)
|
||||
-> <array>
|
||||
|
||||
<array>.rol(n)
|
||||
-> <array>
|
||||
```
|
||||
|
||||
To roll right pass a negative `n` to `.rol(..)`.
|
||||
|
||||
|
||||
#### `<array>.compact()`
|
||||
|
||||
```
|
||||
@ -345,6 +361,7 @@ no effect.
|
||||
|
||||
Generate an array with all duplicate elements removed.
|
||||
|
||||
|
||||
#### `<array>.cmp(..)`
|
||||
|
||||
```
|
||||
@ -381,10 +398,8 @@ This is mostly useful in combination with the [Generator extensions and utilitie
|
||||
|
||||
### Abortable `Array` iteration
|
||||
|
||||
#### `array.StopIteration`
|
||||
|
||||
An exception that if raised while iterating via a supporting iterator method
|
||||
will abort further execution and correctly exit.
|
||||
A an alternative to `Array`'s `.map(..)` / `.filter(..)` / .. methods with ability to
|
||||
stop the iteration process by `throw`ing `StopIteration`.
|
||||
|
||||
```javascript
|
||||
var {StopIteration} = require('ig-types/Array')
|
||||
@ -413,6 +428,13 @@ This can be used in two ways:
|
||||
throw new StopIteration(e) } }))
|
||||
```
|
||||
|
||||
Note that no partial result is returned unless passed through `StopIteration(..)`.
|
||||
|
||||
|
||||
#### `array.StopIteration`
|
||||
|
||||
An exception that if raised while iterating via a supporting iterator method
|
||||
will abort further execution and correctly exit.
|
||||
|
||||
|
||||
#### `<array>.smap(..)` / `<array>.sfilter(..)` / `<array>.sreduce(..)` / `<array>.sforEach(..)`
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-types",
|
||||
"version": "3.7.5",
|
||||
"version": "3.7.6",
|
||||
"description": "Generic JavaScript types and type extensions...",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user