├── .eslintignore ├── .eslintrc ├── .github └── workflows │ ├── ci.yml │ ├── nodejs-14.yml │ └── release.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── benchmark ├── is.date.cjs ├── is.error.cjs └── is.regexp.cjs ├── package.json ├── src ├── index.ts ├── is.ts └── types │ ├── external.ts │ ├── node.ts │ ├── primitive.ts │ ├── std.ts │ └── util.ts ├── test ├── index.d.ts ├── index.test.ts ├── is.test.ts └── types │ ├── external.test.ts │ ├── node.test.ts │ ├── primitive.test.ts │ └── std.test.ts └── tsconfig.json /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | benchmark/ 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "extends": [ 4 | "eslint-config-egg/typescript" 5 | ], 6 | "parserOptions": { 7 | "project": "./tsconfig.json" 8 | }, 9 | "rules": { 10 | "no-new-wrappers": "off", 11 | "@typescript-eslint/ban-ts-comment": "off", 12 | "@typescript-eslint/ban-types": "off", 13 | "@typescript-eslint/no-empty-function": "off", 14 | "@typescript-eslint/no-useless-constructor": "off" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | 7 | pull_request: 8 | branches: [ master ] 9 | 10 | jobs: 11 | Job: 12 | name: Node.js 13 | uses: node-modules/github-actions/.github/workflows/node-test.yml@master 14 | with: 15 | os: 'ubuntu-latest' 16 | version: '16, 18, 20, 22' 17 | -------------------------------------------------------------------------------- /.github/workflows/nodejs-14.yml: -------------------------------------------------------------------------------- 1 | name: Node.js 14 CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: Use Node.js 17 | uses: actions/setup-node@v4 18 | with: 19 | node-version: '14' 20 | - run: npm install 21 | - run: node -v 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | 7 | jobs: 8 | release: 9 | name: Node.js 10 | uses: node-modules/github-actions/.github/workflows/node-release.yml@master 11 | secrets: 12 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 13 | GIT_TOKEN: ${{ secrets.GIT_TOKEN }} 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | coverage 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | .nyc_output 17 | dist 18 | .tshy 19 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [2.2.0](https://github.com/node-modules/is-type-of/compare/v2.1.0...v2.2.0) (2024-12-09) 4 | 5 | 6 | ### Features 7 | 8 | * hasOwn support Node.js 14 ([#25](https://github.com/node-modules/is-type-of/issues/25)) ([857bdff](https://github.com/node-modules/is-type-of/commit/857bdff38486cc327ca93599aad5168150559eaa)) 9 | 10 | ## [2.1.0](https://github.com/node-modules/is-type-of/compare/v2.0.1...v2.1.0) (2023-11-25) 11 | 12 | 13 | ### Features 14 | 15 | * use tshy to build and support ESM and CommonJS both ([#22](https://github.com/node-modules/is-type-of/issues/22)) ([3688cd6](https://github.com/node-modules/is-type-of/commit/3688cd67dbb75304016220ec4f882a56dcc8d38f)) 16 | 17 | ## [2.0.1](https://github.com/node-modules/is-type-of/compare/v2.0.0...v2.0.1) (2023-08-02) 18 | 19 | 20 | ### Bug Fixes 21 | 22 | * compatible regexp ([#20](https://github.com/node-modules/is-type-of/issues/20)) ([a68e5cb](https://github.com/node-modules/is-type-of/commit/a68e5cbd23afc82a9e1d3a9cb48116bc104ca067)) 23 | * use npm instead of tnpm ([96b5b4e](https://github.com/node-modules/is-type-of/commit/96b5b4e436b6ab33568510e8dc9dab9ce4a891a6)) 24 | 25 | ## [2.0.0](https://github.com/node-modules/is-type-of/compare/v1.4.0...v2.0.0) (2023-07-28) 26 | 27 | 28 | ### ⚠ BREAKING CHANGES 29 | 30 | * drop Node.js <= 14 31 | 32 | ### Features 33 | 34 | * rewrite in typescript ([#19](https://github.com/node-modules/is-type-of/issues/19)) ([58b5703](https://github.com/node-modules/is-type-of/commit/58b5703ea3329b29790963e221023e8deeb0f97d)) 35 | 36 | ## [1.4.0](https://github.com/node-modules/is-type-of/compare/v1.3.0...v1.4.0) (2023-05-04) 37 | 38 | 39 | ### Features 40 | 41 | * Add bigInt ([#18](https://github.com/node-modules/is-type-of/issues/18)) ([695bcaf](https://github.com/node-modules/is-type-of/commit/695bcaf30d0b808012ddad169ccc3cef0c55202f)) 42 | 43 | ## [1.3.0](https://github.com/node-modules/is-type-of/compare/v1.2.1...v1.3.0) (2023-05-04) 44 | 45 | 46 | ### Features 47 | 48 | * add d.ts support ([#17](https://github.com/node-modules/is-type-of/issues/17)) ([edcedcc](https://github.com/node-modules/is-type-of/commit/edcedcc70af64c054cc834e5dd2ed79edd4692ff)) 49 | 50 | --- 51 | 52 | 53 | 1.2.1 / 2018-09-22 54 | ================== 55 | 56 | **fixes** 57 | * [[`1468e4e`](http://github.com/node-modules/is-type-of/commit/1468e4e2d2a1221ff25187f430e5546db2fadacc)] - fix: hotfix is-class bug (#10) (fengmk2 <>) 58 | 59 | **others** 60 | * [[`32aab2b`](http://github.com/node-modules/is-type-of/commit/32aab2b5431e76529f2cf9ddf1d112b99ed05394)] - chore: Improve some functions (#8) (Runrioter Wung <>) 61 | 62 | 1.2.0 / 2017-08-10 63 | ================== 64 | 65 | * feat: Add async function test function. (#6) 66 | 67 | 1.1.0 / 2017-07-19 68 | ================== 69 | 70 | * deps: upgrade is-class@0.0.4 71 | * feat: is.array() use Array.isArray() directly 72 | * feat: improve is.error performance 73 | * feat: improve is.regExp performance 74 | * test: support coverage 75 | * feat: imrpove is.date performance (#4) 76 | 77 | 1.0.0 / 2015-11-09 78 | ================== 79 | 80 | * deps: update all dependencies 81 | * detect class 82 | 83 | 0.3.1 / 2014-06-20 84 | ================== 85 | 86 | * add is.finite() 87 | 88 | 0.3.0 / 2014-05-10 89 | ================== 90 | 91 | * split Long from long 92 | * fix history 93 | 94 | 0.2.1 / 2014-04-22 95 | ================== 96 | 97 | * bump deps version 98 | * Merge pull request #1 from node-modules/support-long-module 99 | * is.long() support long module 100 | * refactor 101 | 102 | 0.2.0 / 2014-04-07 103 | ================== 104 | 105 | * add readme for isstream 106 | * add isStream 107 | * add travis-yml 108 | 109 | 0.1.0 / 2014-04-03 110 | ================== 111 | 112 | * add isNaN 113 | 114 | 0.0.2 / 2014-04-01 115 | ================== 116 | 117 | * add is.int32 118 | 119 | 0.0.1 / 2014-03-12 120 | ================== 121 | 122 | * add readme 123 | * add test, add number type checker 124 | * add core-util-is and generator promise 125 | * Initial commit 126 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 dead_horse 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # is-type-of 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![CI](https://github.com/node-modules/is-type-of/actions/workflows/ci.yml/badge.svg)](https://github.com/node-modules/is-type-of/actions/workflows/ci.yml) 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![Known Vulnerabilities][snyk-image]][snyk-url] 7 | [![npm download][download-image]][download-url] 8 | [![Node.js Version][node-version-image]][node-version-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/is-type-of.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/is-type-of 12 | [codecov-image]: https://codecov.io/github/node-modules/is-type-of/coverage.svg?branch=master 13 | [codecov-url]: https://codecov.io/github/node-modules/is-type-of?branch=master 14 | [snyk-image]: https://snyk.io/test/npm/is-type-of/badge.svg?style=flat-square 15 | [snyk-url]: https://snyk.io/test/npm/is-type-of 16 | [download-image]: https://img.shields.io/npm/dm/is-type-of.svg?style=flat-square 17 | [download-url]: https://npmjs.org/package/is-type-of 18 | [node-version-image]: https://img.shields.io/node/v/is-type-of.svg?style=flat-square 19 | [node-version-url]: https://nodejs.org/en/download/ 20 | 21 | Complete type checking for Node 22 | 23 | ## Features 24 | 25 | - 🔒 Useful Type Checking,including Primitive, Standard Object, Node Object 26 | - ✨ Typescript Support 27 | - 🚪 Typescript Type Guard Support 28 | 29 | ## Install 30 | 31 | ```bash 32 | npm install is-type-of 33 | ``` 34 | 35 | ## Usage 36 | 37 | Use ES Module import 38 | 39 | ```typescript 40 | import is from 'is-type-of'; 41 | import { isArray } from 'is-type-of'; 42 | 43 | isArray([]); // => true 44 | is.array([]); // => true 45 | ``` 46 | 47 | Compatible With 48 | 49 | ```javascript 50 | const is = require('is-type-of'); 51 | 52 | is.array([1]); // => true 53 | is.primitive(true); // => true 54 | is.primitive({}); // => false 55 | is.generatorFunction(function * () {}); // => true 56 | is.long(Math.pow(2, 33)); // => true 57 | is.double(0); // => false 58 | ``` 59 | 60 | Use Type Guard 61 | 62 | ```typescript 63 | import { isArray } from 'is-type-of'; 64 | 65 | function checkArray(arr: string[] | string) { 66 | // => parameter) arr: string | string[] 67 | if (isArray(arr)) { 68 | // => (parameter) arr: string[] 69 | arr.forEach(console.log); 70 | } 71 | } 72 | ``` 73 | 74 | 75 | ## API Document 76 | 77 | ### Primitive 78 | 79 | #### isString 80 | 81 | Returns true if `val` is string, not `String` object. 82 | 83 | See Also `is.string` 84 | 85 | #### isNumber 86 | 87 | Returns true if `val` is number, not `Number` object. 88 | 89 | See Also `is.number` 90 | 91 | #### isBoolean 92 | 93 | Returns true if `val` is boolean, not `Boolean` object. 94 | 95 | See Also `is.boolean` 96 | 97 | #### isBigInt 98 | 99 | Returns true if `val` is bigint. 100 | 101 | See Also `is.bigInt` 102 | 103 | #### isSymbol 104 | 105 | Returns true if `val` is symbol. 106 | 107 | See Also `is.symbol` 108 | 109 | #### isUndefined 110 | 111 | Returns true if `val` is undefined. 112 | 113 | See Also `is.undefined` 114 | 115 | #### isNull 116 | 117 | Returns true if `val` is null. 118 | 119 | See Also `is.null` 120 | 121 | #### isNullable 122 | 123 | Returns true if `val` is null or undefined. 124 | 125 | See Also `is.nullable` 126 | 127 | #### isPrimitive 128 | 129 | Returns true if `val` is primitive. 130 | 131 | See Also `is.primitive` 132 | 133 | #### isInteger 134 | 135 | Returns true if `val` is integer. 136 | 137 | See Also `is.integer` 138 | 139 | #### isInteger32 140 | 141 | Returns true if `val` is integer, and between `-2 ** 31` and `2 ** 31 - 1`. 142 | 143 | See Also `is.integer32` 144 | 145 | #### isLong 146 | 147 | Returns true if `val` is integer, and < `-2 ** 31`, and > `2 ** 31 - 1`. 148 | 149 | See Also `is.long` 150 | 151 | #### isSafeInteger 152 | 153 | Returns true if `val` is integer, and between `-(2 ** 53 - 1)` and `2 ** 53 - 1`. 154 | 155 | See Also `is.safeInteger` 156 | 157 | #### isDouble 158 | 159 | Returns true if `val` is Double. 160 | 161 | See Also `is.double` 162 | 163 | #### isNaN 164 | 165 | Returns true if `val` is NaN. 166 | 167 | See Also `is.NaN` 168 | 169 | #### isFinite 170 | 171 | Returns true if `val` is finite. 172 | 173 | See Also `is.finite` 174 | 175 | ### Standard Object 176 | # API Documentation 177 | 178 | #### isArray 179 | 180 | Returns true if `val` is array, it won't check items of array. 181 | 182 | See Also `is.array` 183 | 184 | #### isFunction 185 | 186 | Returns true if `val` is function. 187 | 188 | See Also `is.function` 189 | 190 | #### isGeneratorFunction 191 | 192 | Returns true if `val` is generator function. 193 | 194 | See Also `is.generatorFunction` 195 | 196 | #### isAsyncFunction 197 | 198 | Returns true if `val` is async function. 199 | 200 | See Also `is.asyncFunction` 201 | 202 | #### isAsyncGeneratorFunction 203 | 204 | Returns true if `val` is async generator function. 205 | 206 | See Also `is.asyncGeneratorFunction` 207 | 208 | #### isObject 209 | 210 | Returns true if `val` is object. 211 | 212 | See Also `is.object` 213 | 214 | #### isClass 215 | 216 | Returns true if `val` is class. 217 | 218 | **Note:** "class" is supported in ECMAScript 6, and if the code is using some compiler or transpiler, the checking might fail. 219 | 220 | See Also `is.class` 221 | 222 | #### isRegExp 223 | 224 | Returns true if `val` is regular expression. 225 | 226 | See Also `is.regExp` 227 | 228 | #### isDate 229 | 230 | Returns true if `val` is instance of Date. 231 | 232 | See Also `is.date` 233 | 234 | #### isError 235 | 236 | Returns true if `val` is instance of Error. 237 | 238 | See Also `is.error` 239 | 240 | #### isGenerator 241 | 242 | Returns true if `val` is generator. 243 | 244 | See Also `is.generator` 245 | 246 | #### isPromise 247 | 248 | Returns true if `val` is promise. 249 | 250 | See Also `is.promise` 251 | 252 | #### isPromiseLike 253 | 254 | Returns true if `val` is like promise, if the object has `then` property, the checking will pass. 255 | 256 | See Also `is.promiseLike` 257 | 258 | ### Node Object 259 | 260 | #### isBuffer 261 | 262 | Returns true if `val` is buffer. 263 | 264 | See Also `is.buffer` 265 | 266 | #### isStream 267 | 268 | Returns true if `val` is stream. 269 | 270 | See Also `is.stream` 271 | 272 | #### isReadable 273 | 274 | Returns true if `val` is readable stream. 275 | 276 | See Also `is.readable` 277 | 278 | #### isWritable 279 | 280 | Returns true if `val` is write stream. 281 | 282 | See Also `is.writable` 283 | 284 | #### isDuplex 285 | 286 | Returns true if `val` is duplex stream. 287 | 288 | See Also `is.duplex` 289 | 290 | ### External Object 291 | 292 | #### isLongObject 293 | 294 | returns true if val is LongObject 295 | 296 | LongObject is from npm package [long](https://github.com/dcodeIO/long.js) 297 | 298 | See Also `is.longObject` 299 | 300 | ## Thanks 301 | 302 | - [core-util-is](https://github.com/isaacs/core-util-is) 303 | - [is-stream](https://github.com/rvagg/isstream) 304 | - [is-class](https://github.com/miguelmota/is-class) 305 | 306 | ## License 307 | 308 | [MIT](LICENSE) 309 | 310 | ## Contributors 311 | 312 | [![Contributors](https://contrib.rocks/image?repo=node-modules/is-type-of)](https://github.com/node-modules/is-type-of/graphs/contributors) 313 | 314 | Made with [contributors-img](https://contrib.rocks). 315 | -------------------------------------------------------------------------------- /benchmark/is.date.cjs: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const fs = require('fs'); 3 | const Benchmark = require('benchmark'); 4 | const benchmarks = require('beautify-benchmark'); 5 | const t = require('core-util-is'); 6 | const is = require('is-type-of'); 7 | // console.log(require.resolve('is-type-of')); 8 | 9 | const nowDate = new Date(); 10 | const nowNumber = Date.now(); 11 | const shortString = '一点点点中文abc'; 12 | const longString = fs.readFileSync(path.join(__dirname, '../package.json'), 'utf8'); 13 | const smallBuffer = Buffer.alloc(100); 14 | const bigBuffer = Buffer.alloc(1024 * 1024); 15 | 16 | console.log('is.date(nowDate): %s', is.date(nowDate)); 17 | console.log('is.date(nowNumber): %s', is.date(nowNumber)); 18 | console.log('is.date(shortString): %s', is.date(shortString)); 19 | console.log('is.date(longString): %s', is.date(longString)); 20 | console.log('is.date(smallBuffer): %s', is.date(smallBuffer)); 21 | console.log('is.date(bigBuffer): %s', is.date(bigBuffer)); 22 | 23 | console.log('t.isDate(nowDate): %s', t.isDate(nowDate)); 24 | console.log('t.isDate(nowNumber): %s', t.isDate(nowNumber)); 25 | console.log('t.isDate(shortString): %s', t.isDate(shortString)); 26 | console.log('t.isDate(longString): %s', t.isDate(longString)); 27 | console.log('t.isDate(smallBuffer): %s', t.isDate(smallBuffer)); 28 | console.log('t.isDate(bigBuffer): %s', t.isDate(bigBuffer)); 29 | 30 | const suite = new Benchmark.Suite(); 31 | 32 | suite 33 | .add('is.date(nowDate)', () => { 34 | is.date(nowDate); 35 | }) 36 | .add('is.date(nowNumber)', () => { 37 | is.date(nowNumber); 38 | }) 39 | .add('is.date(shortString)', () => { 40 | is.date(shortString); 41 | }) 42 | .add('is.date(longString)', () => { 43 | is.date(longString); 44 | }) 45 | .add('is.date(smallBuffer)', () => { 46 | is.date(smallBuffer); 47 | }) 48 | .add('is.date(bigBuffer)', () => { 49 | is.date(bigBuffer); 50 | }) 51 | 52 | .add('t.isDate(nowDate)', () => { 53 | t.isDate(nowDate); 54 | }) 55 | .add('t.isDate(nowNumber)', () => { 56 | t.isDate(nowNumber); 57 | }) 58 | .add('t.isDate(shortString)', () => { 59 | t.isDate(shortString); 60 | }) 61 | .add('t.isDate(longString)', () => { 62 | t.isDate(longString); 63 | }) 64 | .add('t.isDate(smallBuffer)', () => { 65 | t.isDate(smallBuffer); 66 | }) 67 | .add('t.isDate(bigBuffer)', () => { 68 | t.isDate(bigBuffer); 69 | }) 70 | 71 | .on('cycle', event => { 72 | benchmarks.add(event.target); 73 | }) 74 | .on('start', event => { 75 | console.log('\n is.date(obj) Benchmark\n node version: %s, date: %s\n Starting...', 76 | process.version, Date()); 77 | }) 78 | .on('complete', () => { 79 | benchmarks.log(); 80 | }) 81 | .run({ 'async': false }); 82 | 83 | // node version: v8.1.4, date: Wed Jul 19 2017 11:34:05 GMT+0800 (CST) 84 | // Starting... 85 | // 12 tests completed. 86 | // 87 | // is.date(nowDate) x 37,744,799 ops/sec ±0.89% (85 runs sampled) 88 | // is.date(nowNumber) x 38,248,974 ops/sec ±0.91% (85 runs sampled) 89 | // is.date(shortString) x 33,738,281 ops/sec ±7.17% (76 runs sampled) 90 | // is.date(longString) x 36,753,097 ops/sec ±2.20% (83 runs sampled) 91 | // is.date(smallBuffer) x 25,582,678 ops/sec ±2.28% (82 runs sampled) 92 | // is.date(bigBuffer) x 25,195,852 ops/sec ±2.27% (83 runs sampled) 93 | // t.isDate(nowDate) x 12,655,669 ops/sec ±1.99% (81 runs sampled) 94 | // t.isDate(nowNumber) x 16,324,753 ops/sec ±2.10% (85 runs sampled) 95 | // t.isDate(shortString) x 10,995,505 ops/sec ±2.19% (83 runs sampled) 96 | // t.isDate(longString) x 11,119,328 ops/sec ±1.35% (86 runs sampled) 97 | // t.isDate(smallBuffer) x 4,051,242 ops/sec ±1.09% (86 runs sampled) 98 | // t.isDate(bigBuffer) x 3,990,640 ops/sec ±1.20% (85 runs sampled) 99 | -------------------------------------------------------------------------------- /benchmark/is.error.cjs: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const fs = require('fs'); 3 | const Benchmark = require('benchmark'); 4 | const benchmarks = require('beautify-benchmark'); 5 | const t = require('core-util-is'); 6 | const is = require('is-type-of'); 7 | 8 | const nowDate = new Date(); 9 | const nowNumber = Date.now(); 10 | const shortString = '一点点点中文abc'; 11 | const longString = fs.readFileSync(path.join(__dirname, '../index.js'), 'utf8'); 12 | const smallBuffer = Buffer.alloc(100); 13 | const bigBuffer = Buffer.alloc(1024 * 1024); 14 | const newRegExpInstance = new RegExp('foobar'); 15 | const regExpInstance = /foobar/; 16 | 17 | class CustomError extends Error { 18 | constructor(message) { 19 | super(message); 20 | Error.captureStackTrace(this, CustomError); 21 | } 22 | } 23 | 24 | const shortTypeError = new TypeError(shortString); 25 | const longTypeError = new TypeError(longString); 26 | const shortCustomError = new CustomError(shortString); 27 | const longCustomError = new CustomError(longString); 28 | 29 | console.log('is.error(nowDate): %s', is.error(nowDate)); 30 | console.log('is.error(nowNumber): %s', is.error(nowNumber)); 31 | console.log('is.error(shortString): %s', is.error(shortString)); 32 | console.log('is.error(longString): %s', is.error(longString)); 33 | console.log('is.error(smallBuffer): %s', is.error(smallBuffer)); 34 | console.log('is.error(bigBuffer): %s', is.error(bigBuffer)); 35 | console.log('is.error(newRegExpInstance): %s', is.error(newRegExpInstance)); 36 | console.log('is.error(regExpInstance): %s', is.error(regExpInstance)); 37 | console.log('is.error(shortTypeError): %s', is.error(shortTypeError)); 38 | console.log('is.error(longTypeError): %s', is.error(longTypeError)); 39 | console.log('is.error(shortCustomError): %s', is.error(shortCustomError)); 40 | console.log('is.error(longCustomError): %s', is.error(longCustomError)); 41 | 42 | console.log('t.isError(nowDate): %s', t.isError(nowDate)); 43 | console.log('t.isError(nowNumber): %s', t.isError(nowNumber)); 44 | console.log('t.isError(shortString): %s', t.isError(shortString)); 45 | console.log('t.isError(longString): %s', t.isError(longString)); 46 | console.log('t.isError(smallBuffer): %s', t.isError(smallBuffer)); 47 | console.log('t.isError(bigBuffer): %s', t.isError(bigBuffer)); 48 | console.log('t.isError(newRegExpInstance): %s', t.isError(newRegExpInstance)); 49 | console.log('t.isError(regExpInstance): %s', t.isError(regExpInstance)); 50 | console.log('t.isError(shortTypeError): %s', t.isError(shortTypeError)); 51 | console.log('t.isError(longTypeError): %s', t.isError(longTypeError)); 52 | console.log('t.isError(shortCustomError): %s', t.isError(shortCustomError)); 53 | console.log('t.isError(longCustomError): %s', t.isError(longCustomError)); 54 | 55 | const suite = new Benchmark.Suite(); 56 | 57 | suite 58 | .add('is.error(nowDate)', () => { 59 | is.error(nowDate); 60 | }) 61 | .add('is.error(nowNumber)', () => { 62 | is.error(nowNumber); 63 | }) 64 | .add('is.error(shortString)', () => { 65 | is.error(shortString); 66 | }) 67 | .add('is.error(longString)', () => { 68 | is.error(longString); 69 | }) 70 | .add('is.error(smallBuffer)', () => { 71 | is.error(smallBuffer); 72 | }) 73 | .add('is.error(bigBuffer)', () => { 74 | is.error(bigBuffer); 75 | }) 76 | .add('is.error(newRegExpInstance)', () => { 77 | is.error(newRegExpInstance); 78 | }) 79 | .add('is.error(regExpInstance)', () => { 80 | is.error(regExpInstance); 81 | }) 82 | .add('is.error(shortTypeError)', () => { 83 | is.error(shortTypeError); 84 | }) 85 | .add('is.error(longTypeError)', () => { 86 | is.error(longTypeError); 87 | }) 88 | .add('is.error(shortCustomError)', () => { 89 | is.error(shortCustomError); 90 | }) 91 | .add('is.error(longCustomError)', () => { 92 | is.error(longCustomError); 93 | }) 94 | 95 | .add('t.isError(nowDate)', () => { 96 | t.isError(nowDate); 97 | }) 98 | .add('t.isError(nowNumber)', () => { 99 | t.isError(nowNumber); 100 | }) 101 | .add('t.isError(shortString)', () => { 102 | t.isError(shortString); 103 | }) 104 | .add('t.isError(longString)', () => { 105 | t.isError(longString); 106 | }) 107 | .add('t.isError(smallBuffer)', () => { 108 | t.isError(smallBuffer); 109 | }) 110 | .add('t.isError(bigBuffer)', () => { 111 | t.isError(bigBuffer); 112 | }) 113 | .add('t.isError(newRegExpInstance)', () => { 114 | t.isError(newRegExpInstance); 115 | }) 116 | .add('t.isError(regExpInstance)', () => { 117 | t.isError(regExpInstance); 118 | }) 119 | .add('t.isError(shortTypeError)', () => { 120 | t.isError(shortTypeError); 121 | }) 122 | .add('t.isError(longTypeError)', () => { 123 | t.isError(longTypeError); 124 | }) 125 | .add('t.isError(shortCustomError)', () => { 126 | t.isError(shortCustomError); 127 | }) 128 | .add('t.isError(longCustomError)', () => { 129 | t.isError(longCustomError); 130 | }) 131 | 132 | .on('cycle', event => { 133 | benchmarks.add(event.target); 134 | }) 135 | .on('start', event => { 136 | console.log('\n is.error(obj) Benchmark\n node version: %s, date: %s\n Starting...', 137 | process.version, Date()); 138 | }) 139 | .on('complete', () => { 140 | benchmarks.log(); 141 | }) 142 | .run({ 'async': false }); 143 | 144 | // node version: v8.1.4, date: Wed Jul 19 2017 14:48:31 GMT+0800 (CST) 145 | // Starting... 146 | // 24 tests completed. 147 | // 148 | // is.error(nowDate) x 28,116,337 ops/sec ±5.42% (78 runs sampled) 149 | // is.error(nowNumber) x 36,817,865 ops/sec ±1.92% (82 runs sampled) 150 | // is.error(shortString) x 36,715,489 ops/sec ±1.97% (82 runs sampled) 151 | // is.error(longString) x 36,129,682 ops/sec ±2.07% (81 runs sampled) 152 | // is.error(smallBuffer) x 23,728,293 ops/sec ±3.16% (80 runs sampled) 153 | // is.error(bigBuffer) x 24,072,056 ops/sec ±2.57% (82 runs sampled) 154 | // is.error(newRegExpInstance) x 29,139,976 ops/sec ±3.27% (81 runs sampled) 155 | // is.error(regExpInstance) x 30,037,776 ops/sec ±1.20% (83 runs sampled) 156 | // is.error(shortTypeError) x 34,769,151 ops/sec ±1.35% (86 runs sampled) 157 | // is.error(longTypeError) x 33,301,941 ops/sec ±2.24% (81 runs sampled) 158 | // is.error(shortCustomError) x 32,532,898 ops/sec ±2.92% (82 runs sampled) 159 | // is.error(longCustomError) x 34,155,274 ops/sec ±1.75% (84 runs sampled) 160 | // t.isError(nowDate) x 12,137,434 ops/sec ±1.12% (85 runs sampled) 161 | // t.isError(nowNumber) x 15,849,631 ops/sec ±1.13% (88 runs sampled) 162 | // t.isError(shortString) x 11,304,206 ops/sec ±1.98% (82 runs sampled) 163 | // t.isError(longString) x 10,395,919 ops/sec ±3.95% (77 runs sampled) 164 | // t.isError(smallBuffer) x 3,846,410 ops/sec ±2.83% (83 runs sampled) 165 | // t.isError(bigBuffer) x 3,823,055 ops/sec ±2.38% (84 runs sampled) 166 | // t.isError(newRegExpInstance) x 13,091,869 ops/sec ±1.05% (86 runs sampled) 167 | // t.isError(regExpInstance) x 13,612,892 ops/sec ±1.18% (85 runs sampled) 168 | // t.isError(shortTypeError) x 14,013,949 ops/sec ±1.62% (81 runs sampled) 169 | // t.isError(longTypeError) x 14,188,227 ops/sec ±1.34% (87 runs sampled) 170 | // t.isError(shortCustomError) x 14,319,689 ops/sec ±2.11% (83 runs sampled) 171 | // t.isError(longCustomError) x 14,823,055 ops/sec ±1.23% (84 runs sampled) 172 | -------------------------------------------------------------------------------- /benchmark/is.regexp.cjs: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const fs = require('fs'); 3 | const Benchmark = require('benchmark'); 4 | const benchmarks = require('beautify-benchmark'); 5 | const t = require('core-util-is'); 6 | const is = require('is-type-of'); 7 | 8 | const nowDate = new Date(); 9 | const nowNumber = Date.now(); 10 | const shortString = '一点点点中文abc'; 11 | const longString = fs.readFileSync(path.join(__dirname, '../index.js'), 'utf8'); 12 | const smallBuffer = Buffer.alloc(100); 13 | const bigBuffer = Buffer.alloc(1024 * 1024); 14 | const newRegExpInstance = new RegExp('foobar'); 15 | const regExpInstance = /foobar/; 16 | 17 | console.log('is.regExp(nowDate): %s', is.regExp(nowDate)); 18 | console.log('is.regExp(nowNumber): %s', is.regExp(nowNumber)); 19 | console.log('is.regExp(shortString): %s', is.regExp(shortString)); 20 | console.log('is.regExp(longString): %s', is.regExp(longString)); 21 | console.log('is.regExp(smallBuffer): %s', is.regExp(smallBuffer)); 22 | console.log('is.regExp(bigBuffer): %s', is.regExp(bigBuffer)); 23 | console.log('is.regExp(newRegExpInstance): %s', is.regExp(newRegExpInstance)); 24 | console.log('is.regExp(regExpInstance): %s', is.regExp(regExpInstance)); 25 | 26 | console.log('t.isRegExp(nowDate): %s', t.isRegExp(nowDate)); 27 | console.log('t.isRegExp(nowNumber): %s', t.isRegExp(nowNumber)); 28 | console.log('t.isRegExp(shortString): %s', t.isRegExp(shortString)); 29 | console.log('t.isRegExp(longString): %s', t.isRegExp(longString)); 30 | console.log('t.isRegExp(smallBuffer): %s', t.isRegExp(smallBuffer)); 31 | console.log('t.isRegExp(bigBuffer): %s', t.isRegExp(bigBuffer)); 32 | console.log('t.isRegExp(newRegExpInstance): %s', t.isRegExp(newRegExpInstance)); 33 | console.log('t.isRegExp(regExpInstance): %s', t.isRegExp(regExpInstance)); 34 | 35 | 36 | const suite = new Benchmark.Suite(); 37 | 38 | suite 39 | .add('is.regExp(nowDate)', () => { 40 | is.regExp(nowDate); 41 | }) 42 | .add('is.regExp(nowNumber)', () => { 43 | is.regExp(nowNumber); 44 | }) 45 | .add('is.regExp(shortString)', () => { 46 | is.regExp(shortString); 47 | }) 48 | .add('is.regExp(longString)', () => { 49 | is.regExp(longString); 50 | }) 51 | .add('is.regExp(smallBuffer)', () => { 52 | is.regExp(smallBuffer); 53 | }) 54 | .add('is.regExp(bigBuffer)', () => { 55 | is.regExp(bigBuffer); 56 | }) 57 | .add('is.regExp(newRegExpInstance)', () => { 58 | is.regExp(newRegExpInstance); 59 | }) 60 | .add('is.regExp(regExpInstance)', () => { 61 | is.regExp(regExpInstance); 62 | }) 63 | 64 | .add('t.isRegExp(nowDate)', () => { 65 | t.isRegExp(nowDate); 66 | }) 67 | .add('t.isRegExp(nowNumber)', () => { 68 | t.isRegExp(nowNumber); 69 | }) 70 | .add('t.isRegExp(shortString)', () => { 71 | t.isRegExp(shortString); 72 | }) 73 | .add('t.isRegExp(longString)', () => { 74 | t.isRegExp(longString); 75 | }) 76 | .add('t.isRegExp(smallBuffer)', () => { 77 | t.isRegExp(smallBuffer); 78 | }) 79 | .add('t.isRegExp(bigBuffer)', () => { 80 | t.isRegExp(bigBuffer); 81 | }) 82 | .add('t.isRegExp(newRegExpInstance)', () => { 83 | t.isRegExp(newRegExpInstance); 84 | }) 85 | .add('t.isRegExp(regExpInstance)', () => { 86 | t.isRegExp(regExpInstance); 87 | }) 88 | 89 | .on('cycle', event => { 90 | benchmarks.add(event.target); 91 | }) 92 | .on('start', event => { 93 | console.log('\n is.regExp(obj) Benchmark\n node version: %s, date: %s\n Starting...', 94 | process.version, Date()); 95 | }) 96 | .on('complete', () => { 97 | benchmarks.log(); 98 | }) 99 | .run({ 'async': false }); 100 | 101 | // node version: v8.1.4, date: Wed Jul 19 2017 14:16:26 GMT+0800 (CST) 102 | // Starting... 103 | // 16 tests completed. 104 | // 105 | // is.regExp(nowDate) x 29,425,671 ops/sec ±1.73% (84 runs sampled) 106 | // is.regExp(nowNumber) x 37,842,887 ops/sec ±1.18% (83 runs sampled) 107 | // is.regExp(shortString) x 37,180,999 ops/sec ±1.16% (81 runs sampled) 108 | // is.regExp(longString) x 37,835,144 ops/sec ±1.02% (85 runs sampled) 109 | // is.regExp(smallBuffer) x 24,988,153 ops/sec ±1.51% (83 runs sampled) 110 | // is.regExp(bigBuffer) x 25,006,147 ops/sec ±1.14% (86 runs sampled) 111 | // is.regExp(newRegExpInstance) x 37,693,104 ops/sec ±1.06% (86 runs sampled) 112 | // is.regExp(regExpInstance) x 37,186,261 ops/sec ±1.69% (83 runs sampled) 113 | // t.isRegExp(nowDate) x 12,413,210 ops/sec ±1.74% (85 runs sampled) 114 | // t.isRegExp(nowNumber) x 16,211,510 ops/sec ±1.78% (85 runs sampled) 115 | // t.isRegExp(shortString) x 11,914,968 ops/sec ±1.01% (88 runs sampled) 116 | // t.isRegExp(longString) x 12,012,244 ops/sec ±1.44% (87 runs sampled) 117 | // t.isRegExp(smallBuffer) x 3,727,496 ops/sec ±1.54% (82 runs sampled) 118 | // t.isRegExp(bigBuffer) x 3,820,998 ops/sec ±1.01% (88 runs sampled) 119 | // t.isRegExp(newRegExpInstance) x 14,799,622 ops/sec ±1.01% (86 runs sampled) 120 | // t.isRegExp(regExpInstance) x 14,935,675 ops/sec ±0.88% (89 runs sampled) 121 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-type-of", 3 | "version": "2.2.0", 4 | "description": "complete type checking for node", 5 | "type": "module", 6 | "tshy": { 7 | "exports": { 8 | "./package.json": "./package.json", 9 | ".": "./src/index.ts" 10 | } 11 | }, 12 | "exports": { 13 | "./package.json": "./package.json", 14 | ".": { 15 | "import": { 16 | "types": "./dist/esm/index.d.ts", 17 | "default": "./dist/esm/index.js" 18 | }, 19 | "require": { 20 | "types": "./dist/commonjs/index.d.ts", 21 | "default": "./dist/commonjs/index.js" 22 | } 23 | } 24 | }, 25 | "files": [ 26 | "dist", 27 | "src" 28 | ], 29 | "scripts": { 30 | "prepublishOnly": "tshy && tshy-after", 31 | "test": "egg-bin test", 32 | "ci": "npm run lint && egg-bin cov && npm run prepublishOnly", 33 | "lint": "eslint . --ext .ts" 34 | }, 35 | "repository": { 36 | "type": "git", 37 | "url": "git://github.com/node-modules/is-type-of.git" 38 | }, 39 | "node": { 40 | "engines": ">= 16.0.0" 41 | }, 42 | "keywords": [ 43 | "typeof", 44 | "checker", 45 | "type", 46 | "is" 47 | ], 48 | "author": "dead_horse ", 49 | "license": "MIT", 50 | "bugs": { 51 | "url": "https://github.com/node-modules/is-type-of/issues" 52 | }, 53 | "homepage": "https://github.com/node-modules/is-type-of", 54 | "devDependencies": { 55 | "@eggjs/tsconfig": "^1.3.3", 56 | "@types/mocha": "^10.0.1", 57 | "@types/node": "^20.6.1", 58 | "beautify-benchmark": "^0.2.4", 59 | "benchmark": "^2.1.4", 60 | "core-util-is": "^1.0.3", 61 | "egg-bin": "^6.5.2", 62 | "eslint": "^8.54.0", 63 | "eslint-config-egg": "^13.0.0", 64 | "long": "^3.2.0", 65 | "semver": "^7.6.3", 66 | "ts-expect": "^1.3.0", 67 | "tshy": "^3.0.2", 68 | "tshy-after": "^1.0.0", 69 | "typescript": "^5.2.2" 70 | }, 71 | "types": "./dist/commonjs/index.d.ts" 72 | } 73 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { is } from './is.js'; 2 | import { isNull, isUndefined, isNaN } from './types/primitive.js'; 3 | import { isClass, isFunction } from './types/std.js'; 4 | 5 | export * from './types/primitive.js'; 6 | export * from './types/std.js'; 7 | export * from './types/node.js'; 8 | export * from './types/external.js'; 9 | export { isInstanceOf } from './types/util.js'; 10 | 11 | export default is; 12 | 13 | // Compatible API, recommand to use `is` 14 | 15 | /** 16 | * @deprecated 17 | * @see {is.boolean} 18 | */ 19 | export const boolean = is.boolean; 20 | 21 | /** 22 | * @deprecated 23 | * @see {is.number} 24 | */ 25 | export const number = is.number; 26 | 27 | /** 28 | * @deprecated 29 | * @see {is.string} 30 | */ 31 | export const string = is.string; 32 | 33 | /** 34 | * @deprecated 35 | * @see {is.symbol} 36 | */ 37 | export const symbol = is.symbol; 38 | 39 | /** 40 | * @deprecated 41 | * @see {is.undefined} 42 | */ 43 | export { isUndefined as undefined }; 44 | 45 | /** 46 | * @deprecated 47 | * @see {is.null} 48 | */ 49 | export { isNull as null }; 50 | 51 | /** 52 | * @deprecated 53 | * @see {is.nullable} 54 | */ 55 | export const nullOrUndefined = is.nullable; 56 | 57 | /** 58 | * @deprecated 59 | * @see {is.bigInt} 60 | */ 61 | export const bigInt = is.bigInt; 62 | 63 | /** 64 | * @deprecated 65 | * @see {is.bigInt} 66 | */ 67 | export const bigint = is.bigInt; 68 | 69 | /** 70 | * @deprecated 71 | * @see {is.integer} 72 | */ 73 | export const int = is.integer; 74 | 75 | /** 76 | * @deprecated 77 | * @see {is.integer32} 78 | */ 79 | export const int32 = is.integer32; 80 | 81 | /** 82 | * @deprecated 83 | * @see {is.long} 84 | */ 85 | export const long = is.long; 86 | 87 | /** 88 | * @deprecated 89 | * @see {is.double} 90 | */ 91 | export const double = is.double; 92 | 93 | /** 94 | * @deprecated 95 | * @see {is.finite} 96 | */ 97 | export const finite = is.finite; 98 | 99 | /** 100 | * @deprecated 101 | * @see {is.NaN} 102 | */ 103 | export { isNaN as NaN }; 104 | 105 | /** 106 | * @deprecated 107 | * @see {is.primitive} 108 | */ 109 | export const primitive = is.primitive; 110 | 111 | /** 112 | * @deprecated 113 | * @see {is.function} 114 | */ 115 | export { isFunction as function }; 116 | 117 | /** 118 | * @deprecated 119 | * @see {is.generatorFunction} 120 | */ 121 | export const generatorFunction = is.generatorFunction; 122 | 123 | /** 124 | * @deprecated 125 | * @see {is.asyncFunction} 126 | */ 127 | export const asyncFunction = is.asyncFunction; 128 | 129 | /** 130 | * @deprecated 131 | * @see {is.class} 132 | */ 133 | export { isClass as class }; 134 | 135 | /** 136 | * @deprecated 137 | * @see {is.array} 138 | */ 139 | export const array = is.array; 140 | 141 | /** 142 | * @deprecated 143 | * @see {is.object} 144 | */ 145 | export const object = is.object; 146 | 147 | /** 148 | * @deprecated 149 | * @see {is.date} 150 | */ 151 | export const date = is.date; 152 | 153 | /** 154 | * @deprecated 155 | * @see {is.error} 156 | */ 157 | export const error = is.error; 158 | 159 | /** 160 | * @deprecated 161 | * @see {is.regExp} 162 | */ 163 | export const regExp = is.regExp; 164 | 165 | /** 166 | * @deprecated 167 | * @see {is.regExp} 168 | */ 169 | export const regexp = is.regExp; 170 | 171 | /** 172 | * @deprecated 173 | * @see {is.generator} 174 | */ 175 | export const generator = is.generator; 176 | 177 | /** 178 | * @deprecated 179 | * @see {is.promiseLike} 180 | */ 181 | export const promise = is.promiseLike; 182 | 183 | /** 184 | * @deprecated 185 | * @see {is.buffer} 186 | */ 187 | export const buffer = is.buffer; 188 | 189 | /** 190 | * @deprecated 191 | * @see {is.stream} 192 | */ 193 | export const stream = is.stream; 194 | 195 | /** 196 | * @deprecated 197 | * @see {is.readable} 198 | */ 199 | export const readable = is.readable; 200 | 201 | /** 202 | * @deprecated 203 | * @see {is.writable} 204 | */ 205 | export const writable = is.writable; 206 | 207 | /** 208 | * @deprecated 209 | * @see {is.duplex} 210 | */ 211 | export const duplex = is.duplex; 212 | 213 | /** 214 | * @deprecated 215 | * @see {is.longObject} 216 | */ 217 | export const Long = is.longObject; 218 | -------------------------------------------------------------------------------- /src/is.ts: -------------------------------------------------------------------------------- 1 | import { 2 | isString, 3 | isNumber, 4 | isBoolean, 5 | isSymbol, 6 | isUndefined, 7 | isNull, 8 | isNullable, 9 | isBigInt, 10 | isPrimitive, 11 | isInteger, 12 | isInteger32, 13 | isNaN, 14 | isFinite, 15 | isLong, 16 | isDouble, 17 | isSafeInteger, 18 | } from './types/primitive.js'; 19 | import { 20 | isArray, 21 | isFunction, 22 | isGeneratorFunction, 23 | isAsyncFunction, 24 | isAsyncGeneratorFunction, 25 | isDate, 26 | isError, 27 | isRegExp, 28 | isClass, 29 | isPromiseLike, 30 | isObject, 31 | isGenerator, 32 | isPromise, 33 | } from './types/std.js'; 34 | import { 35 | isBuffer, 36 | isStream, 37 | isReadable, 38 | isWritable, 39 | isDuplex, 40 | } from './types/node.js'; 41 | import { isLongObject } from './types/external.js'; 42 | 43 | export const is = { 44 | // Primitive 45 | 46 | /** 47 | * @see {isBoolean} 48 | */ 49 | boolean: isBoolean, 50 | 51 | /** 52 | * @see {isNumber} 53 | */ 54 | number: isNumber, 55 | 56 | /** 57 | * @see {isString} 58 | */ 59 | string: isString, 60 | 61 | /** 62 | * @see {isSymbol} 63 | */ 64 | symbol: isSymbol, 65 | 66 | /** 67 | * @see {isUndefined} 68 | */ 69 | undefined: isUndefined, 70 | 71 | /** 72 | * @see {isNull} 73 | */ 74 | null: isNull, 75 | 76 | /** 77 | * @see {isNullable} 78 | */ 79 | nullable: isNullable, 80 | 81 | /** 82 | * @see {isBigInt} 83 | */ 84 | bigInt: isBigInt, 85 | 86 | /** 87 | * @see {isPrimitive} 88 | */ 89 | primitive: isPrimitive, 90 | 91 | /** 92 | * @see {isInteger} 93 | */ 94 | integer: isInteger, 95 | 96 | /** 97 | * @see {isInteger32} 98 | */ 99 | integer32: isInteger32, 100 | 101 | /** 102 | * @see {isLong} 103 | */ 104 | long: isLong, 105 | 106 | /** 107 | * @see {isDouble} 108 | */ 109 | double: isDouble, 110 | 111 | /** 112 | * @see {isFinite} 113 | */ 114 | finite: isFinite, 115 | 116 | /** 117 | * @see {isNaN} 118 | */ 119 | NaN: isNaN, 120 | 121 | /** 122 | * @see {isSafeInteger} 123 | */ 124 | safeInteger: isSafeInteger, 125 | 126 | // Standard Object 127 | 128 | /** 129 | * @see {isFunction} 130 | */ 131 | function: isFunction, 132 | 133 | /** 134 | * @see {isGeneratorFunction} 135 | */ 136 | generatorFunction: isGeneratorFunction, 137 | 138 | /** 139 | * @see {isAsyncFunction} 140 | */ 141 | asyncFunction: isAsyncFunction, 142 | 143 | /** 144 | * @see {isAsyncGeneratorFunction} 145 | */ 146 | asyncGeneratorFunction: isAsyncGeneratorFunction, 147 | 148 | /** 149 | * @see {isClass} 150 | */ 151 | class: isClass, 152 | 153 | /** 154 | * @see {isArray} 155 | */ 156 | array: isArray, 157 | 158 | /** 159 | * @see {isObject} 160 | */ 161 | object: isObject, 162 | 163 | /** 164 | * @see {isDate} 165 | */ 166 | date: isDate, 167 | 168 | /** 169 | * @see {isError} 170 | */ 171 | error: isError, 172 | 173 | /** 174 | * @see {isRegExp} 175 | */ 176 | regExp: isRegExp, 177 | 178 | /** 179 | * @see {isGenerator} 180 | */ 181 | generator: isGenerator, 182 | 183 | /** 184 | * @see {isPromise} 185 | */ 186 | promise: isPromise, 187 | 188 | /** 189 | * @see {isPromiseLike} 190 | */ 191 | promiseLike: isPromiseLike, 192 | 193 | // Node Object 194 | 195 | /** 196 | * @see {isBuffer} 197 | */ 198 | buffer: isBuffer, 199 | 200 | /** 201 | * @see {isStream} 202 | */ 203 | stream: isStream, 204 | 205 | /** 206 | * @see {isReadable} 207 | */ 208 | readable: isReadable, 209 | 210 | /** 211 | * @see {isWritable} 212 | */ 213 | writable: isWritable, 214 | 215 | /** 216 | * @see {isDuplex} 217 | */ 218 | duplex: isDuplex, 219 | 220 | // External Object 221 | 222 | /** 223 | * @see {isLongObject} 224 | */ 225 | longObject: isLongObject, 226 | 227 | }; 228 | -------------------------------------------------------------------------------- /src/types/external.ts: -------------------------------------------------------------------------------- 1 | import { isNumber } from './primitive.js'; 2 | import { isObject } from './std.js'; 3 | import { hasOwnProperty } from './util.js'; 4 | 5 | interface LongObject { 6 | high: number; 7 | low: number; 8 | } 9 | 10 | /** 11 | * returns true if val is LongObject 12 | * 13 | * LongObject is from npm package `long` 14 | */ 15 | export function isLongObject(obj?: unknown): obj is LongObject { 16 | if (!isObject(obj)) return false; 17 | if (!hasOwnProperty(obj, 'high') || !hasOwnProperty(obj, 'low')) return false; 18 | return isNumber(obj.high) && isNumber(obj!.low); 19 | } 20 | -------------------------------------------------------------------------------- /src/types/node.ts: -------------------------------------------------------------------------------- 1 | import { Buffer } from 'node:buffer'; 2 | import { Duplex, Readable, Stream, Writable } from 'node:stream'; 3 | import { hasOwnPropertyInChain, isInstanceOf } from './util.js'; 4 | 5 | /** 6 | * returns true if val is buffer 7 | */ 8 | export function isBuffer(val: unknown): val is Buffer { 9 | return isInstanceOf(val, Buffer); 10 | } 11 | 12 | /** 13 | * returns true if val is stream 14 | */ 15 | export function isStream(val?: unknown): val is Stream { 16 | return isInstanceOf(val, Stream); 17 | } 18 | 19 | /** 20 | * returns true if val is readable stream 21 | */ 22 | export function isReadable(val?: unknown): val is Readable { 23 | return isStream(val) && _isReadable(val); 24 | } 25 | 26 | /** 27 | * returns true if val is write stream 28 | */ 29 | export function isWritable(val?: unknown): val is Writable { 30 | return isStream(val) && _isWritable(val); 31 | } 32 | 33 | /** 34 | * returns true if val is duplex stream 35 | */ 36 | export function isDuplex(val?: unknown): val is Duplex { 37 | return isStream(val) && _isReadable(val) && _isWritable(val); 38 | } 39 | 40 | function _isReadable(val: object): boolean { 41 | if (!hasOwnPropertyInChain(val, '_readableState') || !hasOwnPropertyInChain(val, '_read')) return false; 42 | return typeof val._read === 'function' && typeof val._readableState === 'object'; 43 | } 44 | 45 | function _isWritable(val: object): boolean { 46 | if (!hasOwnPropertyInChain(val, '_writableState') || !hasOwnPropertyInChain(val, '_write')) return false; 47 | return typeof val._write === 'function' && typeof val._writableState === 'object'; 48 | } 49 | -------------------------------------------------------------------------------- /src/types/primitive.ts: -------------------------------------------------------------------------------- 1 | // Javascript Primitive 2 | // https://developer.mozilla.org/en-US/docs/Glossary/Primitive 3 | 4 | type Primitive = string | number | bigint | boolean | symbol | null | undefined; 5 | type Nullable = null | undefined; 6 | 7 | const MAX_INT_31 = Math.pow(2, 31); 8 | 9 | /** 10 | * returns true if val is string, not `String` object 11 | */ 12 | export function isString(val?: unknown): val is string { 13 | return typeof val === 'string'; 14 | } 15 | 16 | /** 17 | * returns true if val is number, not `Number` object 18 | */ 19 | export function isNumber(val?: unknown): val is number { 20 | return typeof val === 'number'; 21 | } 22 | 23 | /** 24 | * returns true if val is boolean, not `Boolean` object 25 | */ 26 | export function isBoolean(val?: unknown): val is boolean { 27 | return typeof val === 'boolean'; 28 | } 29 | 30 | /** 31 | * returns true if val is bigint 32 | */ 33 | export function isBigInt(val?: unknown): val is bigint { 34 | return typeof val === 'bigint'; 35 | } 36 | 37 | /** 38 | * returns true if val is symbol 39 | */ 40 | export function isSymbol(val?: unknown): val is symbol { 41 | return typeof val === 'symbol'; 42 | } 43 | 44 | /** 45 | * returns true if val is undefined 46 | */ 47 | export function isUndefined(val?: unknown): val is undefined { 48 | return typeof val === 'undefined'; 49 | } 50 | 51 | /** 52 | * returns true if val is null 53 | */ 54 | export function isNull(val?: unknown): val is null { 55 | return val === null; 56 | } 57 | 58 | /** 59 | * returns true if val is null or undefined 60 | */ 61 | export function isNullable(val?: unknown): val is Nullable { 62 | return val == null; 63 | } 64 | 65 | /** 66 | * returns true if val is primitive 67 | */ 68 | export function isPrimitive(val?: unknown): val is Primitive { 69 | return val === null || 70 | typeof val === 'boolean' || 71 | typeof val === 'number' || 72 | typeof val === 'string' || 73 | typeof val === 'symbol' || 74 | typeof val === 'bigint' || 75 | typeof val === 'undefined'; 76 | } 77 | 78 | // Number Extension 79 | 80 | /** 81 | * returns true if val is integer 82 | */ 83 | export function isInteger(val?: unknown): val is number { 84 | if (isNullable(val)) return false; 85 | return Number.isInteger(val); 86 | } 87 | 88 | /** 89 | * returns true if val is integer, and between -2 ** 31 and 2 ** 31 - 1 90 | */ 91 | export function isInteger32(val?: unknown): val is number { 92 | if (!isInteger(val)) return false; 93 | return val >= -MAX_INT_31 && val <= MAX_INT_31 - 1; 94 | } 95 | 96 | /** 97 | * returns true if val is integer, and < -2 ** 31, and > 2 ** 31 - 1 98 | */ 99 | export function isLong(val?: unknown): val is number { 100 | if (!isInteger(val)) return false; 101 | return val < -MAX_INT_31 || val > MAX_INT_31 - 1; 102 | } 103 | 104 | /** 105 | * returns true if val is integer, and between -(2 ** 53 - 1) and 2 ** 53 - 1 106 | * @see {Number.isSafeInteger} 107 | */ 108 | export function isSafeInteger(val?: unknown): val is number { 109 | return Number.isSafeInteger(val); 110 | } 111 | 112 | /** 113 | * returns true if val is Double 114 | */ 115 | export function isDouble(val?: unknown): val is number { 116 | if (isNullable(val)) return false; 117 | return isNumber(val) 118 | && !isNaN(val) 119 | && val % 1 !== 0; 120 | } 121 | 122 | /** 123 | * returns true if val is NaN 124 | * @see {Number.isNaN} 125 | */ 126 | export function isNaN(val?: unknown): boolean { 127 | return Number.isNaN(val); 128 | } 129 | 130 | /** 131 | * returns true if val is finite 132 | * @see {Number.isFinite} 133 | */ 134 | export function isFinite(val?: unknown): val is number { 135 | return Number.isFinite(val); 136 | } 137 | -------------------------------------------------------------------------------- /src/types/std.ts: -------------------------------------------------------------------------------- 1 | // Javascript Standard Object 2 | 3 | import { types, debuglog } from 'node:util'; 4 | import { hasOwnProperty, isInstanceOf } from './util.js'; 5 | import type { Class } from './util.js'; 6 | 7 | const debug = debuglog('is-type-of:std'); 8 | 9 | // Array 10 | 11 | /** 12 | * returns true if val is array, it won't check items of array 13 | */ 14 | export function isArray(val?: unknown): val is Array { 15 | return Array.isArray(val); 16 | } 17 | 18 | // Function 19 | 20 | /** 21 | * returns true if val is function 22 | */ 23 | export function isFunction(val?: unknown): val is T { 24 | return typeof val === 'function'; 25 | } 26 | 27 | /** 28 | * returns true if val is generator function 29 | */ 30 | export function isGeneratorFunction(val?: unknown): val is GeneratorFunction { 31 | return types.isGeneratorFunction(val); 32 | } 33 | 34 | /** 35 | * returns true if val is async function 36 | */ 37 | export function isAsyncFunction(val?: unknown): val is Function { 38 | return types.isAsyncFunction(val); 39 | } 40 | 41 | /** 42 | * returns true if val is async generator function 43 | */ 44 | export function isAsyncGeneratorFunction(val?: unknown): val is AsyncGeneratorFunction { 45 | if (!isFunction(val)) return false; 46 | return val.constructor && val.constructor.name === 'AsyncGeneratorFunction'; 47 | } 48 | 49 | // Object 50 | 51 | /** 52 | * returns true if val is object 53 | */ 54 | export function isObject(val?: unknown): val is object { 55 | return typeof val === 'object' && val !== null; 56 | } 57 | 58 | /** 59 | * returns true if val is class 60 | * *Note:* "class" is supported in ECMAScript 6, and if the code is using some compiler or transpiler, the checking might fail 61 | */ 62 | export function isClass(val?: unknown): val is T { 63 | if (!isFunction(val)) return false; 64 | const fnStr = Function.prototype.toString.call(val); 65 | if (/^class[\s{]/.test(fnStr)) return true; 66 | debug('[isClass] fnStr: %s', fnStr); 67 | const fnBody = fnStr.replace(/^[^{]*{\s*/, '').replace(/\s*}[^}]*$/, ''); 68 | debug('[isClass] fnBody: %s', fnBody); 69 | return (/classCallCheck\(/.test(fnBody) || /TypeError\("Cannot call a class as a function"\)/.test(fnBody)); 70 | } 71 | 72 | /** 73 | * returns true if val is regular expression 74 | */ 75 | export function isRegExp(val?: unknown): val is RegExp { 76 | return isInstanceOf(val, RegExp); 77 | } 78 | 79 | /** 80 | * returns true if val is instance of Date 81 | */ 82 | export function isDate(val?: unknown): val is Date { 83 | return isInstanceOf(val, Date); 84 | } 85 | 86 | /** 87 | * returns true if val is instance of Error 88 | */ 89 | export function isError(val?: unknown): val is Error { 90 | return isInstanceOf(val, Error); 91 | } 92 | 93 | /** 94 | * returns true if val is generator 95 | */ 96 | export function isGenerator(val?: unknown): val is Generator { 97 | return types.isGeneratorObject(val); 98 | } 99 | 100 | /** 101 | * returns true if val is promise 102 | */ 103 | export function isPromise(val?: unknown): val is Promise { 104 | return types.isPromise(val); 105 | } 106 | 107 | /** 108 | * returns true if val is like promise, if the object has `then` property, the checking will pass. 109 | */ 110 | export function isPromiseLike(val?: unknown): val is PromiseLike { 111 | if (isPromise(val)) return true; 112 | if (!isObject(val)) return false; 113 | return hasOwnProperty(val, 'then') && typeof val.then === 'function'; 114 | } 115 | -------------------------------------------------------------------------------- /src/types/util.ts: -------------------------------------------------------------------------------- 1 | 2 | export type Class = new (...args: any[]) => any; 3 | 4 | export function isInstanceOf(val: unknown, Clazz: T): val is InstanceType { 5 | return val instanceof Clazz; 6 | } 7 | 8 | export function hasOwnProperty(obj: T, prop: PropertyKey): obj is T & Record { 9 | if (Object.hasOwn) { 10 | // Node.js >= 16 11 | return Object.hasOwn(obj, prop); 12 | } 13 | return Object.prototype.hasOwnProperty.call(obj, prop); 14 | } 15 | 16 | export function hasOwnPropertyInChain(obj: T, prop: PropertyKey): obj is T & Record { 17 | return prop in obj; 18 | } 19 | -------------------------------------------------------------------------------- /test/index.d.ts: -------------------------------------------------------------------------------- 1 | declare module 'long'; 2 | -------------------------------------------------------------------------------- /test/index.test.ts: -------------------------------------------------------------------------------- 1 | import assert from 'node:assert'; 2 | import is from '../src/index.js'; 3 | import { 4 | isBoolean as isBooleanExpect, 5 | isNumber as isNumberExpect, 6 | isString as isStringExpect, 7 | isSymbol as isSymbolExpect, 8 | isUndefined as isUndefinedExpect, 9 | isBigInt as isBigIntExpect, 10 | isNull as isNullExpect, 11 | isNullable as isNullableExpect, 12 | isInteger as isIntegerExpect, 13 | isInteger32 as isInteger32Expect, 14 | isLong as isLongExpect, 15 | isNaN as isNaNExpect, 16 | isFinite as isFiniteExpect, 17 | isSafeInteger as isSafeIntegerExpect, 18 | isLongObject as isLongObjectExpect, 19 | isDouble as isDoubleExpect, 20 | isPrimitive as isPrimitiveExpect, 21 | isArray as isArrayExpect, 22 | isFunction as isFunctionExpect, 23 | isGeneratorFunction as isGeneratorFunctionExpect, 24 | isAsyncFunction as isAsyncFunctionExpect, 25 | isAsyncGeneratorFunction as isAsyncGeneratorFunctionExpect, 26 | isObject as isObjectExpect, 27 | isClass as isClassExpect, 28 | isRegExp as isRegExpExpect, 29 | isDate as isDateExpect, 30 | isPromise as isPromiseExpect, 31 | isPromiseLike as isPromiseLikeExpect, 32 | isGenerator as isGeneratorExpect, 33 | isError as isErrorExpect, 34 | isBuffer as isBufferExpect, 35 | isDuplex as isDuplexExpect, 36 | isReadable as isReadableExpect, 37 | isStream as isStreamExpect, 38 | isWritable as isWritableExpect, 39 | } from '../src/index.js'; 40 | import { 41 | boolean as isBooleanCompatibleExpect, 42 | number as isNumberCompatibleExpect, 43 | string as isStringCompatibleExpect, 44 | symbol as isSymbolCompatibleExpect, 45 | undefined as isUndefinedCompatibleExpect, 46 | bigInt as isBigIntCompatibleExpect, 47 | null as isNullCompatibleExpect, 48 | int32 as isInteger32CompatibleExpect, 49 | Long as isLongObjectCompatibleExpect, 50 | long as isLongCompatibleExpect, 51 | double as isDoubleCompatibleExpect, 52 | NaN as isNaNCompatibleExpect, 53 | finite as isFiniteCompatibleExpect, 54 | primitive as isPrimitiveCompatibleExpect, 55 | array as isArrayCompatibleExpect, 56 | function as isFunctionCompatibleExpect, 57 | generatorFunction as isGeneratorFunctionCompatibleExpect, 58 | asyncFunction as isAsyncFunctionCompatibleExpect, 59 | object as isObjectCompatibleExpect, 60 | class as isClassCompatibleExpect, 61 | regExp as isRegExpCompatibleExpect, 62 | regexp as isRegexpCompatibleExpect, 63 | date as isDateCompatibleExpect, 64 | promise as isPromiseCompatibleExpect, 65 | generator as isGeneratorCompatibleExpect, 66 | error as isErrorCompatibleExpect, 67 | buffer as isBufferCompatibleExpect, 68 | duplex as isDuplexCompatibleExpect, 69 | readable as isReadableCompatibleExpect, 70 | stream as isStreamCompatibleExpect, 71 | writable as isWritableCompatibleExpect, 72 | } from '../src/index.js'; 73 | import { isBoolean, isNumber, isString, isSymbol, isUndefined, isBigInt, isNull, isNullable, isInteger, isInteger32, isLong, isDouble, isPrimitive, isFinite, isNaN, isSafeInteger } from '../src/types/primitive.js'; 74 | import { isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise } from '../src/types/std.js'; 75 | import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../src/types/node.js'; 76 | import { isLongObject } from '../src/types/external.js'; 77 | import { is as realIs } from '../src/is.js'; 78 | 79 | describe('test/index.test.ts', () => { 80 | 81 | it('check default export', async () => { 82 | assert.strictEqual(is, realIs); 83 | }); 84 | 85 | it('check method', async () => { 86 | assert.strictEqual(isBooleanExpect, isBoolean); 87 | assert.strictEqual(isNumberExpect, isNumber); 88 | assert.strictEqual(isStringExpect, isString); 89 | assert.strictEqual(isSymbolExpect, isSymbol); 90 | assert.strictEqual(isUndefinedExpect, isUndefined); 91 | assert.strictEqual(isBigIntExpect, isBigInt); 92 | assert.strictEqual(isNullExpect, isNull); 93 | assert.strictEqual(isNullableExpect, isNullable); 94 | assert.strictEqual(isIntegerExpect, isInteger); 95 | assert.strictEqual(isInteger32Expect, isInteger32); 96 | assert.strictEqual(isLongExpect, isLong); 97 | assert.strictEqual(isNaNExpect, isNaN); 98 | assert.strictEqual(isFiniteExpect, isFinite); 99 | assert.strictEqual(isDoubleExpect, isDouble); 100 | assert.strictEqual(isSafeIntegerExpect, isSafeInteger); 101 | assert.strictEqual(isPrimitiveExpect, isPrimitive); 102 | assert.strictEqual(isArrayExpect, isArray); 103 | assert.strictEqual(isFunctionExpect, isFunction); 104 | assert.strictEqual(isGeneratorFunctionExpect, isGeneratorFunction); 105 | assert.strictEqual(isAsyncFunctionExpect, isAsyncFunction); 106 | assert.strictEqual(isAsyncGeneratorFunctionExpect, isAsyncGeneratorFunction); 107 | assert.strictEqual(isObjectExpect, isObject); 108 | assert.strictEqual(isClassExpect, isClass); 109 | assert.strictEqual(isRegExpExpect, isRegExp); 110 | assert.strictEqual(isDateExpect, isDate); 111 | assert.strictEqual(isPromiseExpect, isPromise); 112 | assert.strictEqual(isPromiseLikeExpect, isPromiseLike); 113 | assert.strictEqual(isGeneratorExpect, isGenerator); 114 | assert.strictEqual(isErrorExpect, isError); 115 | assert.strictEqual(isBufferExpect, isBuffer); 116 | assert.strictEqual(isDuplexExpect, isDuplex); 117 | assert.strictEqual(isReadableExpect, isReadable); 118 | assert.strictEqual(isStreamExpect, isStream); 119 | assert.strictEqual(isWritableExpect, isWritable); 120 | assert.strictEqual(isLongObjectExpect, isLongObject); 121 | }); 122 | 123 | it('check compatible method', async () => { 124 | assert.strictEqual(isBooleanCompatibleExpect, isBoolean); 125 | assert.strictEqual(isNumberCompatibleExpect, isNumber); 126 | assert.strictEqual(isStringCompatibleExpect, isString); 127 | assert.strictEqual(isSymbolCompatibleExpect, isSymbol); 128 | assert.strictEqual(isUndefinedCompatibleExpect, isUndefined); 129 | assert.strictEqual(isBigIntCompatibleExpect, isBigInt); 130 | assert.strictEqual(isNullCompatibleExpect, isNull); 131 | assert.strictEqual(isInteger32CompatibleExpect, isInteger32); 132 | assert.strictEqual(isLongCompatibleExpect, isLong); 133 | assert.strictEqual(isLongObjectCompatibleExpect, isLongObject); 134 | assert.strictEqual(isNaNCompatibleExpect, isNaN); 135 | assert.strictEqual(isFiniteCompatibleExpect, isFinite); 136 | assert.strictEqual(isDoubleCompatibleExpect, isDouble); 137 | assert.strictEqual(isPrimitiveCompatibleExpect, isPrimitive); 138 | assert.strictEqual(isArrayCompatibleExpect, isArray); 139 | assert.strictEqual(isFunctionCompatibleExpect, isFunction); 140 | assert.strictEqual(isGeneratorFunctionCompatibleExpect, isGeneratorFunction); 141 | assert.strictEqual(isAsyncFunctionCompatibleExpect, isAsyncFunction); 142 | assert.strictEqual(isObjectCompatibleExpect, isObject); 143 | assert.strictEqual(isClassCompatibleExpect, isClass); 144 | assert.strictEqual(isRegExpCompatibleExpect, isRegExp); 145 | assert.strictEqual(isRegexpCompatibleExpect, isRegExp); 146 | assert.strictEqual(isDateCompatibleExpect, isDate); 147 | assert.strictEqual(isPromiseCompatibleExpect, isPromiseLike); 148 | assert.strictEqual(isGeneratorCompatibleExpect, isGenerator); 149 | assert.strictEqual(isErrorCompatibleExpect, isError); 150 | assert.strictEqual(isBufferCompatibleExpect, isBuffer); 151 | assert.strictEqual(isDuplexCompatibleExpect, isDuplex); 152 | assert.strictEqual(isReadableCompatibleExpect, isReadable); 153 | assert.strictEqual(isStreamCompatibleExpect, isStream); 154 | assert.strictEqual(isWritableCompatibleExpect, isWritable); 155 | }); 156 | }); 157 | -------------------------------------------------------------------------------- /test/is.test.ts: -------------------------------------------------------------------------------- 1 | import assert from 'node:assert'; 2 | import { is } from '../src/is.js'; 3 | import { isBoolean, isNumber, isString, isSymbol, isUndefined, isBigInt, isNull, isNullable, isInteger, isInteger32, isLong, isDouble, isPrimitive, isFinite, isNaN } from '../src/types/primitive.js'; 4 | import { isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise } from '../src/types/std.js'; 5 | import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../src/types/node.js'; 6 | import { isLongObject } from '../src/types/external.js'; 7 | 8 | describe('is', () => { 9 | 10 | it('check is export', async () => { 11 | assert.strictEqual(is.boolean, isBoolean); 12 | assert.strictEqual(is.number, isNumber); 13 | assert.strictEqual(is.string, isString); 14 | assert.strictEqual(is.symbol, isSymbol); 15 | assert.strictEqual(is.undefined, isUndefined); 16 | assert.strictEqual(is.null, isNull); 17 | assert.strictEqual(is.nullable, isNullable); 18 | assert.strictEqual(is.bigInt, isBigInt); 19 | assert.strictEqual(is.integer, isInteger); 20 | assert.strictEqual(is.integer32, isInteger32); 21 | assert.strictEqual(is.long, isLong); 22 | assert.strictEqual(is.double, isDouble); 23 | assert.strictEqual(is.finite, isFinite); 24 | assert.strictEqual(is.NaN, isNaN); 25 | assert.strictEqual(is.primitive, isPrimitive); 26 | assert.strictEqual(is.function, isFunction); 27 | assert.strictEqual(is.generatorFunction, isGeneratorFunction); 28 | assert.strictEqual(is.asyncFunction, isAsyncFunction); 29 | assert.strictEqual(is.asyncGeneratorFunction, isAsyncGeneratorFunction); 30 | assert.strictEqual(is.class, isClass); 31 | assert.strictEqual(is.array, isArray); 32 | assert.strictEqual(is.object, isObject); 33 | assert.strictEqual(is.date, isDate); 34 | assert.strictEqual(is.error, isError); 35 | assert.strictEqual(is.regExp, isRegExp); 36 | assert.strictEqual(is.generator, isGenerator); 37 | assert.strictEqual(is.promise, isPromise); 38 | assert.strictEqual(is.promiseLike, isPromiseLike); 39 | assert.strictEqual(is.buffer, isBuffer); 40 | assert.strictEqual(is.stream, isStream); 41 | assert.strictEqual(is.readable, isReadable); 42 | assert.strictEqual(is.writable, isWritable); 43 | assert.strictEqual(is.duplex, isDuplex); 44 | assert.strictEqual(is.longObject, isLongObject); 45 | }); 46 | 47 | }); 48 | -------------------------------------------------------------------------------- /test/types/external.test.ts: -------------------------------------------------------------------------------- 1 | import assert from 'node:assert'; 2 | import Long from 'long'; 3 | import { expectType } from 'ts-expect'; 4 | import { isLongObject } from '../../src/types/external.js'; 5 | 6 | describe('test/external.test.ts', () => { 7 | describe('isLongObject', () => { 8 | it('should check value', () => { 9 | assert.strictEqual(isLongObject(Long.fromNumber(Math.pow(2, 31))), true); 10 | assert.strictEqual(isLongObject(Long.fromString('1024102410241024')), true); 11 | assert.strictEqual(isLongObject(Long.fromString('1024102410241024')), true); 12 | 13 | assert.strictEqual(isLongObject(123), false); 14 | assert.strictEqual(isLongObject(), false); 15 | assert.strictEqual(isLongObject({}), false); 16 | assert.strictEqual(isLongObject({ high: 1 }), false); 17 | assert.strictEqual(isLongObject({ high: '1', low: '1' }), false); 18 | }); 19 | 20 | it('should check type', () => { 21 | const val: unknown = '1'; 22 | if (isLongObject(val)) { 23 | expectType<{ high: number, low: number }>(val); 24 | } 25 | }); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /test/types/node.test.ts: -------------------------------------------------------------------------------- 1 | import assert from 'node:assert'; 2 | import { EventEmitter } from 'node:events'; 3 | import { Duplex, PassThrough, Readable, Stream, Transform, Writable } from 'node:stream'; 4 | import util from 'node:util'; 5 | import { expectType } from 'ts-expect'; 6 | import { isBuffer, isDuplex, isReadable, isStream, isWritable } from '../../src/types/node.js'; 7 | 8 | describe('test/node.test.ts', () => { 9 | describe('isBuffer', () => { 10 | it('should check value', () => { 11 | assert.strictEqual(isBuffer(Buffer.alloc(0)), true); 12 | assert.strictEqual(isBuffer(Buffer.from('')), true); 13 | 14 | assert.strictEqual(isBuffer(null), false); 15 | assert.strictEqual(isBuffer({}), false); 16 | }); 17 | 18 | it('should check type', () => { 19 | const val: unknown = '1'; 20 | if (isBuffer(val)) { 21 | expectType(val); 22 | } 23 | }); 24 | }); 25 | 26 | describe('isStream', () => { 27 | it('should check value', () => { 28 | function FakeStream() { 29 | // @ts-ignore 30 | EventEmitter.call(this); 31 | } 32 | util.inherits(FakeStream, EventEmitter); 33 | FakeStream.prototype.pipe = function() {}; 34 | FakeStream.Stream = FakeStream; 35 | 36 | assert.strictEqual(isStream(new Readable()), true); 37 | assert.strictEqual(isStream(new Writable()), true); 38 | assert.strictEqual(isStream(new Transform()), true); 39 | assert.strictEqual(isStream(new Duplex()), true); 40 | assert.strictEqual(isStream(new PassThrough()), true); 41 | // @ts-ignore 42 | assert.strictEqual(isStream(new FakeStream()), false); 43 | assert.strictEqual(isStream({}), false); 44 | assert.strictEqual(isStream(), false); 45 | }); 46 | 47 | it('should check type', () => { 48 | const val: unknown = '1'; 49 | if (isStream(val)) { 50 | expectType(val); 51 | } 52 | }); 53 | }); 54 | 55 | describe('isReadable', () => { 56 | it('should check value', () => { 57 | assert.strictEqual(isReadable(new Readable()), true); 58 | assert.strictEqual(isReadable(new Writable()), false); 59 | assert.strictEqual(isReadable(new Transform()), true); 60 | assert.strictEqual(isReadable(new Duplex()), true); 61 | assert.strictEqual(isReadable(new PassThrough()), true); 62 | }); 63 | 64 | it('should check type', () => { 65 | const val: unknown = '1'; 66 | if (isReadable(val)) { 67 | expectType(val); 68 | expectType(val); 69 | } 70 | }); 71 | }); 72 | 73 | describe('isWritable', () => { 74 | it('should check value', () => { 75 | assert.strictEqual(isWritable(new Readable()), false); 76 | assert.strictEqual(isWritable(new Writable()), true); 77 | assert.strictEqual(isWritable(new Transform()), true); 78 | assert.strictEqual(isWritable(new Duplex()), true); 79 | assert.strictEqual(isWritable(new PassThrough()), true); 80 | }); 81 | 82 | it('should check type', () => { 83 | const val: unknown = '1'; 84 | if (isWritable(val)) { 85 | expectType(val); 86 | expectType(val); 87 | } 88 | }); 89 | }); 90 | 91 | describe('isDuplex', () => { 92 | it('should check value', () => { 93 | assert.strictEqual(isDuplex(new Readable()), false); 94 | assert.strictEqual(isDuplex(new Writable()), false); 95 | assert.strictEqual(isDuplex(new Transform()), true); 96 | assert.strictEqual(isDuplex(new Duplex()), true); 97 | assert.strictEqual(isDuplex(new PassThrough()), true); 98 | }); 99 | 100 | it('should check type', () => { 101 | const val: unknown = '1'; 102 | if (isDuplex(val)) { 103 | expectType(val); 104 | expectType(val); 105 | } 106 | }); 107 | }); 108 | }); 109 | -------------------------------------------------------------------------------- /test/types/primitive.test.ts: -------------------------------------------------------------------------------- 1 | import assert from 'node:assert'; 2 | import { expectNever, expectType } from 'ts-expect'; 3 | import { 4 | isBigInt, isBoolean, isNull, isNullable, isNumber, isPrimitive, isString, isSymbol, 5 | isUndefined, isInteger, isInteger32, isNaN, isFinite, isLong, isDouble, isSafeInteger, 6 | } from '../../src/types/primitive.js'; 7 | 8 | describe('test/primitive.test.ts', () => { 9 | describe('isString', () => { 10 | it('should check value', () => { 11 | assert.strictEqual(isString('1'), true); 12 | assert.strictEqual(isString(1), false); 13 | assert.strictEqual(isString(null), false); 14 | assert.strictEqual(isString(undefined), false); 15 | assert.strictEqual(isString(), false); 16 | assert.strictEqual(isString(new String('1')), false); 17 | }); 18 | 19 | it('should check type', () => { 20 | const val: unknown = '1'; 21 | if (isString(val)) { 22 | expectType(val); 23 | } 24 | }); 25 | }); 26 | 27 | describe('isNumber', () => { 28 | it('should check value', () => { 29 | assert.strictEqual(isNumber(1), true); 30 | assert.strictEqual(isNumber('1'), false); 31 | assert.strictEqual(isNumber(null), false); 32 | assert.strictEqual(isNumber(undefined), false); 33 | assert.strictEqual(isNumber(), false); 34 | assert.strictEqual(isNumber(new Number(1)), false); 35 | }); 36 | 37 | it('should check type', () => { 38 | const val: unknown = 1; 39 | if (isNumber(val)) { 40 | expectType(val); 41 | } 42 | }); 43 | }); 44 | 45 | describe('isBigInt', () => { 46 | it('should check value', () => { 47 | assert.strictEqual(isBigInt(1n), true); 48 | assert.strictEqual(isBigInt(1), false); 49 | assert.strictEqual(isBigInt('1'), false); 50 | assert.strictEqual(isBigInt(null), false); 51 | assert.strictEqual(isBigInt(undefined), false); 52 | assert.strictEqual(isBigInt(), false); 53 | }); 54 | 55 | it('should check type', () => { 56 | const val: unknown = 1n; 57 | if (isBigInt(val)) { 58 | expectType(val); 59 | } 60 | }); 61 | }); 62 | 63 | describe('isBoolean', () => { 64 | it('should check value', () => { 65 | assert.strictEqual(isBoolean(true), true); 66 | assert.strictEqual(isBoolean(false), true); 67 | assert.strictEqual(isBoolean('1'), false); 68 | assert.strictEqual(isBoolean(null), false); 69 | assert.strictEqual(isBoolean(undefined), false); 70 | assert.strictEqual(isBoolean(), false); 71 | assert.strictEqual(isBoolean(1), false); 72 | assert.strictEqual(isBoolean(new Boolean(true)), false); 73 | }); 74 | 75 | it('should check type', () => { 76 | const val: unknown = true; 77 | if (isBoolean(val)) { 78 | expectType(val); 79 | } 80 | }); 81 | }); 82 | 83 | describe('isSymbol', () => { 84 | it('should check value', () => { 85 | assert.strictEqual(isSymbol(Symbol('1')), true); 86 | assert.strictEqual(isSymbol('1'), false); 87 | assert.strictEqual(isSymbol(null), false); 88 | assert.strictEqual(isSymbol(undefined), false); 89 | assert.strictEqual(isSymbol(), false); 90 | assert.strictEqual(isSymbol(1), false); 91 | }); 92 | 93 | it('should check type', () => { 94 | const val: unknown = Symbol('1'); 95 | if (isSymbol(val)) { 96 | expectType(val); 97 | } 98 | }); 99 | }); 100 | 101 | describe('isUndefined', () => { 102 | it('should check value', () => { 103 | assert.strictEqual(isUndefined(undefined), true); 104 | assert.strictEqual(isUndefined(), true); 105 | assert.strictEqual(isUndefined('1'), false); 106 | assert.strictEqual(isUndefined(1), false); 107 | assert.strictEqual(isUndefined(null), false); 108 | }); 109 | 110 | it('should check type', () => { 111 | const val: unknown = undefined; 112 | if (isUndefined(val)) { 113 | expectType(val); 114 | } 115 | }); 116 | }); 117 | 118 | describe('isNull', () => { 119 | it('should check value', () => { 120 | assert.strictEqual(isNull(null), true); 121 | assert.strictEqual(isNull(undefined), false); 122 | assert.strictEqual(isNull(), false); 123 | assert.strictEqual(isNull('1'), false); 124 | assert.strictEqual(isNull(1), false); 125 | }); 126 | 127 | it('should check type', () => { 128 | const val: unknown = null; 129 | if (isNull(val)) { 130 | expectType(val); 131 | } 132 | }); 133 | }); 134 | 135 | describe('isNullable', () => { 136 | it('should check value', () => { 137 | assert.strictEqual(isNullable(null), true); 138 | assert.strictEqual(isNullable(undefined), true); 139 | assert.strictEqual(isNullable(), true); 140 | assert.strictEqual(isNullable('1'), false); 141 | assert.strictEqual(isNullable(1), false); 142 | }); 143 | 144 | it('should check type', () => { 145 | const val: unknown = null; 146 | if (isNullable(val)) { 147 | if (isNull(val)) { 148 | expectType(val); 149 | } else if (isUndefined(val)) { 150 | expectType(val); 151 | } else { 152 | expectNever(val); 153 | } 154 | } 155 | }); 156 | }); 157 | 158 | describe('isPrimitive', () => { 159 | it('should check value', () => { 160 | assert.strictEqual(isPrimitive('1'), true); 161 | assert.strictEqual(isPrimitive(null), true); 162 | assert.strictEqual(isPrimitive(1), true); 163 | assert.strictEqual(isPrimitive(undefined), true); 164 | assert.strictEqual(isPrimitive(), true); 165 | assert.strictEqual(isPrimitive(true), true); 166 | assert.strictEqual(isPrimitive(1n), true); 167 | assert.strictEqual(isPrimitive(Symbol('1')), true); 168 | assert.strictEqual(isPrimitive(new Date()), false); 169 | }); 170 | 171 | it('should check type', () => { 172 | const val: unknown = '1'; 173 | if (isPrimitive(val)) { 174 | if (isString(val)) { 175 | expectType(val); 176 | } else if (isNull(val)) { 177 | expectType(val); 178 | } else if (isNumber(val)) { 179 | expectType(val); 180 | } else if (isUndefined(val)) { 181 | expectType(val); 182 | } else if (isBoolean(val)) { 183 | expectType(val); 184 | } else if (isBigInt(val)) { 185 | expectType(val); 186 | } else if (isSymbol(val)) { 187 | expectType(val); 188 | } else { 189 | expectNever(val); 190 | } 191 | } 192 | }); 193 | }); 194 | 195 | describe('Number', () => { 196 | describe('isInteger', () => { 197 | it('should check value', () => { 198 | assert.strictEqual(isInteger(0), true); 199 | assert.strictEqual(isInteger(-100), true); 200 | assert.strictEqual(isInteger(100), true); 201 | assert.strictEqual(isInteger(Math.pow(2, 31)), true); 202 | assert.strictEqual(isInteger(Math.pow(2, 50)), true); 203 | assert.strictEqual(isInteger(-Math.pow(2, 31)), true); 204 | assert.strictEqual(isInteger(-Math.pow(2, 50)), true); 205 | 206 | assert.strictEqual(isInteger(0.1), false); 207 | assert.strictEqual(isInteger(-0.1), false); 208 | assert.strictEqual(isInteger(-111110.1), false); 209 | assert.strictEqual(isInteger(11110.12312321), false); 210 | assert.strictEqual(isInteger(''), false); 211 | assert.strictEqual(isInteger(), false); 212 | }); 213 | 214 | it('should check type', () => { 215 | const val: unknown = 1; 216 | if (isInteger(val)) { 217 | expectType(val); 218 | } 219 | }); 220 | }); 221 | 222 | describe('isInteger32', () => { 223 | it('should check value', () => { 224 | assert.strictEqual(isInteger32(0), true); 225 | assert.strictEqual(isInteger32(-100), true); 226 | assert.strictEqual(isInteger32(100), true); 227 | assert.strictEqual(isInteger32(Math.pow(2, 31) - 1), true); 228 | assert.strictEqual(isInteger32(-Math.pow(2, 31)), true); 229 | 230 | assert.strictEqual(isInteger32(Math.pow(2, 31)), false); 231 | assert.strictEqual(isInteger32(Math.pow(2, 50)), false); 232 | assert.strictEqual(isInteger32(-Math.pow(2, 31) - 1), false); 233 | assert.strictEqual(isInteger32(-Math.pow(2, 50)), false); 234 | assert.strictEqual(isInteger32(-Math.pow(2, 63)), false); 235 | assert.strictEqual(isInteger32(0.1), false); 236 | assert.strictEqual(isInteger32(-0.1), false); 237 | assert.strictEqual(isInteger32(-111110.1), false); 238 | assert.strictEqual(isInteger32(11110.12312321), false); 239 | assert.strictEqual(isInteger32('1.1'), false); 240 | assert.strictEqual(isInteger32(), false); 241 | }); 242 | 243 | it('should check type', () => { 244 | const val: unknown = 1; 245 | if (isInteger32(val)) { 246 | expectType(val); 247 | } 248 | }); 249 | }); 250 | 251 | describe('isNaN', () => { 252 | it('should check value', () => { 253 | assert.strictEqual(isNaN(NaN), true); 254 | assert.strictEqual(isNaN(Number.NaN), true); 255 | assert.strictEqual(isNaN(0 / 0), true); 256 | 257 | assert.strictEqual(isNaN(undefined), false); 258 | assert.strictEqual(isNaN({}), false); 259 | assert.strictEqual(isNaN(true), false); 260 | assert.strictEqual(isNaN(null), false); 261 | assert.strictEqual(isNaN(37), false); 262 | assert.strictEqual(isNaN('37'), false); 263 | assert.strictEqual(isNaN('37.37'), false); 264 | assert.strictEqual(isNaN(''), false); 265 | assert.strictEqual(isNaN(' '), false); 266 | assert.strictEqual(isNaN('NaN'), false); 267 | assert.strictEqual(isNaN('blabla'), false); 268 | assert.strictEqual(isNaN(), false); 269 | }); 270 | 271 | it('should check type', () => { 272 | const val: unknown = 1; 273 | expectType(isNaN(val)); 274 | }); 275 | }); 276 | 277 | describe('isFinite', () => { 278 | it('should check value', () => { 279 | assert.strictEqual(isFinite(37), true); 280 | 281 | assert.strictEqual(isFinite(NaN), false); 282 | assert.strictEqual(isFinite(Number.NaN), false); 283 | assert.strictEqual(isFinite(0 / 0), false); 284 | assert.strictEqual(isFinite(undefined), false); 285 | assert.strictEqual(isFinite({}), false); 286 | assert.strictEqual(isFinite(true), false); 287 | assert.strictEqual(isFinite(null), false); 288 | assert.strictEqual(isFinite('37'), false); 289 | assert.strictEqual(isFinite('37.37'), false); 290 | assert.strictEqual(isFinite(''), false); 291 | assert.strictEqual(isFinite(' '), false); 292 | assert.strictEqual(isFinite('NaN'), false); 293 | assert.strictEqual(isFinite('blabla'), false); 294 | assert.strictEqual(isFinite(), false); 295 | }); 296 | 297 | it('should check type', () => { 298 | const val: unknown = 1; 299 | if (isFinite(val)) { 300 | expectType(val); 301 | } 302 | }); 303 | }); 304 | 305 | describe('isLong', () => { 306 | it('should check value', () => { 307 | assert.strictEqual(isLong(Math.pow(2, 31)), true); 308 | assert.strictEqual(isLong(Math.pow(2, 50)), true); 309 | assert.strictEqual(isLong(-Math.pow(2, 31) - 1), true); 310 | assert.strictEqual(isLong(-Math.pow(2, 50)), true); 311 | assert.strictEqual(isLong(-Math.pow(2, 63)), true); 312 | 313 | assert.strictEqual(isLong(0.1), false); 314 | assert.strictEqual(isLong(-0.1), false); 315 | assert.strictEqual(isLong(-111110.1), false); 316 | assert.strictEqual(isLong(11110.12312321), false); 317 | assert.strictEqual(isLong('1.1'), false); 318 | assert.strictEqual(isLong(0), false); 319 | assert.strictEqual(isLong(-100), false); 320 | assert.strictEqual(isLong(100), false); 321 | assert.strictEqual(isLong(Math.pow(2, 31) - 1), false); 322 | assert.strictEqual(isLong(-Math.pow(2, 31)), false); 323 | assert.strictEqual(isLong(), false); 324 | }); 325 | 326 | it('should check type', () => { 327 | const val: unknown = 1; 328 | if (isLong(val)) { 329 | expectType(val); 330 | } 331 | }); 332 | }); 333 | 334 | describe('isDouble', () => { 335 | it('should check value', () => { 336 | assert.strictEqual(isDouble(0.1), true); 337 | assert.strictEqual(isDouble(-0.1), true); 338 | assert.strictEqual(isDouble(-111110.1), true); 339 | assert.strictEqual(isDouble(11110.12312321), true); 340 | 341 | assert.strictEqual(isDouble(0), false); 342 | assert.strictEqual(isDouble(-100), false); 343 | assert.strictEqual(isDouble(100), false); 344 | assert.strictEqual(isDouble(Math.pow(2, 31)), false); 345 | assert.strictEqual(isDouble(Math.pow(2, 50)), false); 346 | assert.strictEqual(isDouble(-Math.pow(2, 31)), false); 347 | assert.strictEqual(isDouble(-Math.pow(2, 50)), false); 348 | assert.strictEqual(isDouble(Number.NaN), false); 349 | assert.strictEqual(isDouble(), false); 350 | }); 351 | 352 | it('should check type', () => { 353 | const val: unknown = 1; 354 | if (isDouble(val)) { 355 | expectType(val); 356 | } 357 | }); 358 | }); 359 | 360 | describe('isSafeInteger', () => { 361 | it('should check value', () => { 362 | assert.strictEqual(isSafeInteger(0), true); 363 | assert.strictEqual(isSafeInteger(-100), true); 364 | assert.strictEqual(isSafeInteger(100), true); 365 | assert.strictEqual(isSafeInteger(Math.pow(2, 53) - 1), true); 366 | assert.strictEqual(isSafeInteger(-(Math.pow(2, 53) - 1)), true); 367 | 368 | assert.strictEqual(isSafeInteger(Math.pow(2, 53)), false); 369 | assert.strictEqual(isSafeInteger(-Math.pow(2, 53)), false); 370 | assert.strictEqual(isSafeInteger(0.1), false); 371 | assert.strictEqual(isSafeInteger(-0.1), false); 372 | assert.strictEqual(isSafeInteger(-111110.1), false); 373 | assert.strictEqual(isSafeInteger(11110.12312321), false); 374 | assert.strictEqual(isSafeInteger('1.1'), false); 375 | assert.strictEqual(isSafeInteger(), false); 376 | }); 377 | 378 | it('should check type', () => { 379 | const val: unknown = 1; 380 | if (isSafeInteger(val)) { 381 | expectType(val); 382 | } 383 | }); 384 | }); 385 | }); 386 | 387 | }); 388 | -------------------------------------------------------------------------------- /test/types/std.test.ts: -------------------------------------------------------------------------------- 1 | import { strict as assert } from 'node:assert'; 2 | import { expectType } from 'ts-expect'; 3 | import { 4 | isArray, isFunction, isGeneratorFunction, isAsyncFunction, isAsyncGeneratorFunction, isObject, 5 | isClass, isRegExp, isDate, isPromiseLike, isGenerator, isError, isPromise, 6 | } from '../../src/types/std.js'; 7 | import { Class, isInstanceOf } from '../../src/types/util.js'; 8 | 9 | describe('test/object.test.ts', () => { 10 | 11 | describe('isInstanceOf', () => { 12 | it('should check value', () => { 13 | class A {} 14 | class AA extends A {} 15 | class B {} 16 | const a = new A(); 17 | const aa = new AA(); 18 | assert.strictEqual(isInstanceOf(a, A), true); 19 | assert.strictEqual(isInstanceOf(aa, A), true); 20 | assert.strictEqual(isInstanceOf(aa, AA), true); 21 | 22 | assert.strictEqual(isInstanceOf(a, B), false); 23 | assert.strictEqual(isInstanceOf(a, Date), false); 24 | }); 25 | 26 | it('should check type', () => { 27 | class A { 28 | a: number; 29 | } 30 | class B { 31 | b: number; 32 | } 33 | function test(c: A | B) { 34 | expectType(c); 35 | if (isInstanceOf(c, A)) { 36 | expectType(c); 37 | } else { 38 | expectType(c); 39 | } 40 | } 41 | test(new A()); 42 | }); 43 | }); 44 | 45 | describe('Array', () => { 46 | describe('isArray', () => { 47 | it('should check value', () => { 48 | assert.strictEqual(isArray([]), true); 49 | assert.strictEqual(isArray(new Array([ 1, 2, 3 ])), true); 50 | 51 | assert.strictEqual(isArray({}), false); 52 | assert.strictEqual(isArray(), false); 53 | }); 54 | 55 | it('should check type', () => { 56 | const val: unknown = 1; 57 | if (isArray(val)) { 58 | expectType>(val); 59 | } 60 | }); 61 | }); 62 | }); 63 | 64 | describe('Function', () => { 65 | describe('isFunction', () => { 66 | it('should check value', () => { 67 | assert.strictEqual(isFunction(function() {}), true); 68 | assert.strictEqual(isFunction(() => {}), true); 69 | assert.strictEqual(isFunction(async () => { 70 | // ignore 71 | }), true); 72 | assert.strictEqual(isFunction(function* () { 73 | // ignore 74 | }), true); 75 | assert.strictEqual(isFunction(async function* () { 76 | // ignore 77 | }), true); 78 | 79 | assert.strictEqual(isFunction(null), false); 80 | assert.strictEqual(isFunction({}), false); 81 | assert.strictEqual(isFunction(), false); 82 | }); 83 | 84 | it('should check type', () => { 85 | const val: unknown = 1; 86 | if (isFunction(val)) { 87 | expectType(val); 88 | } 89 | }); 90 | }); 91 | 92 | describe('isGeneratorFunction', () => { 93 | it('should check value', () => { 94 | const gen1 = function* () { 95 | yield 1; 96 | return 2; 97 | }; 98 | assert.strictEqual(isGeneratorFunction(gen1), true); 99 | 100 | const gen2 = function* () { 101 | // ignore 102 | }; 103 | const fun = () => {}; 104 | const obj = {}; 105 | assert.strictEqual(isGeneratorFunction(gen2()), false); 106 | assert.strictEqual(isGeneratorFunction(fun), false); 107 | assert.strictEqual(isGeneratorFunction(obj), false); 108 | assert.strictEqual(isGeneratorFunction(), false); 109 | }); 110 | 111 | it('should check type', () => { 112 | const val: unknown = 1; 113 | if (isGeneratorFunction(val)) { 114 | expectType(val); 115 | expectType(val); 116 | } 117 | }); 118 | }); 119 | 120 | describe('isAsyncFunction', () => { 121 | it('should check value', () => { 122 | const asyncFun1 = async function() { 123 | await 1; 124 | return 2; 125 | }; 126 | assert.strictEqual(isAsyncFunction(asyncFun1), true); 127 | 128 | const asyncFun2 = async function() { 129 | // ignore 130 | }; 131 | assert.strictEqual(isAsyncFunction(asyncFun2()), false); 132 | const fun = () => {}; 133 | assert.strictEqual(isAsyncFunction(fun), false); 134 | const obj = {}; 135 | assert.strictEqual(isAsyncFunction(obj), false); 136 | assert.strictEqual(isAsyncFunction(), false); 137 | }); 138 | 139 | it('should check type', () => { 140 | const val: unknown = 1; 141 | if (isAsyncFunction(val)) { 142 | expectType(val); 143 | // expectType(val); 144 | } 145 | }); 146 | }); 147 | 148 | describe('isAsyncGeneratorFunction', () => { 149 | it('should check value', () => { 150 | const asyncGenFun1 = async function* () { 151 | yield 1; 152 | await 1; 153 | return 2; 154 | }; 155 | assert.strictEqual(isAsyncGeneratorFunction(asyncGenFun1), true); 156 | 157 | const asyncFun2 = async function() { 158 | // ignore 159 | }; 160 | const fun = () => {}; 161 | const obj = {}; 162 | assert.strictEqual(isAsyncGeneratorFunction(asyncFun2()), false); 163 | assert.strictEqual(isAsyncGeneratorFunction(fun), false); 164 | assert.strictEqual(isAsyncGeneratorFunction(obj), false); 165 | assert.strictEqual(isAsyncGeneratorFunction(), false); 166 | }); 167 | 168 | it('should check type', () => { 169 | const val: unknown = 1; 170 | if (isAsyncGeneratorFunction(val)) { 171 | expectType(val); 172 | expectType(val); 173 | } 174 | }); 175 | }); 176 | }); 177 | 178 | describe('Object', () => { 179 | describe('isObject', () => { 180 | it('should check value', () => { 181 | assert.strictEqual(isObject({}), true); 182 | assert.strictEqual(isObject([]), true); 183 | assert.strictEqual(isObject(new RegExp('')), true); 184 | assert.strictEqual(isObject(new Date()), true); 185 | }); 186 | 187 | it('should check type', () => { 188 | const val: unknown = 1; 189 | if (isAsyncGeneratorFunction(val)) { 190 | expectType(val); 191 | expectType(val); 192 | } 193 | }); 194 | }); 195 | 196 | describe('isClass', () => { 197 | // @ts-ignore 198 | it('should check value', () => { 199 | /* eslint-disable */ 200 | class Foo{}; 201 | assert.strictEqual(isClass(Foo), true); 202 | assert.strictEqual(isClass(class {}), true); 203 | assert.strictEqual(isClass(class{}), true); 204 | assert.strictEqual(isClass(class{ }), true); 205 | assert.strictEqual(isClass(class{constructor(){}}), true); 206 | assert.strictEqual(isClass(class {constructor(){}}), true); 207 | assert.strictEqual(isClass(class OK2{}), true); 208 | assert.strictEqual(isClass(class OK{constructor(){}}), true); 209 | assert.strictEqual(isClass(class _{constructor(){}}), true); 210 | assert.strictEqual(isClass(class _F___ {constructor(){}}), true); 211 | assert.strictEqual(isClass(class B extends(Foo){}), true); 212 | assert.strictEqual(isClass(class{constructor(app: any){console.log(app);}configDidLoad(){}}), true); 213 | const _classCallCheck = function (instance: any, Constructor: any) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }; 214 | function Babel() { 215 | // @ts-ignore 216 | _classCallCheck(this, Babel); 217 | }; 218 | assert.strictEqual(isClass(Babel), true); 219 | 220 | assert.strictEqual(isClass(), false); 221 | assert.strictEqual(isClass(''), false); 222 | assert.strictEqual(isClass(1), false); 223 | assert.strictEqual(isClass(null), false); 224 | assert.strictEqual(isClass({}), false); 225 | assert.strictEqual(isClass(function () {}), false); 226 | assert.strictEqual(isClass(async () => {}), false); 227 | assert.strictEqual(isClass(Foo.constructor), false); 228 | const obj = { 229 | m() {}, 230 | }; 231 | assert.strictEqual(isClass(obj.m), false); 232 | /* eslint-disable */ 233 | }); 234 | 235 | it('should check type', () => { 236 | const val: unknown = class {}; 237 | if (isClass(val)) { 238 | expectType(val); 239 | const v = new val(); 240 | expectType>(v); 241 | } 242 | }); 243 | }); 244 | 245 | describe('isRegExp', () => { 246 | it('should check value', () => { 247 | assert.strictEqual(isRegExp(null), false); 248 | assert.strictEqual(isRegExp('1'), false); 249 | assert.strictEqual(isRegExp(new RegExp('')), true); 250 | assert.strictEqual(isRegExp(/foo/), true); 251 | 252 | assert.strictEqual(isRegExp(null), false); 253 | assert.strictEqual(isRegExp('1'), false); 254 | assert.strictEqual(isRegExp(new RegExp('')), true); 255 | assert.strictEqual(isRegExp(/foo/), true); 256 | }); 257 | 258 | it('should check type', () => { 259 | const val: unknown = 1; 260 | if (isRegExp(val)) { 261 | expectType(val); 262 | } 263 | }); 264 | }); 265 | 266 | describe('isDate', () => { 267 | it('should check value', () => { 268 | assert.strictEqual(isDate(null), false); 269 | assert.strictEqual(isDate('1'), false); 270 | assert.strictEqual(isDate(new Date()), true); 271 | }); 272 | 273 | it('should check type', () => { 274 | const val: unknown = 1; 275 | if (isDate(val)) { 276 | expectType(val); 277 | } 278 | }); 279 | }); 280 | 281 | describe('isError', () => { 282 | it('should check value', () => { 283 | class CustomError extends Error { 284 | constructor(message: string) { 285 | super(message); 286 | Error.captureStackTrace(this, CustomError); 287 | } 288 | } 289 | 290 | assert.strictEqual(isError(null), false); 291 | assert.strictEqual(isError({ err: true }), false); 292 | assert.strictEqual(isError(new Error()), true); 293 | assert.strictEqual(isError(new TypeError()), true); 294 | assert.strictEqual(isError(new RangeError()), true); 295 | assert.strictEqual(isError(new ReferenceError()), true); 296 | assert.strictEqual(isError(new CustomError('c')), true); 297 | }); 298 | 299 | it('should check type', () => { 300 | const val: unknown = 1; 301 | if (isError(val)) { 302 | expectType(val); 303 | } 304 | 305 | class CustomError extends Error { 306 | constructor(message: string) { 307 | super(message); 308 | } 309 | getName(): string { 310 | return this.name; 311 | } 312 | } 313 | let val2: Error; 314 | val2 = new CustomError('a'); 315 | 316 | expectType(val2); 317 | if (isInstanceOf(val2, CustomError)) { 318 | expectType(val2); 319 | expectType(val2); 320 | expectType(val2.getName()); 321 | } 322 | }); 323 | }); 324 | 325 | describe('isGenerator', () => { 326 | it('should check value', () => { 327 | const gen = function* () { 328 | yield 1; 329 | return 2; 330 | }; 331 | assert.strictEqual(isGenerator(gen()), true); 332 | 333 | const fun = () => {}; 334 | const obj = {}; 335 | assert.strictEqual(isGenerator(gen), false); 336 | assert.strictEqual(isGenerator(fun), false); 337 | assert.strictEqual(isGenerator(obj), false); 338 | }); 339 | 340 | it('should check type', () => { 341 | const val: unknown = 1; 342 | if (isGenerator(val)) { 343 | expectType(val); 344 | } 345 | }); 346 | }); 347 | 348 | describe('isPromise', () => { 349 | it('should check value', () => { 350 | assert.strictEqual(isPromise(Promise.resolve()), true); 351 | assert.strictEqual(isPromise((async () => {})()), true); 352 | 353 | const pro = { 354 | then: () => {}, 355 | }; 356 | const hasthen = { then: 1 }; 357 | const obj = {}; 358 | const number = 1; 359 | assert.strictEqual(isPromise(pro), false); 360 | assert.strictEqual(isPromise(hasthen), false); 361 | assert.strictEqual(isPromise(obj), false); 362 | assert.strictEqual(isPromise(number), false); 363 | }); 364 | 365 | it('should check type', () => { 366 | const val: unknown = 1; 367 | if (isPromise(val)) { 368 | expectType>(val); 369 | } 370 | }); 371 | }); 372 | 373 | describe('isPromiseLike', () => { 374 | it('should check value', () => { 375 | const pro = { 376 | then: () => {}, 377 | }; 378 | assert.strictEqual(isPromiseLike(pro), true); 379 | assert.strictEqual(isPromiseLike(Promise.resolve()), true); 380 | assert.strictEqual(isPromiseLike((async () => {})()), true); 381 | 382 | const hasthen = { then: 1 }; 383 | const obj = {}; 384 | const number = 1; 385 | assert.strictEqual(isPromiseLike(hasthen), false); 386 | assert.strictEqual(isPromiseLike(obj), false); 387 | assert.strictEqual(isPromiseLike(number), false); 388 | }); 389 | 390 | it('should check type', () => { 391 | const val: unknown = 1; 392 | if (isPromiseLike(val)) { 393 | expectType>(val); 394 | } 395 | if (isPromise(val)) { 396 | expectType>(val); 397 | expectType>(val); 398 | } 399 | }); 400 | }); 401 | 402 | }); 403 | 404 | }); 405 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@eggjs/tsconfig", 3 | "compileOnSave": true, 4 | "compilerOptions": { 5 | "strict": true, 6 | "noImplicitAny": true, 7 | "target": "ES2022", 8 | "module": "NodeNext", 9 | "moduleResolution": "NodeNext", 10 | } 11 | } 12 | --------------------------------------------------------------------------------