@esm-test/guards 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # 1.0.0-beta.2
2
+
3
+ - Fixed package repo link
4
+
5
+ # 1.0.0-beta.1
6
+
7
+ - init commit extracted from [esm-test-parser](https://gitlab.com/esm-test-group/esm-test-parser)
package/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ The ISC License
2
+
3
+ Copyright (c) 2017 Peter Flannery and Contributors
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15
+ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # Esm test guards
2
+
3
+ [![Pipeline status](https://gitlab.com/esm-test-group/esm-test-guards/badges/master/pipeline.svg)](https://gitlab.com/esm-test-group/esm-test-guards/-/pipelines)
4
+ [![The ISC license](https://img.shields.io/badge/license-ISC-orange.png?color=blue&style=flat-square)](http://opensource.org/licenses/ISC)
5
+ [![NPM version](https://img.shields.io/npm/v/esm-test-guards.svg)](https://www.npmjs.org/package/esm-test-guards)
6
+ [![NPM downloads](https://img.shields.io/npm/dm/esm-test-guards.svg)](https://npmjs.org/package/esm-test-guards "View this project on NPM")
7
+
8
+ [![BuyMeACoffee](https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png)](https://www.buymeacoffee.com/peterf)
package/index.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ declare module 'esm-test-guards' {
2
+
3
+ export function notComplexObjectMessage(guardName: string): string;
4
+ export function throwNotComplexObject(guardName: string, guard: object): void;
5
+
6
+ export function notInstanceMessage(guardName: string, guardType: Object): string;
7
+ export function throwNotInstance(guardName: string, guard: object, guardType: Object): void;
8
+ export function notAnyInstanceMessage(guardInstance: object, ...guardTypes: Object[]): string;
9
+ export function throwNotAnyInstance(guardInstance: object, ...guardTypes: Object[]): void;
10
+
11
+ export function nullMessage(guardName: string): string;
12
+ export function throwNull(guardName: string, guard: object): void;
13
+
14
+ export function notObjectKeyMessage(guardName: string, guard: object): string;
15
+ export function throwNotObjectKey(guardName: string, guard: object, guardObjectKey: string): void;
16
+
17
+ export function notTypeMessage(guardName: string, guardType: string): string;
18
+ export function throwNotType(guardName: string, guard: object, guardType: string): void;
19
+
20
+ export function undefinedMessage(guardName: string): string;
21
+ export function throwUndefined(guardName: string, guard: object): void;
22
+
23
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@esm-test/guards",
3
+ "version": "1.0.0-beta.3",
4
+ "description": "A js library for guards objects, instances and types",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "main": "./src/index.js",
9
+ "type": "module",
10
+ "types": "./index.d.ts",
11
+ "exports": {
12
+ "import": "./src/index.js",
13
+ "types": "./index.d.ts"
14
+ },
15
+ "devDependencies": {
16
+ "@types/mocha": "10.0.1",
17
+ "@types/node": "18.11.18",
18
+ "assert": "2.0.0",
19
+ "c8": "7.12.0",
20
+ "js-yaml": "4.1.0",
21
+ "jshint": "2.13.6",
22
+ "mocha": "10.2.0",
23
+ "mocha-ui-esm": "1.0.0-beta.14",
24
+ "npm-build-tasks": "1.0.0-alpha.5",
25
+ "rimraf": "4.1.2",
26
+ "source-map-support": "0.5.21",
27
+ "typescript": "4.9.5"
28
+ },
29
+ "scripts": {
30
+ "clean": "rimraf ./out",
31
+ "coverage": "npm-build-task",
32
+ "fresh": "npm-build-task",
33
+ "generate:types": "npm-build-task",
34
+ "test": "npm-build-task",
35
+ "prepublishOnly": "npm-build-task"
36
+ },
37
+ "author": "pflannery",
38
+ "license": "ISC",
39
+ "keywords": [
40
+ "npm",
41
+ "node",
42
+ "test",
43
+ "guards"
44
+ ],
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "https://gitlab.com/esm-test-group/esm-test-guards.git"
48
+ }
49
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @param {string} guardName
3
+ */
4
+ export const notComplexObjectMessage = (guardName) => (
5
+ `'${guardName}' must be a complex object`
6
+ )
7
+
8
+ /**
9
+ * @param {string} guardName
10
+ * @param {object} guard
11
+ */
12
+ export const throwNotComplexObject = (guardName, guard) => {
13
+ const isDefined = guard !== undefined && guard !== null;
14
+ const isType = isDefined && typeof guard === "object";
15
+ const isComplexObject = isDefined
16
+ && isType
17
+ && guard.constructor
18
+ && guard.constructor === Object;
19
+
20
+ if (isComplexObject === false) {
21
+ throw new Error(notComplexObjectMessage(guardName));
22
+ }
23
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @param {string} guardName
3
+ * @param {Object} guardType
4
+ */
5
+ export const notInstanceMessage = (guardName, guardType) => (
6
+ `'${guardName}' must be an instance of '${guardType.name}'`
7
+ )
8
+
9
+ /**
10
+ * @param {string} guardName
11
+ * @param {object} guard
12
+ * @param {Object} guardType
13
+ */
14
+ export const throwNotInstance = (guardName, guard, guardType) => {
15
+ const isInstance = guard instanceof guardType;
16
+ if (isInstance === false) {
17
+ throw new Error(notInstanceMessage(guardName, guardType));
18
+ }
19
+ }
20
+
21
+ /**
22
+ * @param {object} guardInstance
23
+ * @param {Object[]} guardTypes
24
+ */
25
+ export const notAnyInstanceMessage = (guardInstance, ...guardTypes) => {
26
+ const typeNames = guardTypes.map(t => t.name).join('|');
27
+ return `'${guardInstance.constructor.name}' must be an instance of either '${typeNames}'`
28
+ }
29
+
30
+ /**
31
+ * @param {object} guardInstance
32
+ * @param {Object[]} guardTypes
33
+ */
34
+ export const throwNotAnyInstance = (guardInstance, ...guardTypes) => {
35
+ for (let i = 0; i < guardTypes.length; i++) {
36
+ const isInstance = guardInstance instanceof guardTypes[i];
37
+ if (isInstance) return;
38
+ }
39
+ throw new Error(
40
+ notAnyInstanceMessage(guardInstance, ...guardTypes)
41
+ );
42
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @param {string} guardName
3
+ */
4
+ export const nullMessage = (guardName) => `${guardName} is null`;
5
+
6
+ /**
7
+ * @param {string} guardName
8
+ * @param {object} guard
9
+ */
10
+ export const throwNull = (guardName, guard) => {
11
+ const isNull = guard !== null;
12
+ if (isNull === false) {
13
+ throw new Error(nullMessage(guardName));
14
+ }
15
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @param {string} guardName
3
+ * @param {object} guard
4
+ */
5
+ export const notObjectKeyMessage = (guardName, guard) => (
6
+ `'${guardName}' must be one of '${Object.keys(guard).join(', ')}'`
7
+ )
8
+
9
+ /**
10
+ * @param {string} guardName
11
+ * @param {object} guard
12
+ * @param {string} guardObjectKey
13
+ */
14
+ export const throwNotObjectKey = (guardName, guard, guardObjectKey) => {
15
+ const hasKey = Object.hasOwn(guard, guardObjectKey);
16
+ if (hasKey === false) {
17
+ throw new Error(notObjectKeyMessage(guardName, guard));
18
+ }
19
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @param {string} guardName
3
+ * @param {string} guardType
4
+ */
5
+ export const notTypeMessage = (guardName, guardType) => (
6
+ `'${guardName}' must be a type of '${guardType}'`
7
+ )
8
+
9
+ /**
10
+ * @param {string} guardName
11
+ * @param {object} guard
12
+ * @param {string} guardType
13
+ */
14
+ export const throwNotType = (guardName, guard, guardType) => {
15
+ const isType = typeof guard === guardType;
16
+ if (isType === false) {
17
+ throw new Error(notTypeMessage(guardName, guardType));
18
+ }
19
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @param {string} guardName
3
+ */
4
+ export const undefinedMessage = (guardName) => `${guardName} is not defined`;
5
+
6
+ /**
7
+ * @param {string} guardName
8
+ * @param {object} guard
9
+ */
10
+ export const throwUndefined = (guardName, guard) => {
11
+ const isDefined = guard !== undefined;
12
+ if (isDefined === false) {
13
+ throw new Error(undefinedMessage(guardName));
14
+ }
15
+ }
package/src/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from './guardComplexObject.js';
2
+ export * from './guardObjectKey.js';
3
+ export * from './guardInstance.js';
4
+ export * from './guardNull.js';
5
+ export * from './guardType.js';
6
+ export * from './guardUndefined.js';