aurix-ai 2.7.3 → 2.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/aurix.js +2 -2
- package/dist/agent/AgentLoop.d.ts.map +1 -1
- package/dist/agent/AgentLoop.js +1 -52
- package/dist/agent/AgentLoop.js.map +1 -1
- package/dist/cli/App.d.ts.map +1 -1
- package/dist/cli/App.js +6 -0
- package/dist/cli/App.js.map +1 -1
- package/dist/cli/commands.d.ts.map +1 -1
- package/dist/cli/commands.js +7 -0
- package/dist/cli/commands.js.map +1 -1
- package/dist/tools/Browser.d.ts.map +1 -1
- package/dist/tools/Browser.js +44 -615
- package/dist/tools/Browser.js.map +1 -1
- package/dist/tools/captcha/CaptchaRouter.d.ts +6 -0
- package/dist/tools/captcha/CaptchaRouter.d.ts.map +1 -0
- package/dist/tools/captcha/CaptchaRouter.js +371 -0
- package/dist/tools/captcha/CaptchaRouter.js.map +1 -0
- package/dist/tools/captcha/RecaptchaSolver.d.ts +2 -0
- package/dist/tools/captcha/RecaptchaSolver.d.ts.map +1 -0
- package/dist/tools/captcha/RecaptchaSolver.js +1114 -0
- package/dist/tools/captcha/RecaptchaSolver.js.map +1 -0
- package/dist/tools/captcha/common.d.ts +35 -0
- package/dist/tools/captcha/common.d.ts.map +1 -0
- package/dist/tools/captcha/common.js +445 -0
- package/dist/tools/captcha/common.js.map +1 -0
- package/dist/tools/captcha/index.d.ts +5 -0
- package/dist/tools/captcha/index.d.ts.map +1 -0
- package/dist/tools/captcha/index.js +4 -0
- package/dist/tools/captcha/index.js.map +1 -0
- package/package.json +2 -2
- package/scripts/build.cjs +0 -15
- package/scripts/run-task.mjs +86 -0
- package/training/captcha-training.json +930 -0
- package/native/token-counter/index.d.ts +0 -7
- package/native/token-counter/index.js +0 -316
- package/native/token-counter/node_modules/.package-lock.json +0 -568
- package/native/token-counter/node_modules/2/array.js +0 -25
- package/native/token-counter/node_modules/2/index.js +0 -12
- package/native/token-counter/node_modules/2/iterator.js +0 -13
- package/native/token-counter/node_modules/2/license.txt +0 -21
- package/native/token-counter/node_modules/2/map.js +0 -27
- package/native/token-counter/node_modules/2/number.js +0 -109
- package/native/token-counter/node_modules/2/object.js +0 -23
- package/native/token-counter/node_modules/2/package.json +0 -60
- package/native/token-counter/node_modules/2/readme.md +0 -246
- package/native/token-counter/node_modules/2/string.js +0 -11
- package/native/token-counter/node_modules/2/test.js +0 -520
- package/native/token-counter/node_modules/@lamansky/every/index.js +0 -16
- package/native/token-counter/node_modules/@lamansky/every/license.txt +0 -21
- package/native/token-counter/node_modules/@lamansky/every/package.json +0 -38
- package/native/token-counter/node_modules/@lamansky/every/readme.md +0 -46
- package/native/token-counter/node_modules/@lamansky/flatten/index.js +0 -5
- package/native/token-counter/node_modules/@lamansky/flatten/license.txt +0 -21
- package/native/token-counter/node_modules/@lamansky/flatten/package.json +0 -35
- package/native/token-counter/node_modules/@lamansky/flatten/readme.md +0 -41
- package/native/token-counter/node_modules/@napi-rs/cli/LICENSE +0 -21
- package/native/token-counter/node_modules/@napi-rs/cli/README.md +0 -96
- package/native/token-counter/node_modules/@napi-rs/cli/package.json +0 -65
- package/native/token-counter/node_modules/@napi-rs/cli/scripts/index.js +0 -51371
- package/native/token-counter/node_modules/add-counter/index.js +0 -3
- package/native/token-counter/node_modules/add-counter/license.txt +0 -21
- package/native/token-counter/node_modules/add-counter/package.json +0 -34
- package/native/token-counter/node_modules/add-counter/readme.md +0 -36
- package/native/token-counter/node_modules/array-pad/LICENSE +0 -22
- package/native/token-counter/node_modules/array-pad/README.md +0 -80
- package/native/token-counter/node_modules/array-pad/index.js +0 -30
- package/native/token-counter/node_modules/array-pad/package.json +0 -28
- package/native/token-counter/node_modules/arrify/index.js +0 -8
- package/native/token-counter/node_modules/arrify/license +0 -21
- package/native/token-counter/node_modules/arrify/package.json +0 -33
- package/native/token-counter/node_modules/arrify/readme.md +0 -36
- package/native/token-counter/node_modules/case-insensitive/index.js +0 -72
- package/native/token-counter/node_modules/case-insensitive/license.txt +0 -21
- package/native/token-counter/node_modules/case-insensitive/package.json +0 -39
- package/native/token-counter/node_modules/case-insensitive/readme.md +0 -39
- package/native/token-counter/node_modules/case-insensitive/test.js +0 -114
- package/native/token-counter/node_modules/class-chain/index.js +0 -32
- package/native/token-counter/node_modules/class-chain/license.txt +0 -21
- package/native/token-counter/node_modules/class-chain/package.json +0 -39
- package/native/token-counter/node_modules/class-chain/readme.md +0 -23
- package/native/token-counter/node_modules/class-chain/test.js +0 -77
- package/native/token-counter/node_modules/copy-own/index.js +0 -10
- package/native/token-counter/node_modules/copy-own/license.txt +0 -21
- package/native/token-counter/node_modules/copy-own/package.json +0 -37
- package/native/token-counter/node_modules/copy-own/readme.md +0 -67
- package/native/token-counter/node_modules/def-props/index.js +0 -43
- package/native/token-counter/node_modules/def-props/license.txt +0 -21
- package/native/token-counter/node_modules/def-props/node_modules/is-obj/index.d.ts +0 -22
- package/native/token-counter/node_modules/def-props/node_modules/is-obj/index.js +0 -6
- package/native/token-counter/node_modules/def-props/node_modules/is-obj/license +0 -9
- package/native/token-counter/node_modules/def-props/node_modules/is-obj/package.json +0 -34
- package/native/token-counter/node_modules/def-props/node_modules/is-obj/readme.md +0 -39
- package/native/token-counter/node_modules/def-props/package.json +0 -51
- package/native/token-counter/node_modules/def-props/readme.md +0 -117
- package/native/token-counter/node_modules/empty-iterator/index.js +0 -3
- package/native/token-counter/node_modules/empty-iterator/license.txt +0 -21
- package/native/token-counter/node_modules/empty-iterator/package.json +0 -33
- package/native/token-counter/node_modules/empty-iterator/readme.md +0 -23
- package/native/token-counter/node_modules/enforce-range/index.js +0 -15
- package/native/token-counter/node_modules/enforce-range/license.txt +0 -21
- package/native/token-counter/node_modules/enforce-range/node_modules/2/array.js +0 -43
- package/native/token-counter/node_modules/enforce-range/node_modules/2/index.js +0 -10
- package/native/token-counter/node_modules/enforce-range/node_modules/2/iterator.js +0 -26
- package/native/token-counter/node_modules/enforce-range/node_modules/2/license.txt +0 -21
- package/native/token-counter/node_modules/enforce-range/node_modules/2/map.js +0 -47
- package/native/token-counter/node_modules/enforce-range/node_modules/2/number.js +0 -33
- package/native/token-counter/node_modules/enforce-range/node_modules/2/object.js +0 -61
- package/native/token-counter/node_modules/enforce-range/node_modules/2/package.json +0 -41
- package/native/token-counter/node_modules/enforce-range/node_modules/2/readme.md +0 -210
- package/native/token-counter/node_modules/enforce-range/node_modules/2/string.js +0 -37
- package/native/token-counter/node_modules/enforce-range/node_modules/2/test.js +0 -413
- package/native/token-counter/node_modules/enforce-range/package.json +0 -36
- package/native/token-counter/node_modules/enforce-range/readme.md +0 -53
- package/native/token-counter/node_modules/english-list/.travis.yml +0 -6
- package/native/token-counter/node_modules/english-list/LICENSE +0 -22
- package/native/token-counter/node_modules/english-list/README.md +0 -44
- package/native/token-counter/node_modules/english-list/index.js +0 -34
- package/native/token-counter/node_modules/english-list/package.json +0 -31
- package/native/token-counter/node_modules/english-list/test.log +0 -6
- package/native/token-counter/node_modules/errate/index.js +0 -19
- package/native/token-counter/node_modules/errate/license.txt +0 -21
- package/native/token-counter/node_modules/errate/package.json +0 -39
- package/native/token-counter/node_modules/errate/readme.md +0 -79
- package/native/token-counter/node_modules/ffn/index.js +0 -9
- package/native/token-counter/node_modules/ffn/license.txt +0 -21
- package/native/token-counter/node_modules/ffn/package.json +0 -34
- package/native/token-counter/node_modules/ffn/readme.md +0 -51
- package/native/token-counter/node_modules/get-own-property/index.js +0 -5
- package/native/token-counter/node_modules/get-own-property/license.txt +0 -21
- package/native/token-counter/node_modules/get-own-property/package.json +0 -31
- package/native/token-counter/node_modules/get-own-property/readme.md +0 -22
- package/native/token-counter/node_modules/has-duplicates/index.js +0 -11
- package/native/token-counter/node_modules/has-duplicates/license.txt +0 -21
- package/native/token-counter/node_modules/has-duplicates/package.json +0 -32
- package/native/token-counter/node_modules/has-duplicates/readme.md +0 -23
- package/native/token-counter/node_modules/has-duplicates/test.js +0 -30
- package/native/token-counter/node_modules/if-else-throw/index.js +0 -8
- package/native/token-counter/node_modules/if-else-throw/license.txt +0 -21
- package/native/token-counter/node_modules/if-else-throw/package.json +0 -37
- package/native/token-counter/node_modules/if-else-throw/readme.md +0 -32
- package/native/token-counter/node_modules/is-array-of-length/index.js +0 -7
- package/native/token-counter/node_modules/is-array-of-length/license.txt +0 -21
- package/native/token-counter/node_modules/is-array-of-length/package.json +0 -42
- package/native/token-counter/node_modules/is-array-of-length/readme.md +0 -41
- package/native/token-counter/node_modules/is-class-of/index.js +0 -7
- package/native/token-counter/node_modules/is-class-of/license.txt +0 -21
- package/native/token-counter/node_modules/is-class-of/package.json +0 -35
- package/native/token-counter/node_modules/is-class-of/readme.md +0 -57
- package/native/token-counter/node_modules/is-global-object/index.js +0 -6
- package/native/token-counter/node_modules/is-global-object/license.txt +0 -21
- package/native/token-counter/node_modules/is-global-object/package.json +0 -34
- package/native/token-counter/node_modules/is-global-object/readme.md +0 -22
- package/native/token-counter/node_modules/is-instance-of/index.js +0 -28
- package/native/token-counter/node_modules/is-instance-of/license.txt +0 -21
- package/native/token-counter/node_modules/is-instance-of/package.json +0 -44
- package/native/token-counter/node_modules/is-instance-of/readme.md +0 -55
- package/native/token-counter/node_modules/is-iterable/index.js +0 -5
- package/native/token-counter/node_modules/is-iterable/license +0 -21
- package/native/token-counter/node_modules/is-iterable/package.json +0 -30
- package/native/token-counter/node_modules/is-iterable/readme.md +0 -23
- package/native/token-counter/node_modules/is-nil/LICENSE +0 -21
- package/native/token-counter/node_modules/is-nil/README.md +0 -47
- package/native/token-counter/node_modules/is-nil/index.js +0 -6
- package/native/token-counter/node_modules/is-nil/package.json +0 -46
- package/native/token-counter/node_modules/is-obj/index.js +0 -5
- package/native/token-counter/node_modules/is-obj/license +0 -21
- package/native/token-counter/node_modules/is-obj/package.json +0 -33
- package/native/token-counter/node_modules/is-obj/readme.md +0 -34
- package/native/token-counter/node_modules/is-object/.eslintignore +0 -1
- package/native/token-counter/node_modules/is-object/.eslintrc +0 -5
- package/native/token-counter/node_modules/is-object/.nycrc +0 -13
- package/native/token-counter/node_modules/is-object/.testem.json +0 -14
- package/native/token-counter/node_modules/is-object/CHANGELOG.md +0 -121
- package/native/token-counter/node_modules/is-object/LICENSE +0 -19
- package/native/token-counter/node_modules/is-object/README.md +0 -48
- package/native/token-counter/node_modules/is-object/index.js +0 -5
- package/native/token-counter/node_modules/is-object/package.json +0 -78
- package/native/token-counter/node_modules/is-object/test/index.js +0 -44
- package/native/token-counter/node_modules/is-plain-object/LICENSE +0 -21
- package/native/token-counter/node_modules/is-plain-object/README.md +0 -104
- package/native/token-counter/node_modules/is-plain-object/index.d.ts +0 -5
- package/native/token-counter/node_modules/is-plain-object/index.js +0 -37
- package/native/token-counter/node_modules/is-plain-object/package.json +0 -79
- package/native/token-counter/node_modules/isobject/LICENSE +0 -21
- package/native/token-counter/node_modules/isobject/README.md +0 -122
- package/native/token-counter/node_modules/isobject/index.d.ts +0 -5
- package/native/token-counter/node_modules/isobject/index.js +0 -12
- package/native/token-counter/node_modules/isobject/package.json +0 -74
- package/native/token-counter/node_modules/lodash.set/LICENSE +0 -47
- package/native/token-counter/node_modules/lodash.set/README.md +0 -18
- package/native/token-counter/node_modules/lodash.set/index.js +0 -990
- package/native/token-counter/node_modules/lodash.set/package.json +0 -17
- package/native/token-counter/node_modules/longest-first/index.js +0 -3
- package/native/token-counter/node_modules/longest-first/license.txt +0 -21
- package/native/token-counter/node_modules/longest-first/package.json +0 -36
- package/native/token-counter/node_modules/longest-first/readme.md +0 -29
- package/native/token-counter/node_modules/m-o/index.js +0 -27
- package/native/token-counter/node_modules/m-o/license.txt +0 -21
- package/native/token-counter/node_modules/m-o/node_modules/new-object/index.js +0 -23
- package/native/token-counter/node_modules/m-o/node_modules/new-object/license.txt +0 -21
- package/native/token-counter/node_modules/m-o/node_modules/new-object/package.json +0 -40
- package/native/token-counter/node_modules/m-o/node_modules/new-object/readme.md +0 -55
- package/native/token-counter/node_modules/m-o/package.json +0 -45
- package/native/token-counter/node_modules/m-o/readme.md +0 -87
- package/native/token-counter/node_modules/map-iter/index.js +0 -6
- package/native/token-counter/node_modules/map-iter/license.txt +0 -21
- package/native/token-counter/node_modules/map-iter/package.json +0 -40
- package/native/token-counter/node_modules/map-iter/readme.md +0 -46
- package/native/token-counter/node_modules/new-object/index.js +0 -5
- package/native/token-counter/node_modules/new-object/license.txt +0 -21
- package/native/token-counter/node_modules/new-object/package.json +0 -49
- package/native/token-counter/node_modules/new-object/readme.md +0 -145
- package/native/token-counter/node_modules/ofn/index.js +0 -22
- package/native/token-counter/node_modules/ofn/license.txt +0 -21
- package/native/token-counter/node_modules/ofn/package.json +0 -40
- package/native/token-counter/node_modules/ofn/readme.md +0 -63
- package/native/token-counter/node_modules/otherwise/index.js +0 -11
- package/native/token-counter/node_modules/otherwise/license.txt +0 -21
- package/native/token-counter/node_modules/otherwise/package.json +0 -38
- package/native/token-counter/node_modules/otherwise/readme.md +0 -29
- package/native/token-counter/node_modules/parser-factory/index.js +0 -138
- package/native/token-counter/node_modules/parser-factory/license.txt +0 -21
- package/native/token-counter/node_modules/parser-factory/node_modules/arrify/index.d.ts +0 -38
- package/native/token-counter/node_modules/parser-factory/node_modules/arrify/index.js +0 -23
- package/native/token-counter/node_modules/parser-factory/node_modules/arrify/license +0 -9
- package/native/token-counter/node_modules/parser-factory/node_modules/arrify/package.json +0 -35
- package/native/token-counter/node_modules/parser-factory/node_modules/arrify/readme.md +0 -39
- package/native/token-counter/node_modules/parser-factory/package.json +0 -38
- package/native/token-counter/node_modules/parser-factory/readme.md +0 -15
- package/native/token-counter/node_modules/pfn/index.js +0 -5
- package/native/token-counter/node_modules/pfn/license.txt +0 -21
- package/native/token-counter/node_modules/pfn/package.json +0 -41
- package/native/token-counter/node_modules/pfn/readme.md +0 -111
- package/native/token-counter/node_modules/pfn/strict.js +0 -8
- package/native/token-counter/node_modules/plainify/index.js +0 -5
- package/native/token-counter/node_modules/plainify/license.txt +0 -21
- package/native/token-counter/node_modules/plainify/package.json +0 -36
- package/native/token-counter/node_modules/plainify/readme.md +0 -47
- package/native/token-counter/node_modules/possible-function/changelog.md +0 -7
- package/native/token-counter/node_modules/possible-function/index.js +0 -43
- package/native/token-counter/node_modules/possible-function/license.txt +0 -21
- package/native/token-counter/node_modules/possible-function/package.json +0 -34
- package/native/token-counter/node_modules/possible-function/readme.md +0 -77
- package/native/token-counter/node_modules/possible-function/test.js +0 -32
- package/native/token-counter/node_modules/qfn/index.js +0 -11
- package/native/token-counter/node_modules/qfn/license.txt +0 -21
- package/native/token-counter/node_modules/qfn/package.json +0 -40
- package/native/token-counter/node_modules/qfn/readme.md +0 -45
- package/native/token-counter/node_modules/roadblock/index.js +0 -3
- package/native/token-counter/node_modules/roadblock/license.txt +0 -21
- package/native/token-counter/node_modules/roadblock/package.json +0 -40
- package/native/token-counter/node_modules/roadblock/readme.md +0 -59
- package/native/token-counter/node_modules/round-to/index.d.ts +0 -56
- package/native/token-counter/node_modules/round-to/index.js +0 -37
- package/native/token-counter/node_modules/round-to/license +0 -9
- package/native/token-counter/node_modules/round-to/package.json +0 -38
- package/native/token-counter/node_modules/round-to/readme.md +0 -71
- package/native/token-counter/node_modules/rtrim-array/index.js +0 -10
- package/native/token-counter/node_modules/rtrim-array/license.txt +0 -21
- package/native/token-counter/node_modules/rtrim-array/package.json +0 -41
- package/native/token-counter/node_modules/rtrim-array/readme.md +0 -75
- package/native/token-counter/node_modules/sbo/index.js +0 -25
- package/native/token-counter/node_modules/sbo/license.txt +0 -21
- package/native/token-counter/node_modules/sbo/package.json +0 -50
- package/native/token-counter/node_modules/sbo/readme.md +0 -105
- package/native/token-counter/node_modules/sorp/index.js +0 -3
- package/native/token-counter/node_modules/sorp/license.txt +0 -21
- package/native/token-counter/node_modules/sorp/package.json +0 -34
- package/native/token-counter/node_modules/sorp/readme.md +0 -25
- package/native/token-counter/node_modules/trim-call/index.js +0 -6
- package/native/token-counter/node_modules/trim-call/license.txt +0 -21
- package/native/token-counter/node_modules/trim-call/package.json +0 -40
- package/native/token-counter/node_modules/trim-call/readme.md +0 -80
- package/native/token-counter/node_modules/type-error/LICENSE +0 -21
- package/native/token-counter/node_modules/type-error/README.md +0 -24
- package/native/token-counter/node_modules/type-error/index.js +0 -35
- package/native/token-counter/node_modules/type-error/package.json +0 -11
- package/native/token-counter/node_modules/vfn/index.js +0 -21
- package/native/token-counter/node_modules/vfn/license.txt +0 -21
- package/native/token-counter/node_modules/vfn/package.json +0 -40
- package/native/token-counter/node_modules/vfn/readme.md +0 -81
- package/native/token-counter/node_modules/wfn/index.js +0 -43
- package/native/token-counter/node_modules/wfn/license.txt +0 -21
- package/native/token-counter/node_modules/wfn/package.json +0 -38
- package/native/token-counter/node_modules/wfn/readme.md +0 -81
- package/native/token-counter/package-lock.json +0 -578
- package/native/token-counter/package.json +0 -21
- package/native/token-counter/token-counter.linux-x64-gnu.node +0 -0
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright ©2018 John Lamansky
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "empty-iterator",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "Creates an iterator that finishes immediately.",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"empty",
|
|
7
|
-
"iterator"
|
|
8
|
-
],
|
|
9
|
-
"author": "John Lamansky",
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"homepage": "https://github.com/lamansky/empty-iterator",
|
|
12
|
-
"repository": "lamansky/empty-iterator",
|
|
13
|
-
"main": "index.js",
|
|
14
|
-
"files": [],
|
|
15
|
-
"engines": {
|
|
16
|
-
"node": ">=4.0.0"
|
|
17
|
-
},
|
|
18
|
-
"dependencies": {},
|
|
19
|
-
"devDependencies": {
|
|
20
|
-
"eslint-config-lamansky": "^1.0.0",
|
|
21
|
-
"is-iterator": "^1.0.1",
|
|
22
|
-
"mocha": "^5.1.1"
|
|
23
|
-
},
|
|
24
|
-
"scripts": {
|
|
25
|
-
"test": "mocha"
|
|
26
|
-
},
|
|
27
|
-
"eslintConfig": {
|
|
28
|
-
"extends": "lamansky",
|
|
29
|
-
"env": {
|
|
30
|
-
"mocha": true
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# empty-iterator
|
|
2
|
-
|
|
3
|
-
Creates an iterator that finishes immediately.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
Requires [Node.js](https://nodejs.org/) 4.0.0 or above.
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm i empty-iterator
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## API
|
|
14
|
-
|
|
15
|
-
The module exports a single function with no parameters. Each time it is called, it creates and returns a new empty iterator.
|
|
16
|
-
|
|
17
|
-
## Example
|
|
18
|
-
|
|
19
|
-
```javascript
|
|
20
|
-
const emptyIterator = require('empty-iterator')
|
|
21
|
-
|
|
22
|
-
emptyIterator().next().done // true
|
|
23
|
-
```
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const toNumber = require('2/number')
|
|
4
|
-
|
|
5
|
-
module.exports = function enforceRange (min, max, value) {
|
|
6
|
-
if (arguments.length === 2) return v => enforceRange(min, max, v)
|
|
7
|
-
|
|
8
|
-
if (min === null) min = -Infinity
|
|
9
|
-
if (max === null) max = Infinity
|
|
10
|
-
;[min, max, value] = [min, max, value].map(n => toNumber(n, {fallback: null, finite: false}))
|
|
11
|
-
if (min > max) throw new RangeError('min cannot be more than max')
|
|
12
|
-
value = Math.max(min, value)
|
|
13
|
-
value = Math.min(max, value)
|
|
14
|
-
return value
|
|
15
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright ©2017 John Lamansky
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Converts a map, iterable, object, or primitive into an array.
|
|
5
|
-
* @param {any} thingToConvert
|
|
6
|
-
* @param {object} options
|
|
7
|
-
* @param {bool} [options.detectIndexKeys=false] Whether or not map/object keys
|
|
8
|
-
* should be assumed to represent array indices if the keys go from 0...n-1
|
|
9
|
-
* @return {array}
|
|
10
|
-
*/
|
|
11
|
-
module.exports = function (thingToConvert, {detectIndexKeys = false} = {}) {
|
|
12
|
-
if (typeof thingToConvert === 'undefined' || thingToConvert === null || Number.isNaN(thingToConvert)) {
|
|
13
|
-
return []
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (Array.isArray(thingToConvert)) {
|
|
17
|
-
return thingToConvert
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (thingToConvert instanceof Map) {
|
|
21
|
-
if (detectIndexKeys && JSON.stringify([...thingToConvert.keys()].map(String)) === JSON.stringify([...Array(thingToConvert.size).keys()].map(String))) {
|
|
22
|
-
return Array.from(thingToConvert.values())
|
|
23
|
-
} else {
|
|
24
|
-
return Array.from(thingToConvert)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (typeof thingToConvert[Symbol.iterator] !== 'undefined' && typeof thingToConvert !== 'string') {
|
|
29
|
-
return Array.from(thingToConvert)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (typeof thingToConvert === 'object' && thingToConvert !== null) {
|
|
33
|
-
const objectKeysArray = [...Object.keys(thingToConvert)]
|
|
34
|
-
objectKeysArray.sort() // JS does not guarantee object key order, so sort the keys.
|
|
35
|
-
if (detectIndexKeys && JSON.stringify(objectKeysArray) === JSON.stringify([...Array(Object.keys(thingToConvert).length).keys()].map(String))) {
|
|
36
|
-
return objectKeysArray.map(key => thingToConvert[key])
|
|
37
|
-
} else {
|
|
38
|
-
return Object.entries(thingToConvert)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return [thingToConvert]
|
|
43
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Converts an iterable, an object, or a primitive into an iterator.
|
|
5
|
-
* @param {any} thingToConvert
|
|
6
|
-
* @return {object}
|
|
7
|
-
*/
|
|
8
|
-
module.exports = function (thingToConvert) {
|
|
9
|
-
// Turn undefined/null/NaN into an empty iterator
|
|
10
|
-
if (typeof thingToConvert === 'undefined' || thingToConvert === null || Number.isNaN(thingToConvert)) {
|
|
11
|
-
return [][Symbol.iterator]()
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// Turn a primitive into an iterator
|
|
15
|
-
if (typeof thingToConvert !== 'object') {
|
|
16
|
-
return [thingToConvert][Symbol.iterator]()
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Turn an Array, Set, Map, etc. into an iterator
|
|
20
|
-
if (Symbol.iterator in thingToConvert) {
|
|
21
|
-
return thingToConvert[Symbol.iterator]()
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Turn an object into an iterator
|
|
25
|
-
return Object.entries(thingToConvert)[Symbol.iterator]()
|
|
26
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright ©2017 John Lamansky
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Converts an array or object into a Map.
|
|
5
|
-
* @param {any} thingToConvert
|
|
6
|
-
* @param {object} options
|
|
7
|
-
* @param {Map|null} [options.fallback=new Map()]
|
|
8
|
-
* The Map to return if `thingToConvert` cannot be turned into a Map.
|
|
9
|
-
* Set to null to throw an error instead.
|
|
10
|
-
* @param {bool} [options.detectPairs=true]
|
|
11
|
-
* This option only applies if `thingToConvert` is an array.
|
|
12
|
-
* If set to `true`, an array of two-element arrays (the kind that’s used
|
|
13
|
-
* to construct a Map) will be treated as an array of key-value entries.
|
|
14
|
-
* @param {bool} [options.mirror=false]
|
|
15
|
-
* This option only applies if `thingToConvert` is an array.
|
|
16
|
-
* If set to `true`, array values are used as both keys and values
|
|
17
|
-
* (i.e. the keys and values mirror each other).
|
|
18
|
-
* If `false`, array indices (0 to n-1) are used as the Map keys.
|
|
19
|
-
* @return {Map}
|
|
20
|
-
*/
|
|
21
|
-
module.exports = function (thingToConvert, {fallback = new Map(), detectPairs = true, mirror = false} = {}) {
|
|
22
|
-
if (thingToConvert instanceof Map) {
|
|
23
|
-
return thingToConvert
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (Array.isArray(thingToConvert)) {
|
|
27
|
-
if (detectPairs && thingToConvert.every(item => Array.isArray(item) && item.length === 2)) {
|
|
28
|
-
return new Map(thingToConvert)
|
|
29
|
-
} else if (mirror) {
|
|
30
|
-
return new Map(thingToConvert.map(item => [item, item]))
|
|
31
|
-
} else {
|
|
32
|
-
return new Map(thingToConvert.map((item, index) => [index, item]))
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (typeof thingToConvert === 'object' && thingToConvert !== null) {
|
|
37
|
-
return new Map(Object.keys(thingToConvert).map(
|
|
38
|
-
key => [key, thingToConvert[key]]
|
|
39
|
-
))
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!(fallback instanceof Map)) {
|
|
43
|
-
throw new TypeError('Failed to convert input of an unrecognized type into a Map.')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return fallback
|
|
47
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Converts a string or Number object into a primitive number.
|
|
5
|
-
* @param {any} thingToConvert
|
|
6
|
-
* @param {object} options
|
|
7
|
-
* @param {number|null} [options.fallback=0]
|
|
8
|
-
* The number to return if `thingToConvert` cannot be turned into a number.
|
|
9
|
-
* Set to `null` to throw an error instead.
|
|
10
|
-
* @param {bool} [options.finite=true]
|
|
11
|
-
* If true, only finite numbers are considered valid numbers.
|
|
12
|
-
* Therefore, if `thingToConvert` is `+Infinity` or `-Infinity`,
|
|
13
|
-
* the `fallback` will be returned.
|
|
14
|
-
* @param {bool} [options.round=false]
|
|
15
|
-
* If `true`, floats will be rounded to integers.
|
|
16
|
-
* @return {number}
|
|
17
|
-
*/
|
|
18
|
-
module.exports = function (thingToConvert, {fallback = 0, finite = true, round = false} = {}) {
|
|
19
|
-
let number = thingToConvert === null ? NaN : Number(thingToConvert)
|
|
20
|
-
if (Number.isFinite(number) || (!finite && !Number.isNaN(number))) {
|
|
21
|
-
if (round) number = Math.round(number)
|
|
22
|
-
return number
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (fallback instanceof Number) {
|
|
26
|
-
return Number(fallback)
|
|
27
|
-
} else if (typeof fallback !== 'number') {
|
|
28
|
-
throw new TypeError('Failed to convert input of an unrecognized type into a number.')
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (round) fallback = Math.round(fallback)
|
|
32
|
-
return fallback
|
|
33
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const toString = require('./string')
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Converts a Map or array into an object.
|
|
7
|
-
* @param {any} thingToConvert
|
|
8
|
-
* @param {object} options
|
|
9
|
-
* @param {object|null} [options.fallback={}]
|
|
10
|
-
* The object to return if `thingToConvert` cannot be turned into an object.
|
|
11
|
-
* Set to `null` to throw an error instead.
|
|
12
|
-
* @param {bool} [options.mirror=false]
|
|
13
|
-
* This option only applies if `thingToConvert` is an array.
|
|
14
|
-
* If set to `true`, array values are used as both keys and values
|
|
15
|
-
* (i.e. the keys and values mirror each other).
|
|
16
|
-
* If `false`, array indices (0 to n-1) are used as the object keys.
|
|
17
|
-
* If some array values have types which are among those supported as
|
|
18
|
-
* object keys (strings, numbers, and symbols), then mirroring will not happen.
|
|
19
|
-
* @return {object}
|
|
20
|
-
*/
|
|
21
|
-
module.exports = function (thingToConvert, {fallback = {}, mirror = false} = {}) {
|
|
22
|
-
if (thingToConvert instanceof Map) {
|
|
23
|
-
const object = {}
|
|
24
|
-
for (const [key, value] of thingToConvert.entries()) {
|
|
25
|
-
try {
|
|
26
|
-
object[typeof key === 'symbol' ? key : toString(key, {fallback: null})] = value
|
|
27
|
-
} catch (x) {
|
|
28
|
-
throw new TypeError('Cannot convert map to object because map has keys which objects do not support. Objects can only have string/number/symbol keys.')
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return object
|
|
32
|
-
} else if (Array.isArray(thingToConvert)) {
|
|
33
|
-
if (thingToConvert.every(entry => Array.isArray(entry) && entry.length === 2)) {
|
|
34
|
-
const object = {}
|
|
35
|
-
for (const [key, value] of thingToConvert) {
|
|
36
|
-
object[key] = value
|
|
37
|
-
}
|
|
38
|
-
return object
|
|
39
|
-
} else if (mirror && thingToConvert.every(value => ['string', 'number', 'symbol'].includes(typeof value))) {
|
|
40
|
-
const object = {}
|
|
41
|
-
for (const value of thingToConvert) {
|
|
42
|
-
object[value] = value
|
|
43
|
-
}
|
|
44
|
-
return object
|
|
45
|
-
} else {
|
|
46
|
-
const object = {}
|
|
47
|
-
for (let i = 0; i < thingToConvert.length; i++) {
|
|
48
|
-
object[i] = thingToConvert[i]
|
|
49
|
-
}
|
|
50
|
-
return object
|
|
51
|
-
}
|
|
52
|
-
} else if (typeof thingToConvert === 'object' && thingToConvert !== null) {
|
|
53
|
-
return thingToConvert
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (typeof fallback !== 'object' || fallback === null) {
|
|
57
|
-
throw new TypeError('Failed to convert input of an unrecognized type into an object.')
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return fallback
|
|
61
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "2",
|
|
3
|
-
"version": "1.0.2",
|
|
4
|
-
"description": "The Type Conversion Library. Numbers, Strings, Arrays, Maps, Objects, and Iterators.",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"type",
|
|
7
|
-
"convert",
|
|
8
|
-
"conversion",
|
|
9
|
-
"to",
|
|
10
|
-
"array",
|
|
11
|
-
"iterator",
|
|
12
|
-
"map",
|
|
13
|
-
"number",
|
|
14
|
-
"int",
|
|
15
|
-
"integer",
|
|
16
|
-
"float",
|
|
17
|
-
"object",
|
|
18
|
-
"string"
|
|
19
|
-
],
|
|
20
|
-
"author": "John Lamansky",
|
|
21
|
-
"license": "MIT",
|
|
22
|
-
"homepage": "https://github.com/lamansky/2",
|
|
23
|
-
"repository": "lamansky/2",
|
|
24
|
-
"main": "index.js",
|
|
25
|
-
"engines": {
|
|
26
|
-
"node": ">=7.0.0"
|
|
27
|
-
},
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"eslint-config-lamansky": "^1.0.0",
|
|
30
|
-
"mocha": "^3.2.0"
|
|
31
|
-
},
|
|
32
|
-
"scripts": {
|
|
33
|
-
"test": "mocha"
|
|
34
|
-
},
|
|
35
|
-
"eslintConfig": {
|
|
36
|
-
"extends": "lamansky",
|
|
37
|
-
"env": {
|
|
38
|
-
"mocha": true
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
# “2”: The Type Conversion Library
|
|
2
|
-
|
|
3
|
-
A [Node.js](https://nodejs.org/) module for converting between various JavaScript types: arrays, iterators, maps, numbers, objects, and strings.
|
|
4
|
-
|
|
5
|
-
```javascript
|
|
6
|
-
const {toArray, toIterator, toMap, toNumber, toObject, toString} = require('2')
|
|
7
|
-
|
|
8
|
-
let data = {a: 1, b: 2}
|
|
9
|
-
data = toMap(data)
|
|
10
|
-
data = toArray(data)
|
|
11
|
-
data = toObject(data)
|
|
12
|
-
data = toIterator(data)
|
|
13
|
-
data = toArray(data)
|
|
14
|
-
data = toMap(data)
|
|
15
|
-
data = toObject(data) // {a: 1, b: 2}
|
|
16
|
-
|
|
17
|
-
data = '1.23'
|
|
18
|
-
data = toNumber(data)
|
|
19
|
-
data = toString(data) // '1.23'
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Installation
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
npm install 2 --save
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
## Usage
|
|
29
|
-
|
|
30
|
-
### Requiring the Functions
|
|
31
|
-
|
|
32
|
-
You can require needed functions via a destructuring assignment:
|
|
33
|
-
|
|
34
|
-
```javascript
|
|
35
|
-
const {toArray, toIterator, toMap, toNumber, toObject, toString} = require('2')
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
Or you can require individual functions via submodules:
|
|
39
|
-
|
|
40
|
-
```javascript
|
|
41
|
-
const toArray = require('2/array')
|
|
42
|
-
const toIterator = require('2/iterator')
|
|
43
|
-
const toMap = require('2/map')
|
|
44
|
-
const toNumber = require('2/number')
|
|
45
|
-
const toObject = require('2/object')
|
|
46
|
-
const toString = require('2/string')
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### Converting to Arrays
|
|
50
|
-
```javascript
|
|
51
|
-
const toArray = require('2/array')
|
|
52
|
-
|
|
53
|
-
// Map => Array
|
|
54
|
-
let map = new Map()
|
|
55
|
-
map.set('a', 1)
|
|
56
|
-
map.set('b', 2)
|
|
57
|
-
toArray(map) // [['a', 1], ['b', 2]]
|
|
58
|
-
|
|
59
|
-
// Iterator => Array
|
|
60
|
-
toArray(map.values()) // [1, 2]
|
|
61
|
-
|
|
62
|
-
// Object => Array
|
|
63
|
-
toArray({a: 1, b: 2}) // [['a', 1], ['b', 2]]
|
|
64
|
-
|
|
65
|
-
// Array-like object => Array
|
|
66
|
-
toArray({0: 'first', 1: 'second'}, {detectIndexKeys: true}) // ['first', 'second']
|
|
67
|
-
|
|
68
|
-
// Primitive value => Array
|
|
69
|
-
toArray('test') // ['test']
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Converting to Iterators
|
|
73
|
-
```javascript
|
|
74
|
-
const toIterator = require('2/iterator')
|
|
75
|
-
|
|
76
|
-
// Object => Iterator
|
|
77
|
-
let iterator = toIterator({a: 1, b: 2})
|
|
78
|
-
iterator.next().value // ['a', 1]
|
|
79
|
-
iterator.next().value // ['b', 2]
|
|
80
|
-
iterator.next().done // true
|
|
81
|
-
|
|
82
|
-
// Primitive value => Iterator
|
|
83
|
-
toIterator('test').next().value // 'test'
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
### Converting to Maps
|
|
87
|
-
```javascript
|
|
88
|
-
const toMap = require('2/map')
|
|
89
|
-
|
|
90
|
-
// Array of key/value pairs => Map
|
|
91
|
-
toMap([['a', 1], ['b', 2]])
|
|
92
|
-
map.get('a') // 1
|
|
93
|
-
map.get('b') // 2
|
|
94
|
-
|
|
95
|
-
// Array of values => Map
|
|
96
|
-
let map = toMap(['a', 'b'])
|
|
97
|
-
map.get(0) // 'a'
|
|
98
|
-
map.get(1) // 'b'
|
|
99
|
-
|
|
100
|
-
// Object => Map
|
|
101
|
-
map = toMap({a: 1, b: 2})
|
|
102
|
-
map.get('a') // 1
|
|
103
|
-
map.get('b') // 2
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### Converting to Numbers
|
|
107
|
-
```javascript
|
|
108
|
-
const toNumber = require('2/number')
|
|
109
|
-
|
|
110
|
-
toNumber('1.2') // 1.2
|
|
111
|
-
toNumber(Infinity) // 0
|
|
112
|
-
toNumber(NaN) // 0
|
|
113
|
-
toNumber('not a number') // 0
|
|
114
|
-
|
|
115
|
-
// Can specify a fallback other than zero:
|
|
116
|
-
toNumber('not a number', {fallback: 100}) // 100
|
|
117
|
-
|
|
118
|
-
// You can choose to throw an error for invalid inputs by
|
|
119
|
-
// providing a null fallback:
|
|
120
|
-
toNumber('not a number', {fallback: null}) // throws error
|
|
121
|
-
|
|
122
|
-
// Option to round floats:
|
|
123
|
-
toNumber('4.7') // 4.7
|
|
124
|
-
toNumber('4.7', {round: true}) // 5
|
|
125
|
-
|
|
126
|
-
// By default, Infinity is not considered a valid number,
|
|
127
|
-
// but this can be changed:
|
|
128
|
-
toNumber(Infinity) // 0
|
|
129
|
-
toNumber(Infinity, {finite: false}) // Infinity
|
|
130
|
-
|
|
131
|
-
// Number object => Number
|
|
132
|
-
let numberObject = new Number(123)
|
|
133
|
-
typeof numberObject // 'object'
|
|
134
|
-
typeof toNumber(numberObject) // 'number'
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
### Converting to Objects
|
|
138
|
-
```javascript
|
|
139
|
-
const toObject = require('2/object')
|
|
140
|
-
|
|
141
|
-
// Array of key/value pairs => Object
|
|
142
|
-
let obj = toObject([['a', 1], ['b', 2]])
|
|
143
|
-
obj.a // 1
|
|
144
|
-
obj.b // 2
|
|
145
|
-
|
|
146
|
-
// Array => Object
|
|
147
|
-
let obj = toObject(['first', 'second'])
|
|
148
|
-
Object.keys(obj).length // 2
|
|
149
|
-
obj[0] // 'first'
|
|
150
|
-
obj[1] // 'second'
|
|
151
|
-
|
|
152
|
-
// In the above example, the array indices become the object keys.
|
|
153
|
-
// But you can make the keys mirror the values instead:
|
|
154
|
-
let obj = toObject(['first', 'second'], {mirror: true})
|
|
155
|
-
Object.keys(obj).length // 2
|
|
156
|
-
obj.first // 'first'
|
|
157
|
-
obj.second // 'second'
|
|
158
|
-
|
|
159
|
-
// Map => Object
|
|
160
|
-
let map = new Map()
|
|
161
|
-
map.set('key1', 'value1')
|
|
162
|
-
map.set('key2', 'value2')
|
|
163
|
-
let obj = toObject(map)
|
|
164
|
-
obj.key1 // 'value1'
|
|
165
|
-
obj.key2 // 'value2'
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
### Converting to Strings
|
|
169
|
-
```javascript
|
|
170
|
-
const toString = require('2/string')
|
|
171
|
-
|
|
172
|
-
toString(123) // '123'
|
|
173
|
-
toString(-0) // '0'
|
|
174
|
-
|
|
175
|
-
toString(true) // ''
|
|
176
|
-
toString(false) // ''
|
|
177
|
-
toString(undefined) // ''
|
|
178
|
-
toString(null) // ''
|
|
179
|
-
toString(Infinity) // ''
|
|
180
|
-
toString(NaN) // ''
|
|
181
|
-
toString({}) // ''
|
|
182
|
-
toString([]) // ''
|
|
183
|
-
toString(function () {}) // ''
|
|
184
|
-
toString(Symbol('test')) // ''
|
|
185
|
-
|
|
186
|
-
// Compare the above to standard JavaScript string conversion:
|
|
187
|
-
String(true) // 'true'
|
|
188
|
-
String(false) // 'false'
|
|
189
|
-
String(undefined) // 'undefined'
|
|
190
|
-
String(null) // 'null'
|
|
191
|
-
String(Infinity) // 'Infinity'
|
|
192
|
-
String(NaN) // 'NaN'
|
|
193
|
-
String({}) // '[object Object]'
|
|
194
|
-
String([]) // ''
|
|
195
|
-
String(function () {}) // 'function () {}'
|
|
196
|
-
String(Symbol('test')) // 'Symbol(test)'
|
|
197
|
-
|
|
198
|
-
// Default fallback is an empty string, but you can change it:
|
|
199
|
-
toString(undefined) // ''
|
|
200
|
-
toString(undefined, {fallback: 'N/A'}) // 'N/A'
|
|
201
|
-
|
|
202
|
-
// You can choose to throw an error for invalid inputs by
|
|
203
|
-
// providing a null fallback:
|
|
204
|
-
toString(undefined, {fallback: null}) // throws error
|
|
205
|
-
|
|
206
|
-
// String object => String
|
|
207
|
-
let stringObject = new String('test')
|
|
208
|
-
typeof stringObject // 'object'
|
|
209
|
-
typeof toString(stringObject) // 'string'
|
|
210
|
-
```
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Converts a number or `toString`able object into a primitive string.
|
|
5
|
-
* @param {any} thingToConvert
|
|
6
|
-
* @param {object} options
|
|
7
|
-
* @param {string|null} [options.fallback='']
|
|
8
|
-
* The string to return if `thingToConvert` cannot be turned into a string.
|
|
9
|
-
* Set to `null` to throw an error instead.
|
|
10
|
-
* @return {string}
|
|
11
|
-
*/
|
|
12
|
-
module.exports = function (thingToConvert, {fallback = ''} = {}) {
|
|
13
|
-
if (typeof thingToConvert === 'string') {
|
|
14
|
-
return thingToConvert
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (thingToConvert instanceof String) {
|
|
18
|
-
return thingToConvert + ''
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (typeof thingToConvert === 'number' && Number.isFinite(thingToConvert)) {
|
|
22
|
-
return thingToConvert + ''
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (typeof thingToConvert === 'object' && thingToConvert !== null &&
|
|
26
|
-
thingToConvert.toString !== Object.prototype.toString) {
|
|
27
|
-
return String(thingToConvert)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (fallback instanceof String) {
|
|
31
|
-
return fallback + ''
|
|
32
|
-
} else if (typeof fallback !== 'string') {
|
|
33
|
-
throw new TypeError('Failed to convert input of an unrecognized type into a string.')
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return fallback
|
|
37
|
-
}
|