├── .editorconfig ├── .github ├── FUNDING.yml └── workflows │ ├── integrate.yml │ ├── publish.yml │ └── validate.yml ├── .gitignore ├── .npmignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── array-length ├── coerce.js └── ensure.js ├── array-like ├── ensure.js └── is.js ├── array ├── ensure.js └── is.js ├── big-int ├── coerce.js └── ensure.js ├── commitlint.config.js ├── constructor ├── date ├── ensure.js └── is.js ├── docs ├── array-length.md ├── array-like.md ├── array.md ├── big-int.md ├── constructor.md ├── date.md ├── ensure.md ├── error.md ├── finite.md ├── function.md ├── integer.md ├── iterable.md ├── map.md ├── natural-number.md ├── number.md ├── object.md ├── plain-function.md ├── plain-object.md ├── promise.md ├── prototype.md ├── reg-exp.md ├── safe-integer.md ├── set.md ├── string.md ├── thenable.md ├── time-value.md └── value.md ├── ensure.js ├── error ├── ensure.js └── is.js ├── finite ├── coerce.js └── ensure.js ├── function ├── ensure.js └── is.js ├── integer ├── coerce.js └── ensure.js ├── iterable ├── ensure.js └── is.js ├── lib ├── ensure │ └── min.js ├── is-to-string-tag-supported.js ├── resolve-error-message.js ├── resolve-exception.js ├── safe-to-string.js └── to-short-string.js ├── map ├── ensure.js └── is.js ├── natural-number ├── coerce.js └── ensure.js ├── number ├── coerce.js └── ensure.js ├── object ├── ensure.js └── is.js ├── package.json ├── plain-function ├── ensure.js └── is.js ├── plain-object ├── ensure.js └── is.js ├── promise ├── ensure.js └── is.js ├── prototype └── is.js ├── reg-exp ├── ensure.js └── is.js ├── safe-integer ├── coerce.js └── ensure.js ├── set ├── ensure.js └── is.js ├── string ├── coerce.js └── ensure.js ├── test ├── _lib │ ├── arrow-function-if-supported.js │ └── class-if-supported.js ├── array-length │ ├── coerce.js │ └── ensure.js ├── array-like │ ├── ensure.js │ └── is.js ├── array │ ├── ensure.js │ └── is.js ├── big-int │ ├── coerce.js │ └── ensure.js ├── constructor ├── date │ ├── ensure.js │ └── is.js ├── ensure.js ├── error │ ├── ensure.js │ └── is.js ├── finite │ ├── coerce.js │ └── ensure.js ├── function │ ├── ensure.js │ └── is.js ├── integer │ ├── coerce.js │ └── ensure.js ├── iterable │ ├── ensure.js │ └── is.js ├── lib │ ├── is-to-string-tag-supported.js │ ├── resolve-error-message.js │ ├── resolve-exception.js │ ├── safe-to-string.js │ └── to-short-string.js ├── map │ ├── ensure.js │ └── is.js ├── natural-number │ ├── coerce.js │ └── ensure.js ├── number │ ├── coerce.js │ └── ensure.js ├── object │ ├── ensure.js │ └── is.js ├── plain-function │ ├── ensure.js │ └── is.js ├── plain-object │ ├── ensure.js │ └── is.js ├── promise │ ├── ensure.js │ └── is.js ├── prototype │ └── is.js ├── reg-exp │ ├── ensure.js │ └── is.js ├── safe-integer │ ├── coerce.js │ └── ensure.js ├── set │ ├── ensure.js │ └── is.js ├── string │ ├── coerce.js │ └── ensure.js ├── thenable │ ├── ensure.js │ └── is.js ├── time-value │ ├── coerce.js │ └── ensure.js └── value │ ├── ensure.js │ └── is.js ├── thenable ├── ensure.js └── is.js ├── time-value ├── coerce.js └── ensure.js ├── ts-types ├── array-length │ ├── coerce.d.ts │ └── ensure.d.ts ├── array-like │ ├── ensure.d.ts │ └── is.d.ts ├── array │ ├── ensure.d.ts │ └── is.d.ts ├── big-int │ ├── coerce.d.ts │ └── ensure.d.ts ├── constructor ├── date │ ├── ensure.d.ts │ └── is.d.ts ├── ensure.d.ts ├── error │ ├── ensure.d.ts │ └── is.d.ts ├── finite │ ├── coerce.d.ts │ └── ensure.d.ts ├── function │ ├── ensure.d.ts │ └── is.d.ts ├── integer │ ├── coerce.d.ts │ └── ensure.d.ts ├── iterable │ ├── ensure.d.ts │ └── is.d.ts ├── map │ ├── ensure.d.ts │ └── is.d.ts ├── natural-number │ ├── coerce.d.ts │ └── ensure.d.ts ├── number │ ├── coerce.d.ts │ └── ensure.d.ts ├── object │ ├── ensure.d.ts │ └── is.d.ts ├── plain-function │ ├── ensure.d.ts │ └── is.d.ts ├── plain-object │ ├── ensure.d.ts │ └── is.d.ts ├── promise │ ├── ensure.d.ts │ └── is.d.ts ├── prototype │ └── is.d.ts ├── reg-exp │ ├── ensure.d.ts │ └── is.d.ts ├── safe-integer │ ├── coerce.d.ts │ └── ensure.d.ts ├── set │ ├── ensure.d.ts │ └── is.d.ts ├── string │ ├── coerce.d.ts │ └── ensure.d.ts ├── thenable │ ├── ensure.d.ts │ └── is.d.ts ├── time-value │ ├── coerce.d.ts │ └── ensure.d.ts └── value │ ├── ensure.d.ts │ └── is.d.ts └── value ├── ensure.js └── is.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | end_of_line = lf 9 | insert_final_newline = true 10 | indent_style = tab 11 | trim_trailing_whitespace = true 12 | 13 | [*.{md,yml}] 14 | indent_size = 2 15 | indent_style = space 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: medikoo 2 | -------------------------------------------------------------------------------- /.github/workflows/integrate.yml: -------------------------------------------------------------------------------- 1 | # main only 2 | 3 | name: Integrate 4 | 5 | on: 6 | push: 7 | branches: [main] 8 | 9 | env: 10 | FORCE_COLOR: 1 11 | 12 | jobs: 13 | _: 14 | uses: medikoo/github-actions-workflows/.github/workflows/10-integrate.yml@main 15 | secrets: 16 | USER_GITHUB_TOKEN: ${{ secrets.USER_GITHUB_TOKEN }} 17 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | # Version tags only 2 | 3 | name: Publish 4 | 5 | on: 6 | push: 7 | tags: 8 | - v[0-9]+.[0-9]+.[0-9]+ 9 | 10 | env: 11 | FORCE_COLOR: 1 12 | 13 | jobs: 14 | _: 15 | uses: medikoo/github-actions-workflows/.github/workflows/publish.yml@main 16 | secrets: 17 | USER_GITHUB_TOKEN: ${{ secrets.USER_GITHUB_TOKEN }} 18 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 19 | -------------------------------------------------------------------------------- /.github/workflows/validate.yml: -------------------------------------------------------------------------------- 1 | # PR's only 2 | 3 | name: Validate 4 | 5 | on: 6 | pull_request: 7 | branches: [main] 8 | 9 | env: 10 | FORCE_COLOR: 1 11 | 12 | jobs: 13 | _: 14 | uses: medikoo/github-actions-workflows/.github/workflows/10-validate.yml@main 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.nyc_output 2 | /coverage 3 | /node_modules 4 | npm-debug.log 5 | /package-lock.json 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /.editorconfig 2 | /.github 3 | /commitlint.config.js 4 | /test 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ### [2.7.3](https://github.com/medikoo/type/compare/v2.7.2...v2.7.3) (2024-05-30) 6 | 7 | ### Maintenance Improvements 8 | 9 | - Add security vulnerability policy ([a28cc58](https://github.com/medikoo/type/commit/a28cc586882ee8b1f0af6789b5af927cc4f14227)) 10 | - Prettify ([aaa9256](https://github.com/medikoo/type/commit/aaa92561a812d756ece952e1afade7d4aa0f116c)) 11 | - Upgrade `prettier-elastic` to v3 ([7f10b2c](https://github.com/medikoo/type/commit/7f10b2c995bb590903b2d3d2c1eacfd7e045d73b)) 12 | 13 | ### [2.7.2](https://github.com/medikoo/type/compare/v2.7.1...v2.7.2) (2022-08-05) 14 | 15 | ### Maintenance Improvements 16 | 17 | - **TS:** Improve `ensure` options handling ([#8](https://github.com/medikoo/type/issues/8)) ([4a54066](https://github.com/medikoo/type/commit/4a54066d7b55cef14ac4aa25a6f070296a043a6f)) ([Marco](https://github.com/borracciaBlu)) 18 | 19 | ### [2.7.1](https://github.com/medikoo/type/compare/v2.7.0...v2.7.1) (2022-08-04) 20 | 21 | ### Maintenance Improvements 22 | 23 | - **TS:** Fix support for `isOptional` in `ensure` options ([#7](https://github.com/medikoo/type/issues/7)) ([320f89b](https://github.com/medikoo/type/commit/320f89b89237e3e0ceff5e26b67cb18bd52cb42d)) ([Marco](https://github.com/borracciaBlu)) 24 | 25 | ## [2.7.0](https://github.com/medikoo/type/compare/v2.6.1...v2.7.0) (2022-08-03) 26 | 27 | ### Features 28 | 29 | - `BigInt.coerce` and `BigInt.ensure` ([e49ad78](https://github.com/medikoo/type/commit/e49ad787bd3aa67b7aa9f7a8ea4cde22a08bebc5)) 30 | 31 | ### [2.6.1](https://github.com/medikoo/type/compare/v2.6.0...v2.6.1) (2022-07-29) 32 | 33 | ### Maintenance Improvements 34 | 35 | - Declare TS types ([#6](https://github.com/medikoo/type/issues/6)) ([6378e2c](https://github.com/medikoo/type/commit/6378e2c457670bcb8a9b898e0f2502ed5b942d44)) ([Marco](https://github.com/borracciaBlu)) 36 | 37 | ## [2.6.0](https://github.com/medikoo/type/compare/v2.5.0...v2.6.0) (2022-02-02) 38 | 39 | ### Features 40 | 41 | - `constructor` validation utils ([74b99bb](https://github.com/medikoo/type/commit/74b99bbf6be27083bf9a053961edb2a585ae3e77)) 42 | 43 | ## [2.5.0](https://github.com/medikoo/type/compare/v2.4.0...v2.5.0) (2021-03-08) 44 | 45 | ### Features 46 | 47 | - `errorCode` option for `ensure*` utils ([777a1f2](https://github.com/medikoo/type/commit/777a1f2c9fd76defcd24d3a30cce49491947fef7)) 48 | 49 | ## [2.4.0](https://github.com/medikoo/type/compare/v2.3.0...v2.4.0) (2021-03-08) 50 | 51 | ### Features 52 | 53 | - `set/is` and `set/ensure` utils ([083ec23](https://github.com/medikoo/type/commit/083ec2351718c310f316dcfd8c624a13201e227f)) 54 | 55 | ## [2.3.0](https://github.com/medikoo/type/compare/v2.2.0...v2.3.0) (2021-02-16) 56 | 57 | ### Features 58 | 59 | - `map/is` and `map/ensure` utils ([aafd1cb](https://github.com/medikoo/type/commit/aafd1cbd8c888fda98d39fd17e59f38b078d7bcf)) 60 | 61 | ## [2.2.0](https://github.com/medikoo/type/compare/v2.1.0...v2.2.0) (2021-02-11) 62 | 63 | ### Features 64 | 65 | - Support `ensureItem` option in `array/ensure` ([8f74973](https://github.com/medikoo/type/commit/8f749739df9bfebf44087093e09c8f7341a33a09)) 66 | 67 | ## [2.1.0](https://github.com/medikoo/type/compare/v2.0.0...v2.1.0) (2020-08-21) 68 | 69 | ### Features 70 | 71 | - `ensure` util for cumulated input validation ([814c5a8](https://github.com/medikoo/type/commit/814c5a801ecac23d06d8a5f4bcafc4763a04408c)) 72 | - Provide an alternative error message with `options.name` ([c7751c0](https://github.com/medikoo/type/commit/c7751c084ee4f3d3ed10500db0edde2ff00e03a1)) 73 | - Support `%n` (meaningful name) token in error message resolver ([b0f374e](https://github.com/medikoo/type/commit/b0f374e54345c714fe37a90887ecfe60577ce133)) 74 | - Support `min` validation for natural numbers ([e703512](https://github.com/medikoo/type/commit/e70351248818d3e113110106ad174b42c5fd9b25)) 75 | - Support custom Error constructors ([c6ecb90](https://github.com/medikoo/type/commit/c6ecb90e21c1c778210934204cbe393fb89ef2f6)) 76 | 77 | ### Bug Fixes 78 | 79 | - Fix typo in error message ([2735533](https://github.com/medikoo/type/commit/2735533de28d33dfa13222743698169c92d08c09)) 80 | 81 | ## [2.0.0](https://github.com/medikoo/type/compare/v1.2.0...v2.0.0) (2019-10-10) 82 | 83 | ### Features 84 | 85 | - `allowedKeys` option for plain-object/ensure ([f81e72e](https://github.com/medikoo/type/commit/f81e72e)) 86 | - `ensurePropertyValue` option for plain-object/ensure ([c5ff8fb](https://github.com/medikoo/type/commit/c5ff8fb)) 87 | - Replace `coerceItem` with `ensureItem` option in iterable/ensure ([721494f](https://github.com/medikoo/type/commit/721494f)) 88 | - Seclude lib/resolve-error-message ([12636d9](https://github.com/medikoo/type/commit/12636d9)) 89 | - Validate options.ensureItem in iterable/ensure ([78da6c1](https://github.com/medikoo/type/commit/78da6c1)) 90 | 91 | ### BREAKING CHANGES 92 | 93 | - iterable/ensure no longer supports `coerceItem` option. Instead `ensureItem` was introduced 94 | 95 | ## [1.2.0](https://github.com/medikoo/type/compare/v1.1.0...v1.2.0) (2019-09-20) 96 | 97 | ### Bug Fixes 98 | 99 | - Improve error message so it's not confusing ([97cd6b9](https://github.com/medikoo/type/commit/97cd6b9)) 100 | 101 | ### Features 102 | 103 | - 'coerceItem' option for iterable/ensure ([0818860](https://github.com/medikoo/type/commit/0818860)) 104 | 105 | ## [1.1.0](https://github.com/medikoo/type/compare/v1.0.3...v1.1.0) (2019-09-20) 106 | 107 | ### Features 108 | 109 | - `denyEmpty` option for iterables validation ([301d071](https://github.com/medikoo/type/commit/301d071)) 110 | 111 | ### [1.0.3](https://github.com/medikoo/type/compare/v1.0.2...v1.0.3) (2019-08-06) 112 | 113 | ### Bug Fixes 114 | 115 | - Recognize custom built ES5 era errors ([6462fac](https://github.com/medikoo/type/commit/6462fac)) 116 | 117 | ### [1.0.2](https://github.com/medikoo/type/compare/v1.0.1...v1.0.2) (2019-08-06) 118 | 119 | ### Bug Fixes 120 | 121 | - Recognize host errors (e.g. DOMException) ([96ef399](https://github.com/medikoo/type/commit/96ef399)) 122 | 123 | ## [1.0.1](https://github.com/medikoo/type/compare/v1.0.0...v1.0.1) (2019-04-08) 124 | 125 | # 1.0.0 (2019-04-05) 126 | 127 | ### Bug Fixes 128 | 129 | - ensure 'is' functions can't crash ([59ceb78](https://github.com/medikoo/type/commit/59ceb78)) 130 | 131 | ### Features 132 | 133 | - array-length/coerce ([af8ddec](https://github.com/medikoo/type/commit/af8ddec)) 134 | - array-length/ensure ([d313eb6](https://github.com/medikoo/type/commit/d313eb6)) 135 | - array-like/ensure ([45f1ddd](https://github.com/medikoo/type/commit/45f1ddd)) 136 | - array-like/is ([9a026a5](https://github.com/medikoo/type/commit/9a026a5)) 137 | - array/ensure ([9db1515](https://github.com/medikoo/type/commit/9db1515)) 138 | - array/is ([9672839](https://github.com/medikoo/type/commit/9672839)) 139 | - date/ensure ([44e25a0](https://github.com/medikoo/type/commit/44e25a0)) 140 | - date/is ([0316558](https://github.com/medikoo/type/commit/0316558)) 141 | - ensure to not crash ([3998348](https://github.com/medikoo/type/commit/3998348)) 142 | - ensure/number ([134b5cb](https://github.com/medikoo/type/commit/134b5cb)) 143 | - error/ensure ([d5c8a30](https://github.com/medikoo/type/commit/d5c8a30)) 144 | - error/is-error ([4d6b899](https://github.com/medikoo/type/commit/4d6b899)) 145 | - finite/coerce ([accaad1](https://github.com/medikoo/type/commit/accaad1)) 146 | - finite/ensure ([51e4174](https://github.com/medikoo/type/commit/51e4174)) 147 | - function/ensure ([b624c9a](https://github.com/medikoo/type/commit/b624c9a)) 148 | - function/is ([dab8026](https://github.com/medikoo/type/commit/dab8026)) 149 | - integer/coerce ([89dea2e](https://github.com/medikoo/type/commit/89dea2e)) 150 | - integer/ensure ([44a7071](https://github.com/medikoo/type/commit/44a7071)) 151 | - iterable/ensure ([3d48841](https://github.com/medikoo/type/commit/3d48841)) 152 | - iterable/is ([cf09513](https://github.com/medikoo/type/commit/cf09513)) 153 | - lib/is-to-string-tag-supported ([c8c001d](https://github.com/medikoo/type/commit/c8c001d)) 154 | - natural-number/coerce ([d08fdd9](https://github.com/medikoo/type/commit/d08fdd9)) 155 | - natural-number/ensure ([6c24d12](https://github.com/medikoo/type/commit/6c24d12)) 156 | - number/coerce ([86ccf08](https://github.com/medikoo/type/commit/86ccf08)) 157 | - object/ensure ([a9e8eed](https://github.com/medikoo/type/commit/a9e8eed)) 158 | - object/is ([d2d7251](https://github.com/medikoo/type/commit/d2d7251)) 159 | - plain-function/ensure ([5186518](https://github.com/medikoo/type/commit/5186518)) 160 | - plain-function/is ([51bc791](https://github.com/medikoo/type/commit/51bc791)) 161 | - plain-object/ensure ([91cf5e5](https://github.com/medikoo/type/commit/91cf5e5)) 162 | - plain-object/is ([4dcf393](https://github.com/medikoo/type/commit/4dcf393)) 163 | - promise/ensure ([8d096a4](https://github.com/medikoo/type/commit/8d096a4)) 164 | - promise/is ([a00de02](https://github.com/medikoo/type/commit/a00de02)) 165 | - prototype/is ([b23bdcc](https://github.com/medikoo/type/commit/b23bdcc)) 166 | - reg-exp/ensure ([6f7bbcb](https://github.com/medikoo/type/commit/6f7bbcb)) 167 | - reg-exp/is ([9728519](https://github.com/medikoo/type/commit/9728519)) 168 | - safe-integer/coerce ([b8549c4](https://github.com/medikoo/type/commit/b8549c4)) 169 | - safe-integer/ensure ([a70ef3f](https://github.com/medikoo/type/commit/a70ef3f)) 170 | - string/coerce ([b25c71f](https://github.com/medikoo/type/commit/b25c71f)) 171 | - string/ensure ([b62577d](https://github.com/medikoo/type/commit/b62577d)) 172 | - support 'default' in resolveException ([e08332a](https://github.com/medikoo/type/commit/e08332a)) 173 | - switch config to ES3 based ([37606d9](https://github.com/medikoo/type/commit/37606d9)) 174 | - thenable/ensure ([6762c0d](https://github.com/medikoo/type/commit/6762c0d)) 175 | - thenable/is ([2711d70](https://github.com/medikoo/type/commit/2711d70)) 176 | - time-value/coerce ([27fd109](https://github.com/medikoo/type/commit/27fd109)) 177 | - time-value/ensure ([1f6a8ea](https://github.com/medikoo/type/commit/1f6a8ea)) 178 | - **string/coerce:** restrict toString acceptance ([2a87100](https://github.com/medikoo/type/commit/2a87100)) 179 | - value/ensure ([dd6d8cb](https://github.com/medikoo/type/commit/dd6d8cb)) 180 | - value/is ([fdf4763](https://github.com/medikoo/type/commit/fdf4763)) 181 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2019-2024, Mariusz Nowak, @medikoo, medikoo.com 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 | OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 | PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build status][build-image]][build-url] 2 | [![Tests coverage][cov-image]][cov-url] 3 | [![npm version][npm-image]][npm-url] 4 | 5 | # type 6 | 7 | ## Runtime validation and processing of JavaScript types 8 | 9 | - Respects language nature and acknowledges its quirks 10 | - Allows coercion in restricted forms (rejects clearly invalid input, normalizes permissible type deviations) 11 | - No transpilation implied, written to work in all ECMAScript 3+ engines 12 | 13 | ## Use case 14 | 15 | Validate arguments input in public API endpoints. 16 | 17 | _For validation of more sophisticated input structures (as deeply nested configuration objects) it's recommended to consider more powerful schema based utlities (as [AJV](https://ajv.js.org/) or [@hapi/joi](https://hapi.dev/family/joi/))_ 18 | 19 | ### Example usage 20 | 21 | Bulletproof input arguments normalization and validation: 22 | 23 | ```javascript 24 | const ensureString = require('type/string/ensure') 25 | , ensureDate = require('type/date/ensure') 26 | , ensureNaturalNumber = require('type/natural-number/ensure') 27 | , isObject = require('type/object/is'); 28 | 29 | module.exports = (path, options = { min: 0 }) { 30 | path = ensureString(path, { errorMessage: "%v is not a path" }); 31 | if (!isObject(options)) options = {}; 32 | const min = ensureNaturalNumber(options.min, { default: 0 }) 33 | , max = ensureNaturalNumber(options.max, { isOptional: true }) 34 | , startTime = ensureDate(options.startTime, { isOptional: true }); 35 | 36 | // ...logic 37 | }; 38 | ``` 39 | 40 | ### Installation 41 | 42 | ```bash 43 | npm install type 44 | ``` 45 | 46 | ## Utilities 47 | 48 | Aside of general [`ensure`](docs/ensure.md) validation util, following kind of utilities for recognized JavaScript types are provided: 49 | 50 | ##### `*/coerce` 51 | 52 | Restricted coercion into primitive type. Returns coerced value or `null` if value is not coercible per rules. 53 | 54 | ##### `*/is` 55 | 56 | Object type/kind confirmation, returns either `true` or `false`. 57 | 58 | ##### `*/ensure` 59 | 60 | Value validation. Returns input value (in primitive cases possibly coerced) or if value doesn't meet the constraints throws `TypeError` . 61 | 62 | Each `*/ensure` utility, accepts following options (eventually passed with second argument): 63 | 64 | - `isOptional` - Makes `null` or `undefined` accepted as valid value. In such case instead of `TypeError` being thrown, `null` is returned. 65 | - `default` - A value to be returned if `null` or `undefined` is passed as an input value. 66 | - `errorMessage` - Custom error message. Following placeholders can be used: 67 | - `%v` - To be replaced with short string representation of invalid value 68 | - `%n` - To be replaced with meaninfgul name (to be passed with `name` option) of validated value. Not effective if `name` option is not present 69 | - `errorCode` - Eventual error code to be exposed on `.code` error property 70 | - `name` - Meaningful name for validated value, to be used in error message, assuming it contains `%n` placeholder 71 | - `Error` - Alternative error constructor to be used (defaults to `TypeError`) 72 | 73 | ### Index 74 | 75 | #### General utils: 76 | 77 | - [`ensure`](docs/ensure.md) 78 | 79 | #### Type specific utils: 80 | 81 | - **Value** 82 | - [`value/is`](docs/value.md#valueis) 83 | - [`value/ensure`](docs/value.md#valueensure) 84 | - **Object** 85 | - [`object/is`](docs/object.md#objectis) 86 | - [`object/ensure`](docs/object.md#objectensure) 87 | - **Plain Object** 88 | - [`plain-object/is`](docs/plain-object.md#plain-objectis) 89 | - [`plain-object/ensure`](docs/plain-object.md#plain-objectensure) 90 | - **String** 91 | - [`string/coerce`](docs/string.md#stringcoerce) 92 | - [`string/ensure`](docs/string.md#stringensure) 93 | - **Number** 94 | - [`number/coerce`](docs/number.md#numbercoerce) 95 | - [`number/ensure`](docs/number.md#numberensure) 96 | - **Finite Number** 97 | - [`finite/coerce`](docs/finite.md#finitecoerce) 98 | - [`finite/ensure`](docs/finite.md#finiteensure) 99 | - **Integer Number** 100 | - [`integer/coerce`](docs/integer.md#integercoerce) 101 | - [`integer/ensure`](docs/integer.md#integerensure) 102 | - **Safe Integer Number** 103 | - [`safe-integer/coerce`](docs/safe-integer.md#safe-integercoerce) 104 | - [`safe-integer/ensure`](docs/.md#safe-integerensure) 105 | - **Natural Number** 106 | - [`natural-number/coerce`](docs/natural-number.md#natural-numbercoerce) 107 | - [`natural-number/ensure`](docs/natural-number.md#natural-numberensure) 108 | - **Array Length** 109 | - [`array-length/coerce`](docs/array-length.md#array-lengthcoerce) 110 | - [`array-length/ensure`](docs/array-length.md#array-lengthensure) 111 | - **Time Value** 112 | - [`time-value/coerce`](docs/time-value.md#time-valuecoerce) 113 | - [`time-value/ensure`](docs/time-value.md#time-valueensure) 114 | - **BigInt** 115 | - [`big-int/coerce`](docs/big-int.md#big-intcoerce) 116 | - [`big-int/ensure`](docs/big-int.md#big-intensure) 117 | - **Array Like** 118 | - [`array-like/is`](docs/array-like.md#array-likeis) 119 | - [`array-like/ensure`](docs/array-like.md#array-likeensure) 120 | - **Array** 121 | - [`array/is`](docs/array.md#arrayis) 122 | - [`array/ensure`](docs/array.md#arrayensure) 123 | - **Iterable** 124 | - [`iterable/is`](docs/iterable.md#iterableis) 125 | - [`iterable/ensure`](docs/iterable.md#iterableensure) 126 | - **Set** 127 | - [`set/is`](docs/set.md#setis) 128 | - [`set/ensure`](docs/set.md#setensure) 129 | - **Map** 130 | - [`map/is`](docs/map.md#mapis) 131 | - [`map/ensure`](docs/map.md#mapensure) 132 | - **Date** 133 | - [`date/is`](docs/date.md#dateis) 134 | - [`date/ensure`](docs/date.md#dateensure) 135 | - **Function** 136 | - [`function/is`](docs/function.md#functionis) 137 | - [`function/ensure`](docs/function.md#functionensure) 138 | - **Constructor** 139 | - [`constructor/is`](docs/constructor.md#plain-functionis) 140 | - [`constructor/ensure`](docs/constructor.md#plain-functionensure) 141 | - **Plain Function** 142 | - [`plain-function/is`](docs/plain-function.md#plain-functionis) 143 | - [`plain-function/ensure`](docs/plain-function.md#plain-functionensure) 144 | - **Reg Exp** 145 | - [`reg-exp/is`](docs/reg-exp.md#reg-expis) 146 | - [`reg-exp/ensure`](docs/.md#reg-expensure) 147 | - **Thenable** 148 | - [`thenable/is`](docs/thenable.md#thenableis) 149 | - [`thenable/ensure`](docs/thenable.md#thenableensure) 150 | - **Promise** 151 | - [`promise/is`](docs/promise.md#promiseis) 152 | - [`promise/ensure`](docs/promise.md#promiseensure) 153 | - **Error** 154 | - [`error/is`](docs/error.md#erroris) 155 | - [`error/ensure`](docs/error.md#errorensure) 156 | - **Prototype** 157 | - [`prototype/is`](docs/prototype.md#prototypeis) 158 | 159 | ### Tests 160 | 161 | $ npm test 162 | 163 | [build-image]: https://github.com/medikoo/type/workflows/Integrate/badge.svg 164 | [build-url]: https://github.com/medikoo/type/actions?query=workflow%3AIntegrate 165 | [cov-image]: https://img.shields.io/codecov/c/github/medikoo/type.svg 166 | [cov-url]: https://codecov.io/gh/medikoo/type 167 | [npm-image]: https://img.shields.io/npm/v/type.svg 168 | [npm-url]: https://www.npmjs.com/package/type 169 | 170 | ## Security contact information 171 | 172 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 173 | -------------------------------------------------------------------------------- /array-length/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var coerceToSafeInteger = require("../safe-integer/coerce"); 4 | 5 | module.exports = function (value) { 6 | value = coerceToSafeInteger(value); 7 | if (!value) return value; 8 | if (value < 0) return null; 9 | return value; 10 | }; 11 | -------------------------------------------------------------------------------- /array-length/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , coerce = require("./coerce"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | var coerced = coerce(value); 8 | if (coerced !== null) return coerced; 9 | var options = arguments[1]; 10 | var errorMessage = 11 | options && options.name 12 | ? "Expected an array length for %n, received %v" 13 | : "%v is not an array length"; 14 | return resolveException(value, errorMessage, options); 15 | }; 16 | -------------------------------------------------------------------------------- /array-like/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value, arguments[1])) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name 11 | ? "Expected an array like for %n, received %v" 12 | : "%v is not an array like"; 13 | return resolveException(value, errorMessage, options); 14 | }; 15 | -------------------------------------------------------------------------------- /array-like/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var coerceToArrayLength = require("../array-length/coerce") 4 | , isObject = require("../object/is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (!isObject(value)) { 8 | var options = arguments[1]; 9 | if (isObject(options) && options.allowString && typeof value === "string") return true; 10 | return false; 11 | } 12 | 13 | if (typeof value === "function") return false; 14 | 15 | var length; 16 | try { length = value.length; } 17 | catch (error) { return false; } 18 | 19 | if (coerceToArrayLength(length) === null) return false; 20 | return true; 21 | }; 22 | -------------------------------------------------------------------------------- /array/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , resolveErrorMessage = require("../lib/resolve-error-message") 5 | , toShortString = require("../lib/to-short-string") 6 | , ensurePlainFunction = require("../plain-function/ensure") 7 | , is = require("./is"); 8 | 9 | var objHasOwnProperty = Object.prototype.hasOwnProperty, invalidItemsLimit = 3; 10 | 11 | module.exports = function (value/*, options*/) { 12 | var options = arguments[1]; 13 | var mainErrorMessage = 14 | options && options.name ? "Expected an array for %n, received %v" : "%v is not an array"; 15 | if (!is(value)) return resolveException(value, mainErrorMessage, options); 16 | if (!options) return value; 17 | 18 | var ensureItem = ensurePlainFunction(options.ensureItem, { isOptional: true }); 19 | if (ensureItem) { 20 | var coercedValue = [], invalidItems; 21 | for (var index = 0, length = value.length; index < length; ++index) { 22 | if (!objHasOwnProperty.call(value, index)) continue; 23 | var coercedItem; 24 | try { 25 | coercedItem = ensureItem(value[index]); 26 | } catch (error) { 27 | if (!invalidItems) invalidItems = []; 28 | if (invalidItems.push(toShortString(value[index])) === invalidItemsLimit) break; 29 | } 30 | if (invalidItems) continue; 31 | coercedValue[index] = coercedItem; 32 | } 33 | if (invalidItems) { 34 | throw new TypeError( 35 | resolveErrorMessage(mainErrorMessage, value, options) + 36 | ".\n Following items are invalid: " + 37 | invalidItems.join(", ") 38 | ); 39 | } 40 | return coercedValue; 41 | } 42 | return value; 43 | }; 44 | -------------------------------------------------------------------------------- /array/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isPrototype = require("../prototype/is"); 4 | 5 | var isArray; 6 | if (typeof Array.isArray === "function") { 7 | isArray = Array.isArray; 8 | } else { 9 | var objectToString = Object.prototype.toString, objectTaggedString = objectToString.call([]); 10 | isArray = function (value) { return objectToString.call(value) === objectTaggedString; }; 11 | } 12 | 13 | module.exports = function (value) { 14 | if (!isArray(value)) return false; 15 | 16 | // Sanity check (reject objects which do not expose common Array interface) 17 | if (!hasOwnProperty.call(value, "length")) return false; 18 | try { 19 | if (typeof value.length !== "number") return false; 20 | if (typeof value.push !== "function") return false; 21 | if (typeof value.splice !== "function") return false; 22 | } catch (error) { 23 | return false; 24 | } 25 | 26 | return !isPrototype(value); 27 | }; 28 | -------------------------------------------------------------------------------- /big-int/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isValue = require("../value/is"); 4 | 5 | // Sanity BigInt support check 6 | BigInt(0); 7 | 8 | module.exports = function (value) { 9 | if (!isValue(value)) return null; 10 | if (typeof value === "bigint") return value; 11 | try { return BigInt(value); } 12 | catch (error) { return null; } 13 | }; 14 | -------------------------------------------------------------------------------- /big-int/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , coerce = require("./coerce"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | var coerced = coerce(value); 8 | if (coerced !== null) return coerced; 9 | var options = arguments[1]; 10 | var errorMessage = 11 | options && options.name ? "Expected bigint for %n, received %v" : "%v is not a bigint"; 12 | return resolveException(value, errorMessage, options); 13 | }; 14 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = { 4 | rules: { 5 | "body-leading-blank": [2, "always"], 6 | "body-max-line-length": [2, "always", 72], 7 | "footer-leading-blank": [2, "always"], 8 | "footer-max-line-length": [2, "always", 72], 9 | "header-max-length": [2, "always", 72], 10 | "scope-case": [2, "always", "start-case"], 11 | "scope-enum": [2, "always", [""]], 12 | "subject-case": [2, "always", "sentence-case"], 13 | "subject-empty": [2, "never"], 14 | "subject-full-stop": [2, "never", "."], 15 | "type-case": [2, "always", "lower-case"], 16 | "type-empty": [2, "never"], 17 | "type-enum": [ 18 | 2, "always", 19 | ["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test"] 20 | ] 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /constructor/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name 11 | ? "Expected a constructor function for %n, received %v" 12 | : "%v is not a constructor function"; 13 | return resolveException(value, errorMessage, options); 14 | }; 15 | -------------------------------------------------------------------------------- /constructor/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isFunction = require("../function/is"); 4 | 5 | var constructorRe = /^\s*(?:class[\s{/}]|function[\s(])/ 6 | , functionToString = Function.prototype.toString; 7 | 8 | module.exports = function (value) { 9 | if (!isFunction(value)) return false; 10 | if (!constructorRe.test(functionToString.call(value))) return false; 11 | return true; 12 | }; 13 | -------------------------------------------------------------------------------- /date/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name ? "Expected a date for %n, received %v" : "%v is not a date"; 11 | return resolveException(value, errorMessage, options); 12 | }; 13 | -------------------------------------------------------------------------------- /date/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isPrototype = require("../prototype/is"); 4 | 5 | var dateValueOf = Date.prototype.valueOf; 6 | 7 | module.exports = function (value) { 8 | if (!value) return false; 9 | 10 | try { 11 | // Sanity check (reject objects which do not expose common Date interface) 12 | if (typeof value.getFullYear !== "function") return false; 13 | if (typeof value.getTimezoneOffset !== "function") return false; 14 | if (typeof value.setFullYear !== "function") return false; 15 | 16 | // Ensure its native Date object (has [[DateValue]] slot) 17 | dateValueOf.call(value); 18 | } catch (error) { 19 | return false; 20 | } 21 | 22 | // Ensure it hosts valid date 23 | if (isNaN(value)) return false; 24 | 25 | return !isPrototype(value); 26 | }; 27 | -------------------------------------------------------------------------------- /docs/array-length.md: -------------------------------------------------------------------------------- 1 | # Array length 2 | 3 | _number_ primitive that conforms as valid _array length_ 4 | 5 | ## `array-length/coerce` 6 | 7 | Follows [`safe-integer/coerce`](safe-integer.md#safe-integercoerce) but returns `null` in place of values which are below `0` 8 | 9 | ```javascript 10 | const coerceToArrayLength = require("type/safe-integer/coerce"); 11 | 12 | coerceToArrayLength("12.95"); // 12 13 | coerceToArrayLength(9007199254740992); // null 14 | coerceToArrayLength(null); // null 15 | ``` 16 | 17 | ## `array-length/ensure` 18 | 19 | If given argument is an _array length_ coercible value (via [`array-length/coerce`](#array-lengthcoerce)) returns result number. 20 | Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureArrayLength = require("type/array-length/ensure"); 24 | 25 | ensureArrayLength(12.93); // "12" 26 | ensureArrayLength(9007199254740992); // Thrown TypeError: 9007199254740992 is not a valid array length 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/array-like.md: -------------------------------------------------------------------------------- 1 | # Array Like 2 | 3 | _Array-like_ value (any value with `length` property) 4 | 5 | ## `array-like/is` 6 | 7 | Restricted _array-like_ confirmation. Returns true for every value that meets following contraints 8 | 9 | - is an _object_ (or with `allowString` option, a _string_) 10 | - is not a _function_ 11 | - Exposes `length` that meets [`array-length`](array-length.md#array-lengthcoerce) constraints 12 | 13 | ```javascript 14 | const isArrayLike = require("type/array-like/is"); 15 | 16 | isArrayLike([]); // true 17 | isArrayLike({}); // false 18 | isArrayLike({ length: 0 }); // true 19 | isArrayLike("foo"); // false 20 | isArrayLike("foo", { allowString: true }); // true 21 | ``` 22 | 23 | ## `array-like/ensure` 24 | 25 | If given argument is an _array-like_, it is returned back. Otherwise `TypeError` is thrown. 26 | 27 | ```javascript 28 | const ensureArrayLike = require("type/array-like/ensure"); 29 | 30 | ensureArrayLike({ length: 0 }); // { length: 0 } 31 | ensureArrayLike("foo", { allowString: true }); // "foo" 32 | ensureArrayLike({}); // Thrown TypeError: null is not an iterable 33 | ``` 34 | -------------------------------------------------------------------------------- /docs/array.md: -------------------------------------------------------------------------------- 1 | # Array 2 | 3 | _Array_ instance 4 | 5 | ## `array/is` 6 | 7 | Confirms if given object is a native array 8 | 9 | ```javascript 10 | const isArray = require("type/array/is"); 11 | 12 | isArray([]); // true 13 | isArray({}); // false 14 | isArray("foo"); // false 15 | ``` 16 | 17 | ## `array/ensure` 18 | 19 | If given argument is an array, it is returned back. Otherwise `TypeError` is thrown. 20 | 21 | ```javascript 22 | const ensureArray = require("type/array/ensure"); 23 | 24 | ensureArray(["foo"]); // ["foo"] 25 | ensureArray("foo"); // Thrown TypeError: foo is not an array 26 | ``` 27 | 28 | ### Confirming on items 29 | 30 | Items can be validated by passing `ensureItem` option. Note that in this case: 31 | 32 | - A newly created instance of an array with coerced item values is returned 33 | - Error message lists up to three items which are invalid 34 | 35 | ```javascript 36 | const ensureString = require("type/string/ensure"); 37 | 38 | ensureArray([12], { ensureItem: ensureString }); // ["12"] 39 | 40 | /* 41 | Below invocation with crash with: 42 | TypeError: 23, [object Object], [object Object] is not a valid array. 43 | Following items are invalid: [object Object], [object Object] 44 | */ 45 | ensureArray([23, {}, {}], { ensureItem: ensureString }); 46 | ``` 47 | -------------------------------------------------------------------------------- /docs/big-int.md: -------------------------------------------------------------------------------- 1 | # BigInt 2 | 3 | _bigint_ primitive 4 | 5 | ## `big-int/coerce` 6 | 7 | BigInt coercion. If value can be coerced by `BigInt` its result is returned. 8 | For all other values `null` is returned 9 | 10 | ```javascript 11 | const coerceToBigInt = require("type/big-int/coerce"); 12 | 13 | coerceToBigInt(12); // 12n 14 | coerceToBigInt(undefined); // null 15 | ``` 16 | 17 | ## `big-int/ensure` 18 | 19 | If given argument is a _bigint_ coercible value (via [`big-int/coerce`](#big-intcoerce)) returns result bigint. 20 | Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureBigInt = require("type/big-int/ensure"); 24 | 25 | ensureBigInt(12); // 12n 26 | ensureBigInt(null); // Thrown TypeError: null is not a bigint 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/constructor.md: -------------------------------------------------------------------------------- 1 | # Constructor 2 | 3 | A _Function_ instance that's a _constructor_ (either regular function or _class_) 4 | 5 | ## `constructor/is` 6 | 7 | Confirms if given object is a constructor function\_ 8 | 9 | ```javascript 10 | const isConstructor = require("type/constructor/is"); 11 | 12 | isConstructor(function () {}); // true 13 | isConstructor(() => {}); // false 14 | isConstructor(class {}); // true 15 | isConstructor("foo"); // false 16 | ``` 17 | 18 | ## `constructor/ensure` 19 | 20 | If given argument is a _constructor function_, it is returned back. Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureConstructor = require("type/constructor/ensure"); 24 | 25 | const fn = function () {}; 26 | ensureConstructor(fn); // fn 27 | ensureConstructor(() => {}); // Thrown TypeError: () => {} is not a constructor function 28 | ``` 29 | -------------------------------------------------------------------------------- /docs/date.md: -------------------------------------------------------------------------------- 1 | # Date 2 | 3 | _Date_ instance 4 | 5 | ## `date/is` 6 | 7 | Confirms if given object is a native date, and is not an _Invalid Date_ 8 | 9 | ```javascript 10 | const isDate = require("type/date/is"); 11 | 12 | isDate(new Date()); // true 13 | isDate(new Date("Invalid date")); // false 14 | isDate(Date.now()); // false 15 | isDate("foo"); // false 16 | ``` 17 | 18 | ## `date/ensure` 19 | 20 | If given argument is a date object, it is returned back. Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureDate = require("type/date/ensure"); 24 | 25 | const date = new Date(); 26 | ensureDate(date); // date 27 | ensureDate(123123); // Thrown TypeError: 123123 is not a date object 28 | ``` 29 | -------------------------------------------------------------------------------- /docs/ensure.md: -------------------------------------------------------------------------------- 1 | # `ensure(validationDatum1[, ...validationDatumN[, options]])` 2 | 3 | Provides a complete cumulated input validation for an API endpoint. Validates multiple input arguments and consolidates eventual errors into one. 4 | 5 | ## Arguments 6 | 7 | ### `validationDatum1[, ...validationDatumN]` 8 | 9 | For each argument to be validated a `validationDatum` of following stucture should be defined: 10 | 11 | ```javascript 12 | [argumentName, inputValue, ensureFunction, (options = {})]; 13 | ``` 14 | 15 | - `argumentName` - Name of validated argument (used for meaningful error messaging) 16 | - `inputValue` - An argument value as passed to function 17 | - `ensureFunction` - An `ensureX` function with which argument should be validated (e.g. if we're after string, then we need [string/ensure](string.md#stringensure)) 18 | - `options` - Optional, extra options to be passed to `ensureX` function 19 | 20 | ### `[options]` 21 | 22 | Eventual options be passed to underlying `ensureX` functions. If custom error constructor is passed with an `Error` option, then cumulated error is created with this constructor. 23 | 24 | ## Usage example 25 | 26 | ```javascript 27 | const ensure = require("type/ensure"); 28 | const ensureString = require("type/string/ensure"); 29 | const ensureNaturalNumber = require("type/natural-number/ensure"); 30 | 31 | const resolveRepositoryIssue = (repoName, issueNumber) => { 32 | // Validate input 33 | [repoName, issueNumber] = ensure( 34 | ["repoName", repoName, ensureString], 35 | ["issueNumber", issueNumber, ensureNaturalNumber], 36 | { Error: UserError } 37 | ); 38 | // ... logic 39 | }; 40 | ``` 41 | -------------------------------------------------------------------------------- /docs/error.md: -------------------------------------------------------------------------------- 1 | # Error 2 | 3 | _Error_ instance 4 | 5 | ## `error/is` 6 | 7 | Confirms if given object is a native error object 8 | 9 | ```javascript 10 | const isError = require("type/error/is"); 11 | 12 | isError(new Error()); // true 13 | isError({ message: "Fake error" }); // false 14 | ``` 15 | 16 | ## `error/ensure` 17 | 18 | If given argument is an error object, it is returned back. Otherwise `TypeError` is thrown. 19 | 20 | ```javascript 21 | const ensureError = require("type/error/ensure"); 22 | 23 | const someError = new Error("Some error"); 24 | ensureError(someError); // someError 25 | ensureError({ message: "Fake error" }); // Thrown TypeError: [object Object] is not an error object 26 | ``` 27 | -------------------------------------------------------------------------------- /docs/finite.md: -------------------------------------------------------------------------------- 1 | # Finite Number 2 | 3 | Finite _number_ primitive 4 | 5 | ## `finite/coerce` 6 | 7 | Follows [`number/coerce`](number.md#numbercoerce) additionally rejecting `Infinity` and `-Infinity` values (`null` is returned if given values coerces to them) 8 | 9 | ```javascript 10 | const coerceToFinite = require("type/finite/coerce"); 11 | 12 | coerceToFinite("12"); // 12 13 | coerceToFinite(Infinity); // null 14 | coerceToFinite(null); // null 15 | ``` 16 | 17 | ## `finite/ensure` 18 | 19 | If given argument is a finite number coercible value (via [`finite/coerce`](#finitecoerce)) returns result number. 20 | Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureFinite = require("type/finite/ensure"); 24 | 25 | ensureFinite(12); // "12" 26 | ensureFinite(null); // Thrown TypeError: null is not a finite number 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/function.md: -------------------------------------------------------------------------------- 1 | # Function 2 | 3 | _Function_ instance 4 | 5 | ## `function/is` 6 | 7 | Confirms if given object is a native function 8 | 9 | ```javascript 10 | const isFunction = require("type/function/is"); 11 | 12 | isFunction(function () {}); // true 13 | isFunction(() => {}); // true 14 | isFunction(class {}); // true 15 | isFunction("foo"); // false 16 | ``` 17 | 18 | ## `function/ensure` 19 | 20 | If given argument is a function object, it is returned back. Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureFunction = require("type/function/ensure"); 24 | 25 | const fn = function () {}; 26 | ensureFunction(fn); // fn 27 | ensureFunction(/foo/); // Thrown TypeError: /foo/ is not a function 28 | ``` 29 | -------------------------------------------------------------------------------- /docs/integer.md: -------------------------------------------------------------------------------- 1 | # Integer Number 2 | 3 | Integer _number_ primitive 4 | 5 | ## `integer/coerce` 6 | 7 | Follows [`finite/coerce`](finite.md#finitecoerce) additionally stripping decimal part from the number 8 | 9 | ```javascript 10 | const coerceToInteger = require("type/integer/coerce"); 11 | 12 | coerceToInteger("12.95"); // 12 13 | coerceToInteger(Infinity); // null 14 | coerceToInteger(null); // null 15 | ``` 16 | 17 | ## `integer/ensure` 18 | 19 | If given argument is an integer coercible value (via [`integer/coerce`](#integercoerce)) returns result number. 20 | Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureInteger = require("type/integer/ensure"); 24 | 25 | ensureInteger(12.93); // "12" 26 | ensureInteger(null); // Thrown TypeError: null is not an integer 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/iterable.md: -------------------------------------------------------------------------------- 1 | # Iterable 2 | 3 | Value which implements _iterable_ protocol 4 | 5 | ## `iterable/is` 6 | 7 | Confirms if given object is an _iterable_ and is not a _string_ (unless `allowString` option is passed) 8 | 9 | ```javascript 10 | const isIterable = require("type/iterable/is"); 11 | 12 | isIterable([]); // true 13 | isIterable({}); // false 14 | isIterable("foo"); // false 15 | isIterable("foo", { allowString: true }); // true 16 | ``` 17 | 18 | Supports also `denyEmpty` option 19 | 20 | ```javascript 21 | isIterable([], { denyEmpty: true }); // false 22 | isIterable(["foo"], { denyEmpty: true }); // true 23 | ``` 24 | 25 | ## `iterable/ensure` 26 | 27 | If given argument is an _iterable_, it is returned back. Otherwise `TypeError` is thrown. 28 | By default _string_ primitives are rejected unless `allowString` option is passed. 29 | 30 | ```javascript 31 | const ensureIterable = require("type/iterable/ensure"); 32 | 33 | ensureIterable([]); // [] 34 | ensureIterable("foo", { allowString: true }); // "foo" 35 | ensureIterable({}); // Thrown TypeError: null is not expected iterable 36 | ``` 37 | 38 | ### Denying empty iterables 39 | 40 | Pass `denyEmpty` option to require non empty iterables 41 | 42 | ```javascript 43 | ensureIterable([], { denyEmpty: true }); // Thrown TypeError: [] is not expected iterable 44 | ``` 45 | 46 | ### Confirming on items 47 | 48 | Items can be validated by passing `ensureItem` option. Note that in this case: 49 | 50 | - A newly created instance of array with coerced values is returned 51 | - Error message lists up to three invalid items 52 | 53 | ```javascript 54 | const ensureString = require("type/string/ensure"); 55 | 56 | ensureIterable(new Set(["foo", 12]), { ensureItem: ensureString }); // ["foo", "12"] 57 | 58 | /* 59 | Below invocation with crash with: 60 | TypeError: [object Set] is not expected iterable value. 61 | Following items are invalid: 62 | - [object Object] 63 | */ 64 | ensureIterable(new Set(["foo", {}]), { ensureItem: ensureString }); 65 | ``` 66 | -------------------------------------------------------------------------------- /docs/map.md: -------------------------------------------------------------------------------- 1 | # Map 2 | 3 | _Map_ instance 4 | 5 | ## `map/is` 6 | 7 | Confirms if given object is a native _map_ 8 | 9 | ```javascript 10 | const isMap = require("type/map/is"); 11 | 12 | isMap(new Map()); // true 13 | isMap(new Set()); // false 14 | isMap({}); // false 15 | ``` 16 | 17 | ## `map/ensure` 18 | 19 | If given argument is a _map_, it is returned back. Otherwise `TypeError` is thrown. 20 | 21 | ```javascript 22 | const ensureMap = require("type/map/ensure"); 23 | 24 | const map = new Map(); 25 | ensureMap(map); // map 26 | eensureMap({}); // Thrown TypeError: [object Object] is not a map 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/natural-number.md: -------------------------------------------------------------------------------- 1 | # Natural Number 2 | 3 | Natural _number_ primitive 4 | 5 | ## `natural-number/coerce` 6 | 7 | Follows [`integer/coerce`](integer.md#integercoerce) but returns `null` for values below `0` 8 | 9 | ```javascript 10 | const coerceToNaturalNumber = require("type/natural-number/coerce"); 11 | 12 | coerceToNaturalNumber("12.95"); // 12 13 | coerceToNaturalNumber(-120); // null 14 | coerceToNaturalNumber(null); // null 15 | ``` 16 | 17 | ## `natural-number/ensure` 18 | 19 | If given argument is a natural number coercible value (via [`natural-number/coerce`](#natural-numbercoerce)) returns result number. 20 | Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureNaturalNumber = require("type/natural-number/ensure"); 24 | 25 | ensureNaturalNumber(12.93); // "12" 26 | ensureNaturalNumber(-230); // Thrown TypeError: null is not a natural number 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/number.md: -------------------------------------------------------------------------------- 1 | # Number 2 | 3 | _number_ primitive 4 | 5 | ## `number/coerce` 6 | 7 | Restricted number coercion. Returns number presentation for every value that follows below constraints 8 | 9 | - is implicitly coercible to number 10 | - is neither `null` nor `undefined` 11 | - is not `NaN` and doesn't coerce to `NaN` 12 | 13 | For all other values `null` is returned 14 | 15 | ```javascript 16 | const coerceToNumber = require("type/number/coerce"); 17 | 18 | coerceToNumber("12"); // 12 19 | coerceToNumber({}); // null 20 | coerceToNumber(null); // null 21 | ``` 22 | 23 | ## `number/ensure` 24 | 25 | If given argument is a number coercible value (via [`number/coerce`](#numbercoerce)) returns result number. 26 | Otherwise `TypeError` is thrown. 27 | 28 | ```javascript 29 | const ensureNumber = require("type/number/ensure"); 30 | 31 | ensureNumber(12); // "12" 32 | ensureNumber(null); // Thrown TypeError: null is not a number 33 | ``` 34 | -------------------------------------------------------------------------------- /docs/object.md: -------------------------------------------------------------------------------- 1 | # Object 2 | 3 | _Object_, any non-primitive value 4 | 5 | ## `object/is` 6 | 7 | Confirms if passed value is an object 8 | 9 | ```javascript 10 | const isObject = require("type/object/is"); 11 | 12 | isObject({}); // true 13 | isObject(true); // false 14 | isObject(null); // false 15 | ``` 16 | 17 | ## `object/ensure` 18 | 19 | If given argument is an object, it is returned back. Otherwise `TypeError` is thrown. 20 | 21 | ```javascript 22 | const ensureObject = require("type/object/ensure"); 23 | 24 | const obj = {}; 25 | 26 | ensureObject(obj); // obj 27 | ensureString(null); // Thrown TypeError: null is not an object 28 | ``` 29 | -------------------------------------------------------------------------------- /docs/plain-function.md: -------------------------------------------------------------------------------- 1 | # Plain Function 2 | 3 | A _Function_ instance that is not a _Class_ 4 | 5 | ## `plain-function/is` 6 | 7 | Confirms if given object is a _plain function_ 8 | 9 | ```javascript 10 | const isPlainFunction = require("type/plain-function/is"); 11 | 12 | isPlainFunction(function () {}); // true 13 | isPlainFunction(() => {}); // true 14 | isPlainFunction(class {}); // false 15 | isPlainFunction("foo"); // false 16 | ``` 17 | 18 | ## `plain-function/ensure` 19 | 20 | If given argument is a _plain function_ object, it is returned back. Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensurePlainFunction = require("type/function/ensure"); 24 | 25 | const fn = function () {}; 26 | ensurePlainFunction(fn); // fn 27 | ensurePlainFunction(class {}); // Thrown TypeError: class is not a plain function 28 | ``` 29 | -------------------------------------------------------------------------------- /docs/plain-object.md: -------------------------------------------------------------------------------- 1 | # Plain Object 2 | 3 | A _plain object_ 4 | 5 | - Inherits directly from `Object.prototype` or `null` 6 | - Is not a constructor's `prototype` property 7 | 8 | ## `plain-object/is` 9 | 10 | Confirms if given object is a _plain object_ 11 | 12 | ```javascript 13 | const isPlainObject = require("type/plain-object/is"); 14 | 15 | isPlainObject({}); // true 16 | isPlainObject(Object.create(null)); // true 17 | isPlainObject([]); // false 18 | ``` 19 | 20 | ## `plain-object/ensure` 21 | 22 | If given argument is a plain object it is returned back. Otherwise `TypeError` is thrown. 23 | 24 | ```javascript 25 | const ensurePlainObject = require("type/plain-object/ensure"); 26 | 27 | ensurePlainObject({}); // {} 28 | ensurePlainObject("foo"); // Thrown TypeError: foo is not a plain object 29 | ``` 30 | 31 | ### Confirming on keys 32 | 33 | Keys can be validated by passing `allowedKeys` option. Note that in this case: 34 | 35 | - Error message lists up to three invalid keys 36 | 37 | ```javascript 38 | const allowedKeys = ["foo"]; 39 | 40 | ensurePlainObject({}, { allowedKeys }); // {} 41 | ensurePlainObject({ foo: "bar" }, { allowedKeys }); // { foo: 'bar' } 42 | 43 | /* 44 | Below invocation with crash with: 45 | TypeError: [object Object] is not a valid plain object. 46 | Following keys are unexpected: lorem, ipsum 47 | */ 48 | ensurePlainObject({ foo: "bar", lorem: 1, ipsum: 2 }, { allowedKeys }); 49 | ``` 50 | 51 | ### Confirming on property values 52 | 53 | Property values can be validated by passing `ensurePropertyValue` option. Note that in this case: 54 | 55 | - A newly created instance of plain object with coerced values is returned 56 | - Error message lists up to three keys that contain invalid values 57 | 58 | ```javascript 59 | const ensureString = require("type/string/ensure"); 60 | 61 | ensurePlainObject({ foo: 12 }, { ensurePropertyValue: ensureString }); // { foo: '12' } 62 | 63 | /* 64 | Below invocation with crash with: 65 | TypeError: [object Object] is not a valid plain object. 66 | Valuees for following keys are invalid: lorem, ipsum 67 | */ 68 | ensurePlainObject({ foo: 23, lorem: {}, ipsum: {} }, { ensurePropertyValue: ensureString }); 69 | ``` 70 | -------------------------------------------------------------------------------- /docs/promise.md: -------------------------------------------------------------------------------- 1 | # Promise 2 | 3 | _Promise_ instance 4 | 5 | ## `promise/is` 6 | 7 | Confirms if given object is a native _promise_ 8 | 9 | ```javascript 10 | const isPromise = require("type/promise/is"); 11 | 12 | isPromise(Promise.resolve()); // true 13 | isPromise({ then: () => {} }); // false 14 | isPromise({}); // false 15 | ``` 16 | 17 | ## `promise/ensure` 18 | 19 | If given argument is a promise, it is returned back. Otherwise `TypeError` is thrown. 20 | 21 | ```javascript 22 | const ensurePromise = require("type/promise/ensure"); 23 | 24 | const promise = Promise.resolve(); 25 | ensurePromise(promise); // promise 26 | eensurePromise({}); // Thrown TypeError: [object Object] is not a promise 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/prototype.md: -------------------------------------------------------------------------------- 1 | # Prototype 2 | 3 | Some constructor's `prototype` property 4 | 5 | ## `prototype/is` 6 | 7 | Confirms if given object serves as a _prototype_ property 8 | 9 | ```javascript 10 | const isPrototype = require("type/prototype/is"); 11 | 12 | isPrototype({}); // false 13 | isPrototype(Object.prototype); // true 14 | isPrototype(Array.prototype); // true 15 | ``` 16 | -------------------------------------------------------------------------------- /docs/reg-exp.md: -------------------------------------------------------------------------------- 1 | # RegExp 2 | 3 | _RegExp_ instance 4 | 5 | ## `reg-exp/is` 6 | 7 | Confirms if given object is a native regular expression object 8 | 9 | ```javascript 10 | const isRegExp = require("type/reg-exp/is"); 11 | 12 | isRegExp(/foo/); 13 | isRegExp({}); // false 14 | isRegExp("foo"); // false 15 | ``` 16 | 17 | ## `reg-exp/ensure` 18 | 19 | If given argument is a regular expression object, it is returned back. Otherwise `TypeError` is thrown. 20 | 21 | ```javascript 22 | const ensureRegExp = require("type/reg-exp/ensure"); 23 | 24 | ensureRegExp(/foo/); // /foo/ 25 | ensureRegExp("foo"); // Thrown TypeError: null is not a regular expression object 26 | ``` 27 | -------------------------------------------------------------------------------- /docs/safe-integer.md: -------------------------------------------------------------------------------- 1 | # Safe Integer Number 2 | 3 | Safe integer _number_ primitive 4 | 5 | ## `safe-integer/coerce` 6 | 7 | Follows [`integer/coerce`](integer.md#integercoerce) but returns `null` in place of values which are beyond `Number.MIN_SAFE_INTEGER` and `Number.MAX_SAFE_INTEGER` range. 8 | 9 | ```javascript 10 | const coerceToSafeInteger = require("type/safe-integer/coerce"); 11 | 12 | coerceToInteger("12.95"); // 12 13 | coerceToInteger(9007199254740992); // null 14 | coerceToInteger(null); // null 15 | ``` 16 | 17 | ## `safe-integer/ensure` 18 | 19 | If given argument is a safe integer coercible value (via [`safe-integer/coerce`](#safe-integercoerce)) returns result number. 20 | Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureSafeInteger = require("type/safe-integer/ensure"); 24 | 25 | ensureSafeInteger(12.93); // "12" 26 | ensureSafeInteger(9007199254740992); // Thrown TypeError: null is not a safe integer 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/set.md: -------------------------------------------------------------------------------- 1 | # Set 2 | 3 | _Set_ instance 4 | 5 | ## `set/is` 6 | 7 | Confirms if given object is a native set\_ 8 | 9 | ```javascript 10 | const isSet = require("type/set/is"); 11 | 12 | isSet(new Set()); // true 13 | isSet(new Map()); // false 14 | isSet({}); // false 15 | ``` 16 | 17 | ## `Set/ensure` 18 | 19 | If given argument is a _set_, it is returned back. Otherwise `TypeError` is thrown. 20 | 21 | ```javascript 22 | const ensureSet = require("type/set/ensure"); 23 | 24 | const set = new Set(); 25 | ensureSet(set); // set 26 | eensureSet({}); // Thrown TypeError: [object Object] is not a set 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/string.md: -------------------------------------------------------------------------------- 1 | # String 2 | 3 | _string_ primitive 4 | 5 | ## `string/coerce` 6 | 7 | Restricted string coercion. Returns string presentation for every value that follows below constraints 8 | 9 | - is implicitly coercible to string 10 | - is neither`null` nor `undefined` 11 | - its `toString` method is not `Object.prototype.toString` 12 | 13 | For all other values `null` is returned 14 | 15 | ```javascript 16 | const coerceToString = require("type/string/coerce"); 17 | 18 | coerceToString(12); // "12" 19 | coerceToString(undefined); // null 20 | ``` 21 | 22 | ## `string/ensure` 23 | 24 | If given argument is a string coercible value (via [`string/coerce`](#stringcoerce)) returns result string. 25 | Otherwise `TypeError` is thrown. 26 | 27 | ```javascript 28 | const ensureString = require("type/string/ensure"); 29 | 30 | ensureString(12); // "12" 31 | ensureString(null); // Thrown TypeError: null is not a string 32 | ``` 33 | -------------------------------------------------------------------------------- /docs/thenable.md: -------------------------------------------------------------------------------- 1 | # Thenable 2 | 3 | _Thenable_ object (an object with `then` method) 4 | 5 | ## `thenable/is` 6 | 7 | Confirms if given object is a _thenable_ 8 | 9 | ```javascript 10 | const isThenable = require("type/thenable/is"); 11 | 12 | isThenable(Promise.resolve()); // true 13 | isThenable({ then: () => {} }); // true 14 | isThenable({}); // false 15 | ``` 16 | 17 | ## `thenable/ensure` 18 | 19 | If given argument is a _thenable_ object, it is returned back. Otherwise `TypeError` is thrown. 20 | 21 | ```javascript 22 | const ensureThenable = require("type/thenable/ensure"); 23 | 24 | const promise = Promise.resolve(); 25 | ensureThenable(promise); // promise 26 | ensureThenable({}); // Thrown TypeError: [object Object] is not a thenable object 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/time-value.md: -------------------------------------------------------------------------------- 1 | # Time value 2 | 3 | _number_ primitive which is a valid _time value_ (as used internally in _Date_ instances) 4 | 5 | ## `time-value/coerce` 6 | 7 | Follows [`integer/coerce`](integer.md#integercoerce) but returns `null` in place of values which go beyond 100 000 0000 days from unix epoch 8 | 9 | ```javascript 10 | const coerceToTimeValue = require("type/time-value/coerce"); 11 | 12 | coerceToTimeValue(12312312); // true 13 | coerceToTimeValue(Number.MAX_SAFE_INTEGER); // false 14 | coerceToTimeValue("foo"); // false 15 | ``` 16 | 17 | ## `time-value/ensure` 18 | 19 | If given argument is a _time value_ coercible value (via [`time-value/coerce`](#time-valuecoerce)) returns result number. 20 | Otherwise `TypeError` is thrown. 21 | 22 | ```javascript 23 | const ensureTimeValue = require("type/time-value/ensure"); 24 | 25 | ensureTimeValue(12.93); // "12" 26 | ensureTimeValue(Number.MAX_SAFE_INTEGER); // Thrown TypeError: null is not a natural number 27 | ``` 28 | -------------------------------------------------------------------------------- /docs/value.md: -------------------------------------------------------------------------------- 1 | # Value 2 | 3 | _Value_, any value that's neither `null` nor `undefined` . 4 | 5 | ## `value/is` 6 | 7 | Confirms whether passed argument is a _value_ 8 | 9 | ```javascript 10 | const isValue = require("type/value/is"); 11 | 12 | isValue({}); // true 13 | isValue(null); // false 14 | ``` 15 | 16 | ## `value/ensure` 17 | 18 | Ensures if given argument is a _value_. If it's a value it is returned back, if not `TypeError` is thrown 19 | 20 | ```javascript 21 | const ensureValue = require("type/value/ensure"); 22 | 23 | const obj = {}; 24 | 25 | ensureValue(obj); // obj 26 | ensureValue(null); // Thrown TypeError: Cannot use null 27 | ``` 28 | -------------------------------------------------------------------------------- /ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isArray = require("./array/is") 4 | , toShortString = require("./lib/to-short-string"); 5 | 6 | var objPropertyIsEnumerable = Object.prototype.propertyIsEnumerable; 7 | 8 | var assign = function (target, source) { 9 | for (var key in source) { 10 | if (objPropertyIsEnumerable.call(source, key)) target[key] = source[key]; 11 | } 12 | }; 13 | 14 | module.exports = function (validationDatum1/*, ...validationDatumN, options */) { 15 | var validationData = [validationDatum1]; 16 | var globalOptions; 17 | if (arguments.length > 1) { 18 | var hasOptions = !isArray(arguments[arguments.length - 1]); 19 | if (hasOptions) globalOptions = arguments[arguments.length - 1]; 20 | var lastDatumIndex = hasOptions ? arguments.length - 2 : arguments.length - 1; 21 | for (var i = 1; i <= lastDatumIndex; ++i) validationData.push(arguments[i]); 22 | } 23 | var result = [], errors; 24 | for (var j = 0; j < validationData.length; ++j) { 25 | var validationDatum = validationData[j]; 26 | var options = { name: validationDatum[0] }; 27 | if (globalOptions) assign(options, globalOptions); 28 | if (validationDatum[3]) assign(options, validationDatum[3]); 29 | var resultItem; 30 | if (typeof validationDatum[2] !== "function") { 31 | throw new TypeError(toShortString(validationDatum[2]) + " is not a function"); 32 | } 33 | try { 34 | resultItem = validationDatum[2](validationDatum[1], options); 35 | } catch (error) { 36 | if (!errors) errors = []; 37 | errors.push(error); 38 | } 39 | if (errors) continue; 40 | result.push(resultItem); 41 | } 42 | if (!errors) return result; 43 | 44 | if (errors.length === 1) throw errors[0]; 45 | var ErrorConstructor = (globalOptions && globalOptions.Error) || TypeError; 46 | var errorMessage = "Approached following errors:"; 47 | for (var k = 0; k < errors.length; ++k) { 48 | errorMessage += "\n - " + errors[k].message.split("\n").join("\n "); 49 | } 50 | throw new ErrorConstructor(errorMessage); 51 | }; 52 | -------------------------------------------------------------------------------- /error/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name ? "Expected an error for %n, received %v" : "%v is not an error"; 11 | return resolveException(value, errorMessage, options); 12 | }; 13 | -------------------------------------------------------------------------------- /error/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isPrototype = require("../prototype/is") 4 | , isPlainObject = require("../plain-object/is"); 5 | 6 | var objectToString = Object.prototype.toString; 7 | 8 | // Recognize host specific errors (e.g. DOMException) 9 | var errorTaggedStringRe = /^\[object .*(?:Error|Exception)\]$/ 10 | , errorNameRe = /^[^\s]*(?:Error|Exception)$/; 11 | 12 | module.exports = function (value) { 13 | if (!value) return false; 14 | 15 | var name; 16 | // Sanity check (reject objects which do not expose common Error interface) 17 | try { 18 | name = value.name; 19 | if (typeof name !== "string") return false; 20 | if (typeof value.message !== "string") return false; 21 | } catch (error) { 22 | return false; 23 | } 24 | 25 | // Ensure its a native-like Error object 26 | // (has [[ErrorData]] slot, or was created to resemble one) 27 | // Note: It's not a 100% bulletproof check of confirming that as: 28 | // - In ES2015+ string tag can be overriden via Symbol.toStringTag property 29 | // - Host errors do not share native error tag. Still we rely on assumption that 30 | // tag for each error will end either with `Error` or `Exception` string 31 | // - In pre ES2015 era, no custom errors will share the error tag. 32 | if (!errorTaggedStringRe.test(objectToString.call(value))) { 33 | // Definitely not an ES2015 error instance, but could still be an error 34 | // (created via e.g. CustomError.prototype = Object.create(Error.prototype)) 35 | try { 36 | if (name !== value.constructor.name) return false; 37 | } catch (error) { 38 | return false; 39 | } 40 | if (!errorNameRe.test(name)) return false; 41 | if (isPlainObject(value)) return false; 42 | } 43 | 44 | return !isPrototype(value); 45 | }; 46 | -------------------------------------------------------------------------------- /finite/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var coerceToNumber = require("../number/coerce"); 4 | 5 | module.exports = function (value) { 6 | value = coerceToNumber(value); 7 | return isFinite(value) ? value : null; 8 | }; 9 | -------------------------------------------------------------------------------- /finite/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , coerce = require("./coerce"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | var coerced = coerce(value); 8 | if (coerced !== null) return coerced; 9 | var options = arguments[1]; 10 | var errorMessage = 11 | options && options.name 12 | ? "Expected a finite number for %n, received %v" 13 | : "%v is not a finite number"; 14 | return resolveException(value, errorMessage, options); 15 | }; 16 | -------------------------------------------------------------------------------- /function/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name 11 | ? "Expected a function for %n, received %v" 12 | : "%v is not a function"; 13 | return resolveException(value, errorMessage, options); 14 | }; 15 | -------------------------------------------------------------------------------- /function/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isPrototype = require("../prototype/is"); 4 | 5 | module.exports = function (value) { 6 | if (typeof value !== "function") return false; 7 | 8 | if (!hasOwnProperty.call(value, "length")) return false; 9 | 10 | try { 11 | if (typeof value.length !== "number") return false; 12 | if (typeof value.call !== "function") return false; 13 | if (typeof value.apply !== "function") return false; 14 | } catch (error) { 15 | return false; 16 | } 17 | 18 | return !isPrototype(value); 19 | }; 20 | -------------------------------------------------------------------------------- /integer/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var coerceToFinite = require("../finite/coerce"); 4 | 5 | var abs = Math.abs, floor = Math.floor; 6 | 7 | module.exports = function (value) { 8 | value = coerceToFinite(value); 9 | if (!value) return value; 10 | return (value > 0 ? 1 : -1) * floor(abs(value)); 11 | }; 12 | -------------------------------------------------------------------------------- /integer/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , coerce = require("./coerce"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | var coerced = coerce(value); 8 | if (coerced !== null) return coerced; 9 | var options = arguments[1]; 10 | var errorMessage = 11 | options && options.name 12 | ? "Expected an integer for %n, received %v" 13 | : "%v is not an integer"; 14 | return resolveException(value, errorMessage, options); 15 | }; 16 | -------------------------------------------------------------------------------- /iterable/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , resolveErrorMessage = require("../lib/resolve-error-message") 5 | , toShortString = require("../lib/to-short-string") 6 | , ensurePlainFunction = require("../plain-function/ensure") 7 | , is = require("./is"); 8 | 9 | var invalidItemsLimit = 3; 10 | 11 | module.exports = function (value/*, options*/) { 12 | var options = arguments[1]; 13 | var mainErrorMessage = 14 | options && options.name 15 | ? "Expected an iterable for %n, received %v" 16 | : "%v is not expected iterable"; 17 | if (!is(value, options)) return resolveException(value, mainErrorMessage, options); 18 | if (!options) return value; 19 | 20 | var ensureItem = ensurePlainFunction(options.ensureItem, { isOptional: true }); 21 | if (ensureItem) { 22 | var coercedValue = []; 23 | var iterator = value[Symbol.iterator](); 24 | var item, invalidItems; 25 | while (!(item = iterator.next()).done) { 26 | var newItemValue; 27 | try { 28 | newItemValue = ensureItem(item.value); 29 | } catch (error) { 30 | if (!invalidItems) invalidItems = []; 31 | if (invalidItems.push(item.value) === invalidItemsLimit) break; 32 | } 33 | if (invalidItems) continue; 34 | coercedValue.push(newItemValue); 35 | } 36 | if (invalidItems) { 37 | var errorMessage = 38 | resolveErrorMessage(mainErrorMessage, value, options) + 39 | ".\n Following items are invalid:"; 40 | for (var i = 0; i < invalidItems.length; ++i) { 41 | errorMessage += "\n - " + toShortString(invalidItems[i]); 42 | } 43 | throw new TypeError(errorMessage); 44 | } 45 | return coercedValue; 46 | } 47 | 48 | return value; 49 | }; 50 | -------------------------------------------------------------------------------- /iterable/is.js: -------------------------------------------------------------------------------- 1 | // Polyfills friendly, therefore ES5 syntax 2 | 3 | "use strict"; 4 | 5 | var isObject = require("../object/is"); 6 | 7 | var iteratorSymbol = Symbol.iterator; 8 | 9 | if (!iteratorSymbol) { 10 | throw new Error("Cannot initialize iterator/is due to Symbol.iterator not being implemented"); 11 | } 12 | 13 | module.exports = function (value/*, options*/) { 14 | var options = arguments[1]; 15 | if (!isObject(value)) { 16 | if (!isObject(options) || !options.allowString || typeof value !== "string") return false; 17 | } 18 | try { 19 | if (typeof value[iteratorSymbol] !== "function") return false; 20 | } catch (error) { 21 | return false; 22 | } 23 | if (!options) return true; 24 | if (options.denyEmpty) { 25 | try { 26 | if (value[iteratorSymbol]().next().done) return false; 27 | } catch (error) { 28 | return false; 29 | } 30 | } 31 | return true; 32 | }; 33 | -------------------------------------------------------------------------------- /lib/ensure/min.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../resolve-exception"); 4 | 5 | module.exports = function (value, coerced, options) { 6 | if (coerced >= options.min) return coerced; 7 | var errorMessage = 8 | options && options.name 9 | ? "Expected %n to be greater or equal " + options.min + ", received %v" 10 | : "%v is not greater or equal " + options.min; 11 | return resolveException(value, errorMessage, options); 12 | }; 13 | -------------------------------------------------------------------------------- /lib/is-to-string-tag-supported.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol"; 4 | -------------------------------------------------------------------------------- /lib/resolve-error-message.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var stringCoerce = require("../string/coerce") 4 | , toShortString = require("./to-short-string"); 5 | 6 | module.exports = function (errorMessage, value, inputOptions) { 7 | if (inputOptions && inputOptions.errorMessage) { 8 | errorMessage = stringCoerce(inputOptions.errorMessage); 9 | } 10 | 11 | var valueInsertIndex = errorMessage.indexOf("%v"); 12 | var valueToken = valueInsertIndex > -1 ? toShortString(value) : null; 13 | if (inputOptions && inputOptions.name) { 14 | var nameInsertIndex = errorMessage.indexOf("%n"); 15 | if (nameInsertIndex > -1) { 16 | if (valueInsertIndex > -1) { 17 | var firstToken, secondToken, firstInsertIndex, secondInsertIndex; 18 | if (nameInsertIndex > valueInsertIndex) { 19 | firstToken = valueToken; 20 | firstInsertIndex = valueInsertIndex; 21 | secondToken = inputOptions.name; 22 | secondInsertIndex = nameInsertIndex; 23 | } else { 24 | firstToken = inputOptions.name; 25 | firstInsertIndex = nameInsertIndex; 26 | secondToken = valueToken; 27 | secondInsertIndex = valueInsertIndex; 28 | } 29 | return ( 30 | errorMessage.slice(0, firstInsertIndex) + 31 | firstToken + 32 | errorMessage.slice(firstInsertIndex + 2, secondInsertIndex) + 33 | secondToken + 34 | errorMessage.slice(secondInsertIndex + 2) 35 | ); 36 | } 37 | return ( 38 | errorMessage.slice(0, nameInsertIndex) + 39 | inputOptions.name + 40 | errorMessage.slice(nameInsertIndex + 2) 41 | ); 42 | } 43 | } 44 | if (valueInsertIndex > -1) { 45 | return ( 46 | errorMessage.slice(0, valueInsertIndex) + 47 | valueToken + 48 | errorMessage.slice(valueInsertIndex + 2) 49 | ); 50 | } 51 | return errorMessage; 52 | }; 53 | -------------------------------------------------------------------------------- /lib/resolve-exception.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isValue = require("../value/is") 4 | , resolveErrorMessage = require("./resolve-error-message"); 5 | 6 | module.exports = function (value, defaultMessage, inputOptions) { 7 | if (inputOptions && !isValue(value)) { 8 | if ("default" in inputOptions) return inputOptions["default"]; 9 | if (inputOptions.isOptional) return null; 10 | } 11 | var ErrorConstructor = (inputOptions && inputOptions.Error) || TypeError; 12 | var error = new ErrorConstructor(resolveErrorMessage(defaultMessage, value, inputOptions)); 13 | if (inputOptions && inputOptions.errorCode) error.code = inputOptions.errorCode; 14 | throw error; 15 | }; 16 | -------------------------------------------------------------------------------- /lib/safe-to-string.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = function (value) { 4 | try { 5 | return value.toString(); 6 | } catch (error) { 7 | try { return String(value); } 8 | catch (error2) { return null; } 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /lib/to-short-string.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var safeToString = require("./safe-to-string"); 4 | 5 | var reNewLine = /[\n\r\u2028\u2029]/g; 6 | 7 | module.exports = function (value) { 8 | var string = safeToString(value); 9 | if (string === null) return ""; 10 | // Trim if too long 11 | if (string.length > 100) string = string.slice(0, 99) + "…"; 12 | // Replace eventual new lines 13 | string = string.replace(reNewLine, function (char) { 14 | switch (char) { 15 | case "\n": 16 | return "\\n"; 17 | case "\r": 18 | return "\\r"; 19 | case "\u2028": 20 | return "\\u2028"; 21 | case "\u2029": 22 | return "\\u2029"; 23 | /* istanbul ignore next */ 24 | default: 25 | throw new Error("Unexpected character"); 26 | } 27 | }); 28 | return string; 29 | }; 30 | -------------------------------------------------------------------------------- /map/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name ? "Expected a map for %n, received %v" : "%v is not a map"; 11 | return resolveException(value, errorMessage, options); 12 | }; 13 | -------------------------------------------------------------------------------- /map/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isPrototype = require("../prototype/is"); 4 | 5 | // In theory we could rely on Symbol.toStringTag directly, 6 | // still early native implementation (e.g. in FF) predated symbols 7 | var objectToString = Object.prototype.toString, objectTaggedString = objectToString.call(new Map()); 8 | 9 | module.exports = function (value) { 10 | if (!value) return false; 11 | 12 | // Sanity check (reject objects which do not expose common Promise interface) 13 | try { 14 | if (typeof value.set !== "function") return false; 15 | if (typeof value.get !== "function") return false; 16 | if (typeof value.has !== "function") return false; 17 | if (typeof value.clear !== "function") return false; 18 | } catch (error) { 19 | return false; 20 | } 21 | 22 | // Ensure its native Promise object (has [[MapData]] slot) 23 | // Note: it's not 100% precise as string tag may be overriden 24 | // and other objects could be hacked to expose it 25 | if (objectToString.call(value) !== objectTaggedString) return false; 26 | 27 | return !isPrototype(value); 28 | }; 29 | -------------------------------------------------------------------------------- /natural-number/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var coerceToInteger = require("../integer/coerce"); 4 | 5 | module.exports = function (value) { 6 | value = coerceToInteger(value); 7 | if (!value) return value; 8 | if (value < 0) return null; 9 | return value; 10 | }; 11 | -------------------------------------------------------------------------------- /natural-number/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , ensureMin = require("../lib/ensure/min") 5 | , coerce = require("./coerce"); 6 | 7 | module.exports = function (value/*, options*/) { 8 | var coerced = coerce(value), options = arguments[1]; 9 | if (coerced !== null) { 10 | if (options) { 11 | if (options.min) ensureMin(value, coerced, options); 12 | } 13 | return coerced; 14 | } 15 | 16 | var errorMessage = 17 | options && options.name 18 | ? "Expected a natural number for %n, received %v" 19 | : "%v is not a natural number"; 20 | return resolveException(value, errorMessage, options); 21 | }; 22 | -------------------------------------------------------------------------------- /number/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isValue = require("../value/is"); 4 | 5 | module.exports = function (value) { 6 | if (!isValue(value)) return null; 7 | try { 8 | value = +value; // Ensure implicit coercion 9 | } catch (error) { 10 | return null; 11 | } 12 | if (isNaN(value)) return null; 13 | return value; 14 | }; 15 | -------------------------------------------------------------------------------- /number/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , coerce = require("./coerce"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | var coerced = coerce(value); 8 | if (coerced !== null) return coerced; 9 | var options = arguments[1]; 10 | var errorMessage = 11 | options && options.name ? "Expected a number for %n, received %v" : "%v is not a number"; 12 | return resolveException(value, errorMessage, options); 13 | }; 14 | -------------------------------------------------------------------------------- /object/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name ? "Expected an object for %n, received %v" : "%v is not an object"; 11 | return resolveException(value, errorMessage, options); 12 | }; 13 | -------------------------------------------------------------------------------- /object/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isValue = require("../value/is"); 4 | 5 | // prettier-ignore 6 | var possibleTypes = { "object": true, "function": true, "undefined": true /* document.all */ }; 7 | 8 | module.exports = function (value) { 9 | if (!isValue(value)) return false; 10 | return hasOwnProperty.call(possibleTypes, typeof value); 11 | }; 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "type", 3 | "version": "2.7.3", 4 | "description": "Runtime validation and processing of JavaScript types", 5 | "author": "Mariusz Nowak (https://www.medikoo.com/)", 6 | "keywords": [ 7 | "type", 8 | "coercion" 9 | ], 10 | "repository": "medikoo/type", 11 | "devDependencies": { 12 | "chai": "^4.3.6", 13 | "eslint": "^8.21.0", 14 | "eslint-config-medikoo": "^4.1.2", 15 | "git-list-updated": "^1.2.1", 16 | "github-release-from-cc-changelog": "^2.3.0", 17 | "husky": "^4.3.8", 18 | "lint-staged": "^15.2.5", 19 | "mocha": "^6.2.3", 20 | "nyc": "^15.1.0", 21 | "prettier-elastic": "^3.2.5" 22 | }, 23 | "typesVersions": { 24 | ">=4": { 25 | "*": [ 26 | "ts-types/*" 27 | ] 28 | } 29 | }, 30 | "husky": { 31 | "hooks": { 32 | "pre-commit": "lint-staged" 33 | } 34 | }, 35 | "lint-staged": { 36 | "*.js": [ 37 | "eslint" 38 | ], 39 | "*.{css,html,js,json,md,yaml,yml}": [ 40 | "prettier -c" 41 | ] 42 | }, 43 | "eslintConfig": { 44 | "extends": "medikoo/es3", 45 | "root": true, 46 | "globals": { 47 | "BigInt": true, 48 | "Map": true, 49 | "Promise": true, 50 | "Set": true, 51 | "Symbol": true 52 | }, 53 | "overrides": [ 54 | { 55 | "files": "test/**/*.js", 56 | "env": { 57 | "mocha": true 58 | }, 59 | "rules": { 60 | "no-eval": "off", 61 | "no-new-wrappers": "off" 62 | } 63 | }, 64 | { 65 | "files": [ 66 | "string/coerce.js", 67 | "number/coerce.js" 68 | ], 69 | "rules": { 70 | "no-implicit-coercion": "off" 71 | } 72 | }, 73 | { 74 | "files": "plain-object/is.js", 75 | "rules": { 76 | "no-proto": "off" 77 | } 78 | } 79 | ] 80 | }, 81 | "prettier": { 82 | "printWidth": 100, 83 | "tabWidth": 4, 84 | "trailingComma": "none", 85 | "overrides": [ 86 | { 87 | "files": [ 88 | "*.md", 89 | "*.yml" 90 | ], 91 | "options": { 92 | "tabWidth": 2 93 | } 94 | } 95 | ] 96 | }, 97 | "nyc": { 98 | "all": true, 99 | "exclude": [ 100 | ".github", 101 | "coverage/**", 102 | "test/**", 103 | "*.config.js" 104 | ], 105 | "reporter": [ 106 | "lcov", 107 | "html", 108 | "text-summary" 109 | ] 110 | }, 111 | "scripts": { 112 | "coverage": "nyc npm test", 113 | "lint:updated": "pipe-git-updated --base=main --ext=js -- eslint --ignore-pattern '!*'", 114 | "prettier-check": "prettier -c --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"", 115 | "prettier-check:updated": "pipe-git-updated --base=main --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c", 116 | "prettify": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"", 117 | "prettify:updated": "pipe-git-updated ---base=main -ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier --write", 118 | "test": "mocha --recursive" 119 | }, 120 | "license": "ISC" 121 | } 122 | -------------------------------------------------------------------------------- /plain-function/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name 11 | ? "Expected a plain function for %n, received %v" 12 | : "%v is not a plain function"; 13 | return resolveException(value, errorMessage, options); 14 | }; 15 | -------------------------------------------------------------------------------- /plain-function/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isFunction = require("../function/is"); 4 | 5 | var classRe = /^\s*class[\s{/}]/, functionToString = Function.prototype.toString; 6 | 7 | module.exports = function (value) { 8 | if (!isFunction(value)) return false; 9 | if (classRe.test(functionToString.call(value))) return false; 10 | return true; 11 | }; 12 | -------------------------------------------------------------------------------- /plain-object/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , resolveErrorMessage = require("../lib/resolve-error-message") 5 | , ensurePlainFunction = require("../plain-function/ensure") 6 | , ensureArray = require("../array/ensure") 7 | , is = require("./is"); 8 | 9 | var objHasOwnProperty = Object.prototype.hasOwnProperty, invalidItemsLimit = 3; 10 | 11 | module.exports = function (value/*, options*/) { 12 | var options = arguments[1]; 13 | var mainErrorMessage = 14 | options && options.name 15 | ? "Expected a plain object for %n, received %v" 16 | : "%v is not a plain object"; 17 | if (!is(value)) return resolveException(value, mainErrorMessage, options); 18 | if (!options) return value; 19 | 20 | var invalidKeys, key, errorMessage; 21 | var allowedKeys = ensureArray(options.allowedKeys, { isOptional: true }); 22 | if (allowedKeys) { 23 | for (key in value) { 24 | if (!objHasOwnProperty.call(value, key)) continue; 25 | if (allowedKeys.indexOf(key) > -1) continue; 26 | if (!invalidKeys) invalidKeys = []; 27 | if (invalidKeys.push(key) === invalidItemsLimit) break; 28 | } 29 | if (invalidKeys) { 30 | errorMessage = 31 | resolveErrorMessage(mainErrorMessage, value, options) + 32 | ".\n Following keys are unexpected: " + 33 | invalidKeys.join(", "); 34 | throw new TypeError(errorMessage); 35 | } 36 | } 37 | 38 | var ensurePropertyValue = ensurePlainFunction(options.ensurePropertyValue, { 39 | isOptional: true 40 | }); 41 | if (ensurePropertyValue) { 42 | var coercedValue = {}; 43 | for (key in value) { 44 | if (!objHasOwnProperty.call(value, key)) continue; 45 | var coercedPropertyValue; 46 | try { 47 | coercedPropertyValue = ensurePropertyValue(value[key]); 48 | } catch (error) { 49 | if (!invalidKeys) invalidKeys = []; 50 | if (invalidKeys.push(key) === invalidItemsLimit) break; 51 | } 52 | if (invalidKeys) continue; 53 | coercedValue[key] = coercedPropertyValue; 54 | } 55 | if (invalidKeys) { 56 | errorMessage = 57 | resolveErrorMessage(mainErrorMessage, value, options) + 58 | ".\n Values for following keys are invalid: " + 59 | invalidKeys.join(", "); 60 | throw new TypeError(errorMessage); 61 | } 62 | 63 | return coercedValue; 64 | } 65 | 66 | return value; 67 | }; 68 | -------------------------------------------------------------------------------- /plain-object/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isObject = require("../object/is") 4 | , isPrototype = require("../prototype/is"); 5 | 6 | var getPrototypeOf; 7 | if (typeof Object.getPrototypeOf === "function") { 8 | getPrototypeOf = Object.getPrototypeOf; 9 | } else if ({}.__proto__ === Object.prototype) { 10 | getPrototypeOf = function (object) { return object.__proto__; }; 11 | } 12 | 13 | module.exports = function (value) { 14 | if (!isObject(value)) return false; 15 | var prototype; 16 | if (getPrototypeOf) { 17 | prototype = getPrototypeOf(value); 18 | } else { 19 | try { 20 | var valueConstructor = value.constructor; 21 | if (valueConstructor) prototype = valueConstructor.prototype; 22 | } catch (error) { 23 | return false; 24 | } 25 | } 26 | if (prototype && !hasOwnProperty.call(prototype, "propertyIsEnumerable")) return false; 27 | return !isPrototype(value); 28 | }; 29 | -------------------------------------------------------------------------------- /promise/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name ? "Expected a promise for %n, received %v" : "%v is not a promise"; 11 | return resolveException(value, errorMessage, options); 12 | }; 13 | -------------------------------------------------------------------------------- /promise/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isPrototype = require("../prototype/is"); 4 | 5 | // In theory we could rely on Symbol.toStringTag directly, 6 | // still early native implementation (e.g. in FF) predated symbols 7 | var objectToString = Object.prototype.toString 8 | , objectTaggedString = objectToString.call(Promise.resolve()); 9 | 10 | module.exports = function (value) { 11 | if (!value) return false; 12 | 13 | // Sanity check (reject objects which do not expose common Promise interface) 14 | try { 15 | if (typeof value.then !== "function") return false; 16 | if (typeof value["catch"] !== "function") return false; 17 | } catch (error) { 18 | return false; 19 | } 20 | 21 | // Ensure its native Promise object (has [[PromiseState]] slot) 22 | // Note: it's not 100% precise as string tag may be overriden 23 | // and other objects could be hacked to expose it 24 | if (objectToString.call(value) !== objectTaggedString) return false; 25 | 26 | return !isPrototype(value); 27 | }; 28 | -------------------------------------------------------------------------------- /prototype/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isObject = require("../object/is"); 4 | 5 | module.exports = function (value) { 6 | if (!isObject(value)) return false; 7 | try { 8 | if (!value.constructor) return false; 9 | return value.constructor.prototype === value; 10 | } catch (error) { 11 | return false; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /reg-exp/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name 11 | ? "Expected a regular expression for %n, received %v" 12 | : "%v is not a regular expression"; 13 | return resolveException(value, errorMessage, options); 14 | }; 15 | -------------------------------------------------------------------------------- /reg-exp/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isToStringTagSupported = require("../lib/is-to-string-tag-supported") 4 | , isPrototype = require("../prototype/is"); 5 | 6 | var regExpTest = RegExp.prototype.test 7 | , objectToString = Object.prototype.toString 8 | , objectTaggedString = objectToString.call(/a/); 9 | 10 | module.exports = function (value) { 11 | if (!value) return false; 12 | 13 | // Sanity check (reject objects which do not expose common RegExp interface) 14 | if (!hasOwnProperty.call(value, "lastIndex")) return false; 15 | try { 16 | if (typeof value.lastIndex !== "number") return false; 17 | if (typeof value.test !== "function") return false; 18 | if (typeof value.exec !== "function") return false; 19 | } catch (error) { 20 | return false; 21 | } 22 | 23 | // Ensure its native RegExp object (has [[RegExpMatcher]] slot) 24 | if (isToStringTagSupported && typeof value[Symbol.toStringTag] === "string") { 25 | // Edge case (possibly a regExp with custom Symbol.toStringTag) 26 | try { 27 | var lastIndex = value.lastIndex; 28 | regExpTest.call(value, ""); 29 | if (value.lastIndex !== lastIndex) value.lastIndex = lastIndex; 30 | return true; 31 | } catch (error) { 32 | return false; 33 | } 34 | } 35 | if (objectToString.call(value) !== objectTaggedString) return false; 36 | return !isPrototype(value); 37 | }; 38 | -------------------------------------------------------------------------------- /safe-integer/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var coerceToInteger = require("../integer/coerce"); 4 | 5 | var MAX_SAFE_INTEGER = 9007199254740991, MIN_SAFE_INTEGER = -9007199254740991; 6 | 7 | module.exports = function (value) { 8 | value = coerceToInteger(value); 9 | if (!value) return value; 10 | if (value > MAX_SAFE_INTEGER) return null; 11 | if (value < MIN_SAFE_INTEGER) return null; 12 | return value; 13 | }; 14 | -------------------------------------------------------------------------------- /safe-integer/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , coerce = require("./coerce"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | var coerced = coerce(value); 8 | if (coerced !== null) return coerced; 9 | var options = arguments[1]; 10 | var errorMessage = 11 | options && options.name 12 | ? "Expected a safe integer for %n, received %v" 13 | : "%v is not a safe integer"; 14 | return resolveException(value, errorMessage, options); 15 | }; 16 | -------------------------------------------------------------------------------- /set/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name ? "Expected a set for %n, received %v" : "%v is not a set"; 11 | return resolveException(value, errorMessage, options); 12 | }; 13 | -------------------------------------------------------------------------------- /set/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isPrototype = require("../prototype/is"); 4 | 5 | // In theory we could rely on Symbol.toStringTag directly, 6 | // still early native implementation (e.g. in FF) predated symbols 7 | var objectToString = Object.prototype.toString, objectTaggedString = objectToString.call(new Set()); 8 | 9 | module.exports = function (value) { 10 | if (!value) return false; 11 | 12 | // Sanity check (reject objects which do not expose common Set interface) 13 | try { 14 | if (typeof value.add !== "function") return false; 15 | if (typeof value.has !== "function") return false; 16 | if (typeof value.clear !== "function") return false; 17 | } catch (error) { 18 | return false; 19 | } 20 | 21 | // Ensure its native Set object (has [[SetData]] slot) 22 | // Note: it's not 100% precise as string tag may be overriden 23 | // and other objects could be hacked to expose it 24 | if (objectToString.call(value) !== objectTaggedString) return false; 25 | 26 | return !isPrototype(value); 27 | }; 28 | -------------------------------------------------------------------------------- /string/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isValue = require("../value/is") 4 | , isObject = require("../object/is"); 5 | 6 | var objectToString = Object.prototype.toString; 7 | 8 | module.exports = function (value) { 9 | if (!isValue(value)) return null; 10 | if (isObject(value)) { 11 | // Reject Object.prototype.toString coercion 12 | var valueToString = value.toString; 13 | if (typeof valueToString !== "function") return null; 14 | if (valueToString === objectToString) return null; 15 | // Note: It can be object coming from other realm, still as there's no ES3 and CSP compliant 16 | // way to resolve its realm's Object.prototype.toString it's left as not addressed edge case 17 | } 18 | try { 19 | return "" + value; // Ensure implicit coercion 20 | } catch (error) { 21 | return null; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /string/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , coerce = require("./coerce"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | var coerced = coerce(value); 8 | if (coerced !== null) return coerced; 9 | var options = arguments[1]; 10 | var errorMessage = 11 | options && options.name ? "Expected a string for %n, received %v" : "%v is not a string"; 12 | return resolveException(value, errorMessage, options); 13 | }; 14 | -------------------------------------------------------------------------------- /test/_lib/arrow-function-if-supported.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | try { module.exports = eval("(() => {})"); } 4 | catch (error) {} 5 | -------------------------------------------------------------------------------- /test/_lib/class-if-supported.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | try { module.exports = eval("(class {})"); } 4 | catch (error) {} 5 | -------------------------------------------------------------------------------- /test/array-length/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToArrayLength = require("../../array-length/coerce"); 5 | 6 | describe("array-length/coerce", function () { 7 | it("Should coerce float", function () { 8 | assert.equal(coerceToArrayLength(123.123), 123); 9 | assert.equal(coerceToArrayLength(123.823), 123); 10 | }); 11 | it("Should coerce string", function () { assert.equal(coerceToArrayLength("12.123"), 12); }); 12 | it("Should coerce booleans", function () { assert.equal(coerceToArrayLength(true), 1); }); 13 | it("Should coerce number objects", function () { 14 | assert.equal(coerceToArrayLength(new Number(343)), 343); 15 | }); 16 | it("Should coerce objects", function () { 17 | assert.equal(coerceToArrayLength({ valueOf: function () { return 23; } }), 23); 18 | }); 19 | it("Should reject infinite number", function () { 20 | assert.equal(coerceToArrayLength(Infinity), null); 21 | }); 22 | it("Should reject number beyond Number.MAX_SAFE_INTEGER", function () { 23 | assert.equal(coerceToArrayLength(9007199254740992), null); 24 | }); 25 | it("Should reject negative number", function () { 26 | assert.equal(coerceToArrayLength(-9), null); 27 | }); 28 | 29 | it("Should reject NaN", function () { assert.equal(coerceToArrayLength(NaN), null); }); 30 | 31 | if (typeof Object.create === "function") { 32 | it("Should not coerce objects with no number representation", function () { 33 | assert.equal(coerceToArrayLength(Object.create(null)), null); 34 | }); 35 | } 36 | 37 | it("Should not coerce null", function () { assert.equal(coerceToArrayLength(null), null); }); 38 | it("Should not coerce undefined", function () { 39 | assert.equal(coerceToArrayLength(undefined), null); 40 | }); 41 | 42 | if (typeof Symbol === "function") { 43 | it("Should not coerce symbols", function () { 44 | assert.equal(coerceToArrayLength(Symbol("foo")), null); 45 | }); 46 | } 47 | }); 48 | -------------------------------------------------------------------------------- /test/array-length/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureArrayLength = require("../../array-length/ensure"); 5 | 6 | describe("array-length/ensure", function () { 7 | it("Should return coerced value", function () { 8 | assert.equal(ensureArrayLength("12.23"), 12); 9 | }); 10 | it("Should crash on no value", function () { 11 | try { 12 | ensureArrayLength(-20); 13 | throw new Error("Unexpected"); 14 | } catch (error) { 15 | assert.equal(error.name, "TypeError"); 16 | assert.equal(error.message, "-20 is not an array length"); 17 | } 18 | }); 19 | it("Should provide alternative error message when name option is passed", function () { 20 | try { 21 | ensureArrayLength(-20, { name: "foo" }); 22 | throw new Error("Unexpected"); 23 | } catch (error) { 24 | assert.equal(error.name, "TypeError"); 25 | assert.equal(error.message, "Expected an array length for foo, received -20"); 26 | } 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /test/array-like/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureArrayLike = require("../../array-like/ensure"); 5 | 6 | describe("array-like/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = []; 9 | assert.equal(ensureArrayLike(value), value); 10 | }); 11 | it("Should allow strings with allowString option", function () { 12 | var value = "foo"; 13 | assert.equal(ensureArrayLike(value, { allowString: true }), value); 14 | }); 15 | it("Should crash on invalid value", function () { 16 | try { 17 | ensureArrayLike("foo"); 18 | throw new Error("Unexpected"); 19 | } catch (error) { 20 | assert.equal(error.name, "TypeError"); 21 | assert(error.message.includes("is not an array like")); 22 | } 23 | }); 24 | it("Should provide alternative error message when name option is passed", function () { 25 | try { 26 | ensureArrayLike("foo", { name: "name" }); 27 | throw new Error("Unexpected"); 28 | } catch (error) { 29 | assert.equal(error.name, "TypeError"); 30 | assert.equal(error.message, "Expected an array like for name, received foo"); 31 | } 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /test/array-like/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isArrayLike = require("../../array-like/is"); 5 | 6 | describe("array-like/is", function () { 7 | it("Should return true on array", function () { assert.equal(isArrayLike([]), true); }); 8 | it("Should return true on array-like object", function () { 9 | assert.equal(isArrayLike({ length: 1 }), true); 10 | }); 11 | it("Should by default return false on string", function () { 12 | assert.equal(isArrayLike("foo"), false); 13 | }); 14 | it("Should accept strings if specified", function () { 15 | assert.equal(isArrayLike("foo", { allowString: true }), true); 16 | }); 17 | 18 | it("Should return false on objects with negative length", function () { 19 | assert.equal(isArrayLike({ length: -1 }), false); 20 | }); 21 | 22 | it("Should return false on plain object", function () { 23 | assert.equal(isArrayLike({}), false); 24 | }); 25 | it("Should return false on function", function () { 26 | assert.equal(isArrayLike(function () { return true; }), false); 27 | }); 28 | 29 | if (typeof Object.create === "function") { 30 | it("Should return false on object with no prototype", function () { 31 | assert.equal(isArrayLike(Object.create(null)), false); 32 | }); 33 | } 34 | it("Should return false on number", function () { assert.equal(isArrayLike(123), false); }); 35 | it("Should return false on NaN", function () { assert.equal(isArrayLike(NaN), false); }); 36 | it("Should return false on boolean", function () { assert.equal(isArrayLike(true), false); }); 37 | if (typeof Symbol === "function") { 38 | it("Should return false on symbol", function () { 39 | assert.equal(isArrayLike(Symbol("foo")), false); 40 | }); 41 | } 42 | 43 | it("Should return false on null", function () { assert.equal(isArrayLike(null), false); }); 44 | it("Should return false on undefined", function () { 45 | assert.equal(isArrayLike(void 0), false); 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /test/array/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureString = require("../../string/ensure") 5 | , ensureArray = require("../../array/ensure"); 6 | 7 | describe("array/ensure", function () { 8 | it("Should return input value", function () { 9 | var value = []; 10 | assert.equal(ensureArray(value), value); 11 | }); 12 | it("Should crash on invalid value", function () { 13 | try { 14 | ensureArray(null); 15 | throw new Error("Unexpected"); 16 | } catch (error) { 17 | assert.equal(error.name, "TypeError"); 18 | assert.equal(error.message, "null is not an array"); 19 | } 20 | }); 21 | it("Should provide alternative error message when name option is passed", function () { 22 | try { 23 | ensureArray(null, { name: "name" }); 24 | throw new Error("Unexpected"); 25 | } catch (error) { 26 | assert.equal(error.name, "TypeError"); 27 | assert.equal(error.message, "Expected an array for name, received null"); 28 | } 29 | }); 30 | 31 | it("Should support ensureItem option", function () { 32 | assert.deepEqual(ensureArray(["bar", 12], { ensureItem: ensureString }), ["bar", "12"]); 33 | try { 34 | ensureArray(["bar", {}], { ensureItem: ensureString }); 35 | throw new Error("Unexpected"); 36 | } catch (error) { 37 | assert.equal(error.name, "TypeError"); 38 | assert.equal(error.message.indexOf("is not an array") !== -1, true); 39 | } 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /test/array/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isArray = require("../../array/is"); 5 | 6 | describe("array/is", function () { 7 | it("Should return true on array", function () { assert.equal(isArray([]), true); }); 8 | 9 | it("Should return false on array with no common API exposed", function () { 10 | var value = []; 11 | value.push = null; 12 | assert.equal(isArray(value), false); 13 | }); 14 | it("Should return false on Array.prototype", function () { 15 | assert.equal(isArray(Array.prototype), false); 16 | }); 17 | 18 | it("Should return false on plain object", function () { assert.equal(isArray({}), false); }); 19 | it("Should return false on function", function () { 20 | assert.equal(isArray(function () { return true; }), false); 21 | }); 22 | 23 | if (typeof Object.create === "function") { 24 | it("Should return false on object with no prototype", function () { 25 | assert.equal(isArray(Object.create(null)), false); 26 | }); 27 | } 28 | it("Should return false on string", function () { assert.equal(isArray("foo"), false); }); 29 | it("Should return false on empty string", function () { assert.equal(isArray(""), false); }); 30 | it("Should return false on number", function () { assert.equal(isArray(123), false); }); 31 | it("Should return false on NaN", function () { assert.equal(isArray(NaN), false); }); 32 | it("Should return false on boolean", function () { assert.equal(isArray(true), false); }); 33 | if (typeof Symbol === "function") { 34 | it("Should return false on symbol", function () { 35 | assert.equal(isArray(Symbol("foo")), false); 36 | }); 37 | } 38 | 39 | it("Should return false on null", function () { assert.equal(isArray(null), false); }); 40 | it("Should return false on undefined", function () { assert.equal(isArray(void 0), false); }); 41 | }); 42 | -------------------------------------------------------------------------------- /test/big-int/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToBigInt = require("../../big-int/coerce"); 5 | 6 | describe("big-int/coerce", function () { 7 | it("Should return input bigint", function () { 8 | assert.equal(coerceToBigInt(BigInt(0)), coerceToBigInt(BigInt(0))); 9 | }); 10 | it("Should coerce numbers", function () { assert.equal(coerceToBigInt(12), BigInt(12)); }); 11 | it("Should coerce booleans", function () { assert.equal(coerceToBigInt(true), BigInt(true)); }); 12 | it("Should not coerce null", function () { assert.equal(coerceToBigInt(null), null); }); 13 | it("Should not coerce undefined", function () { 14 | assert.equal(coerceToBigInt(undefined), null); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /test/big-int/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureBigInt = require("../../big-int/ensure"); 5 | 6 | describe("big-int/ensure", function () { 7 | it("Should return coerced value", function () { assert.equal(ensureBigInt(12), BigInt(12)); }); 8 | it("Should crash on no value", function () { 9 | try { 10 | ensureBigInt(null); 11 | throw new Error("Unexpected"); 12 | } catch (error) { 13 | assert.equal(error.name, "TypeError"); 14 | assert.equal(error.message, "null is not a bigint"); 15 | } 16 | }); 17 | it("Should provide alternative error message when name option is passed", function () { 18 | try { 19 | ensureBigInt(null, { name: "name" }); 20 | throw new Error("Unexpected"); 21 | } catch (error) { 22 | assert.equal(error.name, "TypeError"); 23 | assert.equal(error.message, "Expected bigint for name, received null"); 24 | } 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/constructor/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureConstructor = require("../../constructor/ensure"); 5 | 6 | describe("constructor/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = function () { return true; }; 9 | assert.equal(ensureConstructor(value), value); 10 | }); 11 | it("Should crash on invalid value", function () { 12 | try { 13 | ensureConstructor(null); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert(error.message.includes("is not a constructor function")); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureConstructor(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a constructor function for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/constructor/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isConstructor = require("../../constructor/is") 5 | , arrowFunctionIfSupported = require("../_lib/arrow-function-if-supported") 6 | , classIfSupported = require("../_lib/class-if-supported"); 7 | 8 | describe("constructor/is", function () { 9 | it("Should return true on function", function () { 10 | assert.equal(isConstructor(function () { return true; }), true); 11 | assert.equal(isConstructor(eval("(function(){})")), true); 12 | }); 13 | if (arrowFunctionIfSupported) { 14 | it("Should return false on arrow function", function () { 15 | assert.equal(isConstructor(arrowFunctionIfSupported), false); 16 | }); 17 | } 18 | if (classIfSupported) { 19 | it("Should return true on class", function () { 20 | assert.equal(isConstructor(classIfSupported), true); 21 | }); 22 | } 23 | it("Should return false on reg-exp", function () { 24 | assert.equal(isConstructor(/foo/), false); 25 | }); 26 | 27 | it("Should return false on plain object", function () { 28 | assert.equal(isConstructor({}), false); 29 | }); 30 | it("Should return false on array", function () { assert.equal(isConstructor([]), false); }); 31 | if (typeof Object.create === "function") { 32 | it("Should return false on object with no prototype", function () { 33 | assert.equal(isConstructor(Object.create(null)), false); 34 | }); 35 | } 36 | it("Should return false on string", function () { assert.equal(isConstructor("foo"), false); }); 37 | it("Should return false on empty string", function () { 38 | assert.equal(isConstructor(""), false); 39 | }); 40 | it("Should return false on number", function () { assert.equal(isConstructor(123), false); }); 41 | it("Should return false on NaN", function () { assert.equal(isConstructor(NaN), false); }); 42 | it("Should return false on boolean", function () { assert.equal(isConstructor(true), false); }); 43 | if (typeof Symbol === "function") { 44 | it("Should return false on symbol", function () { 45 | assert.equal(isConstructor(Symbol("foo")), false); 46 | }); 47 | } 48 | 49 | it("Should return false on null", function () { assert.equal(isConstructor(null), false); }); 50 | it("Should return false on undefined", function () { 51 | assert.equal(isConstructor(void 0), false); 52 | }); 53 | }); 54 | -------------------------------------------------------------------------------- /test/date/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureDate = require("../../date/ensure"); 5 | 6 | describe("date/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = new Date(); 9 | assert.equal(ensureDate(value), value); 10 | }); 11 | it("Should crash on invalid value", function () { 12 | try { 13 | ensureDate(null); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert(error.message.includes("is not a date")); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureDate(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a date for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/date/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isDate = require("../../date/is"); 5 | 6 | describe("date/is", function () { 7 | it("Should return true on date", function () { assert.equal(isDate(new Date()), true); }); 8 | it("Should return false on invalid date", function () { 9 | assert.equal(isDate(new Date("foo")), false); 10 | }); 11 | 12 | it("Should return false on native date with no common API exposed", function () { 13 | var value = new Date(); 14 | value.getFullYear = null; 15 | assert.equal(isDate(value), false); 16 | }); 17 | it("Should return false on Date.prototype", function () { 18 | assert.equal(isDate(Date.prototype), false); 19 | }); 20 | it("Should return false on time value", function () { assert.equal(isDate(12312313), false); }); 21 | 22 | it("Should return false on plain object", function () { assert.equal(isDate({}), false); }); 23 | it("Should return false on function", function () { 24 | assert.equal(isDate(function () { return true; }), false); 25 | }); 26 | 27 | it("Should return false on array", function () { assert.equal(isDate([]), false); }); 28 | if (typeof Object.create === "function") { 29 | it("Should return false on object with no prototype", function () { 30 | assert.equal(isDate(Object.create(null)), false); 31 | }); 32 | } 33 | it("Should return false on string", function () { assert.equal(isDate("foo"), false); }); 34 | it("Should return false on empty string", function () { assert.equal(isDate(""), false); }); 35 | it("Should return false on number", function () { assert.equal(isDate(123), false); }); 36 | it("Should return false on NaN", function () { assert.equal(isDate(NaN), false); }); 37 | it("Should return false on boolean", function () { assert.equal(isDate(true), false); }); 38 | if (typeof Symbol === "function") { 39 | it("Should return false on symbol", function () { 40 | assert.equal(isDate(Symbol("foo")), false); 41 | }); 42 | } 43 | 44 | it("Should return false on null", function () { assert.equal(isDate(null), false); }); 45 | it("Should return false on undefined", function () { assert.equal(isDate(void 0), false); }); 46 | }); 47 | -------------------------------------------------------------------------------- /test/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensure = require("../ensure") 5 | , ensureNumber = require("../number/ensure"); 6 | 7 | describe("ensure", function () { 8 | it("Should support multiple validation datums", function () { 9 | assert.deepEqual( 10 | ensure(["foo", 12.323, ensureNumber], ["bar", 10, ensureNumber]), [12.323, 10] 11 | ); 12 | }); 13 | it("Should surface only error", function () { 14 | try { 15 | ensure(["foo", null, ensureNumber], ["bar", 10, ensureNumber]); 16 | } catch (error) { 17 | assert.equal(error.name, "TypeError"); 18 | assert.equal(error.message, "Expected a number for foo, received null"); 19 | } 20 | }); 21 | it("Should cumulate errors", function () { 22 | try { 23 | ensure(["foo", null, ensureNumber], ["bar", NaN, ensureNumber]); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal( 27 | error.message, 28 | "Approached following errors:" + 29 | "\n - Expected a number for foo, received null" + 30 | "\n - Expected a number for bar, received NaN" 31 | ); 32 | } 33 | }); 34 | it("Should support Error from global options", function () { 35 | try { 36 | ensure(["foo", null, ensureNumber], ["bar", NaN, ensureNumber], { Error: RangeError }); 37 | } catch (error) { 38 | assert.equal(error.name, "RangeError"); 39 | assert.equal( 40 | error.message, 41 | "Approached following errors:" + 42 | "\n - Expected a number for foo, received null" + 43 | "\n - Expected a number for bar, received NaN" 44 | ); 45 | } 46 | try { 47 | ensure(["foo", null, ensureNumber], ["bar", 10, ensureNumber], { Error: RangeError }); 48 | } catch (error) { 49 | assert.equal(error.name, "RangeError"); 50 | assert.equal(error.message, "Expected a number for foo, received null"); 51 | } 52 | }); 53 | it("Should support individual validation options", function () { 54 | try { 55 | ensure(["foo", null, ensureNumber, { Error: RangeError }], ["bar", 10, ensureNumber]); 56 | } catch (error) { 57 | assert.equal(error.name, "RangeError"); 58 | assert.equal(error.message, "Expected a number for foo, received null"); 59 | } 60 | }); 61 | }); 62 | -------------------------------------------------------------------------------- /test/error/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureError = require("../../error/ensure"); 5 | 6 | describe("error/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = new Error(); 9 | assert.equal(ensureError(value), value); 10 | }); 11 | it("Should crash on invalid value", function () { 12 | try { 13 | ensureError(null); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert(error.message.includes("is not an error")); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureError(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected an error for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/error/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isError = require("../../error/is"); 5 | 6 | describe("error/is", function () { 7 | it("Should return true on error", function () { assert.equal(isError(new Error()), true); }); 8 | 9 | it("Should return false on native error with no common API exposed", function () { 10 | var value = new Error(); 11 | value.message = null; 12 | assert.equal(isError(value), false); 13 | }); 14 | it("Should return false on Error.prototype", function () { 15 | assert.equal(isError(Error.prototype), false); 16 | }); 17 | 18 | if (typeof Object.create === "function") { 19 | it("Should return true on custom built ES5 era error", function () { 20 | var CustomEs5Error = function () { Error.call(this); }; 21 | CustomEs5Error.prototype = Object.create(Error.prototype); 22 | assert.equal(isError(new CustomEs5Error()), true); 23 | }); 24 | 25 | it("Should return false on object with no prototype", function () { 26 | assert.equal(isError(Object.create(null)), false); 27 | }); 28 | } 29 | 30 | it("Should return false on plain object", function () { assert.equal(isError({}), false); }); 31 | it("Should return false on function", function () { 32 | assert.equal(isError(function () { return true; }), false); 33 | }); 34 | 35 | it("Should return false on array", function () { assert.equal(isError([]), false); }); 36 | 37 | it("Should return false on string", function () { assert.equal(isError("foo"), false); }); 38 | it("Should return false on empty string", function () { assert.equal(isError(""), false); }); 39 | it("Should return false on number", function () { assert.equal(isError(123), false); }); 40 | it("Should return false on NaN", function () { assert.equal(isError(NaN), false); }); 41 | it("Should return false on boolean", function () { assert.equal(isError(true), false); }); 42 | if (typeof Symbol === "function") { 43 | it("Should return false on symbol", function () { 44 | assert.equal(isError(Symbol("foo")), false); 45 | }); 46 | } 47 | 48 | it("Should return false on null", function () { assert.equal(isError(null), false); }); 49 | it("Should return false on undefined", function () { assert.equal(isError(void 0), false); }); 50 | }); 51 | -------------------------------------------------------------------------------- /test/finite/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToFinite = require("../../finite/coerce"); 5 | 6 | describe("finite/coerce", function () { 7 | it("Should return input number", function () { 8 | assert.equal(coerceToFinite(123.123), 123.123); 9 | }); 10 | it("Should coerce string", function () { assert.equal(coerceToFinite("12"), 12); }); 11 | it("Should coerce booleans", function () { assert.equal(coerceToFinite(true), 1); }); 12 | it("Should coerce number objects", function () { 13 | assert.equal(coerceToFinite(new Number(343)), 343); 14 | }); 15 | it("Should coerce objects", function () { 16 | assert.equal(coerceToFinite({ valueOf: function () { return 23; } }), 23); 17 | }); 18 | 19 | it("Should reject infinite number", function () { 20 | assert.equal(coerceToFinite(Infinity), null); 21 | }); 22 | it("Should reject NaN", function () { assert.equal(coerceToFinite(NaN), null); }); 23 | 24 | if (typeof Object.create === "function") { 25 | it("Should not coerce objects with no number representation", function () { 26 | assert.equal(coerceToFinite(Object.create(null)), null); 27 | }); 28 | } 29 | 30 | it("Should not coerce null", function () { assert.equal(coerceToFinite(null), null); }); 31 | it("Should not coerce undefined", function () { 32 | assert.equal(coerceToFinite(undefined), null); 33 | }); 34 | 35 | if (typeof Symbol === "function") { 36 | it("Should not coerce symbols", function () { 37 | assert.equal(coerceToFinite(Symbol("foo")), null); 38 | }); 39 | } 40 | }); 41 | -------------------------------------------------------------------------------- /test/finite/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureFinite = require("../../finite/ensure"); 5 | 6 | describe("finite/ensure", function () { 7 | it("Should return coerced value", function () { assert.equal(ensureFinite("12.23"), 12.23); }); 8 | it("Should crash on no value", function () { 9 | try { 10 | ensureFinite(null); 11 | throw new Error("Unexpected"); 12 | } catch (error) { 13 | assert.equal(error.name, "TypeError"); 14 | assert.equal(error.message, "null is not a finite number"); 15 | } 16 | }); 17 | it("Should provide alternative error message when name option is passed", function () { 18 | try { 19 | ensureFinite(null, { name: "name" }); 20 | throw new Error("Unexpected"); 21 | } catch (error) { 22 | assert.equal(error.name, "TypeError"); 23 | assert.equal(error.message, "Expected a finite number for name, received null"); 24 | } 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/function/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureFunction = require("../../function/ensure"); 5 | 6 | describe("function/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = function () { return true; }; 9 | assert.equal(ensureFunction(value), value); 10 | }); 11 | it("Should crash on invalid value", function () { 12 | try { 13 | ensureFunction(null); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert(error.message.includes("is not a function")); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureFunction(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a function for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/function/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isFunction = require("../../function/is") 5 | , arrowFunctionIfSupported = require("../_lib/arrow-function-if-supported") 6 | , classIfSupported = require("../_lib/class-if-supported"); 7 | 8 | describe("function/is", function () { 9 | it("Should return true on function", function () { 10 | assert.equal(isFunction(function () { return true; }), true); 11 | }); 12 | if (arrowFunctionIfSupported) { 13 | it("Should return true on arrow function", function () { 14 | assert.equal(isFunction(arrowFunctionIfSupported), true); 15 | }); 16 | } 17 | if (classIfSupported) { 18 | it("Should return true on class", function () { 19 | assert.equal(isFunction(classIfSupported), true); 20 | }); 21 | } 22 | it("Should return false on reg-exp", function () { assert.equal(isFunction(/foo/), false); }); 23 | 24 | it("Should return false on plain object", function () { assert.equal(isFunction({}), false); }); 25 | it("Should return false on array", function () { assert.equal(isFunction([]), false); }); 26 | if (typeof Object.create === "function") { 27 | it("Should return false on object with no prototype", function () { 28 | assert.equal(isFunction(Object.create(null)), false); 29 | }); 30 | } 31 | it("Should return false on string", function () { assert.equal(isFunction("foo"), false); }); 32 | it("Should return false on empty string", function () { assert.equal(isFunction(""), false); }); 33 | it("Should return false on number", function () { assert.equal(isFunction(123), false); }); 34 | it("Should return false on NaN", function () { assert.equal(isFunction(NaN), false); }); 35 | it("Should return false on boolean", function () { assert.equal(isFunction(true), false); }); 36 | if (typeof Symbol === "function") { 37 | it("Should return false on symbol", function () { 38 | assert.equal(isFunction(Symbol("foo")), false); 39 | }); 40 | } 41 | 42 | it("Should return false on null", function () { assert.equal(isFunction(null), false); }); 43 | it("Should return false on undefined", function () { 44 | assert.equal(isFunction(void 0), false); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /test/integer/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToInteger = require("../../integer/coerce"); 5 | 6 | describe("integer/coerce", function () { 7 | it("Should coerce float to integer", function () { 8 | assert.equal(coerceToInteger(123.123), 123); 9 | assert.equal(coerceToInteger(123.823), 123); 10 | assert.equal(coerceToInteger(-123.123), -123); 11 | assert.equal(coerceToInteger(-123.823), -123); 12 | }); 13 | it("Should coerce string", function () { assert.equal(coerceToInteger("12.123"), 12); }); 14 | it("Should coerce booleans", function () { assert.equal(coerceToInteger(true), 1); }); 15 | it("Should coerce number objects", function () { 16 | assert.equal(coerceToInteger(new Number(343)), 343); 17 | }); 18 | it("Should coerce objects", function () { 19 | assert.equal(coerceToInteger({ valueOf: function () { return 23; } }), 23); 20 | }); 21 | it("Should coerce number beyond Number.MAX_SAFE_INTEGER", function () { 22 | assert.equal(coerceToInteger(9007199254740992), 9007199254740992); 23 | }); 24 | it("Should coerce number beyond Number.MIN_SAFE_INTEGER", function () { 25 | assert.equal(coerceToInteger(-9007199254740992), -9007199254740992); 26 | }); 27 | 28 | it("Should reject infinite number", function () { 29 | assert.equal(coerceToInteger(Infinity), null); 30 | }); 31 | it("Should reject NaN", function () { assert.equal(coerceToInteger(NaN), null); }); 32 | 33 | if (typeof Object.create === "function") { 34 | it("Should not coerce objects with no number representation", function () { 35 | assert.equal(coerceToInteger(Object.create(null)), null); 36 | }); 37 | } 38 | 39 | it("Should not coerce null", function () { assert.equal(coerceToInteger(null), null); }); 40 | it("Should not coerce undefined", function () { 41 | assert.equal(coerceToInteger(undefined), null); 42 | }); 43 | 44 | if (typeof Symbol === "function") { 45 | it("Should not coerce symbols", function () { 46 | assert.equal(coerceToInteger(Symbol("foo")), null); 47 | }); 48 | } 49 | }); 50 | -------------------------------------------------------------------------------- /test/integer/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureInteger = require("../../integer/ensure"); 5 | 6 | describe("integer/ensure", function () { 7 | it("Should return coerced value", function () { assert.equal(ensureInteger("12.23"), 12); }); 8 | it("Should crash on no value", function () { 9 | try { 10 | ensureInteger(null); 11 | throw new Error("Unexpected"); 12 | } catch (error) { 13 | assert.equal(error.name, "TypeError"); 14 | assert.equal(error.message, "null is not an integer"); 15 | } 16 | }); 17 | it("Should provide alternative error message when name option is passed", function () { 18 | try { 19 | ensureInteger(null, { name: "name" }); 20 | throw new Error("Unexpected"); 21 | } catch (error) { 22 | assert.equal(error.name, "TypeError"); 23 | assert.equal(error.message, "Expected an integer for name, received null"); 24 | } 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/iterable/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureString = require("../../string/ensure") 5 | , isArray = require("../../array/is") 6 | , ensureIterable = require("../../iterable/ensure"); 7 | 8 | describe("iterable/ensure", function () { 9 | it("Should return input value", function () { 10 | var value = []; 11 | assert.equal(ensureIterable(value), value); 12 | }); 13 | it("Should allow strings with allowString option", function () { 14 | var value = "foo"; 15 | assert.equal(ensureIterable(value, { allowString: true }), value); 16 | }); 17 | it("Should crash on invalid value", function () { 18 | try { 19 | ensureIterable("foo"); 20 | throw new Error("Unexpected"); 21 | } catch (error) { 22 | assert.equal(error.name, "TypeError"); 23 | assert(error.message.includes("is not expected iterable")); 24 | } 25 | }); 26 | it("Should provide alternative error message when name option is passed", function () { 27 | try { 28 | ensureIterable("foo", { name: "name" }); 29 | throw new Error("Unexpected"); 30 | } catch (error) { 31 | assert.equal(error.name, "TypeError"); 32 | assert.equal(error.message, "Expected an iterable for name, received foo"); 33 | } 34 | }); 35 | describe("Should support 'ensureItem' option", function () { 36 | it("Should resolve coerced array", function () { 37 | var coercedValue = ensureIterable(new Set(["foo", 12]), { ensureItem: ensureString }); 38 | assert(isArray(coercedValue)); 39 | assert.deepEqual(coercedValue, ["foo", "12"]); 40 | }); 41 | it("Should crash if some item is invalid", function () { 42 | try { 43 | ensureIterable(["foo", {}], { ensureItem: ensureString }); 44 | throw new Error("Unexpected"); 45 | } catch (error) { 46 | assert.equal(error.name, "TypeError"); 47 | assert(error.message.includes("is not expected iterable")); 48 | } 49 | }); 50 | }); 51 | }); 52 | -------------------------------------------------------------------------------- /test/iterable/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isIterable = require("../../iterable/is"); 5 | 6 | describe("iterable/is", function () { 7 | it("Should return true on array", function () { assert.equal(isIterable([]), true); }); 8 | it("Should return true on arguments", function () { 9 | assert.equal(isIterable((function () { return arguments; })()), true); 10 | }); 11 | it("Should by default return false on string", function () { 12 | assert.equal(isIterable("foo"), false); 13 | }); 14 | it("Should accept strings if specified", function () { 15 | assert.equal(isIterable("foo", { allowString: true }), true); 16 | }); 17 | 18 | it("Should support denyEmpty option", function () { 19 | assert.equal(isIterable([], { denyEmpty: true }), false); 20 | assert.equal(isIterable([null], { denyEmpty: true }), true); 21 | assert.equal(isIterable("", { allowString: true, denyEmpty: true }), false); 22 | assert.equal(isIterable("foo", { allowString: true, denyEmpty: true }), true); 23 | }); 24 | 25 | if (typeof Set === "function") { 26 | it("Should return true on set", function () { assert.equal(isIterable(new Set()), true); }); 27 | } 28 | if (typeof Map === "function") { 29 | it("Should return true on set", function () { assert.equal(isIterable(new Map()), true); }); 30 | } 31 | 32 | it("Should return false on plain object", function () { assert.equal(isIterable({}), false); }); 33 | it("Should return false on function", function () { 34 | assert.equal(isIterable(function () { return true; }), false); 35 | }); 36 | 37 | if (typeof Object.create === "function") { 38 | it("Should return false on object with no prototype", function () { 39 | assert.equal(isIterable(Object.create(null)), false); 40 | }); 41 | } 42 | it("Should return false on string", function () { assert.equal(isIterable("foo"), false); }); 43 | it("Should return false on empty string", function () { assert.equal(isIterable(""), false); }); 44 | it("Should return false on number", function () { assert.equal(isIterable(123), false); }); 45 | it("Should return false on NaN", function () { assert.equal(isIterable(NaN), false); }); 46 | it("Should return false on boolean", function () { assert.equal(isIterable(true), false); }); 47 | if (typeof Symbol === "function") { 48 | it("Should return false on symbol", function () { 49 | assert.equal(isIterable(Symbol("foo")), false); 50 | }); 51 | } 52 | 53 | it("Should return false on null", function () { assert.equal(isIterable(null), false); }); 54 | it("Should return false on undefined", function () { 55 | assert.equal(isIterable(void 0), false); 56 | }); 57 | }); 58 | -------------------------------------------------------------------------------- /test/lib/is-to-string-tag-supported.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isToStringTagSupported = require("../../lib/is-to-string-tag-supported"); 5 | 6 | describe("lib/is-to-string-tag-supported", function () { 7 | it("Should return boolean", function () { 8 | assert(typeof isToStringTagSupported === "boolean"); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /test/lib/resolve-error-message.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , resolveErrorMessage = require("../../lib/resolve-error-message"); 5 | 6 | describe("lib/resolve-error-message", function () { 7 | it("Should insert value", function () { 8 | assert.equal(resolveErrorMessage("%v is invalid", 12), "12 is invalid"); 9 | assert.equal(resolveErrorMessage("Value is invalid", 12), "Value is invalid"); 10 | }); 11 | it("Should support custom error message via inputOptions.errorMessage", function () { 12 | assert.equal( 13 | resolveErrorMessage("%v is invalid", null, { errorMessage: "%v is not supported age" }), 14 | "null is not supported age" 15 | ); 16 | }); 17 | it("Should support %n (name) token", function () { 18 | assert.equal(resolveErrorMessage("%v is invalid", 12, { name: "foo" }), "12 is invalid"); 19 | assert.equal(resolveErrorMessage("%n is invalid", 12, { name: "foo" }), "foo is invalid"); 20 | assert.equal( 21 | resolveErrorMessage("%v for %n is invalid", 12, { name: "foo" }), 22 | "12 for foo is invalid" 23 | ); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /test/lib/resolve-exception.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , handleException = require("../../lib/resolve-exception"); 5 | 6 | describe("lib/handle-exception", function () { 7 | it("Should throw TypeError", function () { 8 | try { 9 | handleException(12, "Invalid value"); 10 | throw new Error("Unexpected"); 11 | } catch (error) { 12 | assert.equal(error.name, "TypeError"); 13 | assert.equal(error.message, "Invalid value"); 14 | } 15 | }); 16 | it("Should resolve value in default message", function () { 17 | try { 18 | handleException(12, "%v is invalid", {}); 19 | throw new Error("Unexpected"); 20 | } catch (error) { 21 | assert.equal(error.message, "12 is invalid"); 22 | } 23 | }); 24 | it("Should support optional values via inputOptions.isOptional", function () { 25 | assert.equal(handleException(null, "%v is invalid", { isOptional: true }, null)); 26 | }); 27 | it("Should support optional values via inputOptions.default", function () { 28 | // prettier-ignore 29 | assert.equal(handleException(null, "%v is invalid", { "default": "bar" }), "bar"); 30 | }); 31 | it("Should support custom Error constructor", function () { 32 | try { 33 | handleException(12, "Invalid value", { Error: RangeError }); 34 | throw new Error("Unexpected"); 35 | } catch (error) { 36 | assert(error instanceof RangeError); 37 | assert.equal(error.message, "Invalid value"); 38 | } 39 | }); 40 | it("Should support error code option", function () { 41 | try { 42 | handleException(12, "Invalid value", { errorCode: "SOME_CODE" }); 43 | throw new Error("Unexpected"); 44 | } catch (error) { 45 | assert.equal(error.code, "SOME_CODE"); 46 | } 47 | }); 48 | }); 49 | -------------------------------------------------------------------------------- /test/lib/safe-to-string.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , safeToString = require("../../lib/safe-to-string"); 5 | 6 | describe("lib/safe-to-string", function () { 7 | it("Should return input string", function () { assert.equal(safeToString("foo"), "foo"); }); 8 | it("Should coerce numbers", function () { assert.equal(safeToString(12), "12"); }); 9 | it("Should coerce booleans", function () { assert.equal(safeToString(true), "true"); }); 10 | it("Should coerce string objects", function () { 11 | assert.equal(safeToString(new String("bar")), "bar"); 12 | }); 13 | it("Should coerce objects", function () { 14 | assert.equal( 15 | safeToString({ toString: function () { return "Some object"; } }), "Some object" 16 | ); 17 | }); 18 | it("Should coerce null", function () { assert.equal(safeToString(null), "null"); }); 19 | it("Should coerce undefined", function () { 20 | assert.equal(safeToString(undefined), "undefined"); 21 | }); 22 | 23 | if (typeof Symbol === "function") { 24 | it("Should coerce symbols", function () { 25 | // eslint-disable-next-line no-undef 26 | assert.equal(safeToString(Symbol("test")), "Symbol(test)"); 27 | }); 28 | } 29 | it("Should return null for non coercible values", function () { 30 | assert.equal(safeToString({ toString: null }), null); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /test/lib/to-short-string.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , toShortString = require("../../lib/to-short-string"); 5 | 6 | describe("lib/to-short-string", function () { 7 | it("Should return input string", function () { assert.equal(toShortString("foo"), "foo"); }); 8 | it("Should coerce numbers", function () { assert.equal(toShortString(12), "12"); }); 9 | it("Should coerce booleans", function () { assert.equal(toShortString(true), "true"); }); 10 | it("Should coerce string objects", function () { 11 | assert.equal(toShortString(new String("bar")), "bar"); 12 | }); 13 | it("Should coerce objects", function () { 14 | assert.equal( 15 | toShortString({ toString: function () { return "Some object"; } }), "Some object" 16 | ); 17 | }); 18 | it("Should coerce null", function () { assert.equal(toShortString(null), "null"); }); 19 | it("Should coerce undefined", function () { 20 | assert.equal(toShortString(undefined), "undefined"); 21 | }); 22 | 23 | if (typeof Symbol === "function") { 24 | it("Should coerce symbols", function () { 25 | // eslint-disable-next-line no-undef 26 | assert.equal(toShortString(Symbol("test")), "Symbol(test)"); 27 | }); 28 | } 29 | it("Should return replacement non coercible values", function () { 30 | assert.equal(toShortString({ toString: null }), ""); 31 | }); 32 | 33 | it("Should replace new line characters", function () { 34 | assert.equal(toShortString("foo\n\r\u2028\u2029bar"), "foo\\n\\r\\u2028\\u2029bar"); 35 | }); 36 | it("Should truncate long string", function () { 37 | var str = Math.random().toString(36); 38 | while (str.length < 200) str += str; 39 | assert.equal(toShortString(str).length, 100); 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /test/map/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureMap = require("../../map/ensure"); 5 | 6 | describe("map/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = new Map(); 9 | assert.equal(ensureMap(value), value); 10 | }); 11 | it("Should crash on no value", function () { 12 | try { 13 | ensureMap({}); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert.equal(error.message, "[object Object] is not a map"); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureMap(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a map for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/map/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isMap = require("../../map/is"); 5 | 6 | describe("map/is", function () { 7 | if (typeof Map === "function") { 8 | it("Should return true on map", function () { assert.equal(isMap(new Map()), true); }); 9 | } 10 | it("Should return false on object that mimicks map", function () { 11 | assert.equal( 12 | isMap({ 13 | get: function () { /* noop */ }, 14 | set: function () { /* noop */ }, 15 | has: function () { /* noop */ }, 16 | clear: function () { /* noop */ } 17 | }), 18 | false 19 | ); 20 | }); 21 | 22 | it("Should return false on plain object", function () { assert.equal(isMap({}), false); }); 23 | it("Should return false on function", function () { 24 | assert.equal(isMap(function () { return true; }), false); 25 | }); 26 | it("Should return false on array", function () { assert.equal(isMap([]), false); }); 27 | if (typeof Object.create === "function") { 28 | it("Should return false on object with no prototype", function () { 29 | assert.equal(isMap(Object.create(null)), false); 30 | }); 31 | } 32 | it("Should return false on string", function () { assert.equal(isMap("foo"), false); }); 33 | it("Should return false on empty string", function () { assert.equal(isMap(""), false); }); 34 | it("Should return false on number", function () { assert.equal(isMap(123), false); }); 35 | it("Should return false on NaN", function () { assert.equal(isMap(NaN), false); }); 36 | it("Should return false on boolean", function () { assert.equal(isMap(true), false); }); 37 | if (typeof Symbol === "function") { 38 | it("Should return false on symbol", function () { 39 | assert.equal(isMap(Symbol("foo")), false); 40 | }); 41 | } 42 | if (typeof Set === "function") { 43 | it("Should return false on set", function () { assert.equal(isMap(new Set()), false); }); 44 | } 45 | 46 | it("Should return false on null", function () { assert.equal(isMap(null), false); }); 47 | it("Should return false on undefined", function () { assert.equal(isMap(void 0), false); }); 48 | }); 49 | -------------------------------------------------------------------------------- /test/natural-number/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToNaturalNumber = require("../../natural-number/coerce"); 5 | 6 | describe("natural-number/coerce", function () { 7 | it("Should coerce float to integer", function () { 8 | assert.equal(coerceToNaturalNumber(123.123), 123); 9 | assert.equal(coerceToNaturalNumber(123.823), 123); 10 | }); 11 | it("Should coerce string", function () { assert.equal(coerceToNaturalNumber("12.123"), 12); }); 12 | it("Should coerce booleans", function () { assert.equal(coerceToNaturalNumber(true), 1); }); 13 | it("Should coerce number objects", function () { 14 | assert.equal(coerceToNaturalNumber(new Number(343)), 343); 15 | }); 16 | it("Should coerce objects", function () { 17 | assert.equal(coerceToNaturalNumber({ valueOf: function () { return 23; } }), 23); 18 | }); 19 | it("Should coerce number beyond Number.MAX_SAFE_INTEGER", function () { 20 | assert.equal(coerceToNaturalNumber(9007199254740992), 9007199254740992); 21 | }); 22 | 23 | it("Should reject negative number", function () { 24 | assert.equal(coerceToNaturalNumber(-1), null); 25 | }); 26 | it("Should reject infinite number", function () { 27 | assert.equal(coerceToNaturalNumber(Infinity), null); 28 | }); 29 | it("Should reject NaN", function () { assert.equal(coerceToNaturalNumber(NaN), null); }); 30 | 31 | if (typeof Object.create === "function") { 32 | it("Should not coerce objects with no number representation", function () { 33 | assert.equal(coerceToNaturalNumber(Object.create(null)), null); 34 | }); 35 | } 36 | 37 | it("Should not coerce null", function () { assert.equal(coerceToNaturalNumber(null), null); }); 38 | it("Should not coerce undefined", function () { 39 | assert.equal(coerceToNaturalNumber(undefined), null); 40 | }); 41 | 42 | if (typeof Symbol === "function") { 43 | it("Should not coerce symbols", function () { 44 | assert.equal(coerceToNaturalNumber(Symbol("foo")), null); 45 | }); 46 | } 47 | }); 48 | -------------------------------------------------------------------------------- /test/natural-number/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureNaturalNumber = require("../../natural-number/ensure"); 5 | 6 | describe("natural-number/ensure", function () { 7 | it("Should return coerced value", function () { 8 | assert.equal(ensureNaturalNumber("12.23"), 12); 9 | }); 10 | it("Should crash on no value", function () { 11 | try { 12 | ensureNaturalNumber(-20); 13 | throw new Error("Unexpected"); 14 | } catch (error) { 15 | assert.equal(error.name, "TypeError"); 16 | assert.equal(error.message, "-20 is not a natural number"); 17 | } 18 | }); 19 | it("Should provide alternative error message when name option is passed", function () { 20 | try { 21 | ensureNaturalNumber(-20, { name: "name" }); 22 | throw new Error("Unexpected"); 23 | } catch (error) { 24 | assert.equal(error.name, "TypeError"); 25 | assert.equal(error.message, "Expected a natural number for name, received -20"); 26 | } 27 | }); 28 | it("Should support min validation", function () { 29 | try { 30 | ensureNaturalNumber(2, { min: 3 }); 31 | throw new Error("Unexpected"); 32 | } catch (error) { 33 | assert.equal(error.name, "TypeError"); 34 | assert.equal(error.message, "2 is not greater or equal 3"); 35 | } 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /test/number/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToNumber = require("../../number/coerce"); 5 | 6 | describe("number/coerce", function () { 7 | it("Should return input number", function () { 8 | assert.equal(coerceToNumber(123.123), 123.123); 9 | }); 10 | it("Should return input infinite number", function () { 11 | assert.equal(coerceToNumber(Infinity), Infinity); 12 | }); 13 | it("Should coerce string", function () { assert.equal(coerceToNumber("12"), 12); }); 14 | it("Should coerce booleans", function () { assert.equal(coerceToNumber(true), 1); }); 15 | it("Should coerce number objects", function () { 16 | assert.equal(coerceToNumber(new Number(343)), 343); 17 | }); 18 | it("Should coerce objects", function () { 19 | assert.equal(coerceToNumber({ valueOf: function () { return 23; } }), 23); 20 | }); 21 | 22 | it("Should reject NaN", function () { assert.equal(coerceToNumber(NaN), null); }); 23 | 24 | if (typeof Object.create === "function") { 25 | it("Should not coerce objects with no number representation", function () { 26 | assert.equal(coerceToNumber(Object.create(null)), null); 27 | }); 28 | } 29 | 30 | it("Should not coerce null", function () { assert.equal(coerceToNumber(null), null); }); 31 | it("Should not coerce undefined", function () { 32 | assert.equal(coerceToNumber(undefined), null); 33 | }); 34 | 35 | if (typeof Symbol === "function") { 36 | it("Should not coerce symbols", function () { 37 | assert.equal(coerceToNumber(Symbol("foo")), null); 38 | }); 39 | } 40 | }); 41 | -------------------------------------------------------------------------------- /test/number/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureNumber = require("../../number/ensure"); 5 | 6 | describe("number/ensure", function () { 7 | it("Should return coerced value", function () { assert.equal(ensureNumber("12.23"), 12.23); }); 8 | it("Should crash on no value", function () { 9 | try { 10 | ensureNumber(null); 11 | throw new Error("Unexpected"); 12 | } catch (error) { 13 | assert.equal(error.name, "TypeError"); 14 | assert.equal(error.message, "null is not a number"); 15 | } 16 | }); 17 | it("Should provide alternative error message when name option is passed", function () { 18 | try { 19 | ensureNumber(null, { name: "name" }); 20 | throw new Error("Unexpected"); 21 | } catch (error) { 22 | assert.equal(error.name, "TypeError"); 23 | assert.equal(error.message, "Expected a number for name, received null"); 24 | } 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/object/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureObject = require("../../object/ensure"); 5 | 6 | describe("object/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = {}; 9 | assert.equal(ensureObject(value), value); 10 | }); 11 | it("Should crash on no value", function () { 12 | try { 13 | ensureObject(null); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert.equal(error.message, "null is not an object"); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureObject(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected an object for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/object/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isObject = require("../../object/is"); 5 | 6 | describe("object/is", function () { 7 | it("Should return true on object", function () { assert.equal(isObject({}), true); }); 8 | it("Should return true on function", function () { 9 | assert.equal(isObject(function () { return true; }), true); 10 | }); 11 | it("Should return true on array", function () { assert.equal(isObject([]), true); }); 12 | if (typeof Object.create === "function") { 13 | it("Should return true on object with no prototype", function () { 14 | assert.equal(isObject(Object.create(null)), true); 15 | }); 16 | } 17 | it("Should return false on string", function () { assert.equal(isObject("foo"), false); }); 18 | it("Should return false on empty string", function () { assert.equal(isObject(""), false); }); 19 | it("Should return false on number", function () { assert.equal(isObject(123), false); }); 20 | it("Should return false on NaN", function () { assert.equal(isObject(NaN), false); }); 21 | it("Should return false on boolean", function () { assert.equal(isObject(true), false); }); 22 | if (typeof Symbol === "function") { 23 | it("Should return false on symbol", function () { 24 | assert.equal(isObject(Symbol("foo")), false); 25 | }); 26 | } 27 | 28 | it("Should return false on null", function () { assert.equal(isObject(null), false); }); 29 | it("Should return false on undefined", function () { assert.equal(isObject(void 0), false); }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/plain-function/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensurePlainFunction = require("../../plain-function/ensure"); 5 | 6 | describe("plain-function/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = function () { return true; }; 9 | assert.equal(ensurePlainFunction(value), value); 10 | }); 11 | it("Should crash on invalid value", function () { 12 | try { 13 | ensurePlainFunction(null); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert(error.message.includes("is not a plain function")); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensurePlainFunction(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a plain function for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/plain-function/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isPlainFunction = require("../../plain-function/is") 5 | , arrowFunctionIfSupported = require("../_lib/arrow-function-if-supported") 6 | , classIfSupported = require("../_lib/class-if-supported"); 7 | 8 | describe("plain-function/is", function () { 9 | it("Should return true on function", function () { 10 | assert.equal(isPlainFunction(function () { return true; }), true); 11 | }); 12 | if (arrowFunctionIfSupported) { 13 | it("Should return true on arrow function", function () { 14 | assert.equal(isPlainFunction(arrowFunctionIfSupported), true); 15 | }); 16 | } 17 | if (classIfSupported) { 18 | it("Should return false on class", function () { 19 | assert.equal(isPlainFunction(classIfSupported), false); 20 | }); 21 | } 22 | it("Should return false on reg-exp", function () { 23 | assert.equal(isPlainFunction(/foo/), false); 24 | }); 25 | 26 | it("Should return false on plain object", function () { 27 | assert.equal(isPlainFunction({}), false); 28 | }); 29 | it("Should return false on array", function () { assert.equal(isPlainFunction([]), false); }); 30 | if (typeof Object.create === "function") { 31 | it("Should return false on object with no prototype", function () { 32 | assert.equal(isPlainFunction(Object.create(null)), false); 33 | }); 34 | } 35 | it("Should return false on string", function () { 36 | assert.equal(isPlainFunction("foo"), false); 37 | }); 38 | it("Should return false on empty string", function () { 39 | assert.equal(isPlainFunction(""), false); 40 | }); 41 | it("Should return false on number", function () { assert.equal(isPlainFunction(123), false); }); 42 | it("Should return false on NaN", function () { assert.equal(isPlainFunction(NaN), false); }); 43 | it("Should return false on boolean", function () { 44 | assert.equal(isPlainFunction(true), false); 45 | }); 46 | if (typeof Symbol === "function") { 47 | it("Should return false on symbol", function () { 48 | assert.equal(isPlainFunction(Symbol("foo")), false); 49 | }); 50 | } 51 | 52 | it("Should return false on null", function () { assert.equal(isPlainFunction(null), false); }); 53 | it("Should return false on undefined", function () { 54 | assert.equal(isPlainFunction(void 0), false); 55 | }); 56 | }); 57 | -------------------------------------------------------------------------------- /test/plain-object/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureString = require("../../string/ensure") 5 | , ensurePlainObject = require("../../plain-object/ensure"); 6 | 7 | describe("plain-object/ensure", function () { 8 | it("Should return input value", function () { 9 | var value = {}; 10 | assert.equal(ensurePlainObject(value), value); 11 | }); 12 | it("Should crash on invalid value", function () { 13 | try { 14 | ensurePlainObject(null); 15 | throw new Error("Unexpected"); 16 | } catch (error) { 17 | assert.equal(error.name, "TypeError"); 18 | assert.equal(error.message, "null is not a plain object"); 19 | } 20 | }); 21 | it("Should provide alternative error message when name option is passed", function () { 22 | try { 23 | ensurePlainObject(null, { name: "name" }); 24 | throw new Error("Unexpected"); 25 | } catch (error) { 26 | assert.equal(error.name, "TypeError"); 27 | assert.equal(error.message, "Expected a plain object for name, received null"); 28 | } 29 | }); 30 | it("Should support allowedKeys option", function () { 31 | var value = { foo: "bar", marko: "elo" }; 32 | assert.equal(ensurePlainObject(value, { allowedKeys: ["foo", "marko"] }), value); 33 | try { 34 | ensurePlainObject(value, { allowedKeys: ["marko"] }); 35 | throw new Error("Unexpected"); 36 | } catch (error) { 37 | assert.equal(error.name, "TypeError"); 38 | assert.equal(error.message.indexOf("is not a plain object") !== -1, true); 39 | } 40 | }); 41 | 42 | it("Should support ensurePropertyValue option", function () { 43 | assert.deepEqual( 44 | ensurePlainObject({ foo: "bar", marko: 12 }, { ensurePropertyValue: ensureString }), 45 | { foo: "bar", marko: "12" } 46 | ); 47 | try { 48 | ensurePlainObject({ foo: "bar", marko: {} }, { ensurePropertyValue: ensureString }); 49 | throw new Error("Unexpected"); 50 | } catch (error) { 51 | assert.equal(error.name, "TypeError"); 52 | assert.equal(error.message.indexOf("is not a plain object") !== -1, true); 53 | } 54 | }); 55 | }); 56 | -------------------------------------------------------------------------------- /test/plain-object/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isPlainObject = require("../../plain-object/is"); 5 | 6 | describe("plain-object/is", function () { 7 | it("Should return true on plain object", function () { 8 | assert.equal(isPlainObject({}), true); 9 | }); 10 | if (typeof Object.create === "function") { 11 | it("Should return true on object with no prototype", function () { 12 | assert.equal(isPlainObject(Object.create(null)), true); 13 | }); 14 | it("Should return false on object that inherits from object with no prototypes", function () { 15 | assert.equal(isPlainObject(Object.create(Object.create(null))), false); 16 | }); 17 | } 18 | it("Should return false on Object.prototype", function () { 19 | assert.equal(isPlainObject(Object.prototype), false); 20 | }); 21 | it("Should return false on prototype that derives from Object.prototype", function () { 22 | assert.equal(isPlainObject(RegExp.prototype), false); 23 | }); 24 | 25 | it("Should return false on function", function () { 26 | assert.equal(isPlainObject(function () { return true; }), false); 27 | }); 28 | 29 | it("Should return false on string", function () { assert.equal(isPlainObject("foo"), false); }); 30 | it("Should return false on empty string", function () { 31 | assert.equal(isPlainObject(""), false); 32 | }); 33 | it("Should return false on number", function () { assert.equal(isPlainObject(123), false); }); 34 | it("Should return false on NaN", function () { assert.equal(isPlainObject(NaN), false); }); 35 | it("Should return false on boolean", function () { assert.equal(isPlainObject(true), false); }); 36 | if (typeof Symbol === "function") { 37 | it("Should return false on symbol", function () { 38 | assert.equal(isPlainObject(Symbol("foo")), false); 39 | }); 40 | } 41 | 42 | it("Should return false on null", function () { assert.equal(isPlainObject(null), false); }); 43 | it("Should return false on undefined", function () { 44 | assert.equal(isPlainObject(void 0), false); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /test/promise/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensurePromise = require("../../promise/ensure"); 5 | 6 | describe("promise/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = Promise.resolve(); 9 | assert.equal(ensurePromise(value), value); 10 | }); 11 | it("Should crash on no value", function () { 12 | try { 13 | ensurePromise({}); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert.equal(error.message, "[object Object] is not a promise"); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensurePromise(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a promise for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/promise/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isPromise = require("../../promise/is"); 5 | 6 | describe("promise/is", function () { 7 | if (typeof Promise === "function") { 8 | it("Should return true on promise", function () { 9 | assert.equal(isPromise(Promise.resolve()), true); 10 | }); 11 | } 12 | it("Should return false on non-promise thenable", function () { 13 | assert.equal(isPromise({ then: function () { return true; } }), false); 14 | }); 15 | 16 | it("Should return false on plain object", function () { assert.equal(isPromise({}), false); }); 17 | it("Should return false on function", function () { 18 | assert.equal(isPromise(function () { return true; }), false); 19 | }); 20 | it("Should return false on array", function () { assert.equal(isPromise([]), false); }); 21 | if (typeof Object.create === "function") { 22 | it("Should return false on object with no prototype", function () { 23 | assert.equal(isPromise(Object.create(null)), false); 24 | }); 25 | } 26 | it("Should return false on string", function () { assert.equal(isPromise("foo"), false); }); 27 | it("Should return false on empty string", function () { assert.equal(isPromise(""), false); }); 28 | it("Should return false on number", function () { assert.equal(isPromise(123), false); }); 29 | it("Should return false on NaN", function () { assert.equal(isPromise(NaN), false); }); 30 | it("Should return false on boolean", function () { assert.equal(isPromise(true), false); }); 31 | if (typeof Symbol === "function") { 32 | it("Should return false on symbol", function () { 33 | assert.equal(isPromise(Symbol("foo")), false); 34 | }); 35 | } 36 | 37 | it("Should return false on null", function () { assert.equal(isPromise(null), false); }); 38 | it("Should return false on undefined", function () { assert.equal(isPromise(void 0), false); }); 39 | }); 40 | -------------------------------------------------------------------------------- /test/prototype/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isPrototype = require("../../prototype/is"); 5 | 6 | describe("prototype/is", function () { 7 | it("Should return true on prototype", function () { 8 | assert.equal(isPrototype(Object.prototype), true); 9 | }); 10 | it("Should return false on plain object", function () { 11 | assert.equal(isPrototype({}), false); 12 | }); 13 | it("Should return false on function", function () { 14 | assert.equal(isPrototype(function () { return true; }), false); 15 | }); 16 | it("Should return false on array", function () { assert.equal(isPrototype([]), false); }); 17 | if (typeof Object.create === "function") { 18 | it("Should return false on object with no prototype", function () { 19 | assert.equal(isPrototype(Object.create(null)), false); 20 | }); 21 | } 22 | it("Should return false on string", function () { assert.equal(isPrototype("foo"), false); }); 23 | it("Should return false on empty string", function () { 24 | assert.equal(isPrototype(""), false); 25 | }); 26 | it("Should return false on number", function () { assert.equal(isPrototype(123), false); }); 27 | it("Should return false on NaN", function () { assert.equal(isPrototype(NaN), false); }); 28 | it("Should return false on boolean", function () { assert.equal(isPrototype(true), false); }); 29 | if (typeof Symbol === "function") { 30 | it("Should return false on symbol", function () { 31 | assert.equal(isPrototype(Symbol("foo")), false); 32 | }); 33 | } 34 | 35 | it("Should return false on null", function () { assert.equal(isPrototype(null), false); }); 36 | it("Should return false on undefined", function () { 37 | assert.equal(isPrototype(void 0), false); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /test/reg-exp/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureRegExp = require("../../reg-exp/ensure"); 5 | 6 | describe("reg-exp/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = /foo/; 9 | assert.equal(ensureRegExp(value), value); 10 | }); 11 | it("Should crash on invalid value", function () { 12 | try { 13 | ensureRegExp(null); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert(error.message.includes("is not a regular expression")); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureRegExp(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a regular expression for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/reg-exp/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isRegExp = require("../../reg-exp/is"); 5 | 6 | describe("reg-exp/is", function () { 7 | it("Should return true on regular expression", function () { 8 | assert.equal(isRegExp(/foo/), true); 9 | }); 10 | 11 | it("Should return false on native regular expression with no common API exposed", function () { 12 | var re = /foo/; 13 | re.test = null; 14 | assert.equal(isRegExp(re), false); 15 | }); 16 | it("Should return false on RegExp.prototype", function () { 17 | assert.equal(isRegExp(RegExp.prototype), false); 18 | }); 19 | it("Should return false on regexp like string", function () { 20 | assert.equal(isRegExp("/foo/"), false); 21 | }); 22 | 23 | it("Should return false on plain object", function () { assert.equal(isRegExp({}), false); }); 24 | it("Should return false on function", function () { 25 | assert.equal(isRegExp(function () { return true; }), false); 26 | }); 27 | 28 | it("Should return false on array", function () { assert.equal(isRegExp([]), false); }); 29 | if (typeof Object.create === "function") { 30 | it("Should return false on object with no prototype", function () { 31 | assert.equal(isRegExp(Object.create(null)), false); 32 | }); 33 | } 34 | it("Should return false on string", function () { assert.equal(isRegExp("foo"), false); }); 35 | it("Should return false on empty string", function () { assert.equal(isRegExp(""), false); }); 36 | it("Should return false on number", function () { assert.equal(isRegExp(123), false); }); 37 | it("Should return false on NaN", function () { assert.equal(isRegExp(NaN), false); }); 38 | it("Should return false on boolean", function () { assert.equal(isRegExp(true), false); }); 39 | if (typeof Symbol === "function") { 40 | it("Should return false on symbol", function () { 41 | assert.equal(isRegExp(Symbol("foo")), false); 42 | }); 43 | } 44 | 45 | it("Should return false on null", function () { assert.equal(isRegExp(null), false); }); 46 | it("Should return false on undefined", function () { assert.equal(isRegExp(void 0), false); }); 47 | }); 48 | -------------------------------------------------------------------------------- /test/safe-integer/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToSafeInteger = require("../../safe-integer/coerce"); 5 | 6 | describe("safe-integer/coerce", function () { 7 | it("Should coerce float to integer", function () { 8 | assert.equal(coerceToSafeInteger(123.123), 123); 9 | assert.equal(coerceToSafeInteger(123.823), 123); 10 | assert.equal(coerceToSafeInteger(-123.123), -123); 11 | assert.equal(coerceToSafeInteger(-123.823), -123); 12 | }); 13 | it("Should coerce string", function () { assert.equal(coerceToSafeInteger("12.123"), 12); }); 14 | it("Should coerce booleans", function () { assert.equal(coerceToSafeInteger(true), 1); }); 15 | it("Should coerce number objects", function () { 16 | assert.equal(coerceToSafeInteger(new Number(343)), 343); 17 | }); 18 | it("Should coerce objects", function () { 19 | assert.equal(coerceToSafeInteger({ valueOf: function () { return 23; } }), 23); 20 | }); 21 | it("Should reject infinite number", function () { 22 | assert.equal(coerceToSafeInteger(Infinity), null); 23 | }); 24 | it("Should reject number beyond Number.MAX_SAFE_INTEGER", function () { 25 | assert.equal(coerceToSafeInteger(9007199254740992), null); 26 | }); 27 | it("Should reject number beyond Number.MIN_SAFE_INTEGER", function () { 28 | assert.equal(coerceToSafeInteger(-9007199254740992), null); 29 | }); 30 | 31 | it("Should reject NaN", function () { assert.equal(coerceToSafeInteger(NaN), null); }); 32 | 33 | if (typeof Object.create === "function") { 34 | it("Should not coerce objects with no number representation", function () { 35 | assert.equal(coerceToSafeInteger(Object.create(null)), null); 36 | }); 37 | } 38 | 39 | it("Should not coerce null", function () { assert.equal(coerceToSafeInteger(null), null); }); 40 | it("Should not coerce undefined", function () { 41 | assert.equal(coerceToSafeInteger(undefined), null); 42 | }); 43 | 44 | if (typeof Symbol === "function") { 45 | it("Should not coerce symbols", function () { 46 | assert.equal(coerceToSafeInteger(Symbol("foo")), null); 47 | }); 48 | } 49 | }); 50 | -------------------------------------------------------------------------------- /test/safe-integer/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureSafeInteger = require("../../safe-integer/ensure"); 5 | 6 | describe("safe-integer/ensure", function () { 7 | it("Should return coerced value", function () { 8 | assert.equal(ensureSafeInteger("12.23"), 12); 9 | }); 10 | it("Should crash on no value", function () { 11 | try { 12 | ensureSafeInteger(null); 13 | throw new Error("Unexpected"); 14 | } catch (error) { 15 | assert.equal(error.name, "TypeError"); 16 | assert.equal(error.message, "null is not a safe integer"); 17 | } 18 | }); 19 | it("Should provide alternative error message when name option is passed", function () { 20 | try { 21 | ensureSafeInteger(null, { name: "name" }); 22 | throw new Error("Unexpected"); 23 | } catch (error) { 24 | assert.equal(error.name, "TypeError"); 25 | assert.equal(error.message, "Expected a safe integer for name, received null"); 26 | } 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /test/set/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureSet = require("../../set/ensure"); 5 | 6 | describe("set/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = new Set(); 9 | assert.equal(ensureSet(value), value); 10 | }); 11 | it("Should crash on no value", function () { 12 | try { 13 | ensureSet({}); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert.equal(error.message, "[object Object] is not a set"); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureSet(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a set for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/set/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isSet = require("../../set/is"); 5 | 6 | describe("set/is", function () { 7 | if (typeof Set === "function") { 8 | it("Should return true on set", function () { assert.equal(isSet(new Set()), true); }); 9 | } 10 | it("Should return false on object that mimicks map", function () { 11 | assert.equal( 12 | isSet({ 13 | add: function () { /* noop */ }, 14 | has: function () { /* noop */ }, 15 | clear: function () { /* noop */ } 16 | }), 17 | false 18 | ); 19 | }); 20 | 21 | it("Should return false on plain object", function () { assert.equal(isSet({}), false); }); 22 | it("Should return false on function", function () { 23 | assert.equal(isSet(function () { return true; }), false); 24 | }); 25 | it("Should return false on array", function () { assert.equal(isSet([]), false); }); 26 | if (typeof Object.create === "function") { 27 | it("Should return false on object with no prototype", function () { 28 | assert.equal(isSet(Object.create(null)), false); 29 | }); 30 | } 31 | it("Should return false on string", function () { assert.equal(isSet("foo"), false); }); 32 | it("Should return false on empty string", function () { assert.equal(isSet(""), false); }); 33 | it("Should return false on number", function () { assert.equal(isSet(123), false); }); 34 | it("Should return false on NaN", function () { assert.equal(isSet(NaN), false); }); 35 | it("Should return false on boolean", function () { assert.equal(isSet(true), false); }); 36 | if (typeof Symbol === "function") { 37 | it("Should return false on symbol", function () { 38 | assert.equal(isSet(Symbol("foo")), false); 39 | }); 40 | } 41 | if (typeof Map === "function") { 42 | it("Should return false on map", function () { assert.equal(isSet(new Map()), false); }); 43 | } 44 | 45 | it("Should return false on null", function () { assert.equal(isSet(null), false); }); 46 | it("Should return false on undefined", function () { assert.equal(isSet(void 0), false); }); 47 | }); 48 | -------------------------------------------------------------------------------- /test/string/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToString = require("../../string/coerce"); 5 | 6 | describe("string/coerce", function () { 7 | it("Should return input string", function () { assert.equal(coerceToString("foo"), "foo"); }); 8 | it("Should coerce numbers", function () { assert.equal(coerceToString(12), "12"); }); 9 | it("Should coerce booleans", function () { assert.equal(coerceToString(true), "true"); }); 10 | it("Should coerce string objects", function () { 11 | assert.equal(coerceToString(new String("bar")), "bar"); 12 | }); 13 | it("Should coerce objects", function () { 14 | assert.equal( 15 | coerceToString({ toString: function () { return "Some object"; } }), "Some object" 16 | ); 17 | }); 18 | if (typeof Object.create === "function") { 19 | it("Should not coerce objects with no toString", function () { 20 | assert.equal(coerceToString(Object.create(null)), null); 21 | }); 22 | } 23 | it("Should not coerce objects with no custom toString", function () { 24 | assert.equal(coerceToString({}), null); 25 | }); 26 | it("Should not coerce null", function () { assert.equal(coerceToString(null), null); }); 27 | it("Should not coerce undefined", function () { 28 | assert.equal(coerceToString(undefined), null); 29 | }); 30 | 31 | if (typeof Symbol === "function") { 32 | it("Should not coerce symbols", function () { 33 | assert.equal(coerceToString(Symbol("foo")), null); 34 | }); 35 | } 36 | }); 37 | -------------------------------------------------------------------------------- /test/string/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureString = require("../../string/ensure"); 5 | 6 | describe("string/ensure", function () { 7 | it("Should return coerced value", function () { assert.equal(ensureString(12), "12"); }); 8 | it("Should crash on no value", function () { 9 | try { 10 | ensureString(null); 11 | throw new Error("Unexpected"); 12 | } catch (error) { 13 | assert.equal(error.name, "TypeError"); 14 | assert.equal(error.message, "null is not a string"); 15 | } 16 | }); 17 | it("Should provide alternative error message when name option is passed", function () { 18 | try { 19 | ensureString(null, { name: "name" }); 20 | throw new Error("Unexpected"); 21 | } catch (error) { 22 | assert.equal(error.name, "TypeError"); 23 | assert.equal(error.message, "Expected a string for name, received null"); 24 | } 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/thenable/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureThenable = require("../../thenable/ensure"); 5 | 6 | describe("thenable/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = { then: function () { return true; } }; 9 | assert.equal(ensureThenable(value), value); 10 | }); 11 | it("Should crash on no value", function () { 12 | try { 13 | ensureThenable({}); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert.equal(error.message, "[object Object] is not a thenable"); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureThenable({}, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a thenable for name, received [object Object]"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/thenable/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isThenable = require("../../thenable/is"); 5 | 6 | describe("thenable/is", function () { 7 | it("Should return true on object with `then` method", function () { 8 | assert.equal(isThenable({ then: function () { return true; } }), true); 9 | }); 10 | if (typeof Promise === "function") { 11 | it("Should return true on promise", function () { 12 | assert.equal(isThenable(Promise.resolve()), true); 13 | }); 14 | } 15 | it("Should return false on object with `then` non callable property", function () { 16 | assert.equal(isThenable({ then: {} }), false); 17 | }); 18 | 19 | it("Should return false on plain object", function () { assert.equal(isThenable({}), false); }); 20 | it("Should return false on function", function () { 21 | assert.equal(isThenable(function () { return true; }), false); 22 | }); 23 | it("Should return false on array", function () { assert.equal(isThenable([]), false); }); 24 | if (typeof Object.create === "function") { 25 | it("Should return false on object with no prototype", function () { 26 | assert.equal(isThenable(Object.create(null)), false); 27 | }); 28 | } 29 | it("Should return false on string", function () { assert.equal(isThenable("foo"), false); }); 30 | it("Should return false on empty string", function () { assert.equal(isThenable(""), false); }); 31 | it("Should return false on number", function () { assert.equal(isThenable(123), false); }); 32 | it("Should return false on NaN", function () { assert.equal(isThenable(NaN), false); }); 33 | it("Should return false on boolean", function () { assert.equal(isThenable(true), false); }); 34 | if (typeof Symbol === "function") { 35 | it("Should return false on symbol", function () { 36 | assert.equal(isThenable(Symbol("foo")), false); 37 | }); 38 | } 39 | 40 | it("Should return false on null", function () { assert.equal(isThenable(null), false); }); 41 | it("Should return false on undefined", function () { 42 | assert.equal(isThenable(void 0), false); 43 | }); 44 | }); 45 | -------------------------------------------------------------------------------- /test/time-value/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , coerceToTimeValue = require("../../time-value/coerce"); 5 | 6 | describe("time-value/coerce", function () { 7 | it("Should coerce float to time value", function () { 8 | assert.equal(coerceToTimeValue(123.123), 123); 9 | assert.equal(coerceToTimeValue(123.823), 123); 10 | assert.equal(coerceToTimeValue(-123.123), -123); 11 | assert.equal(coerceToTimeValue(-123.823), -123); 12 | }); 13 | it("Should coerce string", function () { assert.equal(coerceToTimeValue("12.123"), 12); }); 14 | it("Should coerce booleans", function () { assert.equal(coerceToTimeValue(true), 1); }); 15 | it("Should coerce number objects", function () { 16 | assert.equal(coerceToTimeValue(new Number(343)), 343); 17 | }); 18 | it("Should coerce objects", function () { 19 | assert.equal(coerceToTimeValue({ valueOf: function () { return 23; } }), 23); 20 | }); 21 | it("Should reject infinite number", function () { 22 | assert.equal(coerceToTimeValue(Infinity), null); 23 | }); 24 | it("Should reject number beyond 100,000,000 days from unix epox", function () { 25 | assert.equal(coerceToTimeValue(8.64e15 + 1), null); 26 | assert.equal(coerceToTimeValue(-8.64e15 - 1), null); 27 | }); 28 | 29 | it("Should reject NaN", function () { assert.equal(coerceToTimeValue(NaN), null); }); 30 | 31 | if (typeof Object.create === "function") { 32 | it("Should not coerce objects with no number representation", function () { 33 | assert.equal(coerceToTimeValue(Object.create(null)), null); 34 | }); 35 | } 36 | 37 | it("Should not coerce null", function () { assert.equal(coerceToTimeValue(null), null); }); 38 | it("Should not coerce undefined", function () { 39 | assert.equal(coerceToTimeValue(undefined), null); 40 | }); 41 | 42 | if (typeof Symbol === "function") { 43 | it("Should not coerce symbols", function () { 44 | assert.equal(coerceToTimeValue(Symbol("foo")), null); 45 | }); 46 | } 47 | }); 48 | -------------------------------------------------------------------------------- /test/time-value/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureTimeValue = require("../../time-value/ensure"); 5 | 6 | describe("time-value/ensure", function () { 7 | it("Should return coerced value", function () { assert.equal(ensureTimeValue("12.23"), 12); }); 8 | it("Should crash on no value", function () { 9 | try { 10 | ensureTimeValue("foo"); 11 | throw new Error("Unexpected"); 12 | } catch (error) { 13 | assert.equal(error.name, "TypeError"); 14 | assert.equal(error.message, "foo is not a time value"); 15 | } 16 | }); 17 | it("Should provide alternative error message when name option is passed", function () { 18 | try { 19 | ensureTimeValue("foo", { name: "name" }); 20 | throw new Error("Unexpected"); 21 | } catch (error) { 22 | assert.equal(error.name, "TypeError"); 23 | assert.equal(error.message, "Expected a time value for name, received foo"); 24 | } 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/value/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , ensureValue = require("../../value/ensure"); 5 | 6 | describe("value/ensure", function () { 7 | it("Should return input value", function () { 8 | var value = {}; 9 | assert.equal(ensureValue(value), value); 10 | }); 11 | it("Should crash on no value", function () { 12 | try { 13 | ensureValue(null); 14 | throw new Error("Unexpected"); 15 | } catch (error) { 16 | assert.equal(error.name, "TypeError"); 17 | assert.equal(error.message, "Cannot use null"); 18 | } 19 | }); 20 | it("Should provide alternative error message when name option is passed", function () { 21 | try { 22 | ensureValue(null, { name: "name" }); 23 | throw new Error("Unexpected"); 24 | } catch (error) { 25 | assert.equal(error.name, "TypeError"); 26 | assert.equal(error.message, "Expected a value for name, received null"); 27 | } 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /test/value/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var assert = require("chai").assert 4 | , isValue = require("../../value/is"); 5 | 6 | describe("value/is", function () { 7 | it("Should return true on object", function () { assert.equal(isValue({}), true); }); 8 | it("Should return true on function", function () { 9 | assert.equal(isValue(function () { return true; }), true); 10 | }); 11 | it("Should return true on array", function () { assert.equal(isValue([]), true); }); 12 | if (typeof Object.create === "function") { 13 | it("Should return true on object with no prototype", function () { 14 | assert.equal(isValue(Object.create(null)), true); 15 | }); 16 | } 17 | it("Should return true on string", function () { assert.equal(isValue("foo"), true); }); 18 | it("Should return true on empty string", function () { assert.equal(isValue(""), true); }); 19 | it("Should return true on number", function () { assert.equal(isValue(123), true); }); 20 | it("Should return true on NaN", function () { assert.equal(isValue(NaN), true); }); 21 | it("Should return true on boolean", function () { assert.equal(isValue(false), true); }); 22 | if (typeof Symbol === "function") { 23 | // eslint-disable-next-line no-undef 24 | it("Should return true on symbol", function () { 25 | assert.equal(isValue(Symbol("test")), true); 26 | }); 27 | } 28 | 29 | it("Should return false on null", function () { assert.equal(isValue(null), false); }); 30 | it("Should return false on undefined", function () { assert.equal(isValue(void 0), false); }); 31 | }); 32 | -------------------------------------------------------------------------------- /thenable/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name 11 | ? "Expected a thenable for %n, received %v" 12 | : "%v is not a thenable"; 13 | return resolveException(value, errorMessage, options); 14 | }; 15 | -------------------------------------------------------------------------------- /thenable/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var isObject = require("../object/is"); 4 | 5 | module.exports = function (value) { 6 | if (!isObject(value)) return false; 7 | try { return typeof value.then === "function"; } 8 | catch (error) { return false; } 9 | }; 10 | -------------------------------------------------------------------------------- /time-value/coerce.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var coerceToInteger = require("../integer/coerce"); 4 | 5 | var abs = Math.abs; 6 | 7 | module.exports = function (value) { 8 | value = coerceToInteger(value); 9 | if (!value) return value; 10 | if (abs(value) > 8.64e15) return null; 11 | return value; 12 | }; 13 | -------------------------------------------------------------------------------- /time-value/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , coerce = require("./coerce"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | var coerced = coerce(value); 8 | if (coerced !== null) return coerced; 9 | var options = arguments[1]; 10 | var errorMessage = 11 | options && options.name 12 | ? "Expected a time value for %n, received %v" 13 | : "%v is not a time value"; 14 | return resolveException(value, errorMessage, options); 15 | }; 16 | -------------------------------------------------------------------------------- /ts-types/array-length/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToArrayLength(value: any): number | null; 2 | export default coerceToArrayLength; 3 | -------------------------------------------------------------------------------- /ts-types/array-length/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureArrayLength(value: any, options?: EnsureBaseOptions): number; 4 | declare function ensureArrayLength(value: any, options?: EnsureBaseOptions & EnsureIsOptional): number | null; 5 | declare function ensureArrayLength(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): number; 6 | 7 | export default ensureArrayLength; 8 | -------------------------------------------------------------------------------- /ts-types/array-like/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | type LengthwiseObject = { length: number } & object; 4 | type ArrayLikeEnsureOptions = { allowString?: boolean }; 5 | 6 | 7 | declare function ensureArrayLike(value: any, options?: ArrayLikeEnsureOptions & EnsureBaseOptions): T[] | string | LengthwiseObject; 8 | declare function ensureArrayLike(value: any, options?: ArrayLikeEnsureOptions & EnsureBaseOptions & EnsureIsOptional): T[] | string | LengthwiseObject | null; 9 | declare function ensureArrayLike(value: any, options?: ArrayLikeEnsureOptions & EnsureBaseOptions & EnsureIsOptional & EnsureDefault): T[] | string | LengthwiseObject; 10 | 11 | export default ensureArrayLike; 12 | -------------------------------------------------------------------------------- /ts-types/array-like/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isArrayLike(value: any, options?: {allowString?: boolean}): boolean; 2 | export default isArrayLike; 3 | -------------------------------------------------------------------------------- /ts-types/array/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureFunction, EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | type EnsureArrayOptions = { ensureItem?: EnsureFunction }; 4 | 5 | 6 | declare function ensureArray(value: any, options?: EnsureArrayOptions & EnsureBaseOptions): T[]; 7 | declare function ensureArray(value: any, options?: EnsureArrayOptions & EnsureBaseOptions & EnsureIsOptional): T[] | null; 8 | declare function ensureArray(value: any, options?: EnsureArrayOptions & EnsureBaseOptions & EnsureIsOptional & EnsureDefault): T[]; 9 | 10 | export default ensureArray; 11 | -------------------------------------------------------------------------------- /ts-types/array/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isArray(value: any): boolean; 2 | export default isArray; 3 | -------------------------------------------------------------------------------- /ts-types/big-int/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToBigInt(value: any): bigint | null; 2 | export default coerceToBigInt; 3 | -------------------------------------------------------------------------------- /ts-types/big-int/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureBigInt(value: any, options?: EnsureBaseOptions): bigint; 4 | declare function ensureBigInt(value: any, options?: EnsureBaseOptions & EnsureIsOptional): bigint | null; 5 | declare function ensureBigInt(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): bigint; 6 | 7 | export default ensureBigInt; 8 | -------------------------------------------------------------------------------- /ts-types/constructor/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureFunction, EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureConstructor(value: any, options?: EnsureBaseOptions): EnsureFunction | object; 4 | declare function ensureConstructor(value: any, options?: EnsureBaseOptions & EnsureIsOptional): EnsureFunction | object | null; 5 | declare function ensureConstructor(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): EnsureFunction | object; 6 | 7 | export default ensureConstructor; 8 | -------------------------------------------------------------------------------- /ts-types/constructor/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isConstructor(value: any): boolean; 2 | export default isConstructor; 3 | -------------------------------------------------------------------------------- /ts-types/date/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureDate(value: any, options?: EnsureBaseOptions): Date; 4 | declare function ensureDate(value: any, options?: EnsureBaseOptions & EnsureIsOptional): Date | null; 5 | declare function ensureDate(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): Date; 6 | 7 | export default ensureDate; 8 | -------------------------------------------------------------------------------- /ts-types/date/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isDate(value: any): boolean; 2 | export default isDate; 3 | -------------------------------------------------------------------------------- /ts-types/ensure.d.ts: -------------------------------------------------------------------------------- 1 | export type EnsureFunction = (...args: any[]) => any; 2 | export interface EnsureBaseOptions { 3 | name?: string; 4 | errorMessage?: string; 5 | errorCode?: number; 6 | Error?: ErrorConstructor; 7 | } 8 | 9 | export interface EnsureIsOptional { 10 | isOptional: boolean; 11 | } 12 | 13 | export interface EnsureDefault { 14 | default: T; 15 | } 16 | 17 | type EnsureOptions = EnsureBaseOptions & { isOptional?: boolean } & { default?: any }; 18 | 19 | type ValidationDatum = [argumentName: string, inputValue: any, ensureFunction: EnsureFunction, options?: object]; 20 | type ValidationDatumList = ValidationDatum[]; 21 | 22 | declare function ensure(...args: [...ValidationDatumList, EnsureOptions]): T; 23 | declare function ensure(...args: [...ValidationDatumList]): T; 24 | export default ensure; 25 | -------------------------------------------------------------------------------- /ts-types/error/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureError(value: any, options?: EnsureBaseOptions): Error; 4 | declare function ensureError(value: any, options?: EnsureBaseOptions & EnsureIsOptional): Error | null; 5 | declare function ensureError(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): Error; 6 | 7 | export default ensureError; 8 | -------------------------------------------------------------------------------- /ts-types/error/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isError(value: any): boolean; 2 | export default isError; 3 | -------------------------------------------------------------------------------- /ts-types/finite/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToFinite(value: any): number | null; 2 | export default coerceToFinite; 3 | -------------------------------------------------------------------------------- /ts-types/finite/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureFinite(value: any, options?: EnsureBaseOptions): number; 4 | declare function ensureFinite(value: any, options?: EnsureBaseOptions & EnsureIsOptional): number | null; 5 | declare function ensureFinite(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): number; 6 | 7 | export default ensureFinite; 8 | -------------------------------------------------------------------------------- /ts-types/function/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureFunction, EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureFunction(value: any, options?: EnsureBaseOptions): EnsureFunction; 4 | declare function ensureFunction(value: any, options?: EnsureBaseOptions & EnsureIsOptional): EnsureFunction | null; 5 | declare function ensureFunction(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): EnsureFunction; 6 | 7 | export default ensureFunction; 8 | -------------------------------------------------------------------------------- /ts-types/function/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isFunction(value: any): boolean; 2 | export default isFunction; 3 | -------------------------------------------------------------------------------- /ts-types/integer/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToInteger(value: any): number | null; 2 | export default coerceToInteger; 3 | -------------------------------------------------------------------------------- /ts-types/integer/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureInteger(value: any, options?: EnsureBaseOptions): number; 4 | declare function ensureInteger(value: any, options?: EnsureBaseOptions & EnsureIsOptional): number | null; 5 | declare function ensureInteger(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): number; 6 | 7 | export default ensureInteger; 8 | -------------------------------------------------------------------------------- /ts-types/iterable/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureFunction, EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | type IterableEnsureOptions = { ensureItem?: EnsureFunction, allowString?: boolean, denyEmpty?: boolean}; 4 | 5 | 6 | declare function ensureIterable(value: any, options?: IterableEnsureOptions & EnsureBaseOptions): T[]; 7 | declare function ensureIterable(value: any, options?: IterableEnsureOptions & EnsureBaseOptions & EnsureIsOptional): T[] | null; 8 | declare function ensureIterable(value: any, options?: IterableEnsureOptions & EnsureBaseOptions & EnsureIsOptional & EnsureDefault): T[]; 9 | 10 | export default ensureIterable; 11 | -------------------------------------------------------------------------------- /ts-types/iterable/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isIterable(value: any, options?: { allowString?: boolean, denyEmpty?: boolean }): boolean; 2 | export default isIterable; 3 | -------------------------------------------------------------------------------- /ts-types/map/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureMap(value: any, options?: EnsureBaseOptions): Map; 4 | declare function ensureMap(value: any, options?: EnsureBaseOptions & EnsureIsOptional): Map | null; 5 | declare function ensureMap(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault>): Map; 6 | 7 | export default ensureMap; 8 | -------------------------------------------------------------------------------- /ts-types/map/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isMap(value: any): boolean; 2 | export default isMap; 3 | -------------------------------------------------------------------------------- /ts-types/natural-number/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToNaturalNumber(value: any): number | null; 2 | export default coerceToNaturalNumber; 3 | -------------------------------------------------------------------------------- /ts-types/natural-number/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureNaturalNumber(value: any, options?: EnsureBaseOptions): number; 4 | declare function ensureNaturalNumber(value: any, options?: EnsureBaseOptions & EnsureIsOptional): number | null; 5 | declare function ensureNaturalNumber(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): number; 6 | 7 | export default ensureNaturalNumber; 8 | -------------------------------------------------------------------------------- /ts-types/number/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToNumber(value: any): number | null; 2 | export default coerceToNumber; 3 | -------------------------------------------------------------------------------- /ts-types/number/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureNumber(value: any, options?: EnsureBaseOptions): number; 4 | declare function ensureNumber(value: any, options?: EnsureBaseOptions & EnsureIsOptional): number | null; 5 | declare function ensureNumber(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): number; 6 | 7 | export default ensureNumber; 8 | -------------------------------------------------------------------------------- /ts-types/object/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureObject(value: any, options?: EnsureBaseOptions): object; 4 | declare function ensureObject(value: any, options?: EnsureBaseOptions & EnsureIsOptional): object | null; 5 | declare function ensureObject(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): object; 6 | 7 | export default ensureObject; 8 | -------------------------------------------------------------------------------- /ts-types/object/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isObject(value: any): boolean; 2 | export default isObject; 3 | -------------------------------------------------------------------------------- /ts-types/plain-function/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureFunction, EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensurePlainFunction(value: any, options?: EnsureBaseOptions): EnsureFunction; 4 | declare function ensurePlainFunction(value: any, options?: EnsureBaseOptions & EnsureIsOptional): EnsureFunction | null; 5 | declare function ensurePlainFunction(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): EnsureFunction; 6 | 7 | export default ensurePlainFunction; 8 | -------------------------------------------------------------------------------- /ts-types/plain-function/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isPlainFunction(value: any): boolean; 2 | export default isPlainFunction; 3 | -------------------------------------------------------------------------------- /ts-types/plain-object/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureFunction, EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | type PlainObjectEnsureOptions = {allowedKeys?: string[], ensurePropertyValue?: EnsureFunction}; 4 | 5 | 6 | declare function ensurePlainObject(value: any, options?: PlainObjectEnsureOptions & EnsureBaseOptions): object; 7 | declare function ensurePlainObject(value: any, options?: PlainObjectEnsureOptions & EnsureBaseOptions & EnsureIsOptional): object | null; 8 | declare function ensurePlainObject(value: any, options?: PlainObjectEnsureOptions & EnsureBaseOptions & EnsureIsOptional & EnsureDefault): object; 9 | 10 | export default ensurePlainObject; 11 | -------------------------------------------------------------------------------- /ts-types/plain-object/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isPlainObject(value: any): boolean; 2 | export default isPlainObject; 3 | -------------------------------------------------------------------------------- /ts-types/promise/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensurePromise(value: any, options?: EnsureBaseOptions): Promise; 4 | declare function ensurePromise(value: any, options?: EnsureBaseOptions & EnsureIsOptional): Promise | null; 5 | declare function ensurePromise(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault>): Promise; 6 | 7 | export default ensurePromise; 8 | -------------------------------------------------------------------------------- /ts-types/promise/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isPromise(value: any): boolean; 2 | export default isPromise; 3 | -------------------------------------------------------------------------------- /ts-types/prototype/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isPrototype(value: any): boolean; 2 | export default isPrototype; 3 | -------------------------------------------------------------------------------- /ts-types/reg-exp/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureRegExp(value: any, options?: EnsureBaseOptions): RegExp; 4 | declare function ensureRegExp(value: any, options?: EnsureBaseOptions & EnsureIsOptional): RegExp | null; 5 | declare function ensureRegExp(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): RegExp; 6 | 7 | export default ensureRegExp; 8 | -------------------------------------------------------------------------------- /ts-types/reg-exp/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isRegExp(value: any): boolean; 2 | export default isRegExp; 3 | -------------------------------------------------------------------------------- /ts-types/safe-integer/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToSafeInteger(value: any): number | null; 2 | export default coerceToSafeInteger; 3 | -------------------------------------------------------------------------------- /ts-types/safe-integer/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureSafeInteger(value: any, options?: EnsureBaseOptions): number; 4 | declare function ensureSafeInteger(value: any, options?: EnsureBaseOptions & EnsureIsOptional): number | null; 5 | declare function ensureSafeInteger(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): number; 6 | 7 | export default ensureSafeInteger; 8 | -------------------------------------------------------------------------------- /ts-types/set/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureSet(value: any, options?: EnsureBaseOptions): Set; 4 | declare function ensureSet(value: any, options?: EnsureBaseOptions & EnsureIsOptional): Set | null; 5 | declare function ensureSet(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault>): Set; 6 | 7 | export default ensureSet; 8 | -------------------------------------------------------------------------------- /ts-types/set/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isSet(value: any): boolean; 2 | export default isSet; 3 | -------------------------------------------------------------------------------- /ts-types/string/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToString(value: any): string | null; 2 | export default coerceToString; 3 | -------------------------------------------------------------------------------- /ts-types/string/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureString(value: any, options?: EnsureBaseOptions): string; 4 | declare function ensureString(value: any, options?: EnsureBaseOptions & EnsureIsOptional): string | null; 5 | declare function ensureString(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): string; 6 | 7 | export default ensureString; 8 | -------------------------------------------------------------------------------- /ts-types/thenable/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureFunction, EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | type ThenableObject = { then: EnsureFunction } & object; 4 | 5 | 6 | declare function ensureThenable(value: any, options?: EnsureBaseOptions): Promise | ThenableObject; 7 | declare function ensureThenable(value: any, options?: EnsureBaseOptions & EnsureIsOptional): Promise | ThenableObject | null; 8 | declare function ensureThenable(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault | ThenableObject>): Promise | ThenableObject; 9 | 10 | export default ensureThenable; 11 | -------------------------------------------------------------------------------- /ts-types/thenable/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isThenable(value: any): boolean; 2 | export default isThenable; 3 | -------------------------------------------------------------------------------- /ts-types/time-value/coerce.d.ts: -------------------------------------------------------------------------------- 1 | declare function coerceToTimeValue(value: any): number | null; 2 | export default coerceToTimeValue; 3 | -------------------------------------------------------------------------------- /ts-types/time-value/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureBaseOptions, EnsureIsOptional, EnsureDefault } from '../ensure'; 2 | 3 | declare function ensureTimeValue(value: any, options?: EnsureBaseOptions & EnsureIsOptional): number | null; 4 | declare function ensureTimeValue(value: any, options?: EnsureBaseOptions & EnsureIsOptional & EnsureDefault): number; 5 | export default ensureTimeValue; 6 | -------------------------------------------------------------------------------- /ts-types/value/ensure.d.ts: -------------------------------------------------------------------------------- 1 | import { EnsureOptions } from '../ensure'; 2 | 3 | declare function ensureValue(value: any, options?: EnsureOptions): T; 4 | export default ensureValue; 5 | -------------------------------------------------------------------------------- /ts-types/value/is.d.ts: -------------------------------------------------------------------------------- 1 | declare function isValue(value: any): boolean; 2 | export default isValue; 3 | -------------------------------------------------------------------------------- /value/ensure.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var resolveException = require("../lib/resolve-exception") 4 | , is = require("./is"); 5 | 6 | module.exports = function (value/*, options*/) { 7 | if (is(value)) return value; 8 | var options = arguments[1]; 9 | var errorMessage = 10 | options && options.name ? "Expected a value for %n, received %v" : "Cannot use %v"; 11 | return resolveException(value, errorMessage, options); 12 | }; 13 | -------------------------------------------------------------------------------- /value/is.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | // ES3 safe 4 | var _undefined = void 0; 5 | 6 | module.exports = function (value) { return value !== _undefined && value !== null; }; 7 | --------------------------------------------------------------------------------