| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | # types.js
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A library of JavaScript type extensions, types and type utilities. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - [types.js](#typesjs) | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  |   - [Installation](#installation) | 
					
						
							|  |  |  |   - [Basic usage](#basic-usage) | 
					
						
							|  |  |  |   - [Built-in type extensions](#built-in-type-extensions) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  |     - [`Object`](#object) | 
					
						
							|  |  |  |       - [`Object.deepKeys(..)`](#objectdeepkeys) | 
					
						
							| 
									
										
										
										
											2020-10-10 06:27:51 +03:00
										 |  |  |       - [`Object.copy(..)` (EXPERIMENTAL)](#objectcopy-experimental) | 
					
						
							| 
									
										
										
										
											2020-10-09 23:20:00 +03:00
										 |  |  |       - [`Object.flatCopy(..)`](#objectflatcopy) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  |       - [`Object.match(..)`](#objectmatch) | 
					
						
							|  |  |  |       - [`Object.matchPartial(..)`](#objectmatchpartial) | 
					
						
							|  |  |  |       - [`<object>.run(..)`](#objectrun) | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  |       - [`Object.sort(..)`](#objectsort) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  |     - [`Array`](#array) | 
					
						
							|  |  |  |       - [`<array>.first(..)` / `<array>.last(..)`](#arrayfirst--arraylast) | 
					
						
							|  |  |  |       - [`<array>.compact()`](#arraycompact) | 
					
						
							|  |  |  |       - [`<array>.len`](#arraylen) | 
					
						
							|  |  |  |       - [`<array>.unique(..)` / `<array>.tailUnique(..)`](#arrayunique--arraytailunique) | 
					
						
							|  |  |  |       - [`<array>.cmp(..)`](#arraycmp) | 
					
						
							|  |  |  |       - [`<array>.setCmp(..)`](#arraysetcmp) | 
					
						
							|  |  |  |       - [`<array>.sortAs(..)`](#arraysortas) | 
					
						
							|  |  |  |       - [`<array>.inplaceSortAs(..)`](#arrayinplacesortas) | 
					
						
							| 
									
										
										
										
											2020-10-11 03:09:48 +03:00
										 |  |  |       - [`<array>.toKeys(..)`](#arraytokeys) | 
					
						
							|  |  |  |       - [`<array>.toMap(..)`](#arraytomap) | 
					
						
							| 
									
										
										
										
											2020-10-15 03:12:42 +03:00
										 |  |  |       - [`Array.zip(..)` / `<array>.zip(..)`](#arrayzip--arrayzip) | 
					
						
							| 
									
										
										
										
											2020-10-11 03:09:48 +03:00
										 |  |  |     - [Large `Array` iteration (chunked)](#large-array-iteration-chunked) | 
					
						
							|  |  |  |       - [`<array>.CHUNK_SIZE`](#arraychunk_size) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  |       - [`<array>.mapChunks(..)`](#arraymapchunks) | 
					
						
							|  |  |  |       - [`<array>.filterChunks(..)`](#arrayfilterchunks) | 
					
						
							|  |  |  |       - [`<array>.reduceChunks(..)`](#arrayreducechunks) | 
					
						
							|  |  |  |     - [`Array` (polyfill)](#array-polyfill) | 
					
						
							|  |  |  |       - [`<array>.flat()`](#arrayflat) | 
					
						
							|  |  |  |       - [`<array>.includes()`](#arrayincludes) | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  |     - [`Map`](#map) | 
					
						
							|  |  |  |       - [`<map>.sort(..)`](#mapsort) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  |     - [`Set`](#set) | 
					
						
							|  |  |  |       - [`<set>.unite(..)`](#setunite) | 
					
						
							|  |  |  |       - [`<set>.intersect(..)`](#setintersect) | 
					
						
							|  |  |  |       - [`<set>.subtract(..)`](#setsubtract) | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  |       - [`<set>.sort(..)`](#setsort) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  |     - [`Date`](#date) | 
					
						
							|  |  |  |       - [`Date.timeStamp(..)`](#datetimestamp) | 
					
						
							|  |  |  |       - [`Date.fromTimeStamp(..)`](#datefromtimestamp) | 
					
						
							|  |  |  |       - [`Date.str2ms(..)`](#datestr2ms) | 
					
						
							|  |  |  |       - [`<date>.toShortDate(..)`](#datetoshortdate) | 
					
						
							|  |  |  |       - [`<date>.getTimeStamp(..)`](#dategettimestamp) | 
					
						
							|  |  |  |       - [`<date>.setTimeStamp(..)`](#datesettimestamp) | 
					
						
							|  |  |  |     - [`String`](#string) | 
					
						
							|  |  |  |       - [`<string>.capitalize()`](#stringcapitalize) | 
					
						
							|  |  |  |     - [`RegExp`](#regexp) | 
					
						
							|  |  |  |       - [`RegExp.quoteRegExp(..)`](#regexpquoteregexp) | 
					
						
							|  |  |  |   - [Containers](#containers) | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  |     - [`containers.UniqueKeyMap()` (`Map`)](#containersuniquekeymap-map) | 
					
						
							| 
									
										
										
										
											2020-10-07 14:48:47 +03:00
										 |  |  |       - [`<unique-key-map>.set(..)`](#unique-key-mapset) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  |       - [`<unique-key-map>.reset(..)`](#unique-key-mapreset) | 
					
						
							|  |  |  |       - [`<unique-key-map>.rename(..)`](#unique-key-maprename) | 
					
						
							| 
									
										
										
										
											2020-10-07 15:09:36 +03:00
										 |  |  |       - [`<unique-key-map>.orderedRename(..)`](#unique-key-maporderedrename) | 
					
						
							| 
									
										
										
										
											2020-10-07 14:48:47 +03:00
										 |  |  |       - [`<unique-key-map>.unorderedRename(..)`](#unique-key-mapunorderedrename) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  |       - [`<unique-key-map>.keysOf(..)`](#unique-key-mapkeysof) | 
					
						
							| 
									
										
										
										
											2020-10-07 14:48:47 +03:00
										 |  |  |       - [`<unique-key-map>.originalKey(..)`](#unique-key-maporiginalkey) | 
					
						
							|  |  |  |       - [`<unique-key-map>.uniqueKey(..)`](#unique-key-mapuniquekey) | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  |       - [`<unique-key-map>.__key_pattern__`](#unique-key-map__key_pattern__) | 
					
						
							| 
									
										
										
										
											2020-10-07 15:09:36 +03:00
										 |  |  |       - [`<unique-key-map>.__unordered_rename__`](#unique-key-map__unordered_rename__) | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  |   - [License](#license) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | ## Installation
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```shell | 
					
						
							|  |  |  | $ npm install -s 'ig-types' | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Basic usage
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To extend everything: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | require('ig-types') | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 23:20:00 +03:00
										 |  |  | To have access to additional library types and utilities: | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | ```javascript | 
					
						
							|  |  |  | var types = require('ig-types') | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `types.js` is organized so as to be able to import/extend only specific  | 
					
						
							|  |  |  | sub-modules mostly independently so... | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 23:20:00 +03:00
										 |  |  | In case there is a need to only extend a specific constructor just import  | 
					
						
							|  |  |  | the module dealing with that constructor (`Array` in this case): | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | ```javascript | 
					
						
							|  |  |  | // require `ig-types/<constructor-name>`... | 
					
						
							|  |  |  | require('ig-types/Array') | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-10-09 23:20:00 +03:00
										 |  |  | Note that type patching modules are mostly independent. | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | And to import specific library modules only: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var containers = require('ig-types/containers') | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Built-in type extensions
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### `Object`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `Object.deepKeys(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 23:20:00 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | Object.deepKeys(<obj>) | 
					
						
							|  |  |  |   -> <keys> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 23:20:00 +03:00
										 |  |  | Get list of keys from all objects in the prototype chain. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is different from `Object.keys(..)` which only gets _own_ keys from the  | 
					
						
							|  |  |  | current object. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Example: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var a = { x: 123 } | 
					
						
							|  |  |  | var b = Object.create(a) | 
					
						
							|  |  |  | b.y = 321 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // get own keys of b... | 
					
						
							|  |  |  | Object.keys(b) // -> ['y'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // get all keys accessible from b... | 
					
						
							|  |  |  | Object.deepKeys(b) // -> ['x', 'y'] | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 06:27:51 +03:00
										 |  |  | #### `Object.copy(..)` (EXPERIMENTAL)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | Object.copy(<obj>) | 
					
						
							|  |  |  |   -> <obj-copy> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Create a copy of `<obj>` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This will: | 
					
						
							|  |  |  | - create a blank `<obj-copy>` | 
					
						
							|  |  |  | - link `<obj-copy>` to the same prototype chain | 
					
						
							|  |  |  | - copy all _own_ keys from `<obj>` to `<obj-copy>` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 17:09:40 +03:00
										 |  |  | Note that this will make no attempt to clone object type. | 
					
						
							| 
									
										
										
										
											2020-10-10 06:27:51 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | _XXX not yet sure how useful this is._ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #### `Object.flatCopy(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 06:27:51 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | Object.flatCopy(<obj>) | 
					
						
							|  |  |  |   -> <new-obj> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Copy all attributes from the prototype chain of `<obj>` into `<new-obj>`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 23:20:00 +03:00
										 |  |  | #### `Object.match(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `Object.matchPartial(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | #### `<object>.run(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | <object>.run(<func>) | 
					
						
							|  |  |  |     -> <object> | 
					
						
							|  |  |  |     -> <other> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Run a function in the context of `<object>` returning either `<object>`  | 
					
						
							|  |  |  | itself (if returning `undefined`) or the result. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that this is accessible from all JavaScript non-primitive objects,  | 
					
						
							|  |  |  | i.e. everything that inherits from `Object`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Example: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var L = [1, 2, 3] | 
					
						
							|  |  |  |     .map(function(e){ | 
					
						
							|  |  |  |         return e * 2 }) | 
					
						
							|  |  |  |     // see if the first element is 1 and prepend 1 if it is not... | 
					
						
							|  |  |  |     .run(function(){ | 
					
						
							|  |  |  |         if(this[0] != 1){ | 
					
						
							|  |  |  |             this.unshift(1) } }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log(L) // -> [1, 2, 6, 8] | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `.run(..)` is also available standalone via: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```shell | 
					
						
							|  |  |  | $ npm install -s object-run | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For more info see:   | 
					
						
							|  |  |  | https://github.com/flynx/object-run.js | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | #### `Object.sort(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 06:27:51 +03:00
										 |  |  | Sort `<obj>` attributes (same as `Array`'s `.sort(..)`) | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | Object.sort(<obj>) | 
					
						
							|  |  |  |   -> <obj> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sort `<obj>` attributes via `<cmp>` function. | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | Object.sort(<obj>, <cmp>) | 
					
						
							|  |  |  |   -> <obj> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sort `<obj>` attributes to the same order of `<order-list>`. | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | Object.sort(<obj>, <order-list>) | 
					
						
							|  |  |  |   -> <obj> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that this rewrites all the keys of `<obj>` thus for very large | 
					
						
							|  |  |  | sets of keys/attributes this may be quite expensive. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that some keys of `Object` may misbehave in JavaScript, currently keys | 
					
						
							|  |  |  | that are string values of numbers are sorted automatically by _number value_ | 
					
						
							|  |  |  | and are not affected by `.sort(..)`, this affects both _Chrome_ and _Firefox_. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Example: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var o = {x: 0, a: 1, '100':2, '0':3, ' 27 ':4, b:5} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // notice that the order is already different to the order of attributes above... | 
					
						
							|  |  |  | Object.keys(o)  | 
					
						
							|  |  |  | //    -> ['0', '100', 'x', 'a', ' 27 ', 'b'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // '0' and '100' are not affected by .sort(..) while ' 27 ' is... | 
					
						
							|  |  |  | Object.keys(Object.sort(o, ['x', 'a', '100']))  | 
					
						
							|  |  |  | //    -> [ '0', '100', 'x', 'a', ' 27 ', 'b' ] | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | ### `Array`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.first(..)` / `<array>.last(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | Get the first/last items of `<array>`. | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | <array>.first() | 
					
						
							|  |  |  |   -> <item> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <array>.last() | 
					
						
							|  |  |  |   -> <item> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Set the first/last items of `<array>`. | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | <array>.first(<item>) | 
					
						
							|  |  |  |   -> <array> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <array>.last(<item>) | 
					
						
							|  |  |  |   -> <array> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that these do not affect `<array>` length unless setting items on  | 
					
						
							|  |  |  | an empty `<array>`. | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.compact()`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | <array>.compact() | 
					
						
							|  |  |  |   -> <compact-array> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | Generate a compact `<array>` from a sparse `<array>`, i.e. removing all | 
					
						
							|  |  |  | the empty slots. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.len`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Number of non-empty slots/elements in `<array>`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | This is similar to: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var L = [,,, 1,, 2, 3,,] | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | // this is the same as L.len... | 
					
						
							|  |  |  | L.compact().length  | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | Note that this is different from `.length` in that writing to `.len` has | 
					
						
							|  |  |  | no effect. | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.unique(..)` / `<array>.tailUnique(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Generate an array with all duplicate elements removed. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.cmp(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | <array>.cmp(<other>) | 
					
						
							|  |  |  |   -> <bool> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Compare `<array>` to `<other>`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This will return `true` if: | 
					
						
							|  |  |  | - `<array>` === `<other>` or, | 
					
						
							|  |  |  | - lengths are the same and, | 
					
						
							|  |  |  | - values on the same positions are equal. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | #### `<array>.setCmp(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.sortAs(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.inplaceSortAs(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 17:27:00 +03:00
										 |  |  | #### `<array>.toKeys(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.toMap(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 03:12:42 +03:00
										 |  |  | #### `Array.zip(..)` / `<array>.zip(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 17:27:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### Large `Array` iteration (chunked)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Iterating over very large `Array` instances in JavaScript can block execution, | 
					
						
							|  |  |  | to avoid this `types.js` implements `.map(..)`/`.filter(..)`/`.reduce(..)` | 
					
						
							| 
									
										
										
										
											2020-10-11 03:09:48 +03:00
										 |  |  | equivalent methods that iterate the array in chunks and do it asynchronously  | 
					
						
							|  |  |  | giving the runtime a chance to run in between. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In the simplest cases these are almost a drop-in replacements for the equivalent | 
					
						
							|  |  |  | methods but return a promise. | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var a = [1,2,3,4,5] | 
					
						
							|  |  |  |     .map(function(e){  | 
					
						
							|  |  |  |         return e*2 }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var b | 
					
						
							|  |  |  | ;[1,2,3,4,5] | 
					
						
							|  |  |  |     .mapChunks(function(e){  | 
					
						
							|  |  |  |         return e*2 }) | 
					
						
							|  |  |  |     .then(function(res){ | 
					
						
							|  |  |  |         b = res }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // or with await... | 
					
						
							|  |  |  | var c = await [1,2,3,4,5] | 
					
						
							|  |  |  |     .mapChunks(function(e){  | 
					
						
							|  |  |  |         return e*2 }) | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | These support setting the chunk size (default: `50`) as the first argument: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.CHUNK_SIZE`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 17:27:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | #### `<array>.mapChunks(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-11 03:09:48 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | <array>.mapChunks(<func>) | 
					
						
							|  |  |  | <array>.mapChunks(<chunk-size>, <func>) | 
					
						
							|  |  |  |   -> <promise> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <func>(<item>, <index>, <array>) | 
					
						
							|  |  |  |   -> <new-item> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | <array>.mapChunks([<func>, <chunk-handler>]) | 
					
						
							|  |  |  | <array>.mapChunks(<chunk-size>, [<func>, <chunk-handler>]) | 
					
						
							|  |  |  |   -> <promise> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <func>(<item>, <index>, <array>) | 
					
						
							|  |  |  |   -> <new-item> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <chunk-handler>(<chunk>, <result>, <offset>) | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | #### `<array>.filterChunks(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.reduceChunks(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### `Array` (polyfill)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.flat()`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<array>.includes()`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | ### `Map`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<map>.sort(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | ### `Set`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<set>.unite(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<set>.intersect(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<set>.subtract(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | #### `<set>.sort(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ### `Date`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `Date.timeStamp(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `Date.fromTimeStamp(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `Date.str2ms(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<date>.toShortDate(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<date>.getTimeStamp(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<date>.setTimeStamp(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### `String`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<string>.capitalize()`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### `RegExp`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `RegExp.quoteRegExp(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Containers
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | ```javascript | 
					
						
							|  |  |  | var containers = require('ig-types').containers | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | or, to only import containers: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var containers = require('ig-types/containers') | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 06:31:29 +03:00
										 |  |  | Note that this will also import `ig-types/Map`. | 
					
						
							| 
									
										
										
										
											2020-10-07 16:46:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | ### `containers.UniqueKeyMap()` (`Map`)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `UniqueKeyMap` implements a key-value container (i.e. `Map`) that supports | 
					
						
							|  |  |  | and maintains _duplicate_ keys by appending an index to them.   | 
					
						
							|  |  |  | The original keys are stored internally thus the renaming mechanics are  | 
					
						
							|  |  |  | stable. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `UniqueKeyMap` extends the `Map` constructor, so all the usual `Map`  | 
					
						
							|  |  |  | methods and properties apply here. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To construct an instance: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var x = new UniqueKeyMap() | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | or: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | // new is optional... | 
					
						
							|  |  |  | var y = UniqueKeyMap() | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `UniqueKeyMap` supports the same initialization signature as `Map` but  | 
					
						
							|  |  |  | treats repeating keys differently. | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var z = UniqueKeyMap([['a', 1], ['a', 2], ['b', 1]]) | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The second `"a"` item will automatically get re-keyed as `"a (1)"`: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | console.log([...z.keys()]) // -> ['a', 'a (1)', 'b'] | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note that `.set(..)` will never rewrite an element: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | z.set('a', 3) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log([...z.keys()]) // -> ['a', 'a (1)', 'b', 'a (2)'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | z.get('a') // -> 1 | 
					
						
							|  |  |  | z.get('a (1)') // -> 2 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To get the generated key: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | var k = z.set('a', 4, true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log(k) // -> 'a (3)' | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To explicitly rewrite an item: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | z.reset('a (1)', 4) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | z.get('a (1)') // -> 4 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | And we can _rename_ items, i.e. change their key: | 
					
						
							|  |  |  | ```javascript | 
					
						
							|  |  |  | z.rename('a (2)', 'c') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.log([...z.keys()]) // -> ['a', 'a (1)', 'b', 'a (3)', 'c'] | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For more info on `Map` see:   | 
					
						
							|  |  |  | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 14:48:47 +03:00
										 |  |  | #### `<unique-key-map>.set(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | <unique-key-map>.reset(<key>, <item>) | 
					
						
							|  |  |  |     -> <unique-key-map> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <unique-key-map>.reset(<key>, <item>, true) | 
					
						
							|  |  |  |     -> <new-key> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Add an `<item>` to `<unique-key-map>`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If `<key>` already exists then add an index to it to make it unique. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Key updating is done via [`<unique-key-map>.__key_pattern__`](#unique-key-map__key_pattern__). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | #### `<unique-key-map>.reset(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 14:48:47 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | <unique-key-map>.reset(<key>, <item>) | 
					
						
							|  |  |  |     -> <unique-key-map> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Explicitly write an `<item>` under `<key>` as-is, this is like `Map`'s `.set(..)`. | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #### `<unique-key-map>.rename(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 15:09:36 +03:00
										 |  |  | ``` | 
					
						
							|  |  |  | <unique-key-map>.rename(<from-key>, <to-key>) | 
					
						
							|  |  |  |     -> <unique-key-map> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <unique-key-map>.rename(<from-key>, <to-key>, true) | 
					
						
							|  |  |  |     -> <new-key> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rename item key from `<from-key>` to `<to-key>`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Same mechanics apply as for [`.set(..)`](#unique-key-mapset) for key uniqueness. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note, if [`.__unordered_rename__`](#unique-key-map__unordered_rename__) is  | 
					
						
							|  |  |  | `false` (default) this calls [`.orderedRename(..)`](#unique-key-maporderedrename)  | 
					
						
							|  |  |  | otherwise [`.unorderedRename(..)`](#unique-key-mapunorderedrename) is called. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<unique-key-map>.orderedRename(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 14:48:47 +03:00
										 |  |  | #### `<unique-key-map>.unorderedRename(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | #### `<unique-key-map>.keysOf(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 14:48:47 +03:00
										 |  |  | #### `<unique-key-map>.originalKey(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `<unique-key-map>.uniqueKey(..)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | #### `<unique-key-map>.__key_pattern__`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 15:09:36 +03:00
										 |  |  | #### `<unique-key-map>.__unordered_rename__`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [BSD 3-Clause License](./LICENSE) | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 07:33:58 +03:00
										 |  |  | Copyright (c) 2020, Alex A. Naanou,   | 
					
						
							|  |  |  | All rights reserved. | 
					
						
							| 
									
										
										
										
											2020-10-07 03:53:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 06:31:29 +03:00
										 |  |  | <!-- vim:set ts=4 sw=4 spell : --> |