├── .github ├── FUNDING.yml ├── assets │ ├── guards-social-cover.svg │ └── type-coercion.png └── workflows │ ├── ci.yaml │ ├── codeql-analysis.yml │ └── publish.yaml ├── .gitignore ├── .nycrc ├── .prettierignore ├── .prettierrc ├── LICENSE ├── README.md ├── SECURITY.md ├── deno ├── LICENSE ├── README.md ├── guards │ ├── convenience.ts │ ├── primitives.ts │ └── structural.ts ├── index.ts └── mod.ts ├── docs ├── README.md └── modules │ ├── convenience.md │ ├── primitives.md │ └── structural.md ├── lib ├── guards │ ├── convenience.ts │ ├── primitives.ts │ └── structural.ts └── index.ts ├── package-lock.json ├── package.json ├── test └── guards │ ├── convenience.test.ts │ ├── primitives.test.ts │ └── structural.test.ts └── tsconfig.json /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | patreon: sniptt 4 | -------------------------------------------------------------------------------- /.github/assets/guards-social-cover.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | guards-social-cover 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.github/assets/type-coercion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sniptt-official/guards/76ff862ed0eb2d3bcc622552e255426cf274a67b/.github/assets/type-coercion.png -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 2 | 3 | name: ci 4 | 5 | on: 6 | push: 7 | branches: [main] 8 | pull_request: 9 | branches: [main] 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | 15 | strategy: 16 | matrix: 17 | node-version: ['14.x', '15.x', '16.x'] 18 | 19 | steps: 20 | - uses: actions/checkout@v2 21 | - name: Use Node.js ${{ matrix.node-version }} 22 | uses: actions/setup-node@v1 23 | with: 24 | node-version: ${{ matrix.node-version }} 25 | - run: npm ci 26 | - run: npm run format:check 27 | - run: npm run build 28 | - run: npm test 29 | env: 30 | CI: true 31 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ main ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ main ] 20 | schedule: 21 | - cron: '0 0 * * 6' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'javascript' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 37 | # Learn more: 38 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 39 | 40 | steps: 41 | - name: Checkout repository 42 | uses: actions/checkout@v2 43 | 44 | # Initializes the CodeQL tools for scanning. 45 | - name: Initialize CodeQL 46 | uses: github/codeql-action/init@v1 47 | with: 48 | languages: ${{ matrix.language }} 49 | # If you wish to specify custom queries, you can do so here or in a config file. 50 | # By default, queries listed here will override any specified in a config file. 51 | # Prefix the list here with "+" to use these queries and those in the config file. 52 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 53 | 54 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 55 | # If this step fails, then you should remove it and run the build manually (see below) 56 | - name: Autobuild 57 | uses: github/codeql-action/autobuild@v1 58 | 59 | # ℹ️ Command-line programs to run using the OS shell. 60 | # 📚 https://git.io/JvXDl 61 | 62 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 63 | # and modify them (or add more) to build your code if your project 64 | # uses a compiled language 65 | 66 | #- run: | 67 | # make bootstrap 68 | # make release 69 | 70 | - name: Perform CodeQL Analysis 71 | uses: github/codeql-action/analyze@v1 72 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/en/actions/language-and-framework-guides/publishing-nodejs-packages 2 | 3 | name: publish 4 | 5 | on: 6 | push: 7 | tags: 8 | - v* 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v1 17 | with: 18 | node-version: '14.x' 19 | registry-url: 'https://registry.npmjs.org' 20 | - run: npm ci 21 | - run: npm run build 22 | - run: npm publish --access public 23 | env: 24 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Microbundle cache 58 | .rpt2_cache/ 59 | .rts2_cache_cjs/ 60 | .rts2_cache_es/ 61 | .rts2_cache_umd/ 62 | 63 | # Optional REPL history 64 | .node_repl_history 65 | 66 | # Output of 'npm pack' 67 | *.tgz 68 | 69 | # Yarn Integrity file 70 | .yarn-integrity 71 | 72 | # dotenv environment variables file 73 | .env 74 | .env.test 75 | .env.production 76 | 77 | # parcel-bundler cache (https://parceljs.org/) 78 | .cache 79 | .parcel-cache 80 | 81 | # Next.js build output 82 | .next 83 | out 84 | 85 | # Nuxt.js build / generate output 86 | .nuxt 87 | dist 88 | 89 | # Gatsby files 90 | .cache/ 91 | # Comment in the public line in if your project uses Gatsby and not Next.js 92 | # https://nextjs.org/blog/next-9-1#public-directory-support 93 | # public 94 | 95 | # vuepress build output 96 | .vuepress/dist 97 | 98 | # Serverless directories 99 | .serverless/ 100 | 101 | # FuseBox cache 102 | .fusebox/ 103 | 104 | # DynamoDB Local files 105 | .dynamodb/ 106 | 107 | # TernJS port file 108 | .tern-port 109 | 110 | # Stores VSCode versions used for testing VSCode extensions 111 | .vscode-test 112 | 113 | # yarn v2 114 | .yarn/cache 115 | .yarn/unplugged 116 | .yarn/build-state.yml 117 | .yarn/install-state.gz 118 | .pnp.* 119 | 120 | # TypeScript build 121 | build 122 | -------------------------------------------------------------------------------- /.nycrc: -------------------------------------------------------------------------------- 1 | { 2 | "reporter": ["lcov", "text", "text-summary"], 3 | "include": ["lib/**/*.ts"], 4 | "exclude": [], 5 | "check-coverage": true, 6 | "all": true, 7 | "branches": 30, 8 | "functions": 70, 9 | "lines": 70, 10 | "statements": 70 11 | } 12 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Microbundle cache 58 | .rpt2_cache/ 59 | .rts2_cache_cjs/ 60 | .rts2_cache_es/ 61 | .rts2_cache_umd/ 62 | 63 | # Optional REPL history 64 | .node_repl_history 65 | 66 | # Output of 'npm pack' 67 | *.tgz 68 | 69 | # Yarn Integrity file 70 | .yarn-integrity 71 | 72 | # dotenv environment variables file 73 | .env 74 | .env.test 75 | .env.production 76 | 77 | # parcel-bundler cache (https://parceljs.org/) 78 | .cache 79 | .parcel-cache 80 | 81 | # Next.js build output 82 | .next 83 | out 84 | 85 | # Nuxt.js build / generate output 86 | .nuxt 87 | dist 88 | 89 | # Gatsby files 90 | .cache/ 91 | # Comment in the public line in if your project uses Gatsby and not Next.js 92 | # https://nextjs.org/blog/next-9-1#public-directory-support 93 | # public 94 | 95 | # vuepress build output 96 | .vuepress/dist 97 | 98 | # Serverless directories 99 | .serverless/ 100 | 101 | # FuseBox cache 102 | .fusebox/ 103 | 104 | # DynamoDB Local files 105 | .dynamodb/ 106 | 107 | # TernJS port file 108 | .tern-port 109 | 110 | # Stores VSCode versions used for testing VSCode extensions 111 | .vscode-test 112 | 113 | # yarn v2 114 | .yarn/cache 115 | .yarn/unplugged 116 | .yarn/build-state.yml 117 | .yarn/install-state.gz 118 | .pnp.* 119 | 120 | # TypeScript build 121 | build 122 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "all", 3 | "tabWidth": 2, 4 | "singleQuote": true 5 | } 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Sniptt (sniptt.com) 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Guards Logo 4 | 5 |

6 | 7 |

8 | If you use this repo, star it ✨ 9 |

10 | 11 | *** 12 | 13 |

👻 Comprehensive collection of type guards for JavaScript and TypeScript

14 | 15 |

16 | Inspired by Elixir guards 17 |

18 | 19 |

20 | Zero dependencies 💪 21 |

22 | 23 | *** 24 | 25 | ## Install 26 | 27 | ### Node.js and the browser 28 | 29 | ```sh 30 | npm install @sniptt/guards 31 | ``` 32 | 33 | ### Deno 34 | 35 | ```typescript 36 | import { ... } from 'https://deno.land/x/guards/mod.ts' 37 | 38 | // TODO 39 | ``` 40 | 41 | ## Usage 42 | 43 | ### Foreword on JavaScript data types and data structures 44 | 45 | The latest ECMAScript standard defines nine types: 46 | 47 | * Six **Data Types** that are primitives, checked by `typeof` operator: 48 | * `undefined`: `typeof instance === "undefined"` 49 | * `Boolean`: `typeof instance === "boolean"` 50 | * `Number`: `typeof instance === "number"` 51 | * `String`: `typeof instance === "string"` 52 | * `BigInt`: `typeof instance === "bigint"` 53 | * `Symbol`: `typeof instance === "symbol"` 54 | * **Structural Types**: 55 | * `Object`: `typeof instance === "object"`. Special non-data but structural type for any constructed object instance also used as data structures: new `Object`, new `Array`, new `Map`, new `Set`, new `WeakMap`, new `WeakSet`, new `Date` and almost everything made with `new` keyword; 56 | * `Function` non data structure, though it also answers for `typeof` operator: `typeof instance === "function"`. This answer is done as a special shorthand for `Function`s, though every `Function` constructor is derived from `Object` constructor. 57 | * **Structural Root** Primitive 58 | * `null`: `typeof instance === "object"`. Special primitive type having additional usage for it's value: if object is not inherited, then `null` is shown; 59 | 60 | Source: 61 | 62 | ### Type coercion 63 | 64 | ![Type coercion example](.github/assets/type-coercion.png) 65 | 66 | ### Primitives 67 | 68 | Sample usage: 69 | 70 | ```typescript 71 | import { primitives } from '@sniptt/guards'; 72 | 73 | primitives.isNumber(val); 74 | ``` 75 | 76 | or 77 | 78 | ```typescript 79 | import { isNumber } from '@sniptt/guards'; 80 | 81 | isNumber(val); 82 | ``` 83 | 84 | #### `isBigInt` 85 | 86 | ```typescript 87 | import { isBigInt } from '@sniptt/guards'; 88 | 89 | let val: bigint | number; 90 | 91 | if (isBigInt(val)) { 92 | // TypeScript will infer val: bigint 93 | } else { 94 | // TypeScript will infer val: number 95 | } 96 | ``` 97 | 98 | #### `isBoolean` 99 | 100 | ```typescript 101 | import { isBoolean } from '@sniptt/guards'; 102 | 103 | let val: boolean | number; 104 | 105 | if (isBoolean(val)) { 106 | // TypeScript will infer val: boolean 107 | } else { 108 | // TypeScript will infer val: number 109 | } 110 | ``` 111 | 112 | #### `isNumber` 113 | 114 | Answers `false` to `NaN`! 115 | 116 | See also: 117 | 118 | * [isNumberOrNaN](#isnumberornan) 119 | * [isInteger](#isinteger) 120 | * [isBigInt](#isbigint) 121 | 122 | ```typescript 123 | import { isNumber } from '@sniptt/guards'; 124 | 125 | let val: number | string; 126 | 127 | if (isNumber(val)) { 128 | // TypeScript will infer val: number 129 | } else { 130 | // TypeScript will infer val: string 131 | } 132 | ``` 133 | 134 | #### `isString` 135 | 136 | ```typescript 137 | import { isString } from '@sniptt/guards'; 138 | 139 | let val: string | number; 140 | 141 | if (isString(val)) { 142 | // TypeScript will infer val: string 143 | } else { 144 | // TypeScript will infer val: number 145 | } 146 | ``` 147 | 148 | #### `isSymbol` 149 | 150 | ```typescript 151 | import { isSymbol } from '@sniptt/guards'; 152 | 153 | let val: symbol | string; 154 | 155 | if (isSymbol(val)) { 156 | // TypeScript will infer val: symbol 157 | } else { 158 | // TypeScript will infer val: string 159 | } 160 | ``` 161 | 162 | #### `isUndefined` 163 | 164 | ```typescript 165 | import { isUndefined } from '@sniptt/guards'; 166 | 167 | let val: undefined | null; 168 | 169 | if (isUndefined(val)) { 170 | // TypeScript will infer val: undefined 171 | } else { 172 | // TypeScript will infer val: null 173 | } 174 | ``` 175 | 176 | ### Structural 177 | 178 | Sample usage: 179 | 180 | ```typescript 181 | import { structural } from '@sniptt/guards'; 182 | 183 | structural.isMap(val); 184 | ``` 185 | 186 | or 187 | 188 | ```typescript 189 | import { isMap } from '@sniptt/guards'; 190 | 191 | isMap(val); 192 | ``` 193 | 194 | #### `isNull` 195 | 196 | Answers `true` if and only if `value === null`. 197 | 198 | #### `isFunction` 199 | 200 | Answers `true` if and only if `typeof value === "function"`. 201 | 202 | #### `isObject` 203 | 204 | Answers `false` to `null`! 205 | 206 | To check for array: 207 | 208 | ```typescript 209 | isArray(term) 210 | ``` 211 | 212 | To check for object *or* null: 213 | 214 | ```typescript 215 | isObjectOrNull(term) 216 | ``` 217 | 218 | #### `isArray` 219 | 220 | Answers `true` if and only if `Array.isArray(value) === true`. 221 | 222 | #### `isMap` 223 | 224 | Answers `true` if and only if `(value instanceof Map) === true`. 225 | 226 | #### `isSet` 227 | 228 | Answers `true` if and only if `(value instanceof Set) === true`. 229 | 230 | #### `isWeakMap` 231 | 232 | Answers `true` if and only if `(value instanceof WeakMap) === true`. 233 | 234 | #### `isWeakSet` 235 | 236 | Answers `true` if and only if `(value instanceof WeakSet) === true`. 237 | 238 | #### `isDate` 239 | 240 | Answers `true` if and only if `(value instanceof Date) === true`. 241 | 242 | ### Convenience 243 | 244 | Sample usage: 245 | 246 | ```typescript 247 | import { convenience } from '@sniptt/guards'; 248 | 249 | convenience.isNonEmptyArray(val); 250 | ``` 251 | 252 | or 253 | 254 | ```typescript 255 | import { isNonEmptyArray } from '@sniptt/guards'; 256 | 257 | isNonEmptyArray(val); 258 | ``` 259 | 260 | #### `isObjectOrNull` 261 | 262 | ```typescript 263 | test("isObjectOrNull", (t) => { 264 | t.is(convenience.isObjectOrNull({}), true); 265 | t.is(convenience.isObjectOrNull(null), true); 266 | t.is(convenience.isObjectOrNull(new Set()), true); 267 | }); 268 | ``` 269 | 270 | #### `isNonEmptyArray` 271 | 272 | ```typescript 273 | test("isNonEmptyArray", (t) => { 274 | t.is(convenience.isNonEmptyArray([1, 2]), true); 275 | t.is(convenience.isNonEmptyArray([1]), true); 276 | t.is(convenience.isNonEmptyArray([]), false); 277 | }); 278 | ``` 279 | 280 | #### `isNonEmptyString` 281 | 282 | ```typescript 283 | test("isNonEmptyString", (t) => { 284 | t.is(convenience.isNonEmptyString("a"), true); 285 | t.is(convenience.isNonEmptyString(""), false); 286 | }); 287 | ``` 288 | 289 | #### `isNumberOrNaN` 290 | 291 | ```typescript 292 | test("isNumberOrNaN", (t) => { 293 | t.is(convenience.isNumberOrNaN(0), true); 294 | t.is(convenience.isNumberOrNaN(42), true); 295 | t.is(convenience.isNumberOrNaN(-42), true); 296 | t.is(convenience.isNumberOrNaN(3.14), true); 297 | t.is(convenience.isNumberOrNaN(-3.14), true); 298 | t.is(convenience.isNumberOrNaN(Infinity), true); 299 | t.is(convenience.isNumberOrNaN(-Infinity), true); 300 | t.is(convenience.isNumberOrNaN(Number.MAX_SAFE_INTEGER), true); 301 | t.is(convenience.isNumberOrNaN(-Number.MAX_SAFE_INTEGER), true); 302 | t.is(convenience.isNumberOrNaN(NaN), true); 303 | t.is(convenience.isNumberOrNaN(BigInt(0)), false); 304 | }); 305 | ``` 306 | 307 | #### `isInteger` 308 | 309 | ```typescript 310 | test("isInteger", (t) => { 311 | t.is(convenience.isInteger(0), true); 312 | t.is(convenience.isInteger(42), true); 313 | t.is(convenience.isInteger(-42), true); 314 | t.is(convenience.isInteger(3.14), false); 315 | t.is(convenience.isInteger(-3.14), false); 316 | t.is(convenience.isInteger(Infinity), false); 317 | t.is(convenience.isInteger(-Infinity), false); 318 | t.is(convenience.isInteger(Number.MAX_SAFE_INTEGER), true); 319 | t.is(convenience.isInteger(-Number.MAX_SAFE_INTEGER), true); 320 | t.is(convenience.isInteger(NaN), false); 321 | }); 322 | ``` 323 | 324 | #### `isPositiveInteger` 325 | 326 | ```typescript 327 | test("isPositiveInteger", (t) => { 328 | t.is(convenience.isPositiveInteger(0), false); 329 | t.is(convenience.isPositiveInteger(42), true); 330 | t.is(convenience.isPositiveInteger(-42), false); 331 | t.is(convenience.isPositiveInteger(3.14), false); 332 | t.is(convenience.isPositiveInteger(-3.14), false); 333 | t.is(convenience.isPositiveInteger(Infinity), false); 334 | t.is(convenience.isPositiveInteger(-Infinity), false); 335 | t.is(convenience.isPositiveInteger(Number.MAX_SAFE_INTEGER), true); 336 | t.is(convenience.isPositiveInteger(-Number.MAX_SAFE_INTEGER), false); 337 | t.is(convenience.isPositiveInteger(NaN), false); 338 | }); 339 | ``` 340 | 341 | #### `isNonNegativeInteger` 342 | 343 | ```typescript 344 | test("isNonNegativeInteger", (t) => { 345 | t.is(convenience.isNonNegativeInteger(0), true); 346 | t.is(convenience.isNonNegativeInteger(42), true); 347 | t.is(convenience.isNonNegativeInteger(-42), false); 348 | t.is(convenience.isNonNegativeInteger(3.14), false); 349 | t.is(convenience.isNonNegativeInteger(-3.14), false); 350 | t.is(convenience.isNonNegativeInteger(Infinity), false); 351 | t.is(convenience.isNonNegativeInteger(-Infinity), false); 352 | t.is(convenience.isNonNegativeInteger(Number.MAX_SAFE_INTEGER), true); 353 | t.is(convenience.isNonNegativeInteger(-Number.MAX_SAFE_INTEGER), false); 354 | t.is(convenience.isNonNegativeInteger(NaN), false); 355 | }); 356 | ``` 357 | 358 | #### `isNegativeInteger` 359 | 360 | ```typescript 361 | test("isNegativeInteger", (t) => { 362 | t.is(convenience.isNegativeInteger(0), false); 363 | t.is(convenience.isNegativeInteger(42), false); 364 | t.is(convenience.isNegativeInteger(-42), true); 365 | t.is(convenience.isNegativeInteger(3.14), false); 366 | t.is(convenience.isNegativeInteger(-3.14), false); 367 | t.is(convenience.isNegativeInteger(Infinity), false); 368 | t.is(convenience.isNegativeInteger(-Infinity), false); 369 | t.is(convenience.isNegativeInteger(Number.MAX_SAFE_INTEGER), false); 370 | t.is(convenience.isNegativeInteger(-Number.MAX_SAFE_INTEGER), true); 371 | t.is(convenience.isNegativeInteger(NaN), false); 372 | }); 373 | ``` 374 | 375 | ## API Docs 376 | 377 | [Full API Documentation](docs/README.md). 378 | 379 | ## License 380 | 381 | See [LICENSE](LICENSE) 382 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | This project participates in the Responsible Disclosure Policy program for the Node.js Security Ecosystem. 2 | 3 | # Responsible Disclosure Policy 4 | 5 | A responsible disclosure policy helps protect the project and its users from security vulnerabilities discovered in the project’s scope by employing a process where vulnerabilities are publicly disclosed after a reasonable time period to allow patching the vulnerability. 6 | 7 | All security bugs are taken seriously and are considered as top priority. 8 | Your efforts to responsibly disclose your findings are appreciated and will be taken into account to acknowledge your contributions. 9 | 10 | ## Reporting a Security Issue 11 | 12 | Any security related issue should be reported to the [Node.js Ecosystem](https://hackerone.com/nodejs-ecosystem 13 | ) program hosted on HackerOne which follows the [3rd party responsible disclosure process](https://github.com/nodejs/security-wg/blob/master/processes/third_party_vuln_process.md) set by the Node.js Security WG. One may also directly contact the project’s maintainers, but through the HackerOne program the Security WG members will take care of triaging the vulnerability and invite project maintainers to participate in the report. 14 | 15 | As an alternative method, vulnerabilities can also be reported by emailing security-ecosystem@nodejs.org. 16 | -------------------------------------------------------------------------------- /deno/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Sniptt (sniptt.com) 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. 22 | -------------------------------------------------------------------------------- /deno/README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Guards Logo 4 | 5 |

6 | 7 |

8 | If you use this repo, star it ✨ 9 |

10 | 11 | *** 12 | 13 |

👻 Comprehensive collection of type guards for JavaScript and TypeScript

14 | 15 |

16 | Inspired by Elixir guards 17 |

18 | 19 |

20 | Zero dependencies 💪 21 |

22 | 23 | *** 24 | 25 | ## Install 26 | 27 | ### Node.js and the browser 28 | 29 | ```sh 30 | npm install @sniptt/guards 31 | ``` 32 | 33 | ### Deno 34 | 35 | ```typescript 36 | import { ... } from 'https://deno.land/x/guards/mod.ts' 37 | 38 | // TODO 39 | ``` 40 | 41 | ## Usage 42 | 43 | ### Foreword on JavaScript data types and data structures 44 | 45 | The latest ECMAScript standard defines nine types: 46 | 47 | * Six **Data Types** that are primitives, checked by `typeof` operator: 48 | * `undefined`: `typeof instance === "undefined"` 49 | * `Boolean`: `typeof instance === "boolean"` 50 | * `Number`: `typeof instance === "number"` 51 | * `String`: `typeof instance === "string"` 52 | * `BigInt`: `typeof instance === "bigint"` 53 | * `Symbol`: `typeof instance === "symbol"` 54 | * **Structural Types**: 55 | * `Object`: `typeof instance === "object"`. Special non-data but structural type for any constructed object instance also used as data structures: new `Object`, new `Array`, new `Map`, new `Set`, new `WeakMap`, new `WeakSet`, new `Date` and almost everything made with `new` keyword; 56 | * `Function` non data structure, though it also answers for `typeof` operator: `typeof instance === "function"`. This answer is done as a special shorthand for `Function`s, though every `Function` constructor is derived from `Object` constructor. 57 | * **Structural Root** Primitive 58 | * `null`: `typeof instance === "object"`. Special primitive type having additional usage for it's value: if object is not inherited, then `null` is shown; 59 | 60 | Source: 61 | 62 | ### Type coercion 63 | 64 | ![Type coercion example](.github/assets/type-coercion.png) 65 | 66 | ### Primitives 67 | 68 | Sample usage: 69 | 70 | ```typescript 71 | import { primitives } from '@sniptt/guards'; 72 | 73 | primitives.isNumber(val); 74 | ``` 75 | 76 | or 77 | 78 | ```typescript 79 | import { isNumber } from '@sniptt/guards'; 80 | 81 | isNumber(val); 82 | ``` 83 | 84 | #### `isBigInt` 85 | 86 | ```typescript 87 | import { isBigInt } from '@sniptt/guards'; 88 | 89 | let val: bigint | number; 90 | 91 | if (isBigInt(val)) { 92 | // TypeScript will infer val: bigint 93 | } else { 94 | // TypeScript will infer val: number 95 | } 96 | ``` 97 | 98 | #### `isBoolean` 99 | 100 | ```typescript 101 | import { isBoolean } from '@sniptt/guards'; 102 | 103 | let val: boolean | number; 104 | 105 | if (isBoolean(val)) { 106 | // TypeScript will infer val: boolean 107 | } else { 108 | // TypeScript will infer val: number 109 | } 110 | ``` 111 | 112 | #### `isNumber` 113 | 114 | Answers `false` to `NaN`! 115 | 116 | See also: 117 | 118 | * [isNumberOrNaN](#isnumberornan) 119 | * [isInteger](#isinteger) 120 | * [isBigInt](#isbigint) 121 | 122 | ```typescript 123 | import { isNumber } from '@sniptt/guards'; 124 | 125 | let val: number | string; 126 | 127 | if (isNumber(val)) { 128 | // TypeScript will infer val: number 129 | } else { 130 | // TypeScript will infer val: string 131 | } 132 | ``` 133 | 134 | #### `isString` 135 | 136 | ```typescript 137 | import { isString } from '@sniptt/guards'; 138 | 139 | let val: string | number; 140 | 141 | if (isString(val)) { 142 | // TypeScript will infer val: string 143 | } else { 144 | // TypeScript will infer val: number 145 | } 146 | ``` 147 | 148 | #### `isSymbol` 149 | 150 | ```typescript 151 | import { isSymbol } from '@sniptt/guards'; 152 | 153 | let val: symbol | string; 154 | 155 | if (isSymbol(val)) { 156 | // TypeScript will infer val: symbol 157 | } else { 158 | // TypeScript will infer val: string 159 | } 160 | ``` 161 | 162 | #### `isUndefined` 163 | 164 | ```typescript 165 | import { isUndefined } from '@sniptt/guards'; 166 | 167 | let val: undefined | null; 168 | 169 | if (isUndefined(val)) { 170 | // TypeScript will infer val: undefined 171 | } else { 172 | // TypeScript will infer val: null 173 | } 174 | ``` 175 | 176 | ### Structural 177 | 178 | Sample usage: 179 | 180 | ```typescript 181 | import { structural } from '@sniptt/guards'; 182 | 183 | structural.isMap(val); 184 | ``` 185 | 186 | or 187 | 188 | ```typescript 189 | import { isMap } from '@sniptt/guards'; 190 | 191 | isMap(val); 192 | ``` 193 | 194 | #### `isNull` 195 | 196 | Answers `true` if and only if `value === null`. 197 | 198 | #### `isFunction` 199 | 200 | Answers `true` if and only if `typeof value === "function"`. 201 | 202 | #### `isObject` 203 | 204 | Answers `false` to `null`! 205 | 206 | To check for array: 207 | 208 | ```typescript 209 | isArray(term) 210 | ``` 211 | 212 | To check for object *or* null: 213 | 214 | ```typescript 215 | isObjectOrNull(term) 216 | ``` 217 | 218 | #### `isArray` 219 | 220 | Answers `true` if and only if `Array.isArray(value) === true`. 221 | 222 | #### `isMap` 223 | 224 | Answers `true` if and only if `(value instanceof Map) === true`. 225 | 226 | #### `isSet` 227 | 228 | Answers `true` if and only if `(value instanceof Set) === true`. 229 | 230 | #### `isWeakMap` 231 | 232 | Answers `true` if and only if `(value instanceof WeakMap) === true`. 233 | 234 | #### `isWeakSet` 235 | 236 | Answers `true` if and only if `(value instanceof WeakSet) === true`. 237 | 238 | #### `isDate` 239 | 240 | Answers `true` if and only if `(value instanceof Date) === true`. 241 | 242 | ### Convenience 243 | 244 | Sample usage: 245 | 246 | ```typescript 247 | import { convenience } from '@sniptt/guards'; 248 | 249 | convenience.isNonEmptyArray(val); 250 | ``` 251 | 252 | or 253 | 254 | ```typescript 255 | import { isNonEmptyArray } from '@sniptt/guards'; 256 | 257 | isNonEmptyArray(val); 258 | ``` 259 | 260 | #### `isObjectOrNull` 261 | 262 | ```typescript 263 | test("isObjectOrNull", (t) => { 264 | t.is(convenience.isObjectOrNull({}), true); 265 | t.is(convenience.isObjectOrNull(null), true); 266 | t.is(convenience.isObjectOrNull(new Set()), true); 267 | }); 268 | ``` 269 | 270 | #### `isNonEmptyArray` 271 | 272 | ```typescript 273 | test("isNonEmptyArray", (t) => { 274 | t.is(convenience.isNonEmptyArray([1, 2]), true); 275 | t.is(convenience.isNonEmptyArray([1]), true); 276 | t.is(convenience.isNonEmptyArray([]), false); 277 | }); 278 | ``` 279 | 280 | #### `isNonEmptyString` 281 | 282 | ```typescript 283 | test("isNonEmptyString", (t) => { 284 | t.is(convenience.isNonEmptyString("a"), true); 285 | t.is(convenience.isNonEmptyString(""), false); 286 | }); 287 | ``` 288 | 289 | #### `isNumberOrNaN` 290 | 291 | ```typescript 292 | test("isNumberOrNaN", (t) => { 293 | t.is(convenience.isNumberOrNaN(0), true); 294 | t.is(convenience.isNumberOrNaN(42), true); 295 | t.is(convenience.isNumberOrNaN(-42), true); 296 | t.is(convenience.isNumberOrNaN(3.14), true); 297 | t.is(convenience.isNumberOrNaN(-3.14), true); 298 | t.is(convenience.isNumberOrNaN(Infinity), true); 299 | t.is(convenience.isNumberOrNaN(-Infinity), true); 300 | t.is(convenience.isNumberOrNaN(Number.MAX_SAFE_INTEGER), true); 301 | t.is(convenience.isNumberOrNaN(-Number.MAX_SAFE_INTEGER), true); 302 | t.is(convenience.isNumberOrNaN(NaN), true); 303 | t.is(convenience.isNumberOrNaN(BigInt(0)), false); 304 | }); 305 | ``` 306 | 307 | #### `isInteger` 308 | 309 | ```typescript 310 | test("isInteger", (t) => { 311 | t.is(convenience.isInteger(0), true); 312 | t.is(convenience.isInteger(42), true); 313 | t.is(convenience.isInteger(-42), true); 314 | t.is(convenience.isInteger(3.14), false); 315 | t.is(convenience.isInteger(-3.14), false); 316 | t.is(convenience.isInteger(Infinity), false); 317 | t.is(convenience.isInteger(-Infinity), false); 318 | t.is(convenience.isInteger(Number.MAX_SAFE_INTEGER), true); 319 | t.is(convenience.isInteger(-Number.MAX_SAFE_INTEGER), true); 320 | t.is(convenience.isInteger(NaN), false); 321 | }); 322 | ``` 323 | 324 | #### `isPositiveInteger` 325 | 326 | ```typescript 327 | test("isPositiveInteger", (t) => { 328 | t.is(convenience.isPositiveInteger(0), false); 329 | t.is(convenience.isPositiveInteger(42), true); 330 | t.is(convenience.isPositiveInteger(-42), false); 331 | t.is(convenience.isPositiveInteger(3.14), false); 332 | t.is(convenience.isPositiveInteger(-3.14), false); 333 | t.is(convenience.isPositiveInteger(Infinity), false); 334 | t.is(convenience.isPositiveInteger(-Infinity), false); 335 | t.is(convenience.isPositiveInteger(Number.MAX_SAFE_INTEGER), true); 336 | t.is(convenience.isPositiveInteger(-Number.MAX_SAFE_INTEGER), false); 337 | t.is(convenience.isPositiveInteger(NaN), false); 338 | }); 339 | ``` 340 | 341 | #### `isNonNegativeInteger` 342 | 343 | ```typescript 344 | test("isNonNegativeInteger", (t) => { 345 | t.is(convenience.isNonNegativeInteger(0), true); 346 | t.is(convenience.isNonNegativeInteger(42), true); 347 | t.is(convenience.isNonNegativeInteger(-42), false); 348 | t.is(convenience.isNonNegativeInteger(3.14), false); 349 | t.is(convenience.isNonNegativeInteger(-3.14), false); 350 | t.is(convenience.isNonNegativeInteger(Infinity), false); 351 | t.is(convenience.isNonNegativeInteger(-Infinity), false); 352 | t.is(convenience.isNonNegativeInteger(Number.MAX_SAFE_INTEGER), true); 353 | t.is(convenience.isNonNegativeInteger(-Number.MAX_SAFE_INTEGER), false); 354 | t.is(convenience.isNonNegativeInteger(NaN), false); 355 | }); 356 | ``` 357 | 358 | #### `isNegativeInteger` 359 | 360 | ```typescript 361 | test("isNegativeInteger", (t) => { 362 | t.is(convenience.isNegativeInteger(0), false); 363 | t.is(convenience.isNegativeInteger(42), false); 364 | t.is(convenience.isNegativeInteger(-42), true); 365 | t.is(convenience.isNegativeInteger(3.14), false); 366 | t.is(convenience.isNegativeInteger(-3.14), false); 367 | t.is(convenience.isNegativeInteger(Infinity), false); 368 | t.is(convenience.isNegativeInteger(-Infinity), false); 369 | t.is(convenience.isNegativeInteger(Number.MAX_SAFE_INTEGER), false); 370 | t.is(convenience.isNegativeInteger(-Number.MAX_SAFE_INTEGER), true); 371 | t.is(convenience.isNegativeInteger(NaN), false); 372 | }); 373 | ``` 374 | 375 | ## API Docs 376 | 377 | [Full API Documentation](docs/README.md). 378 | 379 | ## License 380 | 381 | See [LICENSE](LICENSE) 382 | -------------------------------------------------------------------------------- /deno/guards/convenience.ts: -------------------------------------------------------------------------------- 1 | import { isNumber, isString } from './primitives.ts'; 2 | import { isArray } from './structural.ts'; 3 | 4 | export const isObjectOrNull = (term: T | U): term is T => { 5 | return typeof term === 'object'; 6 | }; 7 | 8 | export const isNonEmptyArray = (term: Array | U): term is Array => { 9 | return isArray(term) && term.length > 0; 10 | }; 11 | 12 | export const isNonEmptyString = (term: string | U): term is string => { 13 | return isString(term) && term.length > 0; 14 | }; 15 | 16 | export const isNumberOrNaN = (term: number | U): term is number => { 17 | return typeof term === 'number'; 18 | }; 19 | 20 | export const isInteger = (term: number | U): term is number => { 21 | return isNumber(term) && Number.isInteger(term); 22 | }; 23 | 24 | export const isPositiveInteger = (term: number | U): term is number => { 25 | return isInteger(term) && term > 0; 26 | }; 27 | 28 | export const isNonNegativeInteger = (term: number | U): term is number => { 29 | return isInteger(term) && term >= 0; 30 | }; 31 | 32 | export const isNegativeInteger = (term: number | U): term is number => { 33 | return isInteger(term) && term < 0; 34 | }; 35 | -------------------------------------------------------------------------------- /deno/guards/primitives.ts: -------------------------------------------------------------------------------- 1 | // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures 2 | 3 | export const isUndefined = (term: T | undefined): term is undefined => { 4 | return typeof term === 'undefined'; 5 | }; 6 | 7 | export const isBoolean = (term: boolean | U): term is boolean => { 8 | return typeof term === 'boolean'; 9 | }; 10 | 11 | export const isNumber = (term: number | U): term is number => { 12 | return typeof term === 'number' && !Number.isNaN(term); 13 | }; 14 | 15 | export const isString = (term: string | U): term is string => { 16 | return typeof term === 'string'; 17 | }; 18 | 19 | export const isBigInt = (term: bigint | U): term is bigint => { 20 | return typeof term === 'bigint'; 21 | }; 22 | 23 | export const isSymbol = (term: symbol | U): term is symbol => { 24 | return typeof term === 'symbol'; 25 | }; 26 | -------------------------------------------------------------------------------- /deno/guards/structural.ts: -------------------------------------------------------------------------------- 1 | // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures 2 | 3 | export const isNull = (term: T | null): term is null => { 4 | return term === null; 5 | }; 6 | 7 | export const isFunction = (term: T | U): term is T => { 8 | return typeof term === 'function'; 9 | }; 10 | 11 | export const isObject = ( 12 | term: T | U, 13 | ): term is NonNullable => { 14 | return !isNull(term) && typeof term === 'object'; 15 | }; 16 | 17 | export const isArray = (term: Array | U): term is Array => { 18 | return Array.isArray(term); 19 | }; 20 | 21 | export const isMap = (term: Map | U): term is Map => { 22 | return term instanceof Map; 23 | }; 24 | 25 | export const isSet = (term: Set | U): term is Set => { 26 | return term instanceof Set; 27 | }; 28 | 29 | export const isWeakMap = ( 30 | term: WeakMap | U, 31 | ): term is WeakMap => { 32 | return term instanceof WeakMap; 33 | }; 34 | 35 | export const isWeakSet = ( 36 | term: WeakSet | U, 37 | ): term is WeakSet => { 38 | return term instanceof WeakSet; 39 | }; 40 | 41 | export const isDate = (term: Date | U): term is Date => { 42 | return term instanceof Date; 43 | }; 44 | -------------------------------------------------------------------------------- /deno/index.ts: -------------------------------------------------------------------------------- 1 | export * as convenience from './guards/convenience.ts'; 2 | export * from './guards/convenience.ts'; 3 | export * as primitives from './guards/primitives.ts'; 4 | export * from './guards/primitives.ts'; 5 | export * as structural from './guards/structural.ts'; 6 | export * from './guards/structural.ts'; 7 | -------------------------------------------------------------------------------- /deno/mod.ts: -------------------------------------------------------------------------------- 1 | export * from './index.ts'; 2 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | @sniptt/guards 2 | 3 | # @sniptt/guards 4 | 5 | ## Table of contents 6 | 7 | ### Namespaces 8 | 9 | - [convenience](modules/convenience.md) 10 | - [primitives](modules/primitives.md) 11 | - [structural](modules/structural.md) 12 | 13 | ### Functions 14 | 15 | - [isArray](README.md#isarray) 16 | - [isBigInt](README.md#isbigint) 17 | - [isBoolean](README.md#isboolean) 18 | - [isDate](README.md#isdate) 19 | - [isFunction](README.md#isfunction) 20 | - [isInteger](README.md#isinteger) 21 | - [isMap](README.md#ismap) 22 | - [isNegativeInteger](README.md#isnegativeinteger) 23 | - [isNonEmptyArray](README.md#isnonemptyarray) 24 | - [isNonEmptyString](README.md#isnonemptystring) 25 | - [isNonNegativeInteger](README.md#isnonnegativeinteger) 26 | - [isNull](README.md#isnull) 27 | - [isNumber](README.md#isnumber) 28 | - [isNumberOrNaN](README.md#isnumberornan) 29 | - [isObject](README.md#isobject) 30 | - [isObjectOrNull](README.md#isobjectornull) 31 | - [isPositiveInteger](README.md#ispositiveinteger) 32 | - [isSet](README.md#isset) 33 | - [isString](README.md#isstring) 34 | - [isSymbol](README.md#issymbol) 35 | - [isUndefined](README.md#isundefined) 36 | - [isWeakMap](README.md#isweakmap) 37 | - [isWeakSet](README.md#isweakset) 38 | 39 | ## Functions 40 | 41 | ### isArray 42 | 43 | ▸ `Const` **isArray**<`T`, `U`\>(`term`): term is T[] 44 | 45 | #### Type parameters 46 | 47 | | Name | 48 | | :------ | 49 | | `T` | 50 | | `U` | 51 | 52 | #### Parameters 53 | 54 | | Name | Type | 55 | | :------ | :------ | 56 | | `term` | `U` \| `T`[] | 57 | 58 | #### Returns 59 | 60 | term is T[] 61 | 62 | #### Defined in 63 | 64 | [guards/structural.ts:17](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L17) 65 | 66 | ___ 67 | 68 | ### isBigInt 69 | 70 | ▸ `Const` **isBigInt**<`U`\>(`term`): term is bigint 71 | 72 | #### Type parameters 73 | 74 | | Name | 75 | | :------ | 76 | | `U` | 77 | 78 | #### Parameters 79 | 80 | | Name | Type | 81 | | :------ | :------ | 82 | | `term` | `bigint` \| `U` | 83 | 84 | #### Returns 85 | 86 | term is bigint 87 | 88 | #### Defined in 89 | 90 | [guards/primitives.ts:19](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L19) 91 | 92 | ___ 93 | 94 | ### isBoolean 95 | 96 | ▸ `Const` **isBoolean**<`U`\>(`term`): term is boolean 97 | 98 | #### Type parameters 99 | 100 | | Name | 101 | | :------ | 102 | | `U` | 103 | 104 | #### Parameters 105 | 106 | | Name | Type | 107 | | :------ | :------ | 108 | | `term` | `boolean` \| `U` | 109 | 110 | #### Returns 111 | 112 | term is boolean 113 | 114 | #### Defined in 115 | 116 | [guards/primitives.ts:7](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L7) 117 | 118 | ___ 119 | 120 | ### isDate 121 | 122 | ▸ `Const` **isDate**<`U`\>(`term`): term is Date 123 | 124 | #### Type parameters 125 | 126 | | Name | 127 | | :------ | 128 | | `U` | 129 | 130 | #### Parameters 131 | 132 | | Name | Type | 133 | | :------ | :------ | 134 | | `term` | `U` \| `Date` | 135 | 136 | #### Returns 137 | 138 | term is Date 139 | 140 | #### Defined in 141 | 142 | [guards/structural.ts:41](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L41) 143 | 144 | ___ 145 | 146 | ### isFunction 147 | 148 | ▸ `Const` **isFunction**<`T`, `U`\>(`term`): term is T 149 | 150 | #### Type parameters 151 | 152 | | Name | Type | 153 | | :------ | :------ | 154 | | `T` | extends `Function` | 155 | | `U` | `U` | 156 | 157 | #### Parameters 158 | 159 | | Name | Type | 160 | | :------ | :------ | 161 | | `term` | `T` \| `U` | 162 | 163 | #### Returns 164 | 165 | term is T 166 | 167 | #### Defined in 168 | 169 | [guards/structural.ts:7](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L7) 170 | 171 | ___ 172 | 173 | ### isInteger 174 | 175 | ▸ `Const` **isInteger**<`U`\>(`term`): term is number 176 | 177 | #### Type parameters 178 | 179 | | Name | 180 | | :------ | 181 | | `U` | 182 | 183 | #### Parameters 184 | 185 | | Name | Type | 186 | | :------ | :------ | 187 | | `term` | `number` \| `U` | 188 | 189 | #### Returns 190 | 191 | term is number 192 | 193 | #### Defined in 194 | 195 | [guards/convenience.ts:20](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L20) 196 | 197 | ___ 198 | 199 | ### isMap 200 | 201 | ▸ `Const` **isMap**<`K`, `V`, `U`\>(`term`): term is Map 202 | 203 | #### Type parameters 204 | 205 | | Name | 206 | | :------ | 207 | | `K` | 208 | | `V` | 209 | | `U` | 210 | 211 | #### Parameters 212 | 213 | | Name | Type | 214 | | :------ | :------ | 215 | | `term` | `U` \| `Map`<`K`, `V`\> | 216 | 217 | #### Returns 218 | 219 | term is Map 220 | 221 | #### Defined in 222 | 223 | [guards/structural.ts:21](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L21) 224 | 225 | ___ 226 | 227 | ### isNegativeInteger 228 | 229 | ▸ `Const` **isNegativeInteger**<`U`\>(`term`): term is number 230 | 231 | #### Type parameters 232 | 233 | | Name | 234 | | :------ | 235 | | `U` | 236 | 237 | #### Parameters 238 | 239 | | Name | Type | 240 | | :------ | :------ | 241 | | `term` | `number` \| `U` | 242 | 243 | #### Returns 244 | 245 | term is number 246 | 247 | #### Defined in 248 | 249 | [guards/convenience.ts:32](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L32) 250 | 251 | ___ 252 | 253 | ### isNonEmptyArray 254 | 255 | ▸ `Const` **isNonEmptyArray**<`T`, `U`\>(`term`): term is T[] 256 | 257 | #### Type parameters 258 | 259 | | Name | 260 | | :------ | 261 | | `T` | 262 | | `U` | 263 | 264 | #### Parameters 265 | 266 | | Name | Type | 267 | | :------ | :------ | 268 | | `term` | `U` \| `T`[] | 269 | 270 | #### Returns 271 | 272 | term is T[] 273 | 274 | #### Defined in 275 | 276 | [guards/convenience.ts:8](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L8) 277 | 278 | ___ 279 | 280 | ### isNonEmptyString 281 | 282 | ▸ `Const` **isNonEmptyString**<`U`\>(`term`): term is string 283 | 284 | #### Type parameters 285 | 286 | | Name | 287 | | :------ | 288 | | `U` | 289 | 290 | #### Parameters 291 | 292 | | Name | Type | 293 | | :------ | :------ | 294 | | `term` | `string` \| `U` | 295 | 296 | #### Returns 297 | 298 | term is string 299 | 300 | #### Defined in 301 | 302 | [guards/convenience.ts:12](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L12) 303 | 304 | ___ 305 | 306 | ### isNonNegativeInteger 307 | 308 | ▸ `Const` **isNonNegativeInteger**<`U`\>(`term`): term is number 309 | 310 | #### Type parameters 311 | 312 | | Name | 313 | | :------ | 314 | | `U` | 315 | 316 | #### Parameters 317 | 318 | | Name | Type | 319 | | :------ | :------ | 320 | | `term` | `number` \| `U` | 321 | 322 | #### Returns 323 | 324 | term is number 325 | 326 | #### Defined in 327 | 328 | [guards/convenience.ts:28](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L28) 329 | 330 | ___ 331 | 332 | ### isNull 333 | 334 | ▸ `Const` **isNull**<`T`\>(`term`): term is null 335 | 336 | #### Type parameters 337 | 338 | | Name | 339 | | :------ | 340 | | `T` | 341 | 342 | #### Parameters 343 | 344 | | Name | Type | 345 | | :------ | :------ | 346 | | `term` | ``null`` \| `T` | 347 | 348 | #### Returns 349 | 350 | term is null 351 | 352 | #### Defined in 353 | 354 | [guards/structural.ts:3](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L3) 355 | 356 | ___ 357 | 358 | ### isNumber 359 | 360 | ▸ `Const` **isNumber**<`U`\>(`term`): term is number 361 | 362 | #### Type parameters 363 | 364 | | Name | 365 | | :------ | 366 | | `U` | 367 | 368 | #### Parameters 369 | 370 | | Name | Type | 371 | | :------ | :------ | 372 | | `term` | `number` \| `U` | 373 | 374 | #### Returns 375 | 376 | term is number 377 | 378 | #### Defined in 379 | 380 | [guards/primitives.ts:11](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L11) 381 | 382 | ___ 383 | 384 | ### isNumberOrNaN 385 | 386 | ▸ `Const` **isNumberOrNaN**<`U`\>(`term`): term is number 387 | 388 | #### Type parameters 389 | 390 | | Name | 391 | | :------ | 392 | | `U` | 393 | 394 | #### Parameters 395 | 396 | | Name | Type | 397 | | :------ | :------ | 398 | | `term` | `number` \| `U` | 399 | 400 | #### Returns 401 | 402 | term is number 403 | 404 | #### Defined in 405 | 406 | [guards/convenience.ts:16](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L16) 407 | 408 | ___ 409 | 410 | ### isObject 411 | 412 | ▸ `Const` **isObject**<`T`, `U`\>(`term`): term is NonNullable 413 | 414 | #### Type parameters 415 | 416 | | Name | Type | 417 | | :------ | :------ | 418 | | `T` | extends `object` | 419 | | `U` | `U` | 420 | 421 | #### Parameters 422 | 423 | | Name | Type | 424 | | :------ | :------ | 425 | | `term` | `T` \| `U` | 426 | 427 | #### Returns 428 | 429 | term is NonNullable 430 | 431 | #### Defined in 432 | 433 | [guards/structural.ts:11](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L11) 434 | 435 | ___ 436 | 437 | ### isObjectOrNull 438 | 439 | ▸ `Const` **isObjectOrNull**<`T`, `U`\>(`term`): term is T 440 | 441 | #### Type parameters 442 | 443 | | Name | Type | 444 | | :------ | :------ | 445 | | `T` | extends `object` | 446 | | `U` | `U` | 447 | 448 | #### Parameters 449 | 450 | | Name | Type | 451 | | :------ | :------ | 452 | | `term` | `T` \| `U` | 453 | 454 | #### Returns 455 | 456 | term is T 457 | 458 | #### Defined in 459 | 460 | [guards/convenience.ts:4](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L4) 461 | 462 | ___ 463 | 464 | ### isPositiveInteger 465 | 466 | ▸ `Const` **isPositiveInteger**<`U`\>(`term`): term is number 467 | 468 | #### Type parameters 469 | 470 | | Name | 471 | | :------ | 472 | | `U` | 473 | 474 | #### Parameters 475 | 476 | | Name | Type | 477 | | :------ | :------ | 478 | | `term` | `number` \| `U` | 479 | 480 | #### Returns 481 | 482 | term is number 483 | 484 | #### Defined in 485 | 486 | [guards/convenience.ts:24](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L24) 487 | 488 | ___ 489 | 490 | ### isSet 491 | 492 | ▸ `Const` **isSet**<`T`, `U`\>(`term`): term is Set 493 | 494 | #### Type parameters 495 | 496 | | Name | 497 | | :------ | 498 | | `T` | 499 | | `U` | 500 | 501 | #### Parameters 502 | 503 | | Name | Type | 504 | | :------ | :------ | 505 | | `term` | `U` \| `Set`<`T`\> | 506 | 507 | #### Returns 508 | 509 | term is Set 510 | 511 | #### Defined in 512 | 513 | [guards/structural.ts:25](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L25) 514 | 515 | ___ 516 | 517 | ### isString 518 | 519 | ▸ `Const` **isString**<`U`\>(`term`): term is string 520 | 521 | #### Type parameters 522 | 523 | | Name | 524 | | :------ | 525 | | `U` | 526 | 527 | #### Parameters 528 | 529 | | Name | Type | 530 | | :------ | :------ | 531 | | `term` | `string` \| `U` | 532 | 533 | #### Returns 534 | 535 | term is string 536 | 537 | #### Defined in 538 | 539 | [guards/primitives.ts:15](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L15) 540 | 541 | ___ 542 | 543 | ### isSymbol 544 | 545 | ▸ `Const` **isSymbol**<`U`\>(`term`): term is symbol 546 | 547 | #### Type parameters 548 | 549 | | Name | 550 | | :------ | 551 | | `U` | 552 | 553 | #### Parameters 554 | 555 | | Name | Type | 556 | | :------ | :------ | 557 | | `term` | `symbol` \| `U` | 558 | 559 | #### Returns 560 | 561 | term is symbol 562 | 563 | #### Defined in 564 | 565 | [guards/primitives.ts:23](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L23) 566 | 567 | ___ 568 | 569 | ### isUndefined 570 | 571 | ▸ `Const` **isUndefined**<`T`\>(`term`): term is undefined 572 | 573 | #### Type parameters 574 | 575 | | Name | 576 | | :------ | 577 | | `T` | 578 | 579 | #### Parameters 580 | 581 | | Name | Type | 582 | | :------ | :------ | 583 | | `term` | `undefined` \| `T` | 584 | 585 | #### Returns 586 | 587 | term is undefined 588 | 589 | #### Defined in 590 | 591 | [guards/primitives.ts:3](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L3) 592 | 593 | ___ 594 | 595 | ### isWeakMap 596 | 597 | ▸ `Const` **isWeakMap**<`K`, `V`, `U`\>(`term`): term is WeakMap 598 | 599 | #### Type parameters 600 | 601 | | Name | Type | 602 | | :------ | :------ | 603 | | `K` | extends `object` | 604 | | `V` | `V` | 605 | | `U` | `U` | 606 | 607 | #### Parameters 608 | 609 | | Name | Type | 610 | | :------ | :------ | 611 | | `term` | `U` \| `WeakMap`<`K`, `V`\> | 612 | 613 | #### Returns 614 | 615 | term is WeakMap 616 | 617 | #### Defined in 618 | 619 | [guards/structural.ts:29](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L29) 620 | 621 | ___ 622 | 623 | ### isWeakSet 624 | 625 | ▸ `Const` **isWeakSet**<`T`, `U`\>(`term`): term is WeakSet 626 | 627 | #### Type parameters 628 | 629 | | Name | Type | 630 | | :------ | :------ | 631 | | `T` | extends `object` | 632 | | `U` | `U` | 633 | 634 | #### Parameters 635 | 636 | | Name | Type | 637 | | :------ | :------ | 638 | | `term` | `U` \| `WeakSet`<`T`\> | 639 | 640 | #### Returns 641 | 642 | term is WeakSet 643 | 644 | #### Defined in 645 | 646 | [guards/structural.ts:35](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L35) 647 | -------------------------------------------------------------------------------- /docs/modules/convenience.md: -------------------------------------------------------------------------------- 1 | [@sniptt/guards](../README.md) / convenience 2 | 3 | # Namespace: convenience 4 | 5 | ## Table of contents 6 | 7 | ### Functions 8 | 9 | - [isInteger](convenience.md#isinteger) 10 | - [isNegativeInteger](convenience.md#isnegativeinteger) 11 | - [isNonEmptyArray](convenience.md#isnonemptyarray) 12 | - [isNonEmptyString](convenience.md#isnonemptystring) 13 | - [isNonNegativeInteger](convenience.md#isnonnegativeinteger) 14 | - [isNumberOrNaN](convenience.md#isnumberornan) 15 | - [isObjectOrNull](convenience.md#isobjectornull) 16 | - [isPositiveInteger](convenience.md#ispositiveinteger) 17 | 18 | ## Functions 19 | 20 | ### isInteger 21 | 22 | ▸ `Const` **isInteger**<`U`\>(`term`): term is number 23 | 24 | #### Type parameters 25 | 26 | | Name | 27 | | :------ | 28 | | `U` | 29 | 30 | #### Parameters 31 | 32 | | Name | Type | 33 | | :------ | :------ | 34 | | `term` | `number` \| `U` | 35 | 36 | #### Returns 37 | 38 | term is number 39 | 40 | #### Defined in 41 | 42 | [guards/convenience.ts:20](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L20) 43 | 44 | ___ 45 | 46 | ### isNegativeInteger 47 | 48 | ▸ `Const` **isNegativeInteger**<`U`\>(`term`): term is number 49 | 50 | #### Type parameters 51 | 52 | | Name | 53 | | :------ | 54 | | `U` | 55 | 56 | #### Parameters 57 | 58 | | Name | Type | 59 | | :------ | :------ | 60 | | `term` | `number` \| `U` | 61 | 62 | #### Returns 63 | 64 | term is number 65 | 66 | #### Defined in 67 | 68 | [guards/convenience.ts:32](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L32) 69 | 70 | ___ 71 | 72 | ### isNonEmptyArray 73 | 74 | ▸ `Const` **isNonEmptyArray**<`T`, `U`\>(`term`): term is T[] 75 | 76 | #### Type parameters 77 | 78 | | Name | 79 | | :------ | 80 | | `T` | 81 | | `U` | 82 | 83 | #### Parameters 84 | 85 | | Name | Type | 86 | | :------ | :------ | 87 | | `term` | `U` \| `T`[] | 88 | 89 | #### Returns 90 | 91 | term is T[] 92 | 93 | #### Defined in 94 | 95 | [guards/convenience.ts:8](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L8) 96 | 97 | ___ 98 | 99 | ### isNonEmptyString 100 | 101 | ▸ `Const` **isNonEmptyString**<`U`\>(`term`): term is string 102 | 103 | #### Type parameters 104 | 105 | | Name | 106 | | :------ | 107 | | `U` | 108 | 109 | #### Parameters 110 | 111 | | Name | Type | 112 | | :------ | :------ | 113 | | `term` | `string` \| `U` | 114 | 115 | #### Returns 116 | 117 | term is string 118 | 119 | #### Defined in 120 | 121 | [guards/convenience.ts:12](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L12) 122 | 123 | ___ 124 | 125 | ### isNonNegativeInteger 126 | 127 | ▸ `Const` **isNonNegativeInteger**<`U`\>(`term`): term is number 128 | 129 | #### Type parameters 130 | 131 | | Name | 132 | | :------ | 133 | | `U` | 134 | 135 | #### Parameters 136 | 137 | | Name | Type | 138 | | :------ | :------ | 139 | | `term` | `number` \| `U` | 140 | 141 | #### Returns 142 | 143 | term is number 144 | 145 | #### Defined in 146 | 147 | [guards/convenience.ts:28](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L28) 148 | 149 | ___ 150 | 151 | ### isNumberOrNaN 152 | 153 | ▸ `Const` **isNumberOrNaN**<`U`\>(`term`): term is number 154 | 155 | #### Type parameters 156 | 157 | | Name | 158 | | :------ | 159 | | `U` | 160 | 161 | #### Parameters 162 | 163 | | Name | Type | 164 | | :------ | :------ | 165 | | `term` | `number` \| `U` | 166 | 167 | #### Returns 168 | 169 | term is number 170 | 171 | #### Defined in 172 | 173 | [guards/convenience.ts:16](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L16) 174 | 175 | ___ 176 | 177 | ### isObjectOrNull 178 | 179 | ▸ `Const` **isObjectOrNull**<`T`, `U`\>(`term`): term is T 180 | 181 | #### Type parameters 182 | 183 | | Name | Type | 184 | | :------ | :------ | 185 | | `T` | extends `object` | 186 | | `U` | `U` | 187 | 188 | #### Parameters 189 | 190 | | Name | Type | 191 | | :------ | :------ | 192 | | `term` | `T` \| `U` | 193 | 194 | #### Returns 195 | 196 | term is T 197 | 198 | #### Defined in 199 | 200 | [guards/convenience.ts:4](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L4) 201 | 202 | ___ 203 | 204 | ### isPositiveInteger 205 | 206 | ▸ `Const` **isPositiveInteger**<`U`\>(`term`): term is number 207 | 208 | #### Type parameters 209 | 210 | | Name | 211 | | :------ | 212 | | `U` | 213 | 214 | #### Parameters 215 | 216 | | Name | Type | 217 | | :------ | :------ | 218 | | `term` | `number` \| `U` | 219 | 220 | #### Returns 221 | 222 | term is number 223 | 224 | #### Defined in 225 | 226 | [guards/convenience.ts:24](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/convenience.ts#L24) 227 | -------------------------------------------------------------------------------- /docs/modules/primitives.md: -------------------------------------------------------------------------------- 1 | [@sniptt/guards](../README.md) / primitives 2 | 3 | # Namespace: primitives 4 | 5 | ## Table of contents 6 | 7 | ### Functions 8 | 9 | - [isBigInt](primitives.md#isbigint) 10 | - [isBoolean](primitives.md#isboolean) 11 | - [isNumber](primitives.md#isnumber) 12 | - [isString](primitives.md#isstring) 13 | - [isSymbol](primitives.md#issymbol) 14 | - [isUndefined](primitives.md#isundefined) 15 | 16 | ## Functions 17 | 18 | ### isBigInt 19 | 20 | ▸ `Const` **isBigInt**<`U`\>(`term`): term is bigint 21 | 22 | #### Type parameters 23 | 24 | | Name | 25 | | :------ | 26 | | `U` | 27 | 28 | #### Parameters 29 | 30 | | Name | Type | 31 | | :------ | :------ | 32 | | `term` | `bigint` \| `U` | 33 | 34 | #### Returns 35 | 36 | term is bigint 37 | 38 | #### Defined in 39 | 40 | [guards/primitives.ts:19](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L19) 41 | 42 | ___ 43 | 44 | ### isBoolean 45 | 46 | ▸ `Const` **isBoolean**<`U`\>(`term`): term is boolean 47 | 48 | #### Type parameters 49 | 50 | | Name | 51 | | :------ | 52 | | `U` | 53 | 54 | #### Parameters 55 | 56 | | Name | Type | 57 | | :------ | :------ | 58 | | `term` | `boolean` \| `U` | 59 | 60 | #### Returns 61 | 62 | term is boolean 63 | 64 | #### Defined in 65 | 66 | [guards/primitives.ts:7](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L7) 67 | 68 | ___ 69 | 70 | ### isNumber 71 | 72 | ▸ `Const` **isNumber**<`U`\>(`term`): term is number 73 | 74 | #### Type parameters 75 | 76 | | Name | 77 | | :------ | 78 | | `U` | 79 | 80 | #### Parameters 81 | 82 | | Name | Type | 83 | | :------ | :------ | 84 | | `term` | `number` \| `U` | 85 | 86 | #### Returns 87 | 88 | term is number 89 | 90 | #### Defined in 91 | 92 | [guards/primitives.ts:11](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L11) 93 | 94 | ___ 95 | 96 | ### isString 97 | 98 | ▸ `Const` **isString**<`U`\>(`term`): term is string 99 | 100 | #### Type parameters 101 | 102 | | Name | 103 | | :------ | 104 | | `U` | 105 | 106 | #### Parameters 107 | 108 | | Name | Type | 109 | | :------ | :------ | 110 | | `term` | `string` \| `U` | 111 | 112 | #### Returns 113 | 114 | term is string 115 | 116 | #### Defined in 117 | 118 | [guards/primitives.ts:15](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L15) 119 | 120 | ___ 121 | 122 | ### isSymbol 123 | 124 | ▸ `Const` **isSymbol**<`U`\>(`term`): term is symbol 125 | 126 | #### Type parameters 127 | 128 | | Name | 129 | | :------ | 130 | | `U` | 131 | 132 | #### Parameters 133 | 134 | | Name | Type | 135 | | :------ | :------ | 136 | | `term` | `symbol` \| `U` | 137 | 138 | #### Returns 139 | 140 | term is symbol 141 | 142 | #### Defined in 143 | 144 | [guards/primitives.ts:23](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L23) 145 | 146 | ___ 147 | 148 | ### isUndefined 149 | 150 | ▸ `Const` **isUndefined**<`T`\>(`term`): term is undefined 151 | 152 | #### Type parameters 153 | 154 | | Name | 155 | | :------ | 156 | | `T` | 157 | 158 | #### Parameters 159 | 160 | | Name | Type | 161 | | :------ | :------ | 162 | | `term` | `undefined` \| `T` | 163 | 164 | #### Returns 165 | 166 | term is undefined 167 | 168 | #### Defined in 169 | 170 | [guards/primitives.ts:3](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/primitives.ts#L3) 171 | -------------------------------------------------------------------------------- /docs/modules/structural.md: -------------------------------------------------------------------------------- 1 | [@sniptt/guards](../README.md) / structural 2 | 3 | # Namespace: structural 4 | 5 | ## Table of contents 6 | 7 | ### Functions 8 | 9 | - [isArray](structural.md#isarray) 10 | - [isDate](structural.md#isdate) 11 | - [isFunction](structural.md#isfunction) 12 | - [isMap](structural.md#ismap) 13 | - [isNull](structural.md#isnull) 14 | - [isObject](structural.md#isobject) 15 | - [isSet](structural.md#isset) 16 | - [isWeakMap](structural.md#isweakmap) 17 | - [isWeakSet](structural.md#isweakset) 18 | 19 | ## Functions 20 | 21 | ### isArray 22 | 23 | ▸ `Const` **isArray**<`T`, `U`\>(`term`): term is T[] 24 | 25 | #### Type parameters 26 | 27 | | Name | 28 | | :------ | 29 | | `T` | 30 | | `U` | 31 | 32 | #### Parameters 33 | 34 | | Name | Type | 35 | | :------ | :------ | 36 | | `term` | `U` \| `T`[] | 37 | 38 | #### Returns 39 | 40 | term is T[] 41 | 42 | #### Defined in 43 | 44 | [guards/structural.ts:17](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L17) 45 | 46 | ___ 47 | 48 | ### isDate 49 | 50 | ▸ `Const` **isDate**<`U`\>(`term`): term is Date 51 | 52 | #### Type parameters 53 | 54 | | Name | 55 | | :------ | 56 | | `U` | 57 | 58 | #### Parameters 59 | 60 | | Name | Type | 61 | | :------ | :------ | 62 | | `term` | `U` \| `Date` | 63 | 64 | #### Returns 65 | 66 | term is Date 67 | 68 | #### Defined in 69 | 70 | [guards/structural.ts:41](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L41) 71 | 72 | ___ 73 | 74 | ### isFunction 75 | 76 | ▸ `Const` **isFunction**<`T`, `U`\>(`term`): term is T 77 | 78 | #### Type parameters 79 | 80 | | Name | Type | 81 | | :------ | :------ | 82 | | `T` | extends `Function` | 83 | | `U` | `U` | 84 | 85 | #### Parameters 86 | 87 | | Name | Type | 88 | | :------ | :------ | 89 | | `term` | `T` \| `U` | 90 | 91 | #### Returns 92 | 93 | term is T 94 | 95 | #### Defined in 96 | 97 | [guards/structural.ts:7](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L7) 98 | 99 | ___ 100 | 101 | ### isMap 102 | 103 | ▸ `Const` **isMap**<`K`, `V`, `U`\>(`term`): term is Map 104 | 105 | #### Type parameters 106 | 107 | | Name | 108 | | :------ | 109 | | `K` | 110 | | `V` | 111 | | `U` | 112 | 113 | #### Parameters 114 | 115 | | Name | Type | 116 | | :------ | :------ | 117 | | `term` | `U` \| `Map`<`K`, `V`\> | 118 | 119 | #### Returns 120 | 121 | term is Map 122 | 123 | #### Defined in 124 | 125 | [guards/structural.ts:21](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L21) 126 | 127 | ___ 128 | 129 | ### isNull 130 | 131 | ▸ `Const` **isNull**<`T`\>(`term`): term is null 132 | 133 | #### Type parameters 134 | 135 | | Name | 136 | | :------ | 137 | | `T` | 138 | 139 | #### Parameters 140 | 141 | | Name | Type | 142 | | :------ | :------ | 143 | | `term` | ``null`` \| `T` | 144 | 145 | #### Returns 146 | 147 | term is null 148 | 149 | #### Defined in 150 | 151 | [guards/structural.ts:3](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L3) 152 | 153 | ___ 154 | 155 | ### isObject 156 | 157 | ▸ `Const` **isObject**<`T`, `U`\>(`term`): term is NonNullable 158 | 159 | #### Type parameters 160 | 161 | | Name | Type | 162 | | :------ | :------ | 163 | | `T` | extends `object` | 164 | | `U` | `U` | 165 | 166 | #### Parameters 167 | 168 | | Name | Type | 169 | | :------ | :------ | 170 | | `term` | `T` \| `U` | 171 | 172 | #### Returns 173 | 174 | term is NonNullable 175 | 176 | #### Defined in 177 | 178 | [guards/structural.ts:11](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L11) 179 | 180 | ___ 181 | 182 | ### isSet 183 | 184 | ▸ `Const` **isSet**<`T`, `U`\>(`term`): term is Set 185 | 186 | #### Type parameters 187 | 188 | | Name | 189 | | :------ | 190 | | `T` | 191 | | `U` | 192 | 193 | #### Parameters 194 | 195 | | Name | Type | 196 | | :------ | :------ | 197 | | `term` | `U` \| `Set`<`T`\> | 198 | 199 | #### Returns 200 | 201 | term is Set 202 | 203 | #### Defined in 204 | 205 | [guards/structural.ts:25](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L25) 206 | 207 | ___ 208 | 209 | ### isWeakMap 210 | 211 | ▸ `Const` **isWeakMap**<`K`, `V`, `U`\>(`term`): term is WeakMap 212 | 213 | #### Type parameters 214 | 215 | | Name | Type | 216 | | :------ | :------ | 217 | | `K` | extends `object` | 218 | | `V` | `V` | 219 | | `U` | `U` | 220 | 221 | #### Parameters 222 | 223 | | Name | Type | 224 | | :------ | :------ | 225 | | `term` | `U` \| `WeakMap`<`K`, `V`\> | 226 | 227 | #### Returns 228 | 229 | term is WeakMap 230 | 231 | #### Defined in 232 | 233 | [guards/structural.ts:29](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L29) 234 | 235 | ___ 236 | 237 | ### isWeakSet 238 | 239 | ▸ `Const` **isWeakSet**<`T`, `U`\>(`term`): term is WeakSet 240 | 241 | #### Type parameters 242 | 243 | | Name | Type | 244 | | :------ | :------ | 245 | | `T` | extends `object` | 246 | | `U` | `U` | 247 | 248 | #### Parameters 249 | 250 | | Name | Type | 251 | | :------ | :------ | 252 | | `term` | `U` \| `WeakSet`<`T`\> | 253 | 254 | #### Returns 255 | 256 | term is WeakSet 257 | 258 | #### Defined in 259 | 260 | [guards/structural.ts:35](https://github.com/sniptt-official/guards/blob/afc9150/lib/guards/structural.ts#L35) 261 | -------------------------------------------------------------------------------- /lib/guards/convenience.ts: -------------------------------------------------------------------------------- 1 | import { isNumber, isString } from './primitives'; 2 | import { isArray } from './structural'; 3 | 4 | export const isObjectOrNull = (term: T | U): term is T => { 5 | return typeof term === 'object'; 6 | }; 7 | 8 | export const isNonEmptyArray = (term: Array | U): term is Array => { 9 | return isArray(term) && term.length > 0; 10 | }; 11 | 12 | export const isNonEmptyString = (term: string | U): term is string => { 13 | return isString(term) && term.length > 0; 14 | }; 15 | 16 | export const isNumberOrNaN = (term: number | U): term is number => { 17 | return typeof term === 'number'; 18 | }; 19 | 20 | export const isInteger = (term: number | U): term is number => { 21 | return isNumber(term) && Number.isInteger(term); 22 | }; 23 | 24 | export const isPositiveInteger = (term: number | U): term is number => { 25 | return isInteger(term) && term > 0; 26 | }; 27 | 28 | export const isNonNegativeInteger = (term: number | U): term is number => { 29 | return isInteger(term) && term >= 0; 30 | }; 31 | 32 | export const isNegativeInteger = (term: number | U): term is number => { 33 | return isInteger(term) && term < 0; 34 | }; 35 | -------------------------------------------------------------------------------- /lib/guards/primitives.ts: -------------------------------------------------------------------------------- 1 | // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures 2 | 3 | export const isUndefined = (term: T | undefined): term is undefined => { 4 | return typeof term === 'undefined'; 5 | }; 6 | 7 | export const isBoolean = (term: boolean | U): term is boolean => { 8 | return typeof term === 'boolean'; 9 | }; 10 | 11 | export const isNumber = (term: number | U): term is number => { 12 | return typeof term === 'number' && !Number.isNaN(term); 13 | }; 14 | 15 | export const isString = (term: string | U): term is string => { 16 | return typeof term === 'string'; 17 | }; 18 | 19 | export const isBigInt = (term: bigint | U): term is bigint => { 20 | return typeof term === 'bigint'; 21 | }; 22 | 23 | export const isSymbol = (term: symbol | U): term is symbol => { 24 | return typeof term === 'symbol'; 25 | }; 26 | -------------------------------------------------------------------------------- /lib/guards/structural.ts: -------------------------------------------------------------------------------- 1 | // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures 2 | 3 | export const isNull = (term: T | null): term is null => { 4 | return term === null; 5 | }; 6 | 7 | export const isFunction = (term: T | U): term is T => { 8 | return typeof term === 'function'; 9 | }; 10 | 11 | export const isObject = ( 12 | term: T | U, 13 | ): term is NonNullable => { 14 | return !isNull(term) && typeof term === 'object'; 15 | }; 16 | 17 | export const isArray = (term: Array | U): term is Array => { 18 | return Array.isArray(term); 19 | }; 20 | 21 | export const isMap = (term: Map | U): term is Map => { 22 | return term instanceof Map; 23 | }; 24 | 25 | export const isSet = (term: Set | U): term is Set => { 26 | return term instanceof Set; 27 | }; 28 | 29 | export const isWeakMap = ( 30 | term: WeakMap | U, 31 | ): term is WeakMap => { 32 | return term instanceof WeakMap; 33 | }; 34 | 35 | export const isWeakSet = ( 36 | term: WeakSet | U, 37 | ): term is WeakSet => { 38 | return term instanceof WeakSet; 39 | }; 40 | 41 | export const isDate = (term: Date | U): term is Date => { 42 | return term instanceof Date; 43 | }; 44 | -------------------------------------------------------------------------------- /lib/index.ts: -------------------------------------------------------------------------------- 1 | export * as convenience from './guards/convenience'; 2 | export * from './guards/convenience'; 3 | export * as primitives from './guards/primitives'; 4 | export * from './guards/primitives'; 5 | export * as structural from './guards/structural'; 6 | export * from './guards/structural'; 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@sniptt/guards", 3 | "version": "0.2.0", 4 | "description": "Comprehensive collection of type guards for JavaScript and TypeScript; Inspired by Elixir", 5 | "main": "build/index.js", 6 | "types": "build", 7 | "engines": { 8 | "node": ">=14", 9 | "npm": ">=7" 10 | }, 11 | "files": [ 12 | "build" 13 | ], 14 | "keywords": [ 15 | "guards", 16 | "javascript", 17 | "js", 18 | "typescript", 19 | "node", 20 | "node.js" 21 | ], 22 | "author": "Sniptt (sniptt.com)", 23 | "license": "MIT", 24 | "homepage": "https://github.com/sniptt-official/guards", 25 | "bugs": "https://github.com/sniptt-official/guards/issues", 26 | "scripts": { 27 | "build": "rm -rf build deno && tsc && denoify && mv deno_build deno && npm run format", 28 | "format": "prettier --loglevel warn --write \"**/*.{ts,js,json,yaml}\"", 29 | "format:check": "prettier --loglevel warn --check \"**/*.{ts,js,json,yaml}\"", 30 | "test": "nyc ava --fail-fast ./test/**/*.test.ts", 31 | "docs": "rm -rf ./docs && typedoc lib/index.ts --excludePrivate --excludeProtected --plugin typedoc-plugin-markdown --readme none --out ./docs", 32 | "preversion": "npm run format:check && npm run build && npm test", 33 | "postversion": "git push --follow-tags" 34 | }, 35 | "devDependencies": { 36 | "ava": "3.15.0", 37 | "denoify": "0.7.2", 38 | "nyc": "15.1.0", 39 | "prettier": "2.3.2", 40 | "ts-node": "10.1.0", 41 | "typedoc": "0.21.4", 42 | "typedoc-plugin-markdown": "3.10.4", 43 | "typescript": "4.3.5" 44 | }, 45 | "ava": { 46 | "extensions": [ 47 | "ts" 48 | ], 49 | "require": [ 50 | "ts-node/register" 51 | ], 52 | "verbose": true 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /test/guards/convenience.test.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | 3 | import * as convenience from '../../lib/guards/convenience'; 4 | 5 | test('isObjectOrNull', (t) => { 6 | t.is(convenience.isObjectOrNull({}), true); 7 | t.is(convenience.isObjectOrNull(null), true); 8 | t.is(convenience.isObjectOrNull(new Set()), true); 9 | t.is( 10 | convenience.isObjectOrNull(() => {}), 11 | false, 12 | ); 13 | t.is(convenience.isObjectOrNull(new Function()), false); 14 | }); 15 | 16 | test('isNonEmptyArray', (t) => { 17 | t.is(convenience.isNonEmptyArray([1, 2]), true); 18 | t.is(convenience.isNonEmptyArray([1]), true); 19 | t.is(convenience.isNonEmptyArray([]), false); 20 | }); 21 | 22 | test('isNonEmptyString', (t) => { 23 | t.is(convenience.isNonEmptyString('a'), true); 24 | t.is(convenience.isNonEmptyString(''), false); 25 | }); 26 | 27 | test('isNumberOrNaN', (t) => { 28 | t.is(convenience.isNumberOrNaN(0), true); 29 | t.is(convenience.isNumberOrNaN(42), true); 30 | t.is(convenience.isNumberOrNaN(-42), true); 31 | t.is(convenience.isNumberOrNaN(3.14), true); 32 | t.is(convenience.isNumberOrNaN(-3.14), true); 33 | t.is(convenience.isNumberOrNaN(Infinity), true); 34 | t.is(convenience.isNumberOrNaN(-Infinity), true); 35 | t.is(convenience.isNumberOrNaN(Number.MAX_SAFE_INTEGER), true); 36 | t.is(convenience.isNumberOrNaN(-Number.MAX_SAFE_INTEGER), true); 37 | t.is(convenience.isNumberOrNaN(NaN), true); 38 | t.is(convenience.isNumberOrNaN(BigInt(0)), false); 39 | }); 40 | 41 | test('isInteger', (t) => { 42 | t.is(convenience.isInteger(0), true); 43 | t.is(convenience.isInteger(42), true); 44 | t.is(convenience.isInteger(-42), true); 45 | t.is(convenience.isInteger(3.14), false); 46 | t.is(convenience.isInteger(-3.14), false); 47 | t.is(convenience.isInteger(Infinity), false); 48 | t.is(convenience.isInteger(-Infinity), false); 49 | t.is(convenience.isInteger(Number.MAX_SAFE_INTEGER), true); 50 | t.is(convenience.isInteger(-Number.MAX_SAFE_INTEGER), true); 51 | t.is(convenience.isInteger(NaN), false); 52 | }); 53 | 54 | test('isPositiveInteger', (t) => { 55 | t.is(convenience.isPositiveInteger(0), false); 56 | t.is(convenience.isPositiveInteger(42), true); 57 | t.is(convenience.isPositiveInteger(-42), false); 58 | t.is(convenience.isPositiveInteger(3.14), false); 59 | t.is(convenience.isPositiveInteger(-3.14), false); 60 | t.is(convenience.isPositiveInteger(Infinity), false); 61 | t.is(convenience.isPositiveInteger(-Infinity), false); 62 | t.is(convenience.isPositiveInteger(Number.MAX_SAFE_INTEGER), true); 63 | t.is(convenience.isPositiveInteger(-Number.MAX_SAFE_INTEGER), false); 64 | t.is(convenience.isPositiveInteger(NaN), false); 65 | }); 66 | 67 | test('isNonNegativeInteger', (t) => { 68 | t.is(convenience.isNonNegativeInteger(0), true); 69 | t.is(convenience.isNonNegativeInteger(42), true); 70 | t.is(convenience.isNonNegativeInteger(-42), false); 71 | t.is(convenience.isNonNegativeInteger(3.14), false); 72 | t.is(convenience.isNonNegativeInteger(-3.14), false); 73 | t.is(convenience.isNonNegativeInteger(Infinity), false); 74 | t.is(convenience.isNonNegativeInteger(-Infinity), false); 75 | t.is(convenience.isNonNegativeInteger(Number.MAX_SAFE_INTEGER), true); 76 | t.is(convenience.isNonNegativeInteger(-Number.MAX_SAFE_INTEGER), false); 77 | t.is(convenience.isNonNegativeInteger(NaN), false); 78 | }); 79 | 80 | test('isNegativeInteger', (t) => { 81 | t.is(convenience.isNegativeInteger(0), false); 82 | t.is(convenience.isNegativeInteger(42), false); 83 | t.is(convenience.isNegativeInteger(-42), true); 84 | t.is(convenience.isNegativeInteger(3.14), false); 85 | t.is(convenience.isNegativeInteger(-3.14), false); 86 | t.is(convenience.isNegativeInteger(Infinity), false); 87 | t.is(convenience.isNegativeInteger(-Infinity), false); 88 | t.is(convenience.isNegativeInteger(Number.MAX_SAFE_INTEGER), false); 89 | t.is(convenience.isNegativeInteger(-Number.MAX_SAFE_INTEGER), true); 90 | t.is(convenience.isNegativeInteger(NaN), false); 91 | }); 92 | -------------------------------------------------------------------------------- /test/guards/primitives.test.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | 3 | import * as primitives from '../../lib/guards/primitives'; 4 | 5 | const getUndefined = () => { 6 | return undefined; 7 | }; 8 | 9 | const getBoolean = () => { 10 | const values = [true, false, Boolean(0), Boolean(1)]; 11 | const index = Math.floor(Math.random() * values.length); 12 | return values[index]; 13 | }; 14 | 15 | const getNumber = () => { 16 | const values = [42, 3.14, Infinity, Number('42')]; 17 | const index = Math.floor(Math.random() * values.length); 18 | return values[index]; 19 | }; 20 | 21 | const getString = () => { 22 | const values = ['str', String(42)]; 23 | const index = Math.floor(Math.random() * values.length); 24 | return values[index]; 25 | }; 26 | 27 | const getBigInt = () => { 28 | const values = [42n, BigInt('42')]; 29 | const index = Math.floor(Math.random() * values.length); 30 | return values[index]; 31 | }; 32 | 33 | const getSymbol = () => { 34 | return Symbol('symbol'); 35 | }; 36 | 37 | test('isUndefined', (t) => { 38 | t.is(primitives.isUndefined(getUndefined()), true); 39 | t.is(primitives.isUndefined(getBoolean()), false); 40 | t.is(primitives.isUndefined(getNumber()), false); 41 | t.is(primitives.isUndefined(getString()), false); 42 | t.is(primitives.isUndefined(getBigInt()), false); 43 | t.is(primitives.isUndefined(getSymbol()), false); 44 | }); 45 | 46 | test('isBoolean', (t) => { 47 | t.is(primitives.isBoolean(getUndefined()), false); 48 | t.is(primitives.isBoolean(getBoolean()), true); 49 | t.is(primitives.isBoolean(getNumber()), false); 50 | t.is(primitives.isBoolean(getString()), false); 51 | t.is(primitives.isBoolean(getBigInt()), false); 52 | t.is(primitives.isBoolean(getSymbol()), false); 53 | }); 54 | 55 | test('isNumber', (t) => { 56 | t.is(primitives.isNumber(getUndefined()), false); 57 | t.is(primitives.isNumber(getBoolean()), false); 58 | t.is(primitives.isNumber(getNumber()), true); 59 | t.is(primitives.isNumber(getString()), false); 60 | t.is(primitives.isNumber(getBigInt()), false); 61 | t.is(primitives.isNumber(getSymbol()), false); 62 | }); 63 | 64 | test('isString', (t) => { 65 | t.is(primitives.isString(getUndefined()), false); 66 | t.is(primitives.isString(getBoolean()), false); 67 | t.is(primitives.isString(getNumber()), false); 68 | t.is(primitives.isString(getString()), true); 69 | t.is(primitives.isString(getBigInt()), false); 70 | t.is(primitives.isString(getSymbol()), false); 71 | }); 72 | 73 | test('isBigInt', (t) => { 74 | t.is(primitives.isBigInt(getUndefined()), false); 75 | t.is(primitives.isBigInt(getBoolean()), false); 76 | t.is(primitives.isBigInt(getNumber()), false); 77 | t.is(primitives.isBigInt(getString()), false); 78 | t.is(primitives.isBigInt(getBigInt()), true); 79 | t.is(primitives.isBigInt(getSymbol()), false); 80 | }); 81 | 82 | test('isSymbol', (t) => { 83 | t.is(primitives.isSymbol(getUndefined()), false); 84 | t.is(primitives.isSymbol(getBoolean()), false); 85 | t.is(primitives.isSymbol(getNumber()), false); 86 | t.is(primitives.isSymbol(getString()), false); 87 | t.is(primitives.isSymbol(getBigInt()), false); 88 | t.is(primitives.isSymbol(getSymbol()), true); 89 | }); 90 | -------------------------------------------------------------------------------- /test/guards/structural.test.ts: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | 3 | import * as structural from '../../lib/guards/structural'; 4 | 5 | test('isNull', (t) => { 6 | t.is(structural.isNull(null), true); 7 | t.is(structural.isNull(undefined), false); 8 | t.is(structural.isNull(0), false); 9 | t.is(structural.isNull(false), false); 10 | }); 11 | 12 | test('isFunction', (t) => { 13 | t.is(structural.isFunction({}), false); 14 | t.is(structural.isFunction(new (class C {})()), false); 15 | t.is(structural.isFunction(new Function()), true); 16 | t.is(structural.isFunction(new Map()), false); 17 | t.is(structural.isFunction(new Set()), false); 18 | t.is(structural.isFunction(new WeakMap()), false); 19 | t.is(structural.isFunction(new WeakSet()), false); 20 | t.is(structural.isFunction(new Date()), false); 21 | t.is( 22 | structural.isFunction(() => {}), 23 | true, 24 | ); 25 | t.is( 26 | structural.isFunction(function () {}), 27 | true, 28 | ); 29 | t.is(structural.isFunction(class C {}), true); 30 | t.is(structural.isFunction(parseInt), true); 31 | t.is(structural.isFunction(null), false); 32 | t.is(structural.isFunction(undefined), false); 33 | t.is(structural.isFunction('str'), false); 34 | t.is(structural.isFunction(42), false); 35 | t.is(structural.isFunction([]), false); 36 | t.is(structural.isFunction(Symbol('symbol')), false); 37 | }); 38 | 39 | test('isObject', (t) => { 40 | t.is(structural.isObject({}), true); 41 | t.is(structural.isObject(new (class C {})()), true); 42 | t.is(structural.isObject(new Function()), false); 43 | t.is(structural.isObject(new Map()), true); 44 | t.is(structural.isObject(new Set()), true); 45 | t.is(structural.isObject(new WeakMap()), true); 46 | t.is(structural.isObject(new WeakSet()), true); 47 | t.is(structural.isObject(new Date()), true); 48 | t.is( 49 | structural.isObject(() => {}), 50 | false, 51 | ); 52 | t.is( 53 | structural.isObject(function () {}), 54 | false, 55 | ); 56 | t.is(structural.isObject(class C {}), false); 57 | t.is(structural.isObject(parseInt), false); 58 | t.is(structural.isObject(null), false); 59 | t.is(structural.isObject(undefined), false); 60 | t.is(structural.isObject('str'), false); 61 | t.is(structural.isObject(42), false); 62 | t.is(structural.isObject([]), true); 63 | t.is(structural.isObject(Symbol('symbol')), false); 64 | }); 65 | 66 | test('isArray', (t) => { 67 | t.is(structural.isArray([]), true); 68 | t.is(structural.isArray(class C {}), false); 69 | t.is(structural.isArray(new Map()), false); 70 | t.is(structural.isArray(new Set()), false); 71 | t.is(structural.isArray(new WeakMap()), false); 72 | t.is(structural.isArray(new WeakSet()), false); 73 | t.is(structural.isArray(new Date()), false); 74 | t.is( 75 | structural.isArray(() => {}), 76 | false, 77 | ); 78 | t.is( 79 | structural.isArray(function () {}), 80 | false, 81 | ); 82 | t.is(structural.isArray(class C {}), false); 83 | t.is(structural.isArray(parseInt), false); 84 | t.is(structural.isArray(null), false); 85 | t.is(structural.isArray(undefined), false); 86 | t.is(structural.isArray('str'), false); 87 | t.is(structural.isArray(42), false); 88 | t.is(structural.isArray(Symbol('symbol')), false); 89 | }); 90 | 91 | test('isMap', (t) => { 92 | t.is(structural.isMap({}), false); 93 | t.is(structural.isMap(new (class C {})()), false); 94 | t.is(structural.isMap(new Map()), true); 95 | t.is(structural.isMap(new Set()), false); 96 | t.is(structural.isMap(new WeakMap()), false); 97 | t.is(structural.isMap(new WeakSet()), false); 98 | t.is(structural.isMap(new Date()), false); 99 | t.is( 100 | structural.isMap(() => {}), 101 | false, 102 | ); 103 | t.is( 104 | structural.isMap(function () {}), 105 | false, 106 | ); 107 | t.is(structural.isMap(class C {}), false); 108 | t.is(structural.isMap(parseInt), false); 109 | t.is(structural.isMap(null), false); 110 | t.is(structural.isMap(undefined), false); 111 | t.is(structural.isMap('str'), false); 112 | t.is(structural.isMap(42), false); 113 | t.is(structural.isMap([]), false); 114 | t.is(structural.isMap(Symbol('symbol')), false); 115 | }); 116 | 117 | test('isSet', (t) => { 118 | t.is(structural.isSet({}), false); 119 | t.is(structural.isSet(new (class C {})()), false); 120 | t.is(structural.isSet(new Map()), false); 121 | t.is(structural.isSet(new Set()), true); 122 | t.is(structural.isSet(new WeakMap()), false); 123 | t.is(structural.isSet(new WeakSet()), false); 124 | t.is(structural.isSet(new Date()), false); 125 | t.is( 126 | structural.isSet(() => {}), 127 | false, 128 | ); 129 | t.is( 130 | structural.isSet(function () {}), 131 | false, 132 | ); 133 | t.is(structural.isSet(class C {}), false); 134 | t.is(structural.isSet(parseInt), false); 135 | t.is(structural.isSet(null), false); 136 | t.is(structural.isSet(undefined), false); 137 | t.is(structural.isSet('str'), false); 138 | t.is(structural.isSet(42), false); 139 | t.is(structural.isSet([]), false); 140 | t.is(structural.isSet(Symbol('symbol')), false); 141 | }); 142 | 143 | test('isWeakMap', (t) => { 144 | t.is(structural.isWeakMap({}), false); 145 | t.is(structural.isWeakMap(new (class C {})()), false); 146 | t.is(structural.isWeakMap(new Map()), false); 147 | t.is(structural.isWeakMap(new Set()), false); 148 | t.is(structural.isWeakMap(new WeakMap()), true); 149 | t.is(structural.isWeakMap(new WeakSet()), false); 150 | t.is(structural.isWeakMap(new Date()), false); 151 | t.is( 152 | structural.isWeakMap(() => {}), 153 | false, 154 | ); 155 | t.is( 156 | structural.isWeakMap(function () {}), 157 | false, 158 | ); 159 | t.is(structural.isWeakMap(class C {}), false); 160 | t.is(structural.isWeakMap(parseInt), false); 161 | t.is(structural.isWeakMap(null), false); 162 | t.is(structural.isWeakMap(undefined), false); 163 | t.is(structural.isWeakMap('str'), false); 164 | t.is(structural.isWeakMap(42), false); 165 | t.is(structural.isWeakMap([]), false); 166 | t.is(structural.isWeakMap(Symbol('symbol')), false); 167 | }); 168 | 169 | test('isWeakSet', (t) => { 170 | t.is(structural.isWeakSet({}), false); 171 | t.is(structural.isWeakSet(new (class C {})()), false); 172 | t.is(structural.isWeakSet(new Map()), false); 173 | t.is(structural.isWeakSet(new Set()), false); 174 | t.is(structural.isWeakSet(new WeakMap()), false); 175 | t.is(structural.isWeakSet(new WeakSet()), true); 176 | t.is(structural.isWeakSet(new Date()), false); 177 | t.is( 178 | structural.isWeakSet(() => {}), 179 | false, 180 | ); 181 | t.is( 182 | structural.isWeakSet(function () {}), 183 | false, 184 | ); 185 | t.is(structural.isWeakSet(class C {}), false); 186 | t.is(structural.isWeakSet(parseInt), false); 187 | t.is(structural.isWeakSet(null), false); 188 | t.is(structural.isWeakSet(undefined), false); 189 | t.is(structural.isWeakSet('str'), false); 190 | t.is(structural.isWeakSet(42), false); 191 | t.is(structural.isWeakSet([]), false); 192 | t.is(structural.isWeakSet(Symbol('symbol')), false); 193 | }); 194 | 195 | test('isDate', (t) => { 196 | t.is(structural.isDate({}), false); 197 | t.is(structural.isDate(new (class C {})()), false); 198 | t.is(structural.isDate(new Map()), false); 199 | t.is(structural.isDate(new Set()), false); 200 | t.is(structural.isDate(new WeakMap()), false); 201 | t.is(structural.isDate(new WeakSet()), false); 202 | t.is(structural.isDate(new Date()), true); 203 | t.is( 204 | structural.isDate(() => {}), 205 | false, 206 | ); 207 | t.is( 208 | structural.isDate(function () {}), 209 | false, 210 | ); 211 | t.is(structural.isDate(class C {}), false); 212 | t.is(structural.isDate(parseInt), false); 213 | t.is(structural.isDate(null), false); 214 | t.is(structural.isDate(undefined), false); 215 | t.is(structural.isDate('str'), false); 216 | t.is(structural.isDate(42), false); 217 | t.is(structural.isDate([]), false); 218 | t.is(structural.isDate(Symbol('symbol')), false); 219 | }); 220 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2020", 4 | "module": "commonjs", 5 | "moduleResolution": "node", 6 | "declaration": true, 7 | "declarationMap": true, 8 | "sourceMap": true, 9 | "outDir": "./build", 10 | "removeComments": false, 11 | "importHelpers": false, 12 | "strict": true, 13 | "noUnusedLocals": true, 14 | "noUnusedParameters": true, 15 | "noImplicitReturns": true, 16 | "noFallthroughCasesInSwitch": true, 17 | "esModuleInterop": true, 18 | "forceConsistentCasingInFileNames": true, 19 | "extendedDiagnostics": true 20 | }, 21 | "include": ["lib"], 22 | "exclude": ["deno", "test"] 23 | } 24 | --------------------------------------------------------------------------------