From dd4ec3af4f2044ff3d9fb4844ab52059060815aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 2 Dec 2024 13:09:40 +0000 Subject: [PATCH 01/13] Initial commit. --- .gitignore | 45 ++++ README.md | 157 +++++++++++- ng-package.json | 8 + package-lock.json | 249 +++++++++++++++++++ package.json | 42 ++++ src/execute-tests.ts | 130 ++++++++++ src/interface/guard-interface.ts | 6 + src/interface/guard-is.interface.ts | 156 ++++++++++++ src/lib/guard-array.func.ts | 18 ++ src/lib/guard-big-int.func.ts | 21 ++ src/lib/guard-boolean.func.ts | 21 ++ src/lib/guard-class.func.ts | 21 ++ src/lib/guard-date.func.ts | 17 ++ src/lib/guard-defined.func.ts | 16 ++ src/lib/guard-false.func.ts | 16 ++ src/lib/guard-function.func.ts | 16 ++ src/lib/guard-instance.func.ts | 21 ++ src/lib/guard-is.object.ts | 68 +++++ src/lib/guard-key.func.ts | 16 ++ src/lib/guard-null.func.ts | 16 ++ src/lib/guard-number-between.func.ts | 27 ++ src/lib/guard-number.func.ts | 19 ++ src/lib/guard-object-key-in.func.ts | 24 ++ src/lib/guard-object-key.func.ts | 24 ++ src/lib/guard-object-keys-in.func.ts | 24 ++ src/lib/guard-object-keys.func.ts | 23 ++ src/lib/guard-object-some-keys.func.ts | 24 ++ src/lib/guard-object.func.ts | 19 ++ src/lib/guard-primitive.func.ts | 22 ++ src/lib/guard-regexp.func.ts | 16 ++ src/lib/guard-string-includes-some.func.ts | 27 ++ src/lib/guard-string-includes.func.ts | 27 ++ src/lib/guard-string-length-between.func.ts | 28 +++ src/lib/guard-string-length.func.ts | 26 ++ src/lib/guard-string.func.ts | 20 ++ src/lib/guard-symbol.func.ts | 16 ++ src/lib/guard-true.func.ts | 16 ++ src/lib/guard-type.func.ts | 18 ++ src/lib/guard-undefined.func.ts | 16 ++ src/lib/guard.object.ts | 6 + src/lib/index.ts | 30 +++ src/public-api.ts | 36 +++ src/test/guard-array.spec.ts | 53 ++++ src/test/guard-bigint.spec.ts | 52 ++++ src/test/guard-boolean.spec.ts | 70 ++++++ src/test/guard-class.spec.ts | 49 ++++ src/test/guard-date.spec.ts | 74 ++++++ src/test/guard-defined.spec.ts | 45 ++++ src/test/guard-false.spec.ts | 59 +++++ src/test/guard-function.spec.ts | 42 ++++ src/test/guard-instance.spec.ts | 89 +++++++ src/test/guard-key.spec.ts | 61 +++++ src/test/guard-null.spec.ts | 48 ++++ src/test/guard-number-between.spec.ts | 54 ++++ src/test/guard-number.spec.ts | 59 +++++ src/test/guard-object-key-in.spec.ts | 96 +++++++ src/test/guard-object-key.spec.ts | 93 +++++++ src/test/guard-object-keys-in.spec.ts | 108 ++++++++ src/test/guard-object-keys.spec.ts | 108 ++++++++ src/test/guard-object-some-keys.spec.ts | 110 ++++++++ src/test/guard-object.spec.ts | 51 ++++ src/test/guard-primitive.spec.ts | 88 +++++++ src/test/guard-regexp.spec.ts | 52 ++++ src/test/guard-string-includes-some.spec.ts | 87 +++++++ src/test/guard-string-includes.spec.ts | 88 +++++++ src/test/guard-string-length-between.spec.ts | 86 +++++++ src/test/guard-string-length.spec.ts | 70 ++++++ src/test/guard-string.spec.ts | 61 +++++ src/test/guard-symbol.spec.ts | 59 +++++ src/test/guard-true.spec.ts | 55 ++++ src/test/guard-type.spec.ts | 132 ++++++++++ src/test/guard-undefined.spec.ts | 50 ++++ src/test/guard.spec.ts | 51 ++++ tsconfig.lib.json | 15 ++ tsconfig.lib.prod.json | 11 + tsconfig.spec.json | 15 ++ 76 files changed, 3788 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 ng-package.json create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/execute-tests.ts create mode 100644 src/interface/guard-interface.ts create mode 100644 src/interface/guard-is.interface.ts create mode 100644 src/lib/guard-array.func.ts create mode 100644 src/lib/guard-big-int.func.ts create mode 100644 src/lib/guard-boolean.func.ts create mode 100644 src/lib/guard-class.func.ts create mode 100644 src/lib/guard-date.func.ts create mode 100644 src/lib/guard-defined.func.ts create mode 100644 src/lib/guard-false.func.ts create mode 100644 src/lib/guard-function.func.ts create mode 100644 src/lib/guard-instance.func.ts create mode 100644 src/lib/guard-is.object.ts create mode 100644 src/lib/guard-key.func.ts create mode 100644 src/lib/guard-null.func.ts create mode 100644 src/lib/guard-number-between.func.ts create mode 100644 src/lib/guard-number.func.ts create mode 100644 src/lib/guard-object-key-in.func.ts create mode 100644 src/lib/guard-object-key.func.ts create mode 100644 src/lib/guard-object-keys-in.func.ts create mode 100644 src/lib/guard-object-keys.func.ts create mode 100644 src/lib/guard-object-some-keys.func.ts create mode 100644 src/lib/guard-object.func.ts create mode 100644 src/lib/guard-primitive.func.ts create mode 100644 src/lib/guard-regexp.func.ts create mode 100644 src/lib/guard-string-includes-some.func.ts create mode 100644 src/lib/guard-string-includes.func.ts create mode 100644 src/lib/guard-string-length-between.func.ts create mode 100644 src/lib/guard-string-length.func.ts create mode 100644 src/lib/guard-string.func.ts create mode 100644 src/lib/guard-symbol.func.ts create mode 100644 src/lib/guard-true.func.ts create mode 100644 src/lib/guard-type.func.ts create mode 100644 src/lib/guard-undefined.func.ts create mode 100644 src/lib/guard.object.ts create mode 100644 src/lib/index.ts create mode 100644 src/public-api.ts create mode 100644 src/test/guard-array.spec.ts create mode 100644 src/test/guard-bigint.spec.ts create mode 100644 src/test/guard-boolean.spec.ts create mode 100644 src/test/guard-class.spec.ts create mode 100644 src/test/guard-date.spec.ts create mode 100644 src/test/guard-defined.spec.ts create mode 100644 src/test/guard-false.spec.ts create mode 100644 src/test/guard-function.spec.ts create mode 100644 src/test/guard-instance.spec.ts create mode 100644 src/test/guard-key.spec.ts create mode 100644 src/test/guard-null.spec.ts create mode 100644 src/test/guard-number-between.spec.ts create mode 100644 src/test/guard-number.spec.ts create mode 100644 src/test/guard-object-key-in.spec.ts create mode 100644 src/test/guard-object-key.spec.ts create mode 100644 src/test/guard-object-keys-in.spec.ts create mode 100644 src/test/guard-object-keys.spec.ts create mode 100644 src/test/guard-object-some-keys.spec.ts create mode 100644 src/test/guard-object.spec.ts create mode 100644 src/test/guard-primitive.spec.ts create mode 100644 src/test/guard-regexp.spec.ts create mode 100644 src/test/guard-string-includes-some.spec.ts create mode 100644 src/test/guard-string-includes.spec.ts create mode 100644 src/test/guard-string-length-between.spec.ts create mode 100644 src/test/guard-string-length.spec.ts create mode 100644 src/test/guard-string.spec.ts create mode 100644 src/test/guard-symbol.spec.ts create mode 100644 src/test/guard-true.spec.ts create mode 100644 src/test/guard-type.spec.ts create mode 100644 src/test/guard-undefined.spec.ts create mode 100644 src/test/guard.spec.ts create mode 100644 tsconfig.lib.json create mode 100644 tsconfig.lib.prod.json create mode 100644 tsconfig.spec.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a183d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +# See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files. + +# Compiled output +/dist +/tmp +/out-tsc +/bazel-out + +# Node +/node_modules +npm-debug.log +yarn-error.log + +# IDEs and editors +.idea/ +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# Visual Studio Code +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +.history/* + +# Miscellaneous +/.angular/cache +.sass-cache/ +/connect.lock +/coverage +/libpeerconnection.log +testem.log +/typings + +# System files +.DS_Store +Thumbs.db + +*.ignore* +temp \ No newline at end of file diff --git a/README.md b/README.md index aeaa4cb..6d7dc81 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,157 @@ -# guard +# Guard + Type guards for typescript. + + + + + +## typescript-package/guard + +Type-safe guards for validating value types in TypeScript. + + +[![npm version][typescript-package-npm-badge-svg]][typescript-package-npm-badge] +[![GitHub issues][typescript-package-badge-issues]][typescript-package-issues] +[![GitHub license][typescript-package-badge-license]][typescript-package-license] + +
+ +## Table of contents + +* [Installation](#installation) +* [Api](#api) +* [Git](#git) + * [Commit](#commit) + * [Versioning](#versioning) +* [License](#license) + +## Installation + +```bash +npm install @typescript-package/is +``` + +## Api + +```typescript +// Prefixed `is` functions. +import { + isArray, + isBigInt, + isBoolean, + isBooleanObject, + isBooleanType, + isClass, + isDate, + isDefined, + isFalse, + isFunction, + isInstance, + isKey, + isNull, + isNumber, + isNumberBetween, + isNumberObject, + isNumberType, + isObject, + isObjectKey, + isObjectKeyIn, + isObjectKeys, + isObjectKeysIn, + isObjectSomeKeys, + isPrimitive, + isRegExp, + isString, + isStringIncludes, + isStringIncludesSome, + isStringLength, + isStringLengthBetween, + isStringObject, + isStringType, + isSymbol, + isTrue, + isType, + isUndefined, +} from '@typescript-package/is'; + +// `is` object. +export { is } from './lib/is.object'; + +// Prefixed `isNot` functions. +export { + isNotBoolean, + isNotDefined, + isNotFunction, + isNotNull, + isNotNumber, + isNotString, + isNotUndefined +} from './not'; + +// `isNot` object. +export { isNot } from './not'; +``` + +## GIT + +### Commit + +* [AngularJS Git Commit Message Conventions][git-commit-angular] +* [Karma Git Commit Msg][git-commit-karma] +* [Conventional Commits][git-commit-conventional] + +### Versioning + +[Semantic Versioning 2.0.0][git-semver] + +**Given a version number MAJOR.MINOR.PATCH, increment the:** + +* MAJOR version when you make incompatible API changes, +* MINOR version when you add functionality in a backwards-compatible manner, and +* PATCH version when you make backwards-compatible bug fixes. + +Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format. + +**FAQ** +How should I deal with revisions in the 0.y.z initial development phase? + +> The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release. + +How do I know when to release 1.0.0? + +> If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0. + +## License + +MIT © typescript-package ([license][typescript-package-license]) + + + + [typescript-package-badge-issues]: https://img.shields.io/github/issues/typescript-package/guard + [isscript-package-badge-forks]: https://img.shields.io/github/forks/typescript-package/guard + [typescript-package-badge-stars]: https://img.shields.io/github/stars/typescript-package/guard + [typescript-package-badge-license]: https://img.shields.io/github/license/typescript-package/guard + + [typescript-package-issues]: https://github.com/typescript-package/guard/issues + [typescript-package-forks]: https://github.com/typescript-package/guard/network + [typescript-package-license]: https://github.com/typescript-package/guard/blob/master/LICENSE + [typescript-package-stars]: https://github.com/typescript-package/guard/stargazers + + + + + [typescript-package-npm-badge-svg]: https://badge.fury.io/js/@typescript-package%2Fguard.svg + [typescript-package-npm-badge]: https://badge.fury.io/js/@typescript-package%2Fguard + + +[git-semver]: http://semver.org/ + + +[git-commit-angular]: https://gist.github.com/stephenparish/9941e89d80e2bc58a153 +[git-commit-karma]: http://karma-runner.github.io/0.10/dev/git-commit-msg.html +[git-commit-conventional]: https://www.conventionalcommits.org/en/v1.0.0/ diff --git a/ng-package.json b/ng-package.json new file mode 100644 index 0000000..73414a4 --- /dev/null +++ b/ng-package.json @@ -0,0 +1,8 @@ +{ + "$schema": "../../node_modules/ng-packagr/ng-package.schema.json", + "dest": "../../dist/guard", + "lib": { + "entryFile": "src/public-api.ts" + }, + "keepLifecycleScripts": true +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..6f5cfe9 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,249 @@ +{ + "name": "@testing-package/guard", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@testing-package/guard", + "version": "1.0.0", + "funding": [ + { + "type": "individual", + "url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29" + } + ], + "license": "MIT", + "devDependencies": { + "@angular-package/testing": "^2.0.0-rc" + }, + "peerDependencies": { + "@typescript-package/core": "^1.0.0", + "@typescript-package/is": "^1.0.0", + "@typescript-package/type": "^1.1.0" + } + }, + "node_modules/@angular-package/testing": { + "version": "2.0.0-rc", + "resolved": "https://registry.npmjs.org/@angular-package/testing/-/testing-2.0.0-rc.tgz", + "integrity": "sha512-DjlOD0gnLlqT8b4Qqr5FlPJWnl1NsO0823fH9B+e+rDxoJZa6Ys2cGg8716ZRBrSVWdBeuVxrjDhMJpYX5GVDw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tslib": "^2.3.0" + }, + "peerDependencies": { + "@angular-package/type": "^5.0.0-rc.0", + "jasmine": "^3.9.0" + } + }, + "node_modules/@angular-package/type": { + "version": "5.0.0-rc.0", + "resolved": "https://registry.npmjs.org/@angular-package/type/-/type-5.0.0-rc.0.tgz", + "integrity": "sha512-NR3ODKJTmmdEqCz7fn6YDO68p9DK/SNWGvCV8pUqQUSFHlbrc0RDaqqF0liIi1iJcRZhbF2UnhWIIOp/iamtsg==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "tslib": "^2.3.0" + } + }, + "node_modules/@typescript-package/core": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@typescript-package/core/-/core-1.0.0.tgz", + "integrity": "sha512-HW+g5qCKbqDtoxCa+igceaZu2wWkw/YMw28VIXSz0hs7KUURHYtkAcuIG14VkOzaYgbgd1ZRBh4LzoVIp/DWVw==", + "funding": [ + { + "type": "individual", + "url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29" + } + ], + "license": "MIT", + "peer": true + }, + "node_modules/@typescript-package/is": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@typescript-package/is/-/is-1.0.0.tgz", + "integrity": "sha512-t2abF/A91Jy2OpYJfd6xwtCoHDZrGyISxEa0A4Ab/ADGUmhZj60hdL6Fw9xPOD16c/PBz0+F4Uf9D9vj5UGmFw==", + "funding": [ + { + "type": "individual", + "url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29" + } + ], + "license": "MIT", + "peer": true, + "peerDependencies": { + "@typescript-package/core": "^1.0.0", + "@typescript-package/type": "^1.0.0" + } + }, + "node_modules/@typescript-package/type": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@typescript-package/type/-/type-1.1.0.tgz", + "integrity": "sha512-vQQD3LqgXDPrm/fbfYOA/JcgfrSf47BkPhutWf+aKnEcQlXlSFnjprlR5Z4WCjLzgCkj+ba2duns084LsyNmcQ==", + "funding": [ + { + "type": "individual", + "url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29" + } + ], + "license": "MIT", + "peer": true + }, + "node_modules/balanced-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "dev": true, + "license": "MIT", + "peer": true + }, + "node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", + "dev": true, + "license": "MIT", + "peer": true + }, + "node_modules/fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", + "dev": true, + "license": "ISC", + "peer": true + }, + "node_modules/glob": { + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", + "deprecated": "Glob versions prior to v9 are no longer supported", + "dev": true, + "license": "ISC", + "peer": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.1.1", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", + "deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.", + "dev": true, + "license": "ISC", + "peer": true, + "dependencies": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true, + "license": "ISC", + "peer": true + }, + "node_modules/jasmine": { + "version": "3.99.0", + "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-3.99.0.tgz", + "integrity": "sha512-YIThBuHzaIIcjxeuLmPD40SjxkEcc8i//sGMDKCgkRMVgIwRJf5qyExtlJpQeh7pkeoBSOe6lQEdg+/9uKg9mw==", + "dev": true, + "license": "MIT", + "peer": true, + "dependencies": { + "glob": "^7.1.6", + "jasmine-core": "~3.99.0" + }, + "bin": { + "jasmine": "bin/jasmine.js" + } + }, + "node_modules/jasmine-core": { + "version": "3.99.1", + "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.99.1.tgz", + "integrity": "sha512-Hu1dmuoGcZ7AfyynN3LsfruwMbxMALMka+YtZeGoLuDEySVmVAPaonkNoBRIw/ectu8b9tVQCJNgp4a4knp+tg==", + "dev": true, + "license": "MIT", + "peer": true + }, + "node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dev": true, + "license": "ISC", + "peer": true, + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dev": true, + "license": "ISC", + "peer": true, + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", + "dev": true, + "license": "MIT", + "peer": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "dev": true, + "license": "0BSD" + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "dev": true, + "license": "ISC", + "peer": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..551c836 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "@testing-package/guard", + "version": "1.0.0", + "author": "wwwdev.io ", + "description": "Type-safe guards for validating value types in TypeScript.", + "license": "MIT", + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org" + }, + "devDependencies": { + "@angular-package/testing": "^2.0.0-rc" + }, + "peerDependencies": { + "@typescript-package/core": "^1.0.0", + "@typescript-package/is": "^1.0.0", + "@typescript-package/type": "^1.1.0" + }, + "scripts": { + "prepublishOnly": "npm run pkg && npm run clean", + "pkg": "npm pkg delete dependencies", + "clean": "npm pkg delete scripts" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/typescript-package/guard.git" + }, + "bugs": { + "url": "https://github.com/typescript-package/guard/issues" + }, + "keywords": [ + "@typescript-package", + "@typescript-package/guard" + ], + "funding": [ + { + "type": "individual", + "url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29" + } + ], + "sideEffects": false +} diff --git a/src/execute-tests.ts b/src/execute-tests.ts new file mode 100644 index 0000000..589af52 --- /dev/null +++ b/src/execute-tests.ts @@ -0,0 +1,130 @@ +export const tests = { + /** + * Switches for the `are` prefixed functions. + */ + are: { + bigint: { describe: true, it: true }, + boolean: { describe: true, it: true }, + date: { describe: true, it: true }, + defined: { describe: true, it: true }, + false: { describe: true, it: true }, + null: { describe: true, it: true }, + number: { describe: true, it: true }, + regexp: { describe: true, it: true }, + string: { describe: true, it: true }, + symbol: { describe: true, it: true }, + true: { describe: true, it: true }, + undefined: { describe: true, it: true }, + }, + object: { + guard: { describe: true, it: true }, + is: { describe: true, it: true }, + isNot: { describe: true, it: true }, + }, + + /** + * Switches for the experimental. + */ + experimental: { + bigint: { describe: true, it: true }, + boolean: { describe: true, it: true }, + number: { describe: true, it: true }, + string: { describe: true, it: true }, + symbol: { describe: true, it: true }, + }, + + /** + * Switches for the `guard` prefixed functions. + */ + guard: { + array: { describe: true, it: true }, + bigint: { describe: true, it: true }, + boolean: { describe: true, it: true }, + class: { describe: true, it: true }, + date: { describe: true, it: true }, + defined: { describe: true, it: true }, + false: { describe: true, it: true }, + function: { describe: true, it: true }, + instance: { describe: true, it: true }, + key: { describe: true, it: true }, + null: { describe: true, it: true }, + number: { describe: true, it: true }, + numberBetween: { describe: true, it: true }, + object: { describe: true, it: true }, + objectKey: { describe: true, it: true }, + objectKeyIn: { describe: true, it: true }, + objectKeys: { describe: true, it: true }, + objectKeysIn: { describe: true, it: true }, + objectSomeKeys: { describe: true, it: true }, + primitive: { describe: true, it: true }, + regexp: { describe: true, it: true }, + string: { describe: true, it: true }, + stringIncludes: { describe: true, it: true }, + stringIncludesSome: { describe: true, it: true }, + stringLength: { describe: true, it: true }, + stringLengthBetween: { describe: true, it: true }, + symbol: { describe: true, it: true }, + true: { describe: true, it: true }, + type: { describe: true, it: true }, + undefined: { describe: true, it: true }, + }, + + /** + * Switches for the `is` prefixed functions. + */ + is: { + array: { describe: true, it: true }, + bigint: { describe: true, it: true }, + boolean: { describe: true, it: true }, + booleanObject: { describe: true, it: true }, + booleanType: { describe: true, it: true }, + class: { describe: true, it: true }, + date: { describe: true, it: true }, + defined: { describe: true, it: true }, + false: { describe: true, it: true }, + function: { describe: true, it: true }, + instance: { describe: true, it: true }, + key: { describe: true, it: true }, + not: { + boolean: { describe: true, it: true }, + defined: { describe: true, it: true }, + function: { describe: true, it: true }, + null: { describe: true, it: true }, + number: { describe: true, it: true }, + string: { describe: true, it: true }, + undefined: { describe: true, it: true }, + }, + null: { describe: true, it: true }, + number: { describe: true, it: true }, + numberBetween: { describe: true, it: true }, + numberObject: { describe: true, it: true }, + numberType: { describe: true, it: true }, + object: { describe: true, it: true }, + objectKey: { describe: true, it: true }, + objectKeyIn: { describe: true, it: true }, + objectKeys: { describe: true, it: true }, + objectKeysIn: { describe: true, it: true }, + objectSomeKeys: { describe: true, it: true }, + param: { describe: true, it: true }, + primitive: { describe: true, it: true }, + regexp: { describe: true, it: true }, + string: { describe: true, it: true }, + stringIncludes: { describe: true, it: true }, + stringIncludesSome: { describe: true, it: true }, + stringLength: { describe: true, it: true }, + stringLengthBetween: { describe: true, it: true }, + stringObject: { describe: true, it: true }, + stringType: { describe: true, it: true }, + symbol: { describe: true, it: true }, + true: { describe: true, it: true }, + type: { describe: true, it: true }, + undefined: { describe: true, it: true } + }, + + /** + * Switches for the recognize. + */ + recognize: { + recognizeValue: { describe: true, it: true } + }, +}; diff --git a/src/interface/guard-interface.ts b/src/interface/guard-interface.ts new file mode 100644 index 0000000..ba811e3 --- /dev/null +++ b/src/interface/guard-interface.ts @@ -0,0 +1,6 @@ +// Interface. +import { GuardIs } from './guard-is.interface'; +/** + * The shape of an object with guard functions. + */ +export interface Guard extends GuardIs { } diff --git a/src/interface/guard-is.interface.ts b/src/interface/guard-is.interface.ts new file mode 100644 index 0000000..8e7db07 --- /dev/null +++ b/src/interface/guard-is.interface.ts @@ -0,0 +1,156 @@ +// Function. +import { + guardArray, + guardBigInt, + guardBoolean, + guardClass, + guardDate, + guardDefined, + guardFalse, + guardFunction, + guardInstance, + guardKey, + guardNull, + guardNumber, + guardNumberBetween, + guardObject, + guardObjectKey, + guardObjectKeyIn, + guardObjectKeys, + guardObjectKeysIn, + guardObjectSomeKeys, + guardPrimitive, + guardRegExp, + guardString, + guardStringIncludes, + guardStringIncludesSome, + guardStringLength, + guardStringLengthBetween, + guardSymbol, + guardTrue, + guardType, + guardUndefined +} from '../lib'; +// Export: Interface. +export interface GuardIs { + /** + * Guards the value to be an `array` of a generic type variable `Type`. + */ + array: typeof guardArray; + /** + * Guards the value to be a `bigint`. + */ + bigint: typeof guardBigInt; + /** + * Guards the value to be `boolean` of any type. + */ + boolean: typeof guardBoolean; + /** + * Guards the value to be a `class` of generic type variable `Class`. + */ + class: typeof guardClass; + /** + * Guards the value to be a date. + */ + date: typeof guardDate; + /** + * Guards the value to be defined, not `undefined`. + */ + defined: typeof guardDefined; + /** + * Guards the provided value to be `false`. + */ + false: typeof guardFalse; + /** + * Guards the value to be a `Function`. + */ + function: typeof guardFunction; + /** + * Guards the value to be an instance of the given `constructor`. + */ + instance: typeof guardInstance; + /** + * Guards the value to be one of `string`, `number`, or `symbol` type. + */ + key: typeof guardKey; + /** + * Guards the value to be `null`. + */ + null: typeof guardNull; + /** + * Guards the value to be a `number` of any type. + */ + number: typeof guardNumber; + /** + * Guards the value to be `number` between the specified range. + */ + numberBetween: typeof guardNumberBetween; + /** + * Guards the value to be an `object` of a generic type variable `Obj`. + */ + object: typeof guardObject; + /** + * Guards the value to be an `object` of generic type variable `Obj` that contains the given `key`. + */ + objectKey: typeof guardObjectKey; + /** + * Guards the value to be an `object` of a generic type variable `Obj` that contains(or its prototype chain) the given `key`. + */ + objectKeyIn: typeof guardObjectKeyIn; + /** + * Guards the value to be an `object` of a generic type variable `Obj` with its specified `keys`. + */ + objectKeys: typeof guardObjectKeys; + /** + * Guards the value to be an `object` of a generic type variable `Obj` with specified keys in it(or its prototype chain). + */ + objectKeysIn: typeof guardObjectKeysIn; + /** + * Guards the value to be an `object` of a generic type variable `Obj` with its specified `keys`. + */ + objectSomeKeys: typeof guardObjectSomeKeys; + /** + * Guards the value to be the `Primitive` type or the given `type` of the `Primitives`. + */ + primitive: typeof guardPrimitive; + /** + * Guards the value to be a `RegExp`. + */ + regexp: typeof guardRegExp; + /** + * Guards the value to be `string` of any type. + */ + string: typeof guardString; + /** + * Guards the value to be a `string` type or an instance of `String` that includes all of the specified words/sentences. + */ + stringIncludes: typeof guardStringIncludes; + /** + * Guards the value to be a `string` type or an instance of `String` that includes some of the specified words/sentences. + */ + stringIncludesSome: typeof guardStringIncludesSome; + /** + * Guards the value to be `string` type or `String` instance of a specified length. + */ + stringLength: typeof guardStringLength; + /** + * Guards the value to be `string` or `String` instance of a length between the specified range. + */ + stringLengthBetween: typeof guardStringLengthBetween; + /** + * Guards the value to be a `symbol`. + */ + symbol: typeof guardSymbol; + /** + * Guards the value to be `true`. + */ + true: typeof guardTrue; + /** + * Guards the value to be a type from a given `type`. + */ + type: typeof guardType; + /** + * Guards the value to be `undefined`. + */ + undefined: typeof guardUndefined; +} diff --git a/src/lib/guard-array.func.ts b/src/lib/guard-array.func.ts new file mode 100644 index 0000000..f135846 --- /dev/null +++ b/src/lib/guard-array.func.ts @@ -0,0 +1,18 @@ +// Function. +import { isArray } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * @description Guards the value to be an `array` of a generic type variable `Type`. + * @template Type + * @template {object} [Payload=object] + * @param {Array} value An `array` of generic type variable `Type` to guard. + * @param {?ResultCallback, Payload>} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Array} The return `value` is a `boolean` indicating whether the `value` is an `array` of a generic type variable `Type`. + */ +export const guardArray = ( + value: Array, + callback?: ResultCallback, Payload>, + payload?: Payload +): value is Array => isArray(value, callback, payload); diff --git a/src/lib/guard-big-int.func.ts b/src/lib/guard-big-int.func.ts new file mode 100644 index 0000000..cd6c5fa --- /dev/null +++ b/src/lib/guard-big-int.func.ts @@ -0,0 +1,21 @@ +// Function. +import { isBigInt } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * @description Guards the value to be a `bigint`. + * @template {bigint} BigInt + * @template {object} [Payload=object] + * @param {BigInt} value A `bigint` type value to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is BigInt} The return `value` is a `boolean` indicating whether the `value` is a `bigint`. + */ +export const guardBigInt = < + BigInt extends bigint, + Payload extends object = object +>( + value: BigInt, + callback?: ResultCallback, + payload?: Payload +): value is BigInt => isBigInt(value, callback, payload); diff --git a/src/lib/guard-boolean.func.ts b/src/lib/guard-boolean.func.ts new file mode 100644 index 0000000..383dcb5 --- /dev/null +++ b/src/lib/guard-boolean.func.ts @@ -0,0 +1,21 @@ +// Function. +import { isBoolean } from '@typescript-package/is'; +// Type. +import { AnyBoolean, ResultCallback } from '@typescript-package/type'; +/** + * @description Guards the value to be `boolean` of any type. + * @template {AnyBoolean} Type + * @template {object} [Payload=object] + * @param {Type} value The value of generic type variable `Type` to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Type} The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean`. + */ +export const guardBoolean = < + Type extends AnyBoolean, + Payload extends object = object +>( + value: Type, + callback?: ResultCallback, + payload?: Payload +): value is Type => isBoolean(value, callback, payload); diff --git a/src/lib/guard-class.func.ts b/src/lib/guard-class.func.ts new file mode 100644 index 0000000..c0aeb58 --- /dev/null +++ b/src/lib/guard-class.func.ts @@ -0,0 +1,21 @@ +// Function. +import { isClass } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * @description Guards the value to be a `class` of generic type variable `Class`. + * @template {Function} Class + * @template {object} [Payload=object] + * @param {Class} value The `class` of a generic type variable `Class` to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Class} The return value is a `boolean` indicating whether the provided `value` is a `class` of a generic type variable `Class`. + */ +export const guardClass = < + Class extends Function, + Payload extends object = object +>( + value: Class, + callback?: ResultCallback, + payload?: Payload +): value is Class => isClass(value, callback, payload); diff --git a/src/lib/guard-date.func.ts b/src/lib/guard-date.func.ts new file mode 100644 index 0000000..7369a35 --- /dev/null +++ b/src/lib/guard-date.func.ts @@ -0,0 +1,17 @@ +// Function. +import { isDate } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * @description Guards the value to be a date. + * @template {object} Payload + * @param {Date} value The value of `Date` type to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Date} The return value is a `boolean` indicating whether the value is a date. + */ +export const guardDate = ( + value: Date, + callback?: ResultCallback, + payload?: Payload +): value is Date => isDate(value, callback, payload); diff --git a/src/lib/guard-defined.func.ts b/src/lib/guard-defined.func.ts new file mode 100644 index 0000000..28b58e3 --- /dev/null +++ b/src/lib/guard-defined.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isDefined } from '@typescript-package/is'; +// Type. +import { Defined, ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be defined, not `undefined`. + * @param value The value of generic type `Defined`, never undefined type captured from itself to guard against `undefined`. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is defined. + */ +export const guardDefined = ( + value: Defined, + callback?: ResultCallback, Payload>, + payload?: Payload +): value is Defined => isDefined(value, callback, payload); diff --git a/src/lib/guard-false.func.ts b/src/lib/guard-false.func.ts new file mode 100644 index 0000000..6e11148 --- /dev/null +++ b/src/lib/guard-false.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isFalse } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the provided value to be `false`. + * @param value The value of `false` type to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean` equal to `false`. + */ +export const guardFalse = ( + value: false, + callback?: ResultCallback, + payload?: Payload +): value is false => isFalse(value, callback, payload); diff --git a/src/lib/guard-function.func.ts b/src/lib/guard-function.func.ts new file mode 100644 index 0000000..90ea900 --- /dev/null +++ b/src/lib/guard-function.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isFunction } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be a `Function`. + * @param value The `function` of a generic type variable `Type` to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `Function`. + */ +export const guardFunction = ( + value: Type, + callback?: ResultCallback, + payload?: Payload +): value is Type => isFunction(value, callback, payload); diff --git a/src/lib/guard-instance.func.ts b/src/lib/guard-instance.func.ts new file mode 100644 index 0000000..86331c8 --- /dev/null +++ b/src/lib/guard-instance.func.ts @@ -0,0 +1,21 @@ +// Function. +import { isInstance } from '@typescript-package/is'; +// Type. +import { Constructor, ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be an instance of the given `constructor`. + * @param value An `object` of a generic type variable `Obj` to guard and be compared with an instance of a given `constructor`. + * @param constructor A `class` or `function` that specifies the type of the `constructor`. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is an instance of a given `constructor`. + */ +export const guardInstance = < + Obj extends object, + Payload extends object = object +>( + value: Obj, + constructor: Constructor, + callback?: ResultCallback, + payload?: Payload +): value is Obj => isInstance(value, constructor, callback, payload); diff --git a/src/lib/guard-is.object.ts b/src/lib/guard-is.object.ts new file mode 100644 index 0000000..07ed331 --- /dev/null +++ b/src/lib/guard-is.object.ts @@ -0,0 +1,68 @@ +// Function. +import { + guardArray, + guardBigInt, + guardBoolean, + guardClass, + guardDate, + guardDefined, + guardFalse, + guardFunction, + guardInstance, + guardKey, + guardNull, + guardNumber, + guardNumberBetween, + guardObject, + guardObjectKey, + guardObjectKeyIn, + guardObjectKeys, + guardObjectKeysIn, + guardObjectSomeKeys, + guardPrimitive, + guardRegExp, + guardString, + guardStringIncludes, + guardStringIncludesSome, + guardStringLength, + guardStringLengthBetween, + guardSymbol, + guardTrue, + guardType, + guardUndefined +} from './index'; +// Interface. +import { GuardIs } from '../interface/guard-is.interface'; +// `guardIs`. +export const guardIs: GuardIs = Object.freeze({ + array: guardArray, + bigint: guardBigInt, + boolean: guardBoolean, + class: guardClass, + date: guardDate, + defined: guardDefined, + false: guardFalse, + function: guardFunction, + instance: guardInstance, + key: guardKey, + null: guardNull, + number: guardNumber, + numberBetween: guardNumberBetween, + object: guardObject, + objectKey: guardObjectKey, + objectKeyIn: guardObjectKeyIn, + objectKeys: guardObjectKeys, + objectKeysIn: guardObjectKeysIn, + objectSomeKeys: guardObjectSomeKeys, + primitive: guardPrimitive, + regexp: guardRegExp, + string: guardString, + stringIncludes: guardStringIncludes, + stringIncludesSome: guardStringIncludesSome, + stringLength: guardStringLength, + stringLengthBetween: guardStringLengthBetween, + symbol: guardSymbol, + true: guardTrue, + type: guardType, + undefined: guardUndefined +}); diff --git a/src/lib/guard-key.func.ts b/src/lib/guard-key.func.ts new file mode 100644 index 0000000..9ff8ca5 --- /dev/null +++ b/src/lib/guard-key.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isKey } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be one of `string`, `number`, or `symbol` type. + * @param value The value of generic type variable `Key` to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `string`, `number`, or `symbol`. + */ +export const guardKey = ( + value: Key, + callback?: ResultCallback, + payload?: Payload +): value is Key => isKey(value, callback, payload); diff --git a/src/lib/guard-null.func.ts b/src/lib/guard-null.func.ts new file mode 100644 index 0000000..3281271 --- /dev/null +++ b/src/lib/guard-null.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isNull } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be `null`. + * @param value The value of `null` type to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is `null`. + */ +export const guardNull = ( + value: null, + callback?: ResultCallback, + payload?: Payload +): value is null => isNull(value, callback, payload); diff --git a/src/lib/guard-number-between.func.ts b/src/lib/guard-number-between.func.ts new file mode 100644 index 0000000..b91cd78 --- /dev/null +++ b/src/lib/guard-number-between.func.ts @@ -0,0 +1,27 @@ +// Function. +import { isNumberBetween } from '@typescript-package/is'; +// Type. +import { AnyNumber, ResultCallback, NumberBetween } from '@typescript-package/type'; +/** + * Guards the value to be `number` type or instance of `Number` between the specified range. + * @param value The value of a generic type variable `Type` to guard. + * @param min The **minimum** range of generic type variable `Min` for a given `value`. + * @param max The **maximum** range of generic type variable `Max` for a given `value`. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `number` type or an instance of `Number` between the + * specified range. + */ +export const guardNumberBetween = < + Type extends AnyNumber, + Min extends number, + Max extends number, + Payload extends object = object +>( + value: Type, + min: Min, + max: Max, + callback?: ResultCallback, + payload?: Payload +): value is NumberBetween => + isNumberBetween(value, min, max, callback, payload); diff --git a/src/lib/guard-number.func.ts b/src/lib/guard-number.func.ts new file mode 100644 index 0000000..72f3846 --- /dev/null +++ b/src/lib/guard-number.func.ts @@ -0,0 +1,19 @@ +// Function. +import { isNumber } from '@typescript-package/is'; +// Type. +import { AnyNumber, ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be a `number` of any type. + * @param value The value of generic type variable `Type` to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `number` type or an instance of `Number`. + */ +export const guardNumber = < + Type extends AnyNumber, + Payload extends object = object +>( + value: Type, + callback?: ResultCallback, + payload?: Payload +): value is Type => isNumber(value, callback, payload); diff --git a/src/lib/guard-object-key-in.func.ts b/src/lib/guard-object-key-in.func.ts new file mode 100644 index 0000000..c0a454e --- /dev/null +++ b/src/lib/guard-object-key-in.func.ts @@ -0,0 +1,24 @@ +// Function. +import { isObjectKeyIn } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the `value` to be an `object` of a generic type variable `Obj` that contains(or its prototype chain) the given `key`. + * @param value An `object` of a generic type variable `Obj`, by default of the type captured from itself that contains(or its prototype + * chain) the given `key`. + * @param key A key of generic type variable `Key` as the property name that the given `value` contains(or its prototype chain). + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is an `object` that contains(or its prototype chain) the given + * `key`. + */ +export const guardObjectKeyIn = < + Obj extends object, + Key extends keyof Obj, + Payload extends object = object +>( + value: Obj, + key: Key, + callback?: ResultCallback, + payload?: Payload +): value is Obj => isObjectKeyIn(value, key, callback, payload as any); diff --git a/src/lib/guard-object-key.func.ts b/src/lib/guard-object-key.func.ts new file mode 100644 index 0000000..a05addf --- /dev/null +++ b/src/lib/guard-object-key.func.ts @@ -0,0 +1,24 @@ +// Function. +import { isObjectKey } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be an `object` of generic type variable `Obj` that contains the given `key`. + * @param value An `object` of a generic type variable `Obj`, by default of the type captured from itself that contains the given `key` to + * guard. + * @param key A key of generic type variable `Key` as the property name that the given `value` contains. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is an `object` of generic type variable `Obj` that contains the + * given `key`. + */ +export const guardObjectKey = < + Obj extends object, + Key extends keyof Obj, + Payload extends object = object +>( + value: Obj, + key: Key, + callback?: ResultCallback, + payload?: Payload +): value is Obj => isObjectKey(value, key, callback, payload as any); diff --git a/src/lib/guard-object-keys-in.func.ts b/src/lib/guard-object-keys-in.func.ts new file mode 100644 index 0000000..1d96cdf --- /dev/null +++ b/src/lib/guard-object-keys-in.func.ts @@ -0,0 +1,24 @@ +// Function. +import { isObjectKeysIn } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be an `object` of a generic type variable `Obj` with specified keys in it(or its prototype chain). + * @param value An object of a generic type variable `Obj`, by default of the type captured from itself that contains(or its prototype + * chain) the given `keys` to guard. + * @param keys An `Array` of property keys to check whether the given `value` contains(or its prototype chain). + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is an `object` with specified `keys` in it(or its prototype + * chain). + */ +export const guardObjectKeysIn = < + Obj extends object, + Key extends keyof Obj, + Payload extends object = object +>( + value: Obj, + keys: Key[], + callback?: ResultCallback, + payload?: Payload +): value is Obj => isObjectKeysIn(value, keys, callback, payload as any); diff --git a/src/lib/guard-object-keys.func.ts b/src/lib/guard-object-keys.func.ts new file mode 100644 index 0000000..0c3d2e4 --- /dev/null +++ b/src/lib/guard-object-keys.func.ts @@ -0,0 +1,23 @@ +// Function. +import { isObjectKeys } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be an `object` of a generic type variable `Obj` with its specified `keys`. + * @param value An object of a generic type variable `Obj`, by default of the type captured from itself that contains the given `keys` to + * guard. + * @param keys An `Array` of property keys to check whether the given `value` contains. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is an `object` with its specified `keys`. + */ +export const guardObjectKeys = < + Obj extends object, + Key extends keyof Obj, + Payload extends object = object +>( + value: Obj, + keys: Key[], + callback?: ResultCallback, + payload?: Payload +): value is Obj => isObjectKeys(value, keys, callback, payload as any); diff --git a/src/lib/guard-object-some-keys.func.ts b/src/lib/guard-object-some-keys.func.ts new file mode 100644 index 0000000..7b6ef69 --- /dev/null +++ b/src/lib/guard-object-some-keys.func.ts @@ -0,0 +1,24 @@ +// Function. +import { isObjectSomeKeys } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be an `object` of a generic type variable `Obj` with some of its `keys` or some groups of its `keys`. + * @param value An object of a generic type variable `Obj`, by default of the type captured from itself that contains some or some of the + * groups of the given `keys`, to guard. + * @param keys An `Array` of property names or a two-dimensional array of property names to check if the given `value` contains some of + * them. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the provided `value` is an `object` with some or some groups of the given + * `keys`. + */ +export const guardObjectSomeKeys = < + Obj extends object, + Payload extends object = object +>( + value: Obj, + keys: (keyof Obj | Array)[], + callback?: ResultCallback, + payload?: Payload +): value is Obj => isObjectSomeKeys(value, keys, callback, payload as any); diff --git a/src/lib/guard-object.func.ts b/src/lib/guard-object.func.ts new file mode 100644 index 0000000..5d818f3 --- /dev/null +++ b/src/lib/guard-object.func.ts @@ -0,0 +1,19 @@ +// Function. +import { isObject } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be an `object` of a generic type variable `Obj`. + * @param value An `object` of a generic type variable `Obj`, by default of the type captured from itself to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the provided `value` is an `object` of a generic type variable `Obj`. + */ +export const guardObject = < + Obj extends object, + Payload extends object = object +>( + value: Obj, + callback?: ResultCallback, + payload?: Payload +): value is Obj => isObject(value, callback, payload); diff --git a/src/lib/guard-primitive.func.ts b/src/lib/guard-primitive.func.ts new file mode 100644 index 0000000..ffebc0c --- /dev/null +++ b/src/lib/guard-primitive.func.ts @@ -0,0 +1,22 @@ +// Function. +import { isPrimitive } from '@typescript-package/is'; +// Type. +import { ResultCallback, Primitive, PrimitiveNames } from '@typescript-package/type'; +/** + * Guards the value to be the `Primitive` type or the given `type` of the `Primitives`. + * @param value The value of a generic type variable `Type` constrained by the `Primitive`, by default of the type captured from itself to + * guard. + * @param type An optional specific type of `Primitives` to check the given value. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is the `Primitive` type or the given `type`. + */ +export const guardPrimitive = < + Type extends Primitive, + Payload extends object = object +>( + value: Type, + type?: PrimitiveNames, + callback?: ResultCallback, + payload?: Payload +): value is Type => isPrimitive(value, type, callback, payload); diff --git a/src/lib/guard-regexp.func.ts b/src/lib/guard-regexp.func.ts new file mode 100644 index 0000000..dbc72c5 --- /dev/null +++ b/src/lib/guard-regexp.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isRegExp } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be a `RegExp`. + * @param value Regular expression of generic type variable `Type` constrained by `RegExp` to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return `value` is a `boolean` indicating whether the `value` is a `RegExp`. + */ +export const guardRegExp = ( + value: Type, + callback?: ResultCallback, + payload?: Payload +): value is Type => isRegExp(value, callback, payload); diff --git a/src/lib/guard-string-includes-some.func.ts b/src/lib/guard-string-includes-some.func.ts new file mode 100644 index 0000000..64726fe --- /dev/null +++ b/src/lib/guard-string-includes-some.func.ts @@ -0,0 +1,27 @@ +// Function. +import { isStringIncludesSome } from '@typescript-package/is'; +import { resultCallback } from '@typescript-package/core'; +// Type. +import { AnyString, ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be a `string` type or an instance of `String` that includes some of the specified words/sentences. + * @param value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to + * check against the `string` that contains some of the words/sentences from a given `includes`. + * @param includes An `Array` of `string` as words/sentences to be case-sensitive searched for within the given `value`. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the provided `value` is a `string` type or an instance of `String` that + * includes all of the specified words/sentences. + */ +export const guardStringIncludesSome = < + Type extends AnyString, + Payload extends object = object +>( + value: Type, + includes: string[], + callback: ResultCallback< + Type, + { includes: typeof includes } & Payload + > = resultCallback, + payload?: Payload +): value is Type => isStringIncludesSome(value, includes, callback, payload); diff --git a/src/lib/guard-string-includes.func.ts b/src/lib/guard-string-includes.func.ts new file mode 100644 index 0000000..60ab065 --- /dev/null +++ b/src/lib/guard-string-includes.func.ts @@ -0,0 +1,27 @@ +// Function. +import { isStringIncludes } from '@typescript-package/is'; +import { resultCallback } from '@typescript-package/core'; +// Type. +import { AnyString, ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be a `string` type or an instance of `String` that includes the specified words/sentences. + * @param value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to + * check against the `string` that contains words/sentences from a given `includes`. + * @param includes An `Array` of `string` as words/sentences to be case-sensitive searched for within the given `value`. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the provided `value` is a `string` type or an instance of `String` that + * includes the specified words/sentences. + */ +export const guardStringIncludes = < + Type extends AnyString, + Payload extends object = object +>( + value: Type, + includes: string[], + callback: ResultCallback< + Type, + { includes: typeof includes } & Payload + > = resultCallback, + payload?: Payload +): value is Type => isStringIncludes(value, includes, callback, payload); diff --git a/src/lib/guard-string-length-between.func.ts b/src/lib/guard-string-length-between.func.ts new file mode 100644 index 0000000..ce9f892 --- /dev/null +++ b/src/lib/guard-string-length-between.func.ts @@ -0,0 +1,28 @@ +// Function. +import { isStringLengthBetween } from '@typescript-package/is'; +// Type. +import { AnyString, ResultCallback, StringOfLength } from '@typescript-package/type'; +/** + * Guards the value to be `string` or `String` instance of a length between the specified range. + * @param value The value of a generic type variable `Type` constrained by `AnyString`, by default of the type captured from itself to + * guard. + * @param min The **minimum** length of generic type variable `Min` of a given `value`. + * @param max The **maximum** length of generic type variable `Max` of a given `value`. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String` of a length between + * the specified range. + */ +export const guardStringLengthBetween = < + Type extends AnyString, + Min extends number, + Max extends number, + Payload extends object = object +>( + value: Type, + min: Min, + max: Max, + callback?: ResultCallback, + payload?: Payload +): value is StringOfLength => + isStringLengthBetween(value, min, max, callback, payload); diff --git a/src/lib/guard-string-length.func.ts b/src/lib/guard-string-length.func.ts new file mode 100644 index 0000000..ed8e298 --- /dev/null +++ b/src/lib/guard-string-length.func.ts @@ -0,0 +1,26 @@ +// Function. +import { isStringLength } from '@typescript-package/is'; +// Type. +import { AnyString, ResultCallback, StringOfLength } from '@typescript-package/type'; +/** + * Guards the value to be `string` type or `String` instance of a specified length. + * @param value The value of a generic type variable `Type` constrained by `AnyString`, by default of the type captured from itself to + * guard. + * @param length The **length** of generic type variable `Length` of a given `value`. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String` of a specified + * `length`. + */ +export const guardStringLength = < + Type extends AnyString, + Length extends number, + Payload extends object = object +>( + value: Type, + length: Length, + callback?: ResultCallback, + payload?: Payload +): value is StringOfLength => + isStringLength(value, length, callback, payload); + diff --git a/src/lib/guard-string.func.ts b/src/lib/guard-string.func.ts new file mode 100644 index 0000000..a05f390 --- /dev/null +++ b/src/lib/guard-string.func.ts @@ -0,0 +1,20 @@ +// Function. +import { isString } from '@typescript-package/is'; +// Type. +import { AnyString, ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be `string` of any type. + * @param value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to + * guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String`. + */ +export const guardString = < + Type extends AnyString, + Payload extends object = object +>( + value: Type, + callback?: ResultCallback, + payload?: Payload +): value is Type => isString(value, callback, payload); diff --git a/src/lib/guard-symbol.func.ts b/src/lib/guard-symbol.func.ts new file mode 100644 index 0000000..73c216d --- /dev/null +++ b/src/lib/guard-symbol.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isSymbol } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be a `symbol`. + * @param value A `symbol` type value to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `symbol`. + */ +export const guardSymbol = ( + value: symbol, + callback?: ResultCallback, + payload?: Payload +): value is symbol => isSymbol(value, callback, payload); diff --git a/src/lib/guard-true.func.ts b/src/lib/guard-true.func.ts new file mode 100644 index 0000000..9330816 --- /dev/null +++ b/src/lib/guard-true.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isTrue } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be `true`. + * @param value The value of `true` type to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean` equal to `true`. + */ +export const guardTrue = ( + value: true, + callback?: ResultCallback, + payload?: Payload +): value is true => isTrue(value, callback, payload); diff --git a/src/lib/guard-type.func.ts b/src/lib/guard-type.func.ts new file mode 100644 index 0000000..6f872ba --- /dev/null +++ b/src/lib/guard-type.func.ts @@ -0,0 +1,18 @@ +// Function. +import { isType } from '@typescript-package/is'; +// Type. +import { ResultCallback, Type, TypeNames } from '@typescript-package/type'; +/** + * Guards the value to be a type of given `type`. + * @param value The value of a generic type variable `T` constrained by the `Type`, by default of the type captured from itself, to guard. + * @param type The value of `string` or `Constructor` type of the `Types` indicates against which type a given `value` is checked. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is a type from a given `type`. + */ +export const guardType = ( + value: T, + type: TypeNames, + callback?: ResultCallback, + payload?: Payload +): value is T => isType(value, type, callback, payload); diff --git a/src/lib/guard-undefined.func.ts b/src/lib/guard-undefined.func.ts new file mode 100644 index 0000000..ed6c7ac --- /dev/null +++ b/src/lib/guard-undefined.func.ts @@ -0,0 +1,16 @@ +// Function. +import { isUndefined } from '@typescript-package/is'; +// Type. +import { ResultCallback } from '@typescript-package/type'; +/** + * Guards the value to be `undefined`. + * @param value The value of an `undefined` type to guard. + * @param callback An optional `ResultCallback` function to handle the result before returns. + * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns The return value is a `boolean` indicating whether the `value` is `undefined`. + */ +export const guardUndefined = ( + value: undefined, + callback?: ResultCallback, + payload?: Payload +): value is undefined => isUndefined(value, callback, payload); diff --git a/src/lib/guard.object.ts b/src/lib/guard.object.ts new file mode 100644 index 0000000..54ee33c --- /dev/null +++ b/src/lib/guard.object.ts @@ -0,0 +1,6 @@ +// Import: Object. +import { guardIs } from './guard-is.object'; +// Import: Interface. +import { Guard } from '../interface/guard-interface'; +// Export: `guard` +export const guard: Guard = Object.freeze({ ...guardIs }); diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..84fb050 --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1,30 @@ +export { guardArray } from './guard-array.func'; +export { guardBigInt } from './guard-big-int.func'; +export { guardBoolean } from './guard-boolean.func'; +export { guardClass } from './guard-class.func'; +export { guardDate } from './guard-date.func'; +export { guardDefined } from './guard-defined.func'; +export { guardFalse } from './guard-false.func'; +export { guardFunction } from './guard-function.func'; +export { guardInstance } from './guard-instance.func'; +export { guardKey } from './guard-key.func'; +export { guardNull } from './guard-null.func'; +export { guardNumber } from './guard-number.func'; +export { guardNumberBetween } from './guard-number-between.func'; +export { guardObject } from './guard-object.func'; +export { guardObjectKey } from './guard-object-key.func'; +export { guardObjectKeyIn } from './guard-object-key-in.func'; +export { guardObjectKeys } from './guard-object-keys.func'; +export { guardObjectKeysIn } from './guard-object-keys-in.func'; +export { guardObjectSomeKeys } from './guard-object-some-keys.func'; +export { guardPrimitive } from './guard-primitive.func'; +export { guardRegExp } from './guard-regexp.func'; +export { guardString } from './guard-string.func'; +export { guardStringIncludes } from './guard-string-includes.func'; +export { guardStringIncludesSome } from './guard-string-includes-some.func'; +export { guardStringLength } from './guard-string-length.func'; +export { guardStringLengthBetween } from './guard-string-length-between.func'; +export { guardSymbol } from './guard-symbol.func'; +export { guardTrue } from './guard-true.func'; +export { guardType } from './guard-type.func'; +export { guardUndefined } from './guard-undefined.func'; \ No newline at end of file diff --git a/src/public-api.ts b/src/public-api.ts new file mode 100644 index 0000000..f76bf35 --- /dev/null +++ b/src/public-api.ts @@ -0,0 +1,36 @@ +/* + * Public API Surface of guard + */ +// `guard` object. +export { guard } from './lib/guard.object'; +// `guard` prefix functions, +export { guardArray } from './lib/guard-array.func'; +export { guardBigInt } from './lib/guard-big-int.func'; +export { guardBoolean } from './lib/guard-boolean.func'; +export { guardClass } from './lib/guard-class.func'; +export { guardDate } from './lib/guard-date.func'; +export { guardDefined } from './lib/guard-defined.func'; +export { guardFalse } from './lib/guard-false.func'; +export { guardFunction } from './lib/guard-function.func'; +export { guardInstance } from './lib/guard-instance.func'; +export { guardKey } from './lib/guard-key.func'; +export { guardNull } from './lib/guard-null.func'; +export { guardNumber } from './lib/guard-number.func'; +export { guardNumberBetween } from './lib/guard-number-between.func'; +export { guardObject } from './lib/guard-object.func'; +export { guardObjectKey } from './lib/guard-object-key.func'; +export { guardObjectKeyIn } from './lib/guard-object-key-in.func'; +export { guardObjectKeysIn } from './lib/guard-object-keys-in.func'; +export { guardObjectKeys } from './lib/guard-object-keys.func'; +export { guardObjectSomeKeys } from './lib/guard-object-some-keys.func'; +export { guardPrimitive } from './lib/guard-primitive.func'; +export { guardRegExp } from './lib/guard-regexp.func'; +export { guardString } from './lib/guard-string.func'; +export { guardStringIncludes } from './lib/guard-string-includes.func'; +export { guardStringIncludesSome } from './lib/guard-string-includes-some.func'; +export { guardStringLength } from './lib/guard-string-length.func'; +export { guardStringLengthBetween } from './lib/guard-string-length-between.func'; +export { guardSymbol } from './lib/guard-symbol.func'; +export { guardTrue } from './lib/guard-true.func'; +export { guardType } from './lib/guard-type.func'; +export { guardUndefined } from './lib/guard-undefined.func'; diff --git a/src/test/guard-array.spec.ts b/src/test/guard-array.spec.ts new file mode 100644 index 0000000..1552203 --- /dev/null +++ b/src/test/guard-array.spec.ts @@ -0,0 +1,53 @@ +// Function. +import { guardArray } from '../lib/guard-array.func'; +// Testing. +import { + // Main. + Testing, + + // Constants. + TESTING_ARRAY_BIGINT, + TESTING_ARRAY_BOOLEAN, + TESTING_ARRAY_CLASS, + TESTING_ARRAY_FUNCTION, + TESTING_ARRAY_NULL, + TESTING_ARRAY_NUMBER, + TESTING_ARRAY_OBJECT_ONE, + TESTING_ARRAY_STRING, + TESTING_ARRAY_SYMBOL_NUMBER, + TESTING_ARRAY_SYMBOL_STRING, + TESTING_ARRAY_UNDEFINED, + + // Class. + TestingClass, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.array.describe, + tests.guard.array.it +); +/** + * Tests. + */ +testing.describe(guardArray.name, () => { + testing + // TRUE + .it('is DEFINED', () => expect(guardArray).toBeDefined()) + .it('Array', () => expect(guardArray(TESTING_ARRAY_BIGINT)).toBeTruthy()) + .it('Array', () => expect(guardArray(TESTING_ARRAY_BOOLEAN)).toBeTruthy()) + .it('Array', () => expect(guardArray(TESTING_ARRAY_CLASS)).toBeTruthy()) + .it('Array', () => expect(guardArray(TESTING_ARRAY_FUNCTION)).toBeTruthy()) + .it('Array', () => expect(guardArray(TESTING_ARRAY_NULL)).toBeTruthy()) + .it('Array', () => expect(guardArray(TESTING_ARRAY_NUMBER)).toBeTruthy()) + .it('Array Array', () => expect(guardArray(TESTING_ARRAY_OBJECT_ONE)).toBeTruthy()) + .it('Array', () => expect(guardArray(TESTING_ARRAY_STRING)).toBeTruthy()) + .it('Array', () => { + expect(guardArray(TESTING_ARRAY_SYMBOL_STRING)).toBeTruthy(); + expect(guardArray(TESTING_ARRAY_SYMBOL_NUMBER)).toBeTruthy(); + }) + .it('Array', () => expect(guardArray(TESTING_ARRAY_UNDEFINED)).toBeTruthy()); +}); diff --git a/src/test/guard-bigint.spec.ts b/src/test/guard-bigint.spec.ts new file mode 100644 index 0000000..842844b --- /dev/null +++ b/src/test/guard-bigint.spec.ts @@ -0,0 +1,52 @@ +// Function. +import { guardBigInt } from '../lib/guard-big-int.func'; +// Object. +import { guard } from '../lib/guard.object'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_BIGINT, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.bigint.describe, + tests.guard.bigint.it +); +/** + * Tests. + */ +testing.describe(guardBigInt.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardBigInt).toBeDefined()) + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardBigInt(TESTING_BIGINT, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_BIGINT); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + // ... primitives. + .describe(`primitive`, () => + testing.describe(`bigint`, () => + testing + .it(`${TESTING_BIGINT}`, () => expect(guardBigInt(TESTING_BIGINT)).toBeTrue()) + .it(`${TESTING_BIGINT}`, () => expect(guard.bigint(TESTING_BIGINT)).toBeTrue()) + )); + }); +}); diff --git a/src/test/guard-boolean.spec.ts b/src/test/guard-boolean.spec.ts new file mode 100644 index 0000000..2da8371 --- /dev/null +++ b/src/test/guard-boolean.spec.ts @@ -0,0 +1,70 @@ +// Function. +import { guardBoolean } from '../lib/guard-boolean.func'; +// Object. +import { guard } from '../lib/guard.object'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_FALSE, + TESTING_FALSE_INSTANCE, + TESTING_TRUE, + TESTING_TRUE_INSTANCE, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.boolean.describe, + tests.guard.boolean.it +); +/** + * Tests. + */ +testing.describe(guardBoolean.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardBoolean).toBeDefined()) + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardBoolean(TESTING_TRUE, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_TRUE); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + // ... primitives. + .describe(`primitive`, () => { + // boolean + testing.describe(`boolean`, () => { + testing + .it(`FALSE`, () => expect(guardBoolean(TESTING_FALSE)).toBeTrue()) + .it(`TRUE`, () => expect(guardBoolean(TESTING_TRUE)).toBeTrue()) + .it(`FALSE`, () => expect(guard.boolean(TESTING_FALSE)).toBeTrue()) + .it(`TRUE`, () => expect(guard.boolean(TESTING_TRUE)).toBeTrue()); + }); + }) + // ... instance. + .describe(`instance`, () => { + // boolean + testing.describe(`boolean`, () => { + testing + .it(`${TESTING_TRUE_INSTANCE}`, () => expect(guardBoolean(TESTING_TRUE_INSTANCE)).toBeTrue()) + .it(`${TESTING_FALSE_INSTANCE}`, () => expect(guardBoolean(TESTING_FALSE_INSTANCE)).toBeTrue()) + .it(`${TESTING_FALSE_INSTANCE}`, () => expect(guard.boolean(TESTING_FALSE_INSTANCE)).toBeTrue()) + .it(`${TESTING_TRUE_INSTANCE}`, () => expect(guard.boolean(TESTING_TRUE_INSTANCE)).toBeTrue()); + }); + }); + }); +}); diff --git a/src/test/guard-class.spec.ts b/src/test/guard-class.spec.ts new file mode 100644 index 0000000..7273c20 --- /dev/null +++ b/src/test/guard-class.spec.ts @@ -0,0 +1,49 @@ +// Object. +import { guard } from '../lib/guard.object'; +// Function. +import { guardClass } from '../lib/guard-class.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_FUNCTION, + TestingPerson +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.class.describe, + tests.guard.class.it +); +/** + * Tests. + */ +testing.describe(guardClass.name, () => { + testing + .it('is DEFINED', () => expect(guardClass).toBeDefined()) + .it('with callback and payload', () => { + guardClass(TestingPerson, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TestingPerson); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + .it(`Class`, () => { + expect(guardClass(TestingPerson)).toBeTrue(); + expect(guard.class(TestingPerson)).toBeTrue(); + }) + .it(`FUNCTION`, () => { + expect(guardClass(TESTING_FUNCTION)).toBeFalse(); + expect(guard.class(TESTING_FUNCTION)).toBeFalse(); + }); +}); diff --git a/src/test/guard-date.spec.ts b/src/test/guard-date.spec.ts new file mode 100644 index 0000000..0bd2961 --- /dev/null +++ b/src/test/guard-date.spec.ts @@ -0,0 +1,74 @@ +// Function. +import { guardDate } from '../lib/guard-date.func'; +// Object. +import { guard } from '../lib/guard.object'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_DATE, + TESTING_FUNCTION, + + // Class. + TestingPerson +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.date.describe, + tests.guard.date.it +); +/** + * Tests. + */ +testing.describe(guardDate.name, () => { + let guardSpy: any; + testing + .it('is DEFINED', () => expect(guardDate).toBeDefined()); + + beforeEach(() => guardSpy = { ...{}, ...guard }); + testing + .it('with callback and payload', () => { + guardDate(TESTING_DATE, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_DATE); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + .it(`called with ${TESTING_DATE}`, () => { + spyOn(guardSpy, 'date').withArgs(TESTING_DATE).and.returnValue(true); + guardSpy.date(TESTING_DATE); + expect(guardSpy.date).toHaveBeenCalled(); + }) + .it(`guard new Date()`, () => { + expect(guardDate(new Date('December 17, 1995 03:24:00'))).toBeTrue(); + expect(guardDate(new Date('1995-12-17T03:24:00'))).toBeTrue(); + expect(guardDate(new Date(1995, 11, 17))).toBeTrue(); + expect(guardDate(new Date(1995, 11, 17, 3, 24, 0))).toBeTrue(); + expect(guardDate(new Date(628021800000))).toBeTrue(); + expect(guardDate(new Date(98, 1))).toBeTrue(); + expect(guardDate(new Date(22, 1))).toBeTrue(); + + expect(guard.date(new Date('December 17, 1995 03:24:00'))).toBeTrue(); + expect(guard.date(new Date('1995-12-17T03:24:00'))).toBeTrue(); + expect(guard.date(new Date(1995, 11, 17))).toBeTrue(); + expect(guard.date(new Date(1995, 11, 17, 3, 24, 0))).toBeTrue(); + expect(guard.date(new Date(628021800000))).toBeTrue(); + expect(guard.date(new Date(98, 1))).toBeTrue(); + expect(guard.date(new Date(22, 1))).toBeTrue(); + }) + .it(`guard new Date() false`, () => { + expect(guardDate(new Date('invalid date'))).toBeFalse(); + expect(guard.date(new Date('invalid date'))).toBeFalse(); + }); +}); diff --git a/src/test/guard-defined.spec.ts b/src/test/guard-defined.spec.ts new file mode 100644 index 0000000..515d8e4 --- /dev/null +++ b/src/test/guard-defined.spec.ts @@ -0,0 +1,45 @@ +// Function. +import { guardDefined } from '../lib/guard-defined.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_STRING, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.defined.describe, + tests.guard.defined.it +); +/** + * Tests. + */ +testing.describe(guardDefined.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardDefined).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardDefined(TESTING_STRING, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_STRING); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + .it('undefined', () => expect(guardDefined(undefined as any)).toBeFalse()); + }); +}); diff --git a/src/test/guard-false.spec.ts b/src/test/guard-false.spec.ts new file mode 100644 index 0000000..e51f9e4 --- /dev/null +++ b/src/test/guard-false.spec.ts @@ -0,0 +1,59 @@ +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_TRUE, + TESTING_FALSE +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +import { guardFalse } from '../lib/guard-false.func'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.false.describe, + tests.guard.false.it +); +/** + * Tests. + */ +testing.describe(guardFalse.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardFalse).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardFalse(TESTING_FALSE, (result, value, payload) => { + expect(result).toBe(TESTING_TRUE); + expect(value).toBeFalse(); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => + testing + .it(`${TESTING_FALSE}`, () => expect(guardFalse(TESTING_FALSE)).toBeTrue()) + .it(`${TESTING_TRUE}`, () => expect(guardFalse(TESTING_TRUE as any)).toBeFalse()) + ) + + // ... object. + .describe(`object`, () => + testing + .it(`new Boolean(${TESTING_TRUE})`, () => expect(guardFalse(new Boolean(TESTING_TRUE) as any)).toBeFalse()) + .it(`new Boolean(${TESTING_FALSE})`, () => expect(guardFalse(new Boolean(TESTING_FALSE) as any)).toBeTrue()) + ); + }); +}); diff --git a/src/test/guard-function.spec.ts b/src/test/guard-function.spec.ts new file mode 100644 index 0000000..ddd5684 --- /dev/null +++ b/src/test/guard-function.spec.ts @@ -0,0 +1,42 @@ +// Function. +import { guardFunction } from '../lib/guard-function.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_FUNCTION, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +import { guardFalse } from '../lib/guard-false.func'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.function.describe, + tests.guard.function.it +); +/** + * Tests. + */ +testing.describe(guardFalse.name, () => { + testing + // TRUE + .it('is DEFINED', () => expect(guardFunction).toBeDefined()) + .it('with callback and payload', () => { + guardFunction(TESTING_FUNCTION, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_FUNCTION); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + .it(`function | Function`, () => expect(guardFunction(TESTING_FUNCTION)).toBeTrue()); +}); diff --git a/src/test/guard-instance.spec.ts b/src/test/guard-instance.spec.ts new file mode 100644 index 0000000..824d82f --- /dev/null +++ b/src/test/guard-instance.spec.ts @@ -0,0 +1,89 @@ +// Function. +import { guardInstance } from '../lib/guard-instance.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_CLASS, + TESTING_FALSE_INSTANCE, + TESTING_NUMBER, + TESTING_NUMBER_CONSTRUCTOR, + TESTING_NUMBER_INSTANCE, + TESTING_STRING, + TESTING_STRING_CONSTRUCTOR, + TESTING_STRING_INSTANCE, + TESTING_TRUE_INSTANCE, + TESTING_FUNCTION_CONSTRUCTOR_PERSON, + TESTING_PERSON, + + // Class. + TestingClass, + TestingPerson, + TestingPersonShape +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.instance.describe, + tests.guard.instance.it +); +/** + * Tests. + */ +testing.describe(guardInstance.name, () => { + const personInstance: TestingPersonShape = new (TESTING_FUNCTION_CONSTRUCTOR_PERSON as any)('First name', 'Sur name', 27); + + testing + // Defined. + .it('is DEFINED', () => expect(guardInstance).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('callback', () => { + guardInstance(TESTING_CLASS, TestingClass, (result, value, payload) => { + expect(result).toBeTrue(); + if (payload) { + expect(value).toEqual(TESTING_CLASS); + } + return result; + }); + }) + // ... instance. + .describe(`instance`, () => { + testing + .it(`CLASS`, () => expect(guardInstance(TESTING_CLASS, TestingClass)).toBeTrue()) + .it(`class TestingPerson`, () => expect(guardInstance(TESTING_PERSON, TestingPerson)).toBeTrue()) + .it(`function`, () => expect(guardInstance(personInstance, TESTING_FUNCTION_CONSTRUCTOR_PERSON as any)).toBeTrue()); + }) + + // ... primitives. + .describe(`primitive`, () => { + testing + // boolean + .describe(`boolean`, () => { + testing + .it(`${TESTING_TRUE_INSTANCE}`, () => expect(guardInstance(TESTING_TRUE_INSTANCE, Boolean)).toBeTrue()) + .it(`${TESTING_FALSE_INSTANCE}`, () => expect(guardInstance(TESTING_FALSE_INSTANCE, Boolean)).toBeTrue()); + }) + + // number + .describe(`number`, () => { + testing + .it(`Number(${TESTING_NUMBER})`, () => expect(guardInstance(TESTING_NUMBER_CONSTRUCTOR, Number)).toBeFalse()) + .it(`new Number(${TESTING_NUMBER})`, () => expect(guardInstance(TESTING_NUMBER_INSTANCE, Number)).toBeTrue()); + }) + + // string + .describe(`string`, () => { + it(`String(${TESTING_STRING})`, () => expect(guardInstance(TESTING_STRING_CONSTRUCTOR, String)).toBeFalse()); + it(`new String(${TESTING_STRING})`, () => expect(guardInstance(TESTING_STRING_INSTANCE, String)).toBeTrue()); + }); + }); + }); +}); diff --git a/src/test/guard-key.spec.ts b/src/test/guard-key.spec.ts new file mode 100644 index 0000000..b10cc37 --- /dev/null +++ b/src/test/guard-key.spec.ts @@ -0,0 +1,61 @@ +// Function. +import { guardKey } from '../lib/guard-key.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_NUMBER, + TESTING_STRING, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.key.describe, + tests.guard.key.it +); +/** + * Tests. + */ +testing.describe(guardKey.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardKey).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardKey(TESTING_NUMBER, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_NUMBER); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => { + testing + // number + .describe(`number`, () => it(`${TESTING_NUMBER}`, () => expect(guardKey(TESTING_NUMBER)).toBeTrue())) + // string + .describe(`string`, () => it(`${TESTING_STRING}`, () => expect(guardKey(TESTING_STRING)).toBeTrue())) + // symbol + .describe(`symbol`, () => { + it(`Symbol(${TESTING_NUMBER})`, () => expect(guardKey(TESTING_SYMBOL_NUMBER)).toBeTrue()); + it(`Symbol(${TESTING_STRING})`, () => expect(guardKey(TESTING_SYMBOL_STRING)).toBeTrue()); + }); + }); + }); +}); diff --git a/src/test/guard-null.spec.ts b/src/test/guard-null.spec.ts new file mode 100644 index 0000000..256a2f6 --- /dev/null +++ b/src/test/guard-null.spec.ts @@ -0,0 +1,48 @@ +// Function. +import { guardNull } from '../lib/guard-null.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_NULL, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.null.describe, + tests.guard.null.it +); +/** + * Tests. + */ +testing.describe(guardNull.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardNull).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + + testing + .it('with callback and payload', () => { + guardNull(TESTING_NULL, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_NULL); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => testing.it(`${TESTING_NULL}`, () => expect(guardNull(TESTING_NULL)).toBeTrue())); + }); +}); diff --git a/src/test/guard-number-between.spec.ts b/src/test/guard-number-between.spec.ts new file mode 100644 index 0000000..c43c0b6 --- /dev/null +++ b/src/test/guard-number-between.spec.ts @@ -0,0 +1,54 @@ +// Function. +import { guardNumberBetween } from '../lib/guard-number-between.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_NUMBER, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.numberBetween.describe, + tests.guard.numberBetween.it +); +/** + * Tests. + */ +testing.describe(guardNumberBetween.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardNumberBetween).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardNumberBetween(TESTING_NUMBER, 1, TESTING_NUMBER, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_NUMBER); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => { + testing + // number + .describe(`number`, () => { + testing + .it(`${TESTING_NUMBER}`, () => expect(guardNumberBetween(TESTING_NUMBER, 1, TESTING_NUMBER)).toBeTrue()); + }); + }); + }); +}); diff --git a/src/test/guard-number.spec.ts b/src/test/guard-number.spec.ts new file mode 100644 index 0000000..76a1fd8 --- /dev/null +++ b/src/test/guard-number.spec.ts @@ -0,0 +1,59 @@ +// Function. +import { guardNumber } from '../lib/guard-number.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_NUMBER, + TESTING_NUMBER_CONSTRUCTOR, + TESTING_NUMBER_INSTANCE, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.number.describe, + tests.guard.number.it +); +/** + * Tests. + */ +testing.describe(guardNumber.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardNumber).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardNumber(TESTING_NUMBER, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_NUMBER); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => { + testing + // number + .describe(`number`, () => + testing + .it(`${TESTING_NUMBER}`, () => expect(guardNumber(TESTING_NUMBER)).toBeTrue()) + .it(`${TESTING_NUMBER_CONSTRUCTOR}`, () => expect(guardNumber(TESTING_NUMBER_CONSTRUCTOR)).toBeTrue())) + // number object + .describe(`number object`, () => + testing.it(`${TESTING_NUMBER_INSTANCE}`, () => expect(guardNumber(TESTING_NUMBER_INSTANCE)).toBeTrue())); + }); + }); +}); diff --git a/src/test/guard-object-key-in.spec.ts b/src/test/guard-object-key-in.spec.ts new file mode 100644 index 0000000..0d2e69f --- /dev/null +++ b/src/test/guard-object-key-in.spec.ts @@ -0,0 +1,96 @@ +// Function. +import { guardObjectKeyIn } from '../lib/guard-object-key-in.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_CLASS, + TESTING_NUMBER, + TESTING_OBJECT, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.objectKeyIn.describe, + tests.guard.objectKeyIn.it +); +/** + * Tests. + */ +testing.describe(guardObjectKeyIn.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardObjectKeyIn).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardObjectKeyIn(TESTING_CLASS, 'firstName', (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_CLASS); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... instance. + .describe(`instance`, () => { + testing.describe(`CLASS`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectKeyIn(TESTING_CLASS, 1030405027)).toBeTrue(); + expect(guardObjectKeyIn(TESTING_CLASS, 5)).toBeTrue(); + }) + + .it('finds getter number', () => expect(guardObjectKeyIn(TESTING_CLASS, TESTING_NUMBER)).toBeTrue()) + + // string. + .it('has string key', () => expect(guardObjectKeyIn(TESTING_CLASS, 'surname')).toBeTrue()) + + // symbol. + .it('finds getter symbol key', () => { + expect(guardObjectKeyIn(TESTING_CLASS, TESTING_SYMBOL_NUMBER)).toBeTrue(); + expect(guardObjectKeyIn(TESTING_CLASS, TESTING_SYMBOL_STRING)).toBeTrue(); + }); + }); + }) + + // ... objects. + .describe('object', () => { + testing.describe(`OBJECT_ONE`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectKeyIn(TESTING_OBJECT, 1030405027)).toBeTrue(); + expect(guardObjectKeyIn(TESTING_OBJECT, 5)).toBeTrue(); + expect(guardObjectKeyIn(TESTING_OBJECT, TESTING_NUMBER)).toBeTrue(); + }) + + // string. + .it('has string key', () => { + expect(guardObjectKeyIn(TESTING_OBJECT, 'key as string')).toBeTrue(); + expect(guardObjectKeyIn(TESTING_OBJECT, 'x')).toBeTrue(); + }) + + // symbol. + .it('has symbol key', () => { + expect(guardObjectKeyIn(TESTING_OBJECT, TESTING_NUMBER)).toBeTrue(); + expect(guardObjectKeyIn(TESTING_OBJECT, TESTING_SYMBOL_STRING)).toBeTrue(); + }); + }); + }); + }); +}); diff --git a/src/test/guard-object-key.spec.ts b/src/test/guard-object-key.spec.ts new file mode 100644 index 0000000..187ffee --- /dev/null +++ b/src/test/guard-object-key.spec.ts @@ -0,0 +1,93 @@ +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_CLASS, + TESTING_FALSE, + TESTING_NUMBER, + TESTING_OBJECT, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +import { guardObjectKey } from '../lib/guard-object-key.func'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.objectKey.describe, + tests.guard.objectKey.it +); +/** + * Tests. + */ +testing.describe(guardObjectKey.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardObjectKey).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('callback', () => { + guardObjectKey(TESTING_CLASS, 'firstName', (result, value, payload) => { + expect(result).toBeTrue(); + if (payload) { + expect(value).toEqual(TESTING_CLASS); + } + return result; + }); + }) + + // ... instance. + .describe(`instance`, () => { + testing.describe(`CLASS`, () => { + // number. + it('has number key', () => { + expect(guardObjectKey(TESTING_CLASS, 1030405027)).toBeTrue(); + expect(guardObjectKey(TESTING_CLASS, 5)).toBeTrue(); + }); + + it('finds getter number', () => expect(guardObjectKey(TESTING_CLASS, TESTING_NUMBER)).toBe(TESTING_FALSE)); + + // string. + it('has string key', () => expect(guardObjectKey(TESTING_CLASS, 'surname')).toBeTrue()); + + // symbol. + it('finds getter symbol key', () => { + expect(guardObjectKey(TESTING_CLASS, TESTING_SYMBOL_NUMBER)).toBe(TESTING_FALSE); + expect(guardObjectKey(TESTING_CLASS, TESTING_SYMBOL_STRING)).toBe(TESTING_FALSE); + }); + }); + }) + + // ... objects. + .describe('object', () => { + testing.describe(`OBJECT_ONE`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectKey(TESTING_OBJECT, 1030405027)).toBeTrue(); + expect(guardObjectKey(TESTING_OBJECT, 5)).toBeTrue(); + expect(guardObjectKey(TESTING_OBJECT, TESTING_NUMBER)).toBeTrue(); + }) + + // string. + .it('has string key', () => { + expect(guardObjectKey(TESTING_OBJECT, 'key as string')).toBeTrue(); + expect(guardObjectKey(TESTING_OBJECT, 'x')).toBeTrue(); + }) + + // symbol. + .it('has symbol key', () => { + expect(guardObjectKey(TESTING_OBJECT, TESTING_NUMBER)).toBeTrue(); + expect(guardObjectKey(TESTING_OBJECT, TESTING_SYMBOL_STRING)).toBeTrue(); + }); + }); + }); + }); +}); diff --git a/src/test/guard-object-keys-in.spec.ts b/src/test/guard-object-keys-in.spec.ts new file mode 100644 index 0000000..7b9bb4b --- /dev/null +++ b/src/test/guard-object-keys-in.spec.ts @@ -0,0 +1,108 @@ +// Function. +import { guardObjectKeysIn } from '../lib/guard-object-keys-in.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_CLASS, + TESTING_NUMBER, + TESTING_OBJECT, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.objectKeysIn.describe, + tests.guard.objectKeysIn.it +); +/** + * Tests. + */ +testing.describe(guardObjectKeysIn.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardObjectKeysIn).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + // ... instance. + .describe(`instance`, () => { + testing.describe(`TESTING_CLASS`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectKeysIn(TESTING_CLASS, [1030405027])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_CLASS, [5])).toBeTrue(); + }) + + .it('finds getter number', () => expect(guardObjectKeysIn(TESTING_CLASS, [TESTING_NUMBER])).toBeTrue()) + + // string. + .it('has string key', () => expect(guardObjectKeysIn(TESTING_CLASS, ['surname'])).toBeTrue()) + + // symbol. + .it('finds getter symbol key', () => { + expect(guardObjectKeysIn(TESTING_CLASS, [TESTING_SYMBOL_NUMBER])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_CLASS, [TESTING_SYMBOL_STRING])).toBeTrue(); + }); + }); + }) + + // ... objects. + .describe('object', () => { + testing + .describe(`TESTING_OBJECT`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectKeysIn(TESTING_OBJECT, [1030405027])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_OBJECT, [5])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_NUMBER])).toBeTrue(); + }) + + // string. + .it('has string key', () => { + expect(guardObjectKeysIn(TESTING_OBJECT, ['key as string'])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_OBJECT, ['x'])).toBeTrue(); + }) + + // symbol. + .it('has symbol key', () => { + expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_SYMBOL_NUMBER, TESTING_SYMBOL_STRING])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_SYMBOL_STRING])).toBeTrue(); + }); + }); + }) + + .describe('object with some and every key', () => { + testing.describe(`TESTING_OBJECT`, () => { + testing + // number. + .it('has number key or any key', () => { + expect(guardObjectKeysIn(TESTING_OBJECT, [1030405027, 'key as string'])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_OBJECT, [5])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_NUMBER])).toBeTrue(); + }) + + // string. + .it('has string key', () => { + expect(guardObjectKeysIn(TESTING_OBJECT, ['key as string'])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_OBJECT, ['x'])).toBeTrue(); + }) + + // symbol. + .it('has symbol key', () => { + expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_SYMBOL_NUMBER])).toBeTrue(); + expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_SYMBOL_STRING])).toBeTrue(); + }); + }); + }); + }); +}); diff --git a/src/test/guard-object-keys.spec.ts b/src/test/guard-object-keys.spec.ts new file mode 100644 index 0000000..6221b04 --- /dev/null +++ b/src/test/guard-object-keys.spec.ts @@ -0,0 +1,108 @@ +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_CLASS, + TESTING_NUMBER, + TESTING_OBJECT, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +import { guardObjectKeys } from '../lib/guard-object-keys.func'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.objectKeys.describe, + tests.guard.objectKeys.it +); +/** + * Tests. + */ +testing.describe(guardObjectKeys.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardObjectKeys).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + // ... instance. + .describe(`instance`, () => { + testing.describe(`TESTING_CLASS`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectKeys(TESTING_CLASS, [1030405027])).toBeTrue(); + expect(guardObjectKeys(TESTING_CLASS, [5])).toBeTrue(); + }) + + .it('finds getter number', () => expect(guardObjectKeys(TESTING_CLASS, [TESTING_NUMBER])).toBeFalse()) + + // string. + .it('has string key', () => expect(guardObjectKeys(TESTING_CLASS, ['surname'])).toBeTrue()) + + // symbol. + .it('finds getter symbol key', () => { + expect(guardObjectKeys(TESTING_CLASS, [TESTING_SYMBOL_NUMBER])).toBeFalse(); + expect(guardObjectKeys(TESTING_CLASS, [TESTING_SYMBOL_STRING])).toBeFalse(); + }); + }); + }) + + // ... objects. + .describe('object', () => { + testing + .describe(`TESTING_OBJECT`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectKeys(TESTING_OBJECT, [1030405027])).toBeTrue(); + expect(guardObjectKeys(TESTING_OBJECT, [5])).toBeTrue(); + expect(guardObjectKeys(TESTING_OBJECT, [TESTING_NUMBER])).toBeTrue(); + }) + + // string. + .it('has string key', () => { + expect(guardObjectKeys(TESTING_OBJECT, ['key as string'])).toBeTrue(); + expect(guardObjectKeys(TESTING_OBJECT, ['x'])).toBeTrue(); + }) + + // symbol. + .it('has symbol key', () => { + expect(guardObjectKeys(TESTING_OBJECT, [TESTING_SYMBOL_NUMBER, TESTING_SYMBOL_STRING])).toBeTrue(); + expect(guardObjectKeys(TESTING_OBJECT, [TESTING_SYMBOL_STRING])).toBeTrue(); + }); + }); + }) + + .describe('object with some and every key', () => { + testing.describe(`TESTING_OBJECT`, () => { + testing + // number. + .it('has number key or any key', () => { + expect(guardObjectKeys(TESTING_OBJECT, [1030405027, 'key as string'])).toBeTrue(); + expect(guardObjectKeys(TESTING_OBJECT, [5])).toBeTrue(); + expect(guardObjectKeys(TESTING_OBJECT, [TESTING_NUMBER])).toBeTrue(); + }) + + // string. + .it('has string key', () => { + expect(guardObjectKeys(TESTING_OBJECT, ['key as string'])).toBeTrue(); + expect(guardObjectKeys(TESTING_OBJECT, ['x'])).toBeTrue(); + }) + + // symbol. + .it('has symbol key', () => { + expect(guardObjectKeys(TESTING_OBJECT, [TESTING_SYMBOL_NUMBER])).toBeTrue(); + expect(guardObjectKeys(TESTING_OBJECT, [TESTING_SYMBOL_STRING])).toBeTrue(); + }); + }); + }); + }); +}); diff --git a/src/test/guard-object-some-keys.spec.ts b/src/test/guard-object-some-keys.spec.ts new file mode 100644 index 0000000..0e361b9 --- /dev/null +++ b/src/test/guard-object-some-keys.spec.ts @@ -0,0 +1,110 @@ +// Function. +import { guardObjectSomeKeys } from '../lib/guard-object-some-keys.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_CLASS, + TESTING_NUMBER, + TESTING_OBJECT, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.objectSomeKeys.describe, + tests.guard.objectSomeKeys.it +); +/** + * Tests. + */ +testing.describe(guardObjectSomeKeys.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardObjectSomeKeys).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + // ... instance. + .describe(`instance`, () => { + testing + .describe(`TESTING_CLASS`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectSomeKeys(TESTING_CLASS, [1030405027])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_CLASS, [5])).toBeTrue(); + }) + + .it('finds getter number', () => expect(guardObjectSomeKeys(TESTING_CLASS, [TESTING_NUMBER])).toBeFalse()) + + // string. + .it('has string key', () => expect(guardObjectSomeKeys(TESTING_CLASS, ['surname'])).toBeTrue()) + + // symbol. + .it('finds getter symbol key', () => { + expect(guardObjectSomeKeys(TESTING_CLASS, [TESTING_SYMBOL_NUMBER])).toBeFalse(); + expect(guardObjectSomeKeys(TESTING_CLASS, [TESTING_SYMBOL_STRING])).toBeFalse(); + }); + }); + }) + + // ... objects. + .describe('object', () => { + testing + .describe(`TESTING_OBJECT`, () => { + testing + // number. + .it('has number key', () => { + expect(guardObjectSomeKeys(TESTING_OBJECT, [1030405027])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_OBJECT, [5])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_OBJECT, [TESTING_NUMBER])).toBeTrue(); + }) + + // string. + .it('has string key', () => { + expect(guardObjectSomeKeys(TESTING_OBJECT, ['key as string'])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_OBJECT, ['x'])).toBeTrue(); + }) + + // symbol. + .it('has symbol key', () => { + expect(guardObjectSomeKeys(TESTING_OBJECT, [TESTING_SYMBOL_NUMBER, TESTING_SYMBOL_STRING])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_OBJECT, [TESTING_SYMBOL_STRING])).toBeTrue(); + }); + }); + }) + + .describe('object with some and every key', () => { + testing + .describe(`TESTING_OBJECT`, () => { + testing + // number. + .it('has number key or any key', () => { + expect(guardObjectSomeKeys(TESTING_OBJECT, [1030405027, [5, TESTING_NUMBER]])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_OBJECT, [[TESTING_NUMBER], [5]])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_OBJECT, [TESTING_NUMBER])).toBeTrue(); + }) + + // string. + .it('has string key', () => { + expect(guardObjectSomeKeys(TESTING_OBJECT, ['key as string', ['!@#$%^&*()Company', 'x']])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_OBJECT, [['x', '!@#$%^&*()Company'], 'key as string'])).toBeTrue(); + }) + + // symbol. + .it('has symbol key', () => { + expect(guardObjectSomeKeys(TESTING_OBJECT, [TESTING_SYMBOL_NUMBER, [TESTING_SYMBOL_NUMBER, TESTING_SYMBOL_STRING]])).toBeTrue(); + expect(guardObjectSomeKeys(TESTING_OBJECT, [TESTING_SYMBOL_STRING])).toBeTrue(); + }); + }); + }); + }); +}); diff --git a/src/test/guard-object.spec.ts b/src/test/guard-object.spec.ts new file mode 100644 index 0000000..2e51fe1 --- /dev/null +++ b/src/test/guard-object.spec.ts @@ -0,0 +1,51 @@ +// Function. +import { guardObject } from '../lib/guard-object.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_CLASS, + TESTING_DATE, + TESTING_OBJECT, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.object.describe, + tests.guard.object.it +); +/** + * Tests. + */ +testing.describe(guardObject.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardObject).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('callback', () => { + guardObject(TESTING_OBJECT, (result, value, payload) => { + expect(result).toBeTrue(); + if (payload) { + expect(value).toEqual(TESTING_OBJECT); + } + return result; + }); + }) + + // ... objects. + .describe('object', () => { + testing + .it(`CLASS`, () => expect(guardObject(TESTING_CLASS)).toBeTrue()) + .it(`OBJECT_ONE`, () => expect(guardObject(TESTING_OBJECT)).toBeTrue()) + .it(`TESTING_DATE`, () => expect(guardObject(TESTING_DATE)).toBeTrue()); + }); + }); +}); diff --git a/src/test/guard-primitive.spec.ts b/src/test/guard-primitive.spec.ts new file mode 100644 index 0000000..e16a6cb --- /dev/null +++ b/src/test/guard-primitive.spec.ts @@ -0,0 +1,88 @@ +// Function. +import { guardPrimitive } from '../lib/guard-primitive.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_BIGINT, + TESTING_FALSE, + TESTING_NULL, + TESTING_NUMBER, + TESTING_STRING, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, + TESTING_TRUE, + TESTING_UNDEFINED, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.primitive.describe, + tests.guard.primitive.it +); +/** + * Tests. + */ +testing.describe(guardPrimitive.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardPrimitive).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('callback', () => { + guardPrimitive(TESTING_STRING, 'string' , (result, value, payload) => { + expect(result).toBeTrue(); + if (payload) { + expect(value).toEqual(TESTING_STRING); + } + return result; + }); + }) + + // ... primitives. + .describe(`primitive`, () => { + testing + // bigint + .describe(`bigint`, () => it(`${TESTING_BIGINT}`, () => expect(guardPrimitive(TESTING_BIGINT, 'bigint')).toBeTrue())) + + // boolean + .describe(`boolean`, () => { + testing + .it(`${TESTING_TRUE}`, () => expect(guardPrimitive(TESTING_TRUE, 'boolean')).toBeTrue()) + .it(`${TESTING_FALSE}`, () => expect(guardPrimitive(TESTING_FALSE, 'boolean')).toBeTrue()); + }) + + // null + .it(`${TESTING_NULL}`, () => expect(guardPrimitive(TESTING_NULL, 'null')).toBeTrue()) + + // number + .describe(`number`, () => { + testing + .it(`${TESTING_NUMBER}`, () => expect(guardPrimitive(TESTING_NUMBER, 'number')).toBeTrue()); + }) + + // string + .describe(`string`, () => { + testing + .it(`${TESTING_STRING}`, () => expect(guardPrimitive(TESTING_STRING, 'string')).toBeTrue()); + }) + + // symbol + .describe(`symbol`, () => { + testing + .it(`Symbol(${TESTING_NUMBER})`, () => expect(guardPrimitive(TESTING_SYMBOL_NUMBER, 'symbol')).toBeTrue()) + .it(`Symbol(${TESTING_STRING})`, () => expect(guardPrimitive(TESTING_SYMBOL_STRING, 'symbol')).toBeTrue()); + }) + + // undefined + .it(`${TESTING_UNDEFINED}`, () => expect(guardPrimitive(TESTING_UNDEFINED, 'undefined')).toBeTrue()); + }); + }); +}); diff --git a/src/test/guard-regexp.spec.ts b/src/test/guard-regexp.spec.ts new file mode 100644 index 0000000..e3d083a --- /dev/null +++ b/src/test/guard-regexp.spec.ts @@ -0,0 +1,52 @@ +// Function. +import { guardRegExp } from '../lib/guard-regexp.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_TRUE, + TESTING_REGEXP +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.regexp.describe, + tests.guard.regexp.it +); +/** + * Tests. + */ +testing.describe(guardRegExp.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardRegExp).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardRegExp(TESTING_REGEXP, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_REGEXP); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => { + testing + // bigint + .describe(`RegExp`, () => it(`${TESTING_REGEXP}`, () => expect(guardRegExp(TESTING_REGEXP)).toBe(TESTING_TRUE))); + }); + }); +}); diff --git a/src/test/guard-string-includes-some.spec.ts b/src/test/guard-string-includes-some.spec.ts new file mode 100644 index 0000000..8facc24 --- /dev/null +++ b/src/test/guard-string-includes-some.spec.ts @@ -0,0 +1,87 @@ +// Function. +import { guardStringIncludesSome } from '../lib/guard-string-includes-some.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_STRING, + TESTING_STRING_CONSTRUCTOR, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.is.stringIncludesSome.describe, + tests.is.stringIncludesSome.it +); +/** + * Tests. + */ +testing.describe(guardStringIncludesSome.name, () => { + const TESTING_STRING_LONG = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. + Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, + when an unknown printer took a galley of type and scrambled it to make a type specimen book. + It has survived not only five centuries, but also the leap into electronic typesetting, + remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset + sheets containing Lorem Ipsum passages, and more recently with desktop publishing software + like Aldus PageMaker including versions of Lorem Ipsum.`; + + const TESTING_STRING_LONG_INSTANCE = new String(TESTING_STRING_LONG); + + testing + // Defined. + .it('is DEFINED', () => expect(guardStringIncludesSome).toBeDefined()) + + // Checks ... + .describe(`checks`, () => { + testing + .it('callback', () => { + guardStringIncludesSome(TESTING_STRING, ['Company'], (result, value, payload) => { + expect(result).toBeTrue(); + if (payload) { + expect(value).toEqual(TESTING_STRING); + } + return result; + }); + }) + // ... primitives. + .describe(`primitive`, () => { + testing + // string + .describe(`string`, () => { + testing + .it(`TESTING_STRING, TESTING_STRING_LONG`, () => { + expect(guardStringIncludesSome(TESTING_STRING, [TESTING_STRING, '!@#$%^&*()', '3'])).toBeTrue(); + // No word exists. + expect(guardStringIncludesSome(TESTING_STRING_LONG, [ + 'no text', + 'no text is here' + ])).toBeFalse(); + + // Every word exists. + expect(guardStringIncludesSome(TESTING_STRING_LONG_INSTANCE, [ + 'Lorem', + 'unchanged', + 'versions', + 'only', + ])).toBeTrue(); + + // Some word exists. + expect(guardStringIncludesSome(TESTING_STRING_LONG, [ + 'Lorem', + 'unchanged', + 'versions', + 'only', + 'no text is here' + ])).toBeTrue(); + }) + .it(`String(${TESTING_STRING})`, () => + expect(guardStringIncludesSome(TESTING_STRING_CONSTRUCTOR, [TESTING_STRING, '!@#$%^&*()', '3'])).toBeTrue()); + }); + }); + }); +}); \ No newline at end of file diff --git a/src/test/guard-string-includes.spec.ts b/src/test/guard-string-includes.spec.ts new file mode 100644 index 0000000..9437501 --- /dev/null +++ b/src/test/guard-string-includes.spec.ts @@ -0,0 +1,88 @@ +// Function. +import { guardStringIncludes } from '../lib/guard-string-includes.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_STRING, + TESTING_STRING_CONSTRUCTOR, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.stringIncludes.describe, + tests.guard.stringIncludes.it +); +/** + * Tests. + */ +testing.describe(guardStringIncludes.name, () => { + const TESTING_STRING_LONG = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. + Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, + when an unknown printer took a galley of type and scrambled it to make a type specimen book. + It has survived not only five centuries, but also the leap into electronic typesetting, + remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset + sheets containing Lorem Ipsum passages, and more recently with desktop publishing software + like Aldus PageMaker including versions of Lorem Ipsum.`; + + const TESTING_STRING_LONG_INSTANCE = new String(TESTING_STRING_LONG); + + testing + // Defined. + .it('is DEFINED', () => expect(guardStringIncludes).toBeDefined()) + + // Checks ... + .describe(`checks`, () => { + testing + .it('callback', () => { + guardStringIncludes(TESTING_STRING, ['Company'], (result, value, payload) => { + expect(result).toBeTrue(); + if (payload) { + expect(value).toEqual(TESTING_STRING); + } + return result; + }); + }) + // ... primitives. + .describe(`primitive`, () => { + testing + // string + .describe(`string`, () => { + testing + .it(`${TESTING_STRING}`, () => { + expect(guardStringIncludes(TESTING_STRING, ['Company'])).toBeTrue(); + expect(guardStringIncludes(TESTING_STRING, [TESTING_STRING, '!@#$%^&*()'])).toBeTrue(); + // No word exists. + expect(guardStringIncludes(TESTING_STRING_LONG, [ + 'no text', + 'no text is here' + ])).toBeFalse(); + + // Every word exists. + expect(guardStringIncludes(TESTING_STRING_LONG_INSTANCE, [ + 'Lorem', + 'unchanged', + 'versions', + 'only', + ])).toBeTrue(); + + // Some word exists. + expect(guardStringIncludes(TESTING_STRING_LONG, [ + 'Lorem', + 'unchanged', + 'versions', + 'only', + 'no text is here' + ])).toBeFalse(); + }) + .it(`String(${TESTING_STRING})`, () => + expect(guardStringIncludes(TESTING_STRING_CONSTRUCTOR, [TESTING_STRING, '!@#$%^&*()'])).toBeTrue()); + }); + }); + }); +}); diff --git a/src/test/guard-string-length-between.spec.ts b/src/test/guard-string-length-between.spec.ts new file mode 100644 index 0000000..290ee17 --- /dev/null +++ b/src/test/guard-string-length-between.spec.ts @@ -0,0 +1,86 @@ +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_STRING, + TESTING_STRING_INSTANCE, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +import { guardStringLengthBetween } from '../lib/guard-string-length-between.func'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.stringLengthBetween.describe, + tests.guard.stringLengthBetween.it +); +/** + * Tests. + */ +testing.describe(`guardStringLengthBetween()`, () => { + // Defined. + testing + .it('is DEFINED', () => expect(guardStringLengthBetween).toBeDefined()) + + // Checks ... + .describe(`guards`, () => + testing + .it('with callback and payload', () => { + guardStringLengthBetween(TESTING_STRING, 3, 1000, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_STRING); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => + testing + .describe(`string`, () => + testing + .it(`${TESTING_STRING} minimum 3`, () => + expect(guardStringLengthBetween(TESTING_STRING, 3, 1000)).toBeTrue() + ) + .it(`${TESTING_STRING} maximum 3`, () => + expect(guardStringLengthBetween(TESTING_STRING, 0, 3)).toBeFalse() + ) + ) + .it(`${TESTING_STRING} minimum 18`, () => + expect(guardStringLengthBetween(TESTING_STRING, 18, 1000)).toBeFalse() + ) + .it(`${TESTING_STRING} maximum 17`, () => + expect(guardStringLengthBetween(TESTING_STRING, 0, 17)).toBeTrue() + ) + .it(`${TESTING_STRING} minimum 5 maximum 21`, () => + expect(guardStringLengthBetween(TESTING_STRING, 5, 21)).toBeTrue() + ) + ) + ) + .describe(`String`, () => + testing + .it(`new String(${TESTING_STRING}) minimum 3`, () => + expect(guardStringLengthBetween(TESTING_STRING_INSTANCE, 3, 1000)).toBeTrue() + ) + .it(`new String(${TESTING_STRING}) maximum 3`, () => + expect(guardStringLengthBetween(TESTING_STRING_INSTANCE, 0, 3)).toBeFalse() + ) + .it(`new String(${TESTING_STRING}) minimum 18`, () => + expect(guardStringLengthBetween(TESTING_STRING_INSTANCE, 18, 1000)).toBeFalse() + ) + .it(`new String(${TESTING_STRING}) maximum 17`, () => + expect(guardStringLengthBetween(TESTING_STRING_INSTANCE, 0, 17)).toBeTrue() + ) + .it(`new String(${TESTING_STRING}) minimum 5 maximum 21`, () => + expect(guardStringLengthBetween(TESTING_STRING_INSTANCE, 5, 21)).toBeTrue() + ) + ); +}); diff --git a/src/test/guard-string-length.spec.ts b/src/test/guard-string-length.spec.ts new file mode 100644 index 0000000..0d92238 --- /dev/null +++ b/src/test/guard-string-length.spec.ts @@ -0,0 +1,70 @@ +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_STRING, + TESTING_STRING_INSTANCE, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +import { guardStringLength } from '../lib/guard-string-length.func'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.stringLength.describe, + tests.guard.stringLength.it +); +/** + * Tests. + */ +testing.describe(`guardStringLength()`, () => { + // Defined. + testing + .it('is DEFINED', () => expect(guardStringLength).toBeDefined()) + + // Checks ... + .describe(`guards`, () => + testing + .it('with callback and payload', () => { + guardStringLength(TESTING_STRING, 17, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_STRING); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => + testing + .describe(`string`, () => + testing + .it(`${TESTING_STRING}`, () => + expect(guardStringLength(TESTING_STRING, 17)).toBeTrue() + ) + .it(`${TESTING_STRING}`, () => + expect(guardStringLength(TESTING_STRING, 16)).toBeFalse() + ) + .it(`${TESTING_STRING}`, () => + expect(guardStringLength(TESTING_STRING, 18)).toBeFalse() + ) + ) + ) + ) + .describe(`String`, () => + testing + .it(`new String(${TESTING_STRING})`, () => + (expect(guardStringLength(TESTING_STRING_INSTANCE, 17)).toBeTrue(), + expect(guardStringLength(TESTING_STRING_INSTANCE, 16)).toBeFalse(), + expect(guardStringLength(TESTING_STRING_INSTANCE, 18)).toBeFalse()) + ) + ); +}); diff --git a/src/test/guard-string.spec.ts b/src/test/guard-string.spec.ts new file mode 100644 index 0000000..1cbc586 --- /dev/null +++ b/src/test/guard-string.spec.ts @@ -0,0 +1,61 @@ +// Function. +import { guardString } from '../lib/guard-string.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_STRING, + TESTING_STRING_INSTANCE, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.string.describe, + tests.guard.string.it +); +/** + * Tests. + */ +testing.describe(guardString.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardString).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardString(TESTING_STRING, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_STRING); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + // ... primitives. + .describe(`primitive`, () => { + testing + .describe(`string`, () => + testing.it(`${TESTING_STRING}`, () => + expect(guardString(TESTING_STRING)).toBeTrue() + ) + ) + .describe(`object`, () => + testing.describe(`String`, () => + testing.it(`new String(${TESTING_STRING})`, () => + expect(guardString(TESTING_STRING_INSTANCE)).toBeTrue() + ) + ) + ); + }); + }); +}); diff --git a/src/test/guard-symbol.spec.ts b/src/test/guard-symbol.spec.ts new file mode 100644 index 0000000..d461d90 --- /dev/null +++ b/src/test/guard-symbol.spec.ts @@ -0,0 +1,59 @@ +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_NUMBER, + TESTING_STRING, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, + TESTING_TRUE, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +import { guardSymbol } from '../lib/guard-symbol.func'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.symbol.describe, + tests.guard.symbol.it +); +/** + * Tests. + */ +testing.describe(guardSymbol.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardSymbol).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardSymbol(TESTING_SYMBOL_STRING, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_SYMBOL_STRING); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + + // ... primitives. + .describe(`primitive`, () => { + testing + // symbol + .describe(`symbol`, () => { + testing + .it(`Symbol(${TESTING_NUMBER})`, () => expect(guardSymbol(TESTING_SYMBOL_NUMBER)).toBe(TESTING_TRUE)) + .it(`Symbol(${TESTING_STRING})`, () => expect(guardSymbol(TESTING_SYMBOL_STRING)).toBe(TESTING_TRUE)); + }); + }); + }); +}); diff --git a/src/test/guard-true.spec.ts b/src/test/guard-true.spec.ts new file mode 100644 index 0000000..f04883a --- /dev/null +++ b/src/test/guard-true.spec.ts @@ -0,0 +1,55 @@ +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_TRUE, + TESTING_FALSE +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +import { guardTrue } from '../lib/guard-true.func'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.true.describe, + tests.guard.true.it +); +/** + * Tests. + */ +testing.describe(guardTrue.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardTrue).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing.it('callback', () => { + guardTrue(TESTING_TRUE, (result, value, payload) => { + expect(result).toBe(TESTING_TRUE); + if (payload) { + expect(value).toBeTrue(); + } + return result; + }); + }) + + // ... primitives. + .describe(`primitive`, () => + testing + .it(`${TESTING_TRUE}`, () => expect(guardTrue(TESTING_TRUE)).toBe(TESTING_TRUE)) + .it(`${TESTING_FALSE}`, () => expect(guardTrue(TESTING_FALSE as any)).toBe(TESTING_FALSE)) + ) + + // ... object. + .describe(`object`, () => + testing + .it(`new Boolean(${TESTING_TRUE})`, () => expect(guardTrue(new Boolean(TESTING_TRUE) as any)).toBe(TESTING_TRUE)) + .it(`new Boolean(${TESTING_FALSE})`, () => expect(guardTrue(new Boolean(TESTING_FALSE) as any)).toBe(TESTING_FALSE)) + ); + }); +}); diff --git a/src/test/guard-type.spec.ts b/src/test/guard-type.spec.ts new file mode 100644 index 0000000..fe77fd2 --- /dev/null +++ b/src/test/guard-type.spec.ts @@ -0,0 +1,132 @@ +// Function. +import { guardType } from '../lib/guard-type.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_BIGINT, + TESTING_CLASS, + TESTING_FALSE, + TESTING_FALSE_INSTANCE, + TESTING_FUNCTION, + TESTING_NULL, + TESTING_NUMBER, + TESTING_NUMBER_CONSTRUCTOR, + TESTING_NUMBER_INSTANCE, + TESTING_OBJECT, + TESTING_STRING, + TESTING_STRING_CONSTRUCTOR, + TESTING_STRING_INSTANCE, + TESTING_SYMBOL_NUMBER, + TESTING_SYMBOL_STRING, + TESTING_TRUE, + TESTING_TRUE_INSTANCE, + TESTING_UNDEFINED, + + // Class. + TestingClass, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +// Function. +/** + * Initialize testing. + */ +const testing = new Testing(tests.is.type.describe, tests.is.type.it); +/** + * Tests. + */ +testing.describe(guardType.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardType).toBeDefined()) + + // Checks ... + .describe(`checks`, () => { + testing + .it('callback', () => { + guardType(TESTING_STRING, 'string', (result, value, payload) => { + expect(result).toBeTrue(); + if (payload) { + expect(value).toEqual(TESTING_STRING); + } + return result; + }); + guardType(TESTING_NUMBER, 'number', (result, value, payload) => { + expect(result).toBeTrue(); + if (payload) { + expect(value).toEqual(TESTING_NUMBER); + } + return result; + }); + }) + // ... instance. + .describe(`instance`, () => testing.it(`Class`, () => expect(guardType(TESTING_CLASS, TestingClass)).toBeTrue())) + // ... function. + .describe(`function`, () => { + testing + .it(`${TESTING_FUNCTION}`, () => expect(guardType(TESTING_FUNCTION, 'function')).toBeTrue()) + .it(`${TESTING_CLASS}`, () => expect(guardType(TestingClass, 'function')).toBeFalse()); + }) + // ... objects. + .describe('object', () => { + testing + .it(`CLASS`, () => expect(guardType(TESTING_CLASS, 'object')).toBeTrue()) + .it(`TESTING_OBJECT`, () => expect(guardType(TESTING_OBJECT, 'object')).toBeTrue()); + }) + // ... primitives. + .describe(`primitive`, () => { + testing + // bigint + .describe(`bigint`, () => testing.it(`${TESTING_BIGINT}`, () => expect(guardType(TESTING_BIGINT, 'bigint')).toBeTrue())) + // boolean + .describe(`boolean`, () => { + testing + .it(`${TESTING_TRUE}`, () => expect(guardType(TESTING_TRUE, 'boolean')).toBeTrue()) + .it(`${TESTING_FALSE}`, () => expect(guardType(TESTING_FALSE, 'boolean')).toBeTrue()); + }) + // null + .it(`${TESTING_NULL}`, () => expect(guardType(TESTING_NULL, 'null')).toBeTrue()) + // number + .describe(`number`, () => { + testing + .it(`${TESTING_NUMBER}`, () => expect(guardType(TESTING_NUMBER, 'number')).toBeTrue()) + .it(`Number(${TESTING_NUMBER})`, () => expect(guardType(TESTING_NUMBER_CONSTRUCTOR, 'number')).toBeTrue()); + }) + // string + .describe(`string`, () => { + testing + .it(`${TESTING_STRING}`, () => expect(guardType(TESTING_STRING, 'string')).toBeTrue()) + .it(`String(${TESTING_STRING})`, () => expect(guardType(TESTING_STRING_CONSTRUCTOR, 'string')).toBeTrue()); + }) + // symbol + .describe(`symbol`, () => { + testing + .it(`Symbol(${TESTING_NUMBER})`, () => expect(guardType(TESTING_SYMBOL_NUMBER, 'symbol')).toBeTrue()) + .it(`Symbol(${TESTING_STRING})`, () => expect(guardType(TESTING_SYMBOL_STRING, 'symbol')).toBeTrue()); + }) + // undefined + .it(`${TESTING_UNDEFINED}`, () => expect(guardType(TESTING_UNDEFINED, 'undefined')).toBeTrue()) + // ... object. + .describe(`object`, () => { + testing + // BigInt + .describe(`BigInt`, () => testing.it(`${TESTING_BIGINT}`, () => expect(guardType(TESTING_BIGINT, 'bigint')).toBeTrue())) + // Boolean + .describe(`Boolean`, () => { + testing + .it(`${TESTING_TRUE_INSTANCE}`, () => expect(guardType(TESTING_TRUE_INSTANCE, 'boolean')).toBeFalse()) + .it(`${TESTING_FALSE_INSTANCE}`, () => expect(guardType(TESTING_FALSE_INSTANCE, 'boolean')).toBeFalse()); + }) + // Number + .describe(`Number`, () => + testing.it(`new Number(${TESTING_NUMBER})`, () => expect(guardType(TESTING_NUMBER_INSTANCE, 'number')).toBeFalse())) + // String + .describe(`String`, () => + testing.it(`new String(${TESTING_STRING})`, () => expect(guardType(TESTING_STRING_INSTANCE, 'string')).toBeFalse())); + }); + }); + }); +}); diff --git a/src/test/guard-undefined.spec.ts b/src/test/guard-undefined.spec.ts new file mode 100644 index 0000000..6e7f9da --- /dev/null +++ b/src/test/guard-undefined.spec.ts @@ -0,0 +1,50 @@ +// Function. +import { guardUndefined } from '../lib/guard-undefined.func'; +// Testing. +import { + // Main. + Testing, + + // Constant. + TESTING_UNDEFINED, +} from '@angular-package/testing'; +// Execute tests. +import { tests } from '../execute-tests'; +/** + * Initialize testing. + */ +const testing = new Testing( + tests.guard.undefined.describe, + tests.guard.undefined.it +); +/** + * Tests. + */ +testing.describe(guardUndefined.name, () => { + testing + // Defined. + .it('is DEFINED', () => expect(guardUndefined).toBeDefined()) + + // Checks ... + .describe(`guards`, () => { + testing + .it('with callback and payload', () => { + guardUndefined(TESTING_UNDEFINED, (result, value, payload) => { + expect(result).toBeTrue(); + expect(value).toEqual(TESTING_UNDEFINED); + if (payload) { + expect(payload.action).toEqual('action'); + expect(payload.name).toEqual('name'); + expect(payload.param).toEqual('param'); + } + return result; + }, { action: 'action', name: 'name', param: 'param' }); + }) + // ... primitives. + .describe(`primitive`, () => + testing.it(`${TESTING_UNDEFINED}`, () => + expect(guardUndefined(TESTING_UNDEFINED)).toBeTrue() + ) + ); + }); +}); diff --git a/src/test/guard.spec.ts b/src/test/guard.spec.ts new file mode 100644 index 0000000..4cc1bb2 --- /dev/null +++ b/src/test/guard.spec.ts @@ -0,0 +1,51 @@ +import { guard } from '../lib/guard.object'; +// Testing. +import { + // Main. + Testing, + } from '@angular-package/testing'; + // Execute tests. +import { tests } from '../execute-tests'; + /** + * Initialize testing. + */ +const testing = new Testing( + tests.object.guard.describe, + tests.object.guard.it + ); + /** + * Tests. + */ +testing.describe(`guard`, () => { + testing + .describe('DEFINED', () => { + testing + .it('guard', () => expect(guard).toBeDefined()) + .it('guard.array()', () => expect(guard.array).toBeDefined()) + .it('guard.bigint()', () => expect(guard.bigint).toBeDefined()) + .it('guard.boolean()', () => expect(guard.boolean).toBeDefined()) + .it('guard.class()', () => expect(guard.class).toBeDefined()) + .it('guard.defined()', () => expect(guard.defined).toBeDefined()) + .it('guard.false()', () => expect(guard.false).toBeDefined()) + .it('guard.function()', () => expect(guard.function).toBeDefined()) + .it('guard.instance()', () => expect(guard.instance).toBeDefined()) + .it('guard.key()', () => expect(guard.key).toBeDefined()) + .it('guard.null()', () => expect(guard.null).toBeDefined()) + .it('guard.number()', () => expect(guard.number).toBeDefined()) + .it('guard.numberBetween()', () => expect(guard.numberBetween).toBeDefined()) + .it('guard.object()', () => expect(guard.object).toBeDefined()) + .it('guard.objectKey()', () => expect(guard.objectKey).toBeDefined()) + .it('guard.objectKeyIn()', () => expect(guard.objectKeyIn).toBeDefined()) + .it('guard.objectKeys()', () => expect(guard.objectKeys).toBeDefined()) + .it('guard.objectKeysIn()', () => expect(guard.objectKeysIn).toBeDefined()) + .it('guard.objectSomeKeys()', () => expect(guard.objectSomeKeys).toBeDefined()) + .it('guard.primitive()', () => expect(guard.primitive).toBeDefined()) + .it('guard.string()', () => expect(guard.string).toBeDefined()) + .it('guard.stringIncludes()', () => expect(guard.stringIncludes).toBeDefined()) + .it('guard.stringIncludesSome()', () => expect(guard.stringIncludesSome).toBeDefined()) + .it('guard.symbol()', () => expect(guard.symbol).toBeDefined()) + .it('guard.true()', () => expect(guard.true).toBeDefined()) + .it('guard.type()', () => expect(guard.type).toBeDefined()) + .it('guard.undefined()', () => expect(guard.undefined).toBeDefined()); + }); +}); diff --git a/tsconfig.lib.json b/tsconfig.lib.json new file mode 100644 index 0000000..2359bf6 --- /dev/null +++ b/tsconfig.lib.json @@ -0,0 +1,15 @@ +/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */ +/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "../../out-tsc/lib", + "declaration": true, + "declarationMap": true, + "inlineSources": true, + "types": [] + }, + "exclude": [ + "**/*.spec.ts" + ] +} diff --git a/tsconfig.lib.prod.json b/tsconfig.lib.prod.json new file mode 100644 index 0000000..9215caa --- /dev/null +++ b/tsconfig.lib.prod.json @@ -0,0 +1,11 @@ +/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */ +/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */ +{ + "extends": "./tsconfig.lib.json", + "compilerOptions": { + "declarationMap": false + }, + "angularCompilerOptions": { + "compilationMode": "partial" + } +} diff --git a/tsconfig.spec.json b/tsconfig.spec.json new file mode 100644 index 0000000..254686d --- /dev/null +++ b/tsconfig.spec.json @@ -0,0 +1,15 @@ +/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */ +/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "../../out-tsc/spec", + "types": [ + "jasmine" + ] + }, + "include": [ + "**/*.spec.ts", + "**/*.d.ts" + ] +} From 9b34c9e2acb750817709117659e41b72b3e144a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 2 Dec 2024 13:19:26 +0000 Subject: [PATCH 02/13] docs(README.md): update. --- README.md | 96 +++++++++++++++++++++---------------------------------- 1 file changed, 36 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index 6d7dc81..ce5f41d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,3 @@ -# Guard - -Type guards for typescript. - Date: Mon, 2 Dec 2024 17:37:06 +0000 Subject: [PATCH 03/13] chore(interface/index): add. --- src/interface/index.ts | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/interface/index.ts diff --git a/src/interface/index.ts b/src/interface/index.ts new file mode 100644 index 0000000..2e63c95 --- /dev/null +++ b/src/interface/index.ts @@ -0,0 +1,2 @@ +export type { Guard } from './guard-interface'; +export type { GuardIs } from './guard-is.interface'; \ No newline at end of file From 4b15cc66c26292adeb43588dbffbfe3b49edd828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Mon, 2 Dec 2024 17:37:23 +0000 Subject: [PATCH 04/13] chore(lib/index): add `guard` object. --- src/lib/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/index.ts b/src/lib/index.ts index 84fb050..bca06b8 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,3 +1,6 @@ +// `guard` object. +export { guard } from './guard.object'; +// `guard` prefixed functions. export { guardArray } from './guard-array.func'; export { guardBigInt } from './guard-big-int.func'; export { guardBoolean } from './guard-boolean.func'; From 852cc68fcf499606cc7594611ee3cdd4fe58c380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Tue, 3 Dec 2024 06:03:55 +0000 Subject: [PATCH 05/13] docs(README.md): update. --- README.md | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ce5f41d..a2d52c6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## typescript-package/guard -Type-safe guards for validating value types in TypeScript. +Type-safe guards for guarding value types in TypeScript. [![npm version][typescript-package-npm-badge-svg]][typescript-package-npm-badge] @@ -21,6 +21,14 @@ Type-safe guards for validating value types in TypeScript. * [Installation](#installation) * [Api](#api) +* [Benefits](#benefits) +* [Type](#type) + * [Enforce](#enforce) + * [Indicate](#indicate) + * [Check](#check) + * [Guard](#guard) + * [Narrow](#narrow) + * [Summary](#summary) * [Git](#git) * [Commit](#commit) * [Versioning](#versioning) @@ -73,6 +81,161 @@ import { } from '@typescript-package/guard'; ``` +## Benefits + +Benefits of properly using the guards. + +1. Development, compile and runtime **validation**. + * Development-time **validation** ensures the developer can only provide a specific type through the static typing in the IDE. + * Compile-time **validation** performs static analysis during compilation, ensuring no type violations are present in the code. + * Runtime **validation** confirms the type of a value at runtime, especially useful when working with data from external sources (e.g., APIs, user inputs). + +2. Type-safe code. + * **Validates** external or dynamic data at runtime. + * **Ensures** developers write type-safe code. + +3. Error **reduction** and **prevention**. + * **Error reduction** by combining static analysis and runtime checks. + * **Error prevention** by combining runtime validation with compile-time type checks reduces the risk of runtime errors due to unexpected types. + +4. **Enhanced code clarity** by reducing ambiguity in data types. + +5. **Enhanced application reliability** by ensuring unvalidated data does not cause severe issues. + +6. **Type safety layer** by ensuring handle of unexpected or invalid data. + +## Type + +### Enforce + +A type enforce uses static typing to ensure only allowed types can be passed, and uses TypeScript's compile-time checks to enforce correctness. + +**Role**: A development and compile-time type restrictor and enforcer. +**Scope**: Development and compile-time. +**Purpose**: + +* Development and compile-time type **restriction** and **enforcement**. +* **Disallows** invalid types at development and compile-time, but doesn't inherently perform runtime checks, that is performed in the type checking. +* **Ensures** that only values of the specified type can be used before runtime. +* **Enforces** type restrictions during development (via IDE feedback) and compile-time. // (via static analysis). + +Example: + +```typescript +const acceptNumber = (value: number): void => { + console.log(value); +}; + +acceptNumber(42); // v Allowed +acceptNumber("42"); // x Development and compile-time error +``` + +### Indicate + +Type indicate as the name suggest indicates the value type resulting in `boolean` type. + +Example: + +```typescript +const isNumber = ( + value: unknown +): value is number // type indicate +=> { + return typeof value === "number"; // type check +} // type check +``` + +### Check + +Operate only at runtime, allowing flexible validation of dynamically-typed or unknown values. + +**Role**: Runtime checker. +**Scope**: Runtime only. +**Purpose**: + +* **Checks** whether a given value is of a specific type during execution. +* Used to create the type narrow. + +**Enforce**: o +**Restrict**: o +**Check**: v + +### Guard + +Combine development, and compile-time restriction with runtime validation, ensures the type of a value matches the expected type on any time, ensuring stricter enforcement of types. + +**Role**: A development, compile, and run-time type restrictor and enforcer. +**Scope**: Development, compile, and run-time. +**Purpose**: + +* **Ensures** the developer can only provide a specific type through the static typing. +* **Enforces** type restrictions during development (via IDE feedback) and compile-time. +* **Checks** whether a given value is of a specific type during execution. + +**Enforce**: v +**Restrict**: v +**Check**: v + +Example: + +```typescript +const guardNumber = ( + value: number // type enforce +): value is number // type indicator +=> { + return typeof value === "number"; // type check +} // overall type guard +``` + +### Narrow + +It's a type-guard that ensures the narrowed type of a value matches the expected type on runtime. + +**Role**: A development, compile, and run-time type narrower. +**Scope**: Development, compile-time, and runtime. +**Purpose**: + +* Compile-time **restriction** and **enforcement**. +* **Reduces** or **refines** the possible types of a value within a specific scope. +* **Enables** precise typing based on context, such as conditional checks or type guards, affecting behavior at development, compile, and runtime. + +**Enforce**: v +**Restrict**: v +**Narrow**: v + +// Providing additional checks beyond type narrowing (e.g., custom business rules or value constraints). + +Example: + +```typescript +const processValue = ( + value: string | number // type enforce +): void => { + if (typeof value === "string") { + console.log(value.toUpperCase()); // Type narrowed to 'string' + } else { + console.log(value.toFixed(2)); // Type narrowed to 'number' + } +}; // type narrow + +processValue(42); // v Logs: 42.00 +processValue("hello"); // v Logs: HELLO +``` + +> It looks at these special checks (called type guards) and assignments, and the process of refining types to more specific types than declared is called narrowing. ["Typescript"](https://www.typescriptlang.org/docs/handbook/2/narrowing.html) + +### Summary + +Time scope. + +Feature | Development-time | Compile-time | Runtime | +-------- | ---------------- | ------------ | ------- | +Enforce | v | v | x | +Indicate | v | x | x | +Check | x | x | v | +Guard | v | v | v | +Narrow | v | v | v | + ## GIT ### Commit From 3936cb0c24a555ca8536059e72b7dfe90b4784e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Tue, 3 Dec 2024 08:33:45 +0000 Subject: [PATCH 06/13] docs(README.md): update. --- README.md | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index a2d52c6..9575807 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Benefits of properly using the guards. 1. Development, compile and runtime **validation**. * Development-time **validation** ensures the developer can only provide a specific type through the static typing in the IDE. * Compile-time **validation** performs static analysis during compilation, ensuring no type violations are present in the code. - * Runtime **validation** confirms the type of a value at runtime, especially useful when working with data from external sources (e.g., APIs, user inputs). + * Runtime **validation** confirms the type of a value at runtime, useful when working with data from external sources (e.g., APIs, user inputs). 2. Type-safe code. * **Validates** external or dynamic data at runtime. @@ -110,14 +110,18 @@ Benefits of properly using the guards. A type enforce uses static typing to ensure only allowed types can be passed, and uses TypeScript's compile-time checks to enforce correctness. -**Role**: A development and compile-time type restrictor and enforcer. -**Scope**: Development and compile-time. +**Role**: A development and compile-time type restrictor and enforcer. +**Scope**: Development and compile-time. **Purpose**: * Development and compile-time type **restriction** and **enforcement**. -* **Disallows** invalid types at development and compile-time, but doesn't inherently perform runtime checks, that is performed in the type checking. +* **Enforces** type restrictions during development (via IDE feedback) and compile-time. * **Ensures** that only values of the specified type can be used before runtime. -* **Enforces** type restrictions during development (via IDE feedback) and compile-time. // (via static analysis). +* **Disallows** invalid types at development and compile-time, but doesn't perform runtime checks, that is performed in the type checking. + +Enforce | Restrict | Check | +------- | -------- | ----- | +v | v | x | Example: @@ -132,7 +136,7 @@ acceptNumber("42"); // x Development and compile-time error ### Indicate -Type indicate as the name suggest indicates the value type resulting in `boolean` type. +Indicates as an expression `value is type` the value is of the specified type resulting in a `boolean` type. Example: @@ -147,34 +151,34 @@ const isNumber = ( ### Check -Operate only at runtime, allowing flexible validation of dynamically-typed or unknown values. +Operate only at runtime, allowing validation of dynamically-typed or unknown values. -**Role**: Runtime checker. -**Scope**: Runtime only. +**Role**: Runtime checker. +**Scope**: Runtime only. **Purpose**: * **Checks** whether a given value is of a specific type during execution. * Used to create the type narrow. -**Enforce**: o -**Restrict**: o -**Check**: v +Enforce | Restrict | Check | +------- | -------- | ----- | +o | o | v | ### Guard Combine development, and compile-time restriction with runtime validation, ensures the type of a value matches the expected type on any time, ensuring stricter enforcement of types. -**Role**: A development, compile, and run-time type restrictor and enforcer. -**Scope**: Development, compile, and run-time. +**Role**: A development, compile, and run-time type restrictor and enforcer. +**Scope**: Development, compile, and run-time. **Purpose**: * **Ensures** the developer can only provide a specific type through the static typing. * **Enforces** type restrictions during development (via IDE feedback) and compile-time. * **Checks** whether a given value is of a specific type during execution. -**Enforce**: v -**Restrict**: v -**Check**: v +Enforce | Restrict | Check | +------- | -------- | ----- | +v | v | v | Example: @@ -191,19 +195,17 @@ const guardNumber = ( It's a type-guard that ensures the narrowed type of a value matches the expected type on runtime. -**Role**: A development, compile, and run-time type narrower. -**Scope**: Development, compile-time, and runtime. +**Role**: A development, compile, and run-time type narrower. +**Scope**: Development, compile-time, and runtime. **Purpose**: * Compile-time **restriction** and **enforcement**. * **Reduces** or **refines** the possible types of a value within a specific scope. * **Enables** precise typing based on context, such as conditional checks or type guards, affecting behavior at development, compile, and runtime. -**Enforce**: v -**Restrict**: v -**Narrow**: v - -// Providing additional checks beyond type narrowing (e.g., custom business rules or value constraints). +Enforce | Restrict | Narrow | +------- | -------- | ------ | +v | v | v | Example: From 52dcc70e7e26c30c31f557944be6a24ab27c7d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Tue, 3 Dec 2024 10:13:30 +0000 Subject: [PATCH 07/13] docs(README.md): update. --- README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9575807..09c2707 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## typescript-package/guard -Type-safe guards for guarding value types in TypeScript. +Type-safe guards for guarding the value types in TypeScript. [![npm version][typescript-package-npm-badge-svg]][typescript-package-npm-badge] @@ -85,7 +85,7 @@ import { Benefits of properly using the guards. -1. Development, compile and runtime **validation**. +1. Development, compile, and runtime **validation**. * Development-time **validation** ensures the developer can only provide a specific type through the static typing in the IDE. * Compile-time **validation** performs static analysis during compilation, ensuring no type violations are present in the code. * Runtime **validation** confirms the type of a value at runtime, useful when working with data from external sources (e.g., APIs, user inputs). @@ -96,19 +96,19 @@ Benefits of properly using the guards. 3. Error **reduction** and **prevention**. * **Error reduction** by combining static analysis and runtime checks. - * **Error prevention** by combining runtime validation with compile-time type checks reduces the risk of runtime errors due to unexpected types. + * **Error prevention** by combining runtime validation with compile-time type checks reducing the risk of runtime errors due to unexpected types. 4. **Enhanced code clarity** by reducing ambiguity in data types. 5. **Enhanced application reliability** by ensuring unvalidated data does not cause severe issues. -6. **Type safety layer** by ensuring handle of unexpected or invalid data. +6. **Type safety layer** by ensuring the handling of unexpected or invalid data. ## Type ### Enforce -A type enforce uses static typing to ensure only allowed types can be passed, and uses TypeScript's compile-time checks to enforce correctness. +A type enforce uses static typing to ensure only allowed types can be passed, and uses TypeScript's compile-time static analysis to enforce correctness. **Role**: A development and compile-time type restrictor and enforcer. **Scope**: Development and compile-time. @@ -126,7 +126,9 @@ v | v | x | Example: ```typescript -const acceptNumber = (value: number): void => { +const acceptNumber = ( + value: number // type enforce/restrict +): void => { console.log(value); }; @@ -162,11 +164,11 @@ Operate only at runtime, allowing validation of dynamically-typed or unknown val Enforce | Restrict | Check | ------- | -------- | ----- | -o | o | v | +x | x | v | ### Guard -Combine development, and compile-time restriction with runtime validation, ensures the type of a value matches the expected type on any time, ensuring stricter enforcement of types. +Combine development, and compile-time restriction with runtime validation, ensures the value type matches the expected type on any time, ensuring stricter enforcement of types. **Role**: A development, compile, and run-time type restrictor and enforcer. **Scope**: Development, compile, and run-time. From c364f8f92c8cd719077a0757041f5db5f7d4f976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Tue, 3 Dec 2024 16:47:54 +0000 Subject: [PATCH 08/13] docs: update guard functions . --- src/lib/guard-defined.func.ts | 12 +++++++----- src/lib/guard-false.func.ts | 11 ++++++----- src/lib/guard-function.func.ts | 12 +++++++----- src/lib/guard-instance.func.ts | 14 ++++++++------ src/lib/guard-key.func.ts | 12 +++++++----- src/lib/guard-null.func.ts | 11 ++++++----- src/lib/guard-number-between.func.ts | 20 ++++++++++++-------- src/lib/guard-number.func.ts | 12 +++++++----- src/lib/guard-object-key-in.func.ts | 15 +++++++++------ src/lib/guard-object-key.func.ts | 15 +++++++++------ src/lib/guard-object-keys-in.func.ts | 15 +++++++++------ src/lib/guard-object-keys.func.ts | 15 +++++++++------ src/lib/guard-object-some-keys.func.ts | 14 ++++++++------ src/lib/guard-object.func.ts | 12 +++++++----- src/lib/guard-primitive.func.ts | 14 ++++++++------ src/lib/guard-regexp.func.ts | 12 +++++++----- src/lib/guard-string-includes-some.func.ts | 17 +++++++++++------ src/lib/guard-string-includes.func.ts | 17 +++++++++++------ src/lib/guard-string-length-between.func.ts | 18 +++++++++++------- src/lib/guard-string-length.func.ts | 15 +++++++++------ src/lib/guard-string.func.ts | 12 +++++++----- src/lib/guard-symbol.func.ts | 11 ++++++----- src/lib/guard-true.func.ts | 11 ++++++----- src/lib/guard-type.func.ts | 14 ++++++++------ src/lib/guard-undefined.func.ts | 11 ++++++----- src/lib/guard.object.ts | 5 ++++- 26 files changed, 205 insertions(+), 142 deletions(-) diff --git a/src/lib/guard-defined.func.ts b/src/lib/guard-defined.func.ts index 28b58e3..47fa975 100644 --- a/src/lib/guard-defined.func.ts +++ b/src/lib/guard-defined.func.ts @@ -3,11 +3,13 @@ import { isDefined } from '@typescript-package/is'; // Type. import { Defined, ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be defined, not `undefined`. - * @param value The value of generic type `Defined`, never undefined type captured from itself to guard against `undefined`. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is defined. + * @description Guards the value to be defined, not `undefined`. + * @template Type + * @template {object} [Payload=object] + * @param {Defined} value The value of generic type `Defined`, never undefined type captured from itself to guard against `undefined`. + * @param {?ResultCallback, Payload>} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Defined} The return value is a `boolean` indicating whether the `value` is defined. */ export const guardDefined = ( value: Defined, diff --git a/src/lib/guard-false.func.ts b/src/lib/guard-false.func.ts index 6e11148..a3049ba 100644 --- a/src/lib/guard-false.func.ts +++ b/src/lib/guard-false.func.ts @@ -3,11 +3,12 @@ import { isFalse } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the provided value to be `false`. - * @param value The value of `false` type to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean` equal to `false`. + * @description Guards the provided value to be `false`. + * @template {object} Payload + * @param {false} value The value of `false` type to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is false} The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean` equal to `false`. */ export const guardFalse = ( value: false, diff --git a/src/lib/guard-function.func.ts b/src/lib/guard-function.func.ts index 90ea900..b3345f8 100644 --- a/src/lib/guard-function.func.ts +++ b/src/lib/guard-function.func.ts @@ -3,11 +3,13 @@ import { isFunction } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be a `Function`. - * @param value The `function` of a generic type variable `Type` to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `Function`. + * @description Guards the value to be a `Function`. + * @template {Function} Type + * @template {object} Payload + * @param {Type} value The `function` of a generic type variable `Type` to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Type} The return value is a `boolean` indicating whether the `value` is a `Function`. */ export const guardFunction = ( value: Type, diff --git a/src/lib/guard-instance.func.ts b/src/lib/guard-instance.func.ts index 86331c8..a1a0547 100644 --- a/src/lib/guard-instance.func.ts +++ b/src/lib/guard-instance.func.ts @@ -3,12 +3,14 @@ import { isInstance } from '@typescript-package/is'; // Type. import { Constructor, ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be an instance of the given `constructor`. - * @param value An `object` of a generic type variable `Obj` to guard and be compared with an instance of a given `constructor`. - * @param constructor A `class` or `function` that specifies the type of the `constructor`. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is an instance of a given `constructor`. + * @description Guards the value to be an instance of the given `constructor`. + * @template {object} Obj + * @template {object} [Payload=object] + * @param {Obj} value An `object` of a generic type variable `Obj` to guard and be compared with an instance of a given `constructor`. + * @param {Constructor} constructor A `class` or `function` that specifies the type of the `constructor`. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Obj} The return value is a `boolean` indicating whether the `value` is an instance of a given `constructor`. */ export const guardInstance = < Obj extends object, diff --git a/src/lib/guard-key.func.ts b/src/lib/guard-key.func.ts index 9ff8ca5..030ee8e 100644 --- a/src/lib/guard-key.func.ts +++ b/src/lib/guard-key.func.ts @@ -3,11 +3,13 @@ import { isKey } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be one of `string`, `number`, or `symbol` type. - * @param value The value of generic type variable `Key` to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `string`, `number`, or `symbol`. + * @description Guards the value to be one of `string`, `number`, or `symbol` type. + * @template {PropertyKey} Key + * @template {object} Payload + * @param {Key} value The value of generic type variable `Key` to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Key} The return value is a `boolean` indicating whether the `value` is a `string`, `number`, or `symbol`. */ export const guardKey = ( value: Key, diff --git a/src/lib/guard-null.func.ts b/src/lib/guard-null.func.ts index 3281271..7ca7838 100644 --- a/src/lib/guard-null.func.ts +++ b/src/lib/guard-null.func.ts @@ -3,11 +3,12 @@ import { isNull } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be `null`. - * @param value The value of `null` type to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is `null`. + * @description Guards the value to be `null`. + * @template {object} Payload + * @param {null} value The value of `null` type to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is null} The returned value is a `boolean` indicating whether the `value` is `null`. */ export const guardNull = ( value: null, diff --git a/src/lib/guard-number-between.func.ts b/src/lib/guard-number-between.func.ts index b91cd78..9e7c8ae 100644 --- a/src/lib/guard-number-between.func.ts +++ b/src/lib/guard-number-between.func.ts @@ -1,15 +1,19 @@ // Function. import { isNumberBetween } from '@typescript-package/is'; // Type. -import { AnyNumber, ResultCallback, NumberBetween } from '@typescript-package/type'; +import { AnyNumber, NumberBetween, ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be `number` type or instance of `Number` between the specified range. - * @param value The value of a generic type variable `Type` to guard. - * @param min The **minimum** range of generic type variable `Min` for a given `value`. - * @param max The **maximum** range of generic type variable `Max` for a given `value`. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `number` type or an instance of `Number` between the + * @description Guards the value to be `number` type or instance of `Number` between the specified range. + * @template {AnyNumber} Type + * @template {number} Min + * @template {number} Max + * @template {object} [Payload=object] + * @param {Type} value The value of a generic type variable `Type` to guard. + * @param {Min} min The **minimum** range of generic type variable `Min` for a given `value`. + * @param {Max} max The **maximum** range of generic type variable `Max` for a given `value`. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is NumberBetween} The return value is a `boolean` indicating whether the `value` is a `number` type or an instance of `Number` between the * specified range. */ export const guardNumberBetween = < diff --git a/src/lib/guard-number.func.ts b/src/lib/guard-number.func.ts index 72f3846..d646fc0 100644 --- a/src/lib/guard-number.func.ts +++ b/src/lib/guard-number.func.ts @@ -3,11 +3,13 @@ import { isNumber } from '@typescript-package/is'; // Type. import { AnyNumber, ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be a `number` of any type. - * @param value The value of generic type variable `Type` to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `number` type or an instance of `Number`. + * @description Guards the value to be a `number` of any type. + * @template {AnyNumber} Type + * @template {object} [Payload=object] + * @param {Type} value The value of generic type variable `Type` to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Type} The return value is a `boolean` indicating whether the `value` is a `number` type or an instance of `Number`. */ export const guardNumber = < Type extends AnyNumber, diff --git a/src/lib/guard-object-key-in.func.ts b/src/lib/guard-object-key-in.func.ts index c0a454e..07250c1 100644 --- a/src/lib/guard-object-key-in.func.ts +++ b/src/lib/guard-object-key-in.func.ts @@ -3,13 +3,16 @@ import { isObjectKeyIn } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the `value` to be an `object` of a generic type variable `Obj` that contains(or its prototype chain) the given `key`. - * @param value An `object` of a generic type variable `Obj`, by default of the type captured from itself that contains(or its prototype + * @description Guards the `value` to be an `object` of a generic type variable `Obj` that contains(or its prototype chain) the given `key`. + * @template {object} Obj + * @template {keyof Obj} Key + * @template {object} [Payload=object] + * @param {Obj} value An `object` of a generic type variable `Obj`, by default of the type captured from itself that contains(or its prototype * chain) the given `key`. - * @param key A key of generic type variable `Key` as the property name that the given `value` contains(or its prototype chain). - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is an `object` that contains(or its prototype chain) the given + * @param {Key} key A key of generic type variable `Key` as the property name that the given `value` contains(or its prototype chain). + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Obj} The return value is a `boolean` indicating whether the `value` is an `object` that contains(or its prototype chain) the given * `key`. */ export const guardObjectKeyIn = < diff --git a/src/lib/guard-object-key.func.ts b/src/lib/guard-object-key.func.ts index a05addf..8e9acaa 100644 --- a/src/lib/guard-object-key.func.ts +++ b/src/lib/guard-object-key.func.ts @@ -3,13 +3,16 @@ import { isObjectKey } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be an `object` of generic type variable `Obj` that contains the given `key`. - * @param value An `object` of a generic type variable `Obj`, by default of the type captured from itself that contains the given `key` to + * @description Guards the value to be an `object` of generic type variable `Obj` that contains the given `key`. + * @template {object} Obj + * @template {keyof Obj} Key + * @template {object} [Payload=object] + * @param {Obj} value An `object` of a generic type variable `Obj`, by default of the type captured from itself that contains the given `key` to * guard. - * @param key A key of generic type variable `Key` as the property name that the given `value` contains. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is an `object` of generic type variable `Obj` that contains the + * @param {Key} key A key of generic type variable `Key` as the property name that the given `value` contains. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Obj} The return value is a `boolean` indicating whether the `value` is an `object` of generic type variable `Obj` that contains the * given `key`. */ export const guardObjectKey = < diff --git a/src/lib/guard-object-keys-in.func.ts b/src/lib/guard-object-keys-in.func.ts index 1d96cdf..b18a916 100644 --- a/src/lib/guard-object-keys-in.func.ts +++ b/src/lib/guard-object-keys-in.func.ts @@ -3,13 +3,16 @@ import { isObjectKeysIn } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be an `object` of a generic type variable `Obj` with specified keys in it(or its prototype chain). - * @param value An object of a generic type variable `Obj`, by default of the type captured from itself that contains(or its prototype + * @description Guards the value to be an `object` of a generic type variable `Obj` with specified keys in it(or its prototype chain). + * @template {object} Obj + * @template {keyof Obj} Key + * @template {object} [Payload=object] + * @param {Obj} value An object of a generic type variable `Obj`, by default of the type captured from itself that contains(or its prototype * chain) the given `keys` to guard. - * @param keys An `Array` of property keys to check whether the given `value` contains(or its prototype chain). - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is an `object` with specified `keys` in it(or its prototype + * @param {Key[]} keys An `Array` of property keys to check whether the given `value` contains(or its prototype chain). + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Obj} The return value is a `boolean` indicating whether the `value` is an `object` with specified `keys` in it(or its prototype * chain). */ export const guardObjectKeysIn = < diff --git a/src/lib/guard-object-keys.func.ts b/src/lib/guard-object-keys.func.ts index 0c3d2e4..ea214a0 100644 --- a/src/lib/guard-object-keys.func.ts +++ b/src/lib/guard-object-keys.func.ts @@ -3,13 +3,16 @@ import { isObjectKeys } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be an `object` of a generic type variable `Obj` with its specified `keys`. - * @param value An object of a generic type variable `Obj`, by default of the type captured from itself that contains the given `keys` to + * @description Guards the value to be an `object` of a generic type variable `Obj` with its specified `keys`. + * @template {object} Obj + * @template {keyof Obj} Key + * @template {object} [Payload=object] + * @param {Obj} value An object of a generic type variable `Obj`, by default of the type captured from itself that contains the given `keys` to * guard. - * @param keys An `Array` of property keys to check whether the given `value` contains. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is an `object` with its specified `keys`. + * @param {Key[]} keys An `Array` of property keys to check whether the given `value` contains. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Obj} The return value is a `boolean` indicating whether the `value` is an `object` with its specified `keys`. */ export const guardObjectKeys = < Obj extends object, diff --git a/src/lib/guard-object-some-keys.func.ts b/src/lib/guard-object-some-keys.func.ts index 7b6ef69..3ccfa66 100644 --- a/src/lib/guard-object-some-keys.func.ts +++ b/src/lib/guard-object-some-keys.func.ts @@ -3,14 +3,16 @@ import { isObjectSomeKeys } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be an `object` of a generic type variable `Obj` with some of its `keys` or some groups of its `keys`. - * @param value An object of a generic type variable `Obj`, by default of the type captured from itself that contains some or some of the + * @description Guards the value to be an `object` of a generic type variable `Obj` with some of its `keys` or some groups of its `keys`. + * @template {object} Obj + * @template {object} [Payload=object] + * @param {Obj} value An object of a generic type variable `Obj`, by default of the type captured from itself that contains some or some of the * groups of the given `keys`, to guard. - * @param keys An `Array` of property names or a two-dimensional array of property names to check if the given `value` contains some of + * @param {(keyof Obj | Array)[]} keys An `Array` of property names or a two-dimensional array of property names to check if the given `value` contains some of * them. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the provided `value` is an `object` with some or some groups of the given + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Obj} The return value is a `boolean` indicating whether the provided `value` is an `object` with some or some groups of the given * `keys`. */ export const guardObjectSomeKeys = < diff --git a/src/lib/guard-object.func.ts b/src/lib/guard-object.func.ts index 5d818f3..5ab294b 100644 --- a/src/lib/guard-object.func.ts +++ b/src/lib/guard-object.func.ts @@ -3,11 +3,13 @@ import { isObject } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be an `object` of a generic type variable `Obj`. - * @param value An `object` of a generic type variable `Obj`, by default of the type captured from itself to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the provided `value` is an `object` of a generic type variable `Obj`. + * @description Guards the value to be an `object` of a generic type variable `Obj`. + * @template {object} Obj + * @template {object} [Payload=object] + * @param {Obj} value An `object` of a generic type variable `Obj`, by default of the type captured from itself to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Obj} The return value is a `boolean` indicating whether the provided `value` is an `object` of a generic type variable `Obj`. */ export const guardObject = < Obj extends object, diff --git a/src/lib/guard-primitive.func.ts b/src/lib/guard-primitive.func.ts index ffebc0c..7da155b 100644 --- a/src/lib/guard-primitive.func.ts +++ b/src/lib/guard-primitive.func.ts @@ -3,13 +3,15 @@ import { isPrimitive } from '@typescript-package/is'; // Type. import { ResultCallback, Primitive, PrimitiveNames } from '@typescript-package/type'; /** - * Guards the value to be the `Primitive` type or the given `type` of the `Primitives`. - * @param value The value of a generic type variable `Type` constrained by the `Primitive`, by default of the type captured from itself to + * @description Guards the value to be the `Primitive` type or the given `type` of the `Primitives`. + * @template {Primitive} Type + * @template {object} [Payload=object] + * @param {Type} value The value of a generic type variable `Type` constrained by the `Primitive`, by default of the type captured from itself to * guard. - * @param type An optional specific type of `Primitives` to check the given value. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is the `Primitive` type or the given `type`. + * @param {?PrimitiveNames} [type] An optional specific type of `Primitives` to check the given value. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Type} The return value is a `boolean` indicating whether the `value` is the `Primitive` type or the given `type`. */ export const guardPrimitive = < Type extends Primitive, diff --git a/src/lib/guard-regexp.func.ts b/src/lib/guard-regexp.func.ts index dbc72c5..bf14962 100644 --- a/src/lib/guard-regexp.func.ts +++ b/src/lib/guard-regexp.func.ts @@ -3,11 +3,13 @@ import { isRegExp } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be a `RegExp`. - * @param value Regular expression of generic type variable `Type` constrained by `RegExp` to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return `value` is a `boolean` indicating whether the `value` is a `RegExp`. + * @description Guards the value to be a `RegExp`. + * @template {RegExp} Type + * @template {object} Payload + * @param {Type} value Regular expression of generic type variable `Type` constrained by `RegExp` to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Type} The return `value` is a `boolean` indicating whether the `value` is a `RegExp`. */ export const guardRegExp = ( value: Type, diff --git a/src/lib/guard-string-includes-some.func.ts b/src/lib/guard-string-includes-some.func.ts index 64726fe..882e516 100644 --- a/src/lib/guard-string-includes-some.func.ts +++ b/src/lib/guard-string-includes-some.func.ts @@ -4,13 +4,18 @@ import { resultCallback } from '@typescript-package/core'; // Type. import { AnyString, ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be a `string` type or an instance of `String` that includes some of the specified words/sentences. - * @param value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to + * @description Guards the value to be a `string` type or an instance of `String` that includes some of the specified words/sentences. + * @template {AnyString} Type + * @template {object} [Payload=object] + * @param {Type} value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to * check against the `string` that contains some of the words/sentences from a given `includes`. - * @param includes An `Array` of `string` as words/sentences to be case-sensitive searched for within the given `value`. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the provided `value` is a `string` type or an instance of `String` that + * @param {string[]} includes An `Array` of `string` as words/sentences to be case-sensitive searched for within the given `value`. + * @param {ResultCallback< + * Type, + * { includes: typeof includes } & Payload + * >} [callback=resultCallback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Type} The return value is a `boolean` indicating whether the provided `value` is a `string` type or an instance of `String` that * includes all of the specified words/sentences. */ export const guardStringIncludesSome = < diff --git a/src/lib/guard-string-includes.func.ts b/src/lib/guard-string-includes.func.ts index 60ab065..1b20dcd 100644 --- a/src/lib/guard-string-includes.func.ts +++ b/src/lib/guard-string-includes.func.ts @@ -4,13 +4,18 @@ import { resultCallback } from '@typescript-package/core'; // Type. import { AnyString, ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be a `string` type or an instance of `String` that includes the specified words/sentences. - * @param value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to + * @description Guards the value to be a `string` type or an instance of `String` that includes the specified words/sentences. + * @template {AnyString} Type + * @template {object} [Payload=object] + * @param {Type} value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to * check against the `string` that contains words/sentences from a given `includes`. - * @param includes An `Array` of `string` as words/sentences to be case-sensitive searched for within the given `value`. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the provided `value` is a `string` type or an instance of `String` that + * @param {string[]} includes An `Array` of `string` as words/sentences to be case-sensitive searched for within the given `value`. + * @param {ResultCallback< + * Type, + * { includes: typeof includes } & Payload + * >} [callback=resultCallback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Type} The return value is a `boolean` indicating whether the provided `value` is a `string` type or an instance of `String` that * includes the specified words/sentences. */ export const guardStringIncludes = < diff --git a/src/lib/guard-string-length-between.func.ts b/src/lib/guard-string-length-between.func.ts index ce9f892..3e2fa9c 100644 --- a/src/lib/guard-string-length-between.func.ts +++ b/src/lib/guard-string-length-between.func.ts @@ -3,14 +3,18 @@ import { isStringLengthBetween } from '@typescript-package/is'; // Type. import { AnyString, ResultCallback, StringOfLength } from '@typescript-package/type'; /** - * Guards the value to be `string` or `String` instance of a length between the specified range. - * @param value The value of a generic type variable `Type` constrained by `AnyString`, by default of the type captured from itself to + * @description Guards the value to be `string` or `String` instance of a length between the specified range. + * @template {AnyString} Type + * @template {number} Min + * @template {number} Max + * @template {object} [Payload=object] + * @param {Type} value The value of a generic type variable `Type` constrained by `AnyString`, by default of the type captured from itself to * guard. - * @param min The **minimum** length of generic type variable `Min` of a given `value`. - * @param max The **maximum** length of generic type variable `Max` of a given `value`. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String` of a length between + * @param {Min} min The **minimum** length of generic type variable `Min` of a given `value`. + * @param {Max} max The **maximum** length of generic type variable `Max` of a given `value`. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is StringOfLength} The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String` of a length between * the specified range. */ export const guardStringLengthBetween = < diff --git a/src/lib/guard-string-length.func.ts b/src/lib/guard-string-length.func.ts index ed8e298..7498e22 100644 --- a/src/lib/guard-string-length.func.ts +++ b/src/lib/guard-string-length.func.ts @@ -3,13 +3,16 @@ import { isStringLength } from '@typescript-package/is'; // Type. import { AnyString, ResultCallback, StringOfLength } from '@typescript-package/type'; /** - * Guards the value to be `string` type or `String` instance of a specified length. - * @param value The value of a generic type variable `Type` constrained by `AnyString`, by default of the type captured from itself to + * @description Guards the value to be `string` type or `String` instance of a specified length. + * @template {AnyString} Type + * @template {number} Length + * @template {object} [Payload=object] + * @param {Type} value The value of a generic type variable `Type` constrained by `AnyString`, by default of the type captured from itself to * guard. - * @param length The **length** of generic type variable `Length` of a given `value`. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String` of a specified + * @param {Length} length The **length** of generic type variable `Length` of a given `value`. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is StringOfLength} The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String` of a specified * `length`. */ export const guardStringLength = < diff --git a/src/lib/guard-string.func.ts b/src/lib/guard-string.func.ts index a05f390..4b19c7a 100644 --- a/src/lib/guard-string.func.ts +++ b/src/lib/guard-string.func.ts @@ -3,12 +3,14 @@ import { isString } from '@typescript-package/is'; // Type. import { AnyString, ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be `string` of any type. - * @param value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to + * @description Guards the value to be `string` of any type. + * @template {AnyString} Type + * @template {object} [Payload=object] + * @param {Type} value The value of a generic type variable `Type` constrained by the `AnyString`, by default of the type captured from itself to * guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String`. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is Type} The return value is a `boolean` indicating whether the `value` is a `string` type or an instance of `String`. */ export const guardString = < Type extends AnyString, diff --git a/src/lib/guard-symbol.func.ts b/src/lib/guard-symbol.func.ts index 73c216d..7d8d0ed 100644 --- a/src/lib/guard-symbol.func.ts +++ b/src/lib/guard-symbol.func.ts @@ -3,11 +3,12 @@ import { isSymbol } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be a `symbol`. - * @param value A `symbol` type value to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `symbol`. + * @description Guards the value to be a `symbol`. + * @template {object} Payload + * @param {symbol} value A `symbol` type value to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is symbol} The return value is a `boolean` indicating whether the `value` is a `symbol`. */ export const guardSymbol = ( value: symbol, diff --git a/src/lib/guard-true.func.ts b/src/lib/guard-true.func.ts index 9330816..dce9250 100644 --- a/src/lib/guard-true.func.ts +++ b/src/lib/guard-true.func.ts @@ -3,11 +3,12 @@ import { isTrue } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be `true`. - * @param value The value of `true` type to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean` equal to `true`. + * @description Guards the value to be `true`. + * @template {object} Payload + * @param {true} value The value of `true` type to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is true} The return value is a `boolean` indicating whether the `value` is a `boolean` type or an instance of `Boolean` equal to `true`. */ export const guardTrue = ( value: true, diff --git a/src/lib/guard-type.func.ts b/src/lib/guard-type.func.ts index 6f872ba..39675ab 100644 --- a/src/lib/guard-type.func.ts +++ b/src/lib/guard-type.func.ts @@ -3,12 +3,14 @@ import { isType } from '@typescript-package/is'; // Type. import { ResultCallback, Type, TypeNames } from '@typescript-package/type'; /** - * Guards the value to be a type of given `type`. - * @param value The value of a generic type variable `T` constrained by the `Type`, by default of the type captured from itself, to guard. - * @param type The value of `string` or `Constructor` type of the `Types` indicates against which type a given `value` is checked. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is a type from a given `type`. + * @description Guards the value to be a type of given `type`. + * @template {Type} T + * @template {object} [Payload=object] + * @param {T} value The value of a generic type variable `T` constrained by the `Type`, by default of the type captured from itself, to guard. + * @param {TypeNames} type The value of `string` or `Constructor` type of the `Types` indicates against which type a given `value` is checked. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is T} The return value is a `boolean` indicating whether the `value` is a type from a given `type`. */ export const guardType = ( value: T, diff --git a/src/lib/guard-undefined.func.ts b/src/lib/guard-undefined.func.ts index ed6c7ac..889758e 100644 --- a/src/lib/guard-undefined.func.ts +++ b/src/lib/guard-undefined.func.ts @@ -3,11 +3,12 @@ import { isUndefined } from '@typescript-package/is'; // Type. import { ResultCallback } from '@typescript-package/type'; /** - * Guards the value to be `undefined`. - * @param value The value of an `undefined` type to guard. - * @param callback An optional `ResultCallback` function to handle the result before returns. - * @param payload Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. - * @returns The return value is a `boolean` indicating whether the `value` is `undefined`. + * @description Guards the value to be `undefined`. + * @template {object} Payload + * @param {undefined} value The value of an `undefined` type to guard. + * @param {?ResultCallback} [callback] An optional `ResultCallback` function to handle the result before returns. + * @param {?Payload} [payload] Optional `object` of generic type variable `Payload` is assigned to the `payload` of the provided `callback` function. + * @returns {value is undefined} The return value is a `boolean` indicating whether the `value` is `undefined`. */ export const guardUndefined = ( value: undefined, diff --git a/src/lib/guard.object.ts b/src/lib/guard.object.ts index 54ee33c..4dc1bc8 100644 --- a/src/lib/guard.object.ts +++ b/src/lib/guard.object.ts @@ -2,5 +2,8 @@ import { guardIs } from './guard-is.object'; // Import: Interface. import { Guard } from '../interface/guard-interface'; -// Export: `guard` +/** + * @description + * @type {Guard} + */ export const guard: Guard = Object.freeze({ ...guardIs }); From d44e4384fe7e5c9778ef41feda2f4d07c28f68e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Tue, 3 Dec 2024 16:48:11 +0000 Subject: [PATCH 09/13] chore(api): use one export. --- src/public-api.ts | 69 ++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/src/public-api.ts b/src/public-api.ts index f76bf35..240e614 100644 --- a/src/public-api.ts +++ b/src/public-api.ts @@ -1,36 +1,39 @@ /* * Public API Surface of guard */ -// `guard` object. -export { guard } from './lib/guard.object'; -// `guard` prefix functions, -export { guardArray } from './lib/guard-array.func'; -export { guardBigInt } from './lib/guard-big-int.func'; -export { guardBoolean } from './lib/guard-boolean.func'; -export { guardClass } from './lib/guard-class.func'; -export { guardDate } from './lib/guard-date.func'; -export { guardDefined } from './lib/guard-defined.func'; -export { guardFalse } from './lib/guard-false.func'; -export { guardFunction } from './lib/guard-function.func'; -export { guardInstance } from './lib/guard-instance.func'; -export { guardKey } from './lib/guard-key.func'; -export { guardNull } from './lib/guard-null.func'; -export { guardNumber } from './lib/guard-number.func'; -export { guardNumberBetween } from './lib/guard-number-between.func'; -export { guardObject } from './lib/guard-object.func'; -export { guardObjectKey } from './lib/guard-object-key.func'; -export { guardObjectKeyIn } from './lib/guard-object-key-in.func'; -export { guardObjectKeysIn } from './lib/guard-object-keys-in.func'; -export { guardObjectKeys } from './lib/guard-object-keys.func'; -export { guardObjectSomeKeys } from './lib/guard-object-some-keys.func'; -export { guardPrimitive } from './lib/guard-primitive.func'; -export { guardRegExp } from './lib/guard-regexp.func'; -export { guardString } from './lib/guard-string.func'; -export { guardStringIncludes } from './lib/guard-string-includes.func'; -export { guardStringIncludesSome } from './lib/guard-string-includes-some.func'; -export { guardStringLength } from './lib/guard-string-length.func'; -export { guardStringLengthBetween } from './lib/guard-string-length-between.func'; -export { guardSymbol } from './lib/guard-symbol.func'; -export { guardTrue } from './lib/guard-true.func'; -export { guardType } from './lib/guard-type.func'; -export { guardUndefined } from './lib/guard-undefined.func'; +export { + // `guard` object. + guard, + + // Prefixed `guard` functions. + guardArray, + guardBigInt, + guardBoolean, + guardClass, + guardDate, + guardDefined, + guardFalse, + guardFunction, + guardInstance, + guardKey, + guardNull, + guardNumber, + guardNumberBetween, + guardObject, + guardObjectKey, + guardObjectKeyIn, + guardObjectKeysIn, + guardObjectKeys, + guardObjectSomeKeys, + guardPrimitive, + guardRegExp, + guardString, + guardStringIncludes, + guardStringIncludesSome, + guardStringLength, + guardStringLengthBetween, + guardSymbol, + guardTrue, + guardType, + guardUndefined +} from './lib'; From fc887c738f7c486713128ea30b25719f9bbf31b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Tue, 3 Dec 2024 16:49:30 +0000 Subject: [PATCH 10/13] chore(guardIs); use `interface` location. --- src/lib/guard-is.object.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/guard-is.object.ts b/src/lib/guard-is.object.ts index 07ed331..860c44c 100644 --- a/src/lib/guard-is.object.ts +++ b/src/lib/guard-is.object.ts @@ -32,7 +32,7 @@ import { guardUndefined } from './index'; // Interface. -import { GuardIs } from '../interface/guard-is.interface'; +import { GuardIs } from '../interface'; // `guardIs`. export const guardIs: GuardIs = Object.freeze({ array: guardArray, From d8d7222270841b21501b983cbf3f873968ccc4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Wed, 4 Dec 2024 07:29:11 +0000 Subject: [PATCH 11/13] refactor(interface): remove `GuardIs`. --- src/interface/guard-interface.ts | 6 ------ src/interface/{guard-is.interface.ts => guard.interface.ts} | 2 +- src/interface/index.ts | 3 +-- 3 files changed, 2 insertions(+), 9 deletions(-) delete mode 100644 src/interface/guard-interface.ts rename src/interface/{guard-is.interface.ts => guard.interface.ts} (99%) diff --git a/src/interface/guard-interface.ts b/src/interface/guard-interface.ts deleted file mode 100644 index ba811e3..0000000 --- a/src/interface/guard-interface.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Interface. -import { GuardIs } from './guard-is.interface'; -/** - * The shape of an object with guard functions. - */ -export interface Guard extends GuardIs { } diff --git a/src/interface/guard-is.interface.ts b/src/interface/guard.interface.ts similarity index 99% rename from src/interface/guard-is.interface.ts rename to src/interface/guard.interface.ts index 8e7db07..42abff9 100644 --- a/src/interface/guard-is.interface.ts +++ b/src/interface/guard.interface.ts @@ -32,7 +32,7 @@ import { guardUndefined } from '../lib'; // Export: Interface. -export interface GuardIs { +export interface Guard { /** * Guards the value to be an `array` of a generic type variable `Type`. */ diff --git a/src/interface/index.ts b/src/interface/index.ts index 2e63c95..2265c78 100644 --- a/src/interface/index.ts +++ b/src/interface/index.ts @@ -1,2 +1 @@ -export type { Guard } from './guard-interface'; -export type { GuardIs } from './guard-is.interface'; \ No newline at end of file +export type { Guard } from './guard.interface'; \ No newline at end of file From e6b62e284f0f57334e504cf217ff7b7678607bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Wed, 4 Dec 2024 07:29:39 +0000 Subject: [PATCH 12/13] refactor(guardIs): preserve the `guard` object only. --- src/lib/guard-is.object.ts | 68 --------------------------------- src/lib/guard.object.ts | 77 +++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 77 deletions(-) delete mode 100644 src/lib/guard-is.object.ts diff --git a/src/lib/guard-is.object.ts b/src/lib/guard-is.object.ts deleted file mode 100644 index 860c44c..0000000 --- a/src/lib/guard-is.object.ts +++ /dev/null @@ -1,68 +0,0 @@ -// Function. -import { - guardArray, - guardBigInt, - guardBoolean, - guardClass, - guardDate, - guardDefined, - guardFalse, - guardFunction, - guardInstance, - guardKey, - guardNull, - guardNumber, - guardNumberBetween, - guardObject, - guardObjectKey, - guardObjectKeyIn, - guardObjectKeys, - guardObjectKeysIn, - guardObjectSomeKeys, - guardPrimitive, - guardRegExp, - guardString, - guardStringIncludes, - guardStringIncludesSome, - guardStringLength, - guardStringLengthBetween, - guardSymbol, - guardTrue, - guardType, - guardUndefined -} from './index'; -// Interface. -import { GuardIs } from '../interface'; -// `guardIs`. -export const guardIs: GuardIs = Object.freeze({ - array: guardArray, - bigint: guardBigInt, - boolean: guardBoolean, - class: guardClass, - date: guardDate, - defined: guardDefined, - false: guardFalse, - function: guardFunction, - instance: guardInstance, - key: guardKey, - null: guardNull, - number: guardNumber, - numberBetween: guardNumberBetween, - object: guardObject, - objectKey: guardObjectKey, - objectKeyIn: guardObjectKeyIn, - objectKeys: guardObjectKeys, - objectKeysIn: guardObjectKeysIn, - objectSomeKeys: guardObjectSomeKeys, - primitive: guardPrimitive, - regexp: guardRegExp, - string: guardString, - stringIncludes: guardStringIncludes, - stringIncludesSome: guardStringIncludesSome, - stringLength: guardStringLength, - stringLengthBetween: guardStringLengthBetween, - symbol: guardSymbol, - true: guardTrue, - type: guardType, - undefined: guardUndefined -}); diff --git a/src/lib/guard.object.ts b/src/lib/guard.object.ts index 4dc1bc8..1b0cf73 100644 --- a/src/lib/guard.object.ts +++ b/src/lib/guard.object.ts @@ -1,9 +1,68 @@ -// Import: Object. -import { guardIs } from './guard-is.object'; -// Import: Interface. -import { Guard } from '../interface/guard-interface'; -/** - * @description - * @type {Guard} - */ -export const guard: Guard = Object.freeze({ ...guardIs }); +// Function. +import { + guardArray, + guardBigInt, + guardBoolean, + guardClass, + guardDate, + guardDefined, + guardFalse, + guardFunction, + guardInstance, + guardKey, + guardNull, + guardNumber, + guardNumberBetween, + guardObject, + guardObjectKey, + guardObjectKeyIn, + guardObjectKeys, + guardObjectKeysIn, + guardObjectSomeKeys, + guardPrimitive, + guardRegExp, + guardString, + guardStringIncludes, + guardStringIncludesSome, + guardStringLength, + guardStringLengthBetween, + guardSymbol, + guardTrue, + guardType, + guardUndefined +} from './index'; +// Interface. +import { Guard } from '../interface'; +// `guardIs`. +export const guard: Guard = Object.freeze({ + array: guardArray, + bigint: guardBigInt, + boolean: guardBoolean, + class: guardClass, + date: guardDate, + defined: guardDefined, + false: guardFalse, + function: guardFunction, + instance: guardInstance, + key: guardKey, + null: guardNull, + number: guardNumber, + numberBetween: guardNumberBetween, + object: guardObject, + objectKey: guardObjectKey, + objectKeyIn: guardObjectKeyIn, + objectKeys: guardObjectKeys, + objectKeysIn: guardObjectKeysIn, + objectSomeKeys: guardObjectSomeKeys, + primitive: guardPrimitive, + regexp: guardRegExp, + string: guardString, + stringIncludes: guardStringIncludes, + stringIncludesSome: guardStringIncludesSome, + stringLength: guardStringLength, + stringLengthBetween: guardStringLengthBetween, + symbol: guardSymbol, + true: guardTrue, + type: guardType, + undefined: guardUndefined +}); From 5fec24991941f3b14c416f08f930f526a9b24852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Acibor=20Rudnicki?= Date: Fri, 6 Dec 2024 14:00:31 +0000 Subject: [PATCH 13/13] chore(package): update. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 551c836..e475dff 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@testing-package/guard", "version": "1.0.0", "author": "wwwdev.io ", - "description": "Type-safe guards for validating value types in TypeScript.", + "description": "Type-safe guards for guarding the value types in TypeScript.", "license": "MIT", "publishConfig": { "access": "public",