@esm-test/guards 1.0.0-beta.10 → 1.0.0-beta.11

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 CHANGED
@@ -1,3 +1,8 @@
1
+ # 1.0.0-beta.11
2
+
3
+ - added aggregate guard `throwNotInstanceOrObject`
4
+ - added `notInstanceOrObjectMessage`
5
+
1
6
  # 1.0.0-beta.10
2
7
 
3
8
  - updated source to typescript 5.9.3
package/README.md CHANGED
@@ -39,12 +39,16 @@ throwUndefinedOrNull('myVar', null); // Throws Error: myVar is null
39
39
  ### Instances
40
40
 
41
41
  ```typescript
42
- import { throwNotInstance } from '@esm-test/guards';
42
+ import { throwNotInstance, throwNotInstanceOrObject } from '@esm-test/guards';
43
43
 
44
44
  class MyClass {}
45
45
  const instance = new MyClass();
46
46
 
47
47
  throwNotInstance('instance', {}, MyClass); // Throws Error: 'instance' must be an instance of 'MyClass'
48
+
49
+ // Accepts either an instance of MyClass or a complex (plain) object
50
+ throwNotInstanceOrObject('instance', [], MyClass); // Throws Error: 'instance' must be an instance of 'MyClass' or a complex object
51
+ throwNotInstanceOrObject('instance', {}, MyClass); // Does not throw
48
52
  ```
49
53
 
50
54
  ### Complex Objects
package/dist/index.min.js CHANGED
@@ -1,2 +1,2 @@
1
- var p=t=>`${t} is empty`,s=(t,n)=>{if(n==="")throw new Error(p(t))};var a=t=>`${t} is null`,r=(t,n)=>{if(n!==null===!1)throw new Error(a(t))};var f=(t,n)=>`'${t}' must be a type of '${n}'`,i=(t,n,o)=>{if(typeof n===o===!1)throw new Error(f(t,o))};var y=t=>`${t} is not defined`,c=(t,n)=>{if(n!==void 0===!1)throw new Error(y(t))};var N=(t,n)=>{c(t,n),r(t,n)},O=(t,n)=>{i(t,n,"string"),s(t,n)};var m=t=>`'${t}' must be a complex object`,C=(t,n)=>{if(l(n)===!1)throw new Error(m(t))},l=t=>t!=null&&t.constructor&&t.constructor===Object;var x=(t,n)=>`'${t}' must be an instance of '${n.name}'`,D=(t,n,o)=>{if(n instanceof o===!1)throw new Error(x(t,o))},w=(t,...n)=>{let o=n.map(e=>e.name).join("|");return`'${t.constructor.name}' must be an instance of either '${o}'`},K=(t,...n)=>{for(let o=0;o<n.length;o++)if(t instanceof n[o])return;throw new Error(w(t,...n))};var g=(t,n)=>`'${t}' must be one of '${Object.keys(n).join(", ")}'`,U=(t,n,o)=>{if(Object.hasOwn(n,o)===!1)throw new Error(g(t,n))};export{p as emptyMessage,l as isComplexObject,w as notAnyInstanceMessage,m as notComplexObjectMessage,x as notInstanceMessage,g as notObjectKeyMessage,f as notTypeMessage,a as nullMessage,s as throwEmpty,K as throwNotAnyInstance,C as throwNotComplexObject,D as throwNotInstance,U as throwNotObjectKey,O as throwNotStringOrEmpty,i as throwNotType,r as throwNull,c as throwUndefined,N as throwUndefinedOrNull,y as undefinedMessage};
1
+ var p=t=>`'${t}' must be a complex object`,b=(t,n)=>{if(s(n)===!1)throw new Error(p(t))},s=t=>t!=null&&t.constructor&&t.constructor===Object;var f=t=>`${t} is empty`,r=(t,n)=>{if(n==="")throw new Error(f(t))};var m=t=>`${t} is null`,i=(t,n)=>{if(n!==null===!1)throw new Error(m(t))};var l=(t,n)=>`'${t}' must be a type of '${n}'`,c=(t,n,o)=>{if(typeof n===o===!1)throw new Error(l(t,o))};var y=t=>`${t} is not defined`,a=(t,n)=>{if(n!==void 0===!1)throw new Error(y(t))};var M=(t,n)=>{a(t,n),i(t,n)},D=(t,n)=>{c(t,n,"string"),r(t,n)},x=(t,n)=>`'${t}' must be an instance of '${n.name}' or a complex object`,K=(t,n,o)=>{if(s(n))return;if(n instanceof o===!1)throw new Error(x(t,o))};var w=(t,n)=>`'${t}' must be an instance of '${n.name}'`,U=(t,n,o)=>{if(n instanceof o===!1)throw new Error(w(t,o))},g=(t,...n)=>{let o=n.map(e=>e.name).join("|");return`'${t.constructor.name}' must be an instance of either '${o}'`},A=(t,...n)=>{for(let o=0;o<n.length;o++)if(t instanceof n[o])return;throw new Error(g(t,...n))};var h=(t,n)=>`'${t}' must be one of '${Object.keys(n).join(", ")}'`,k=(t,n,o)=>{if(Object.hasOwn(n,o)===!1)throw new Error(h(t,n))};export{f as emptyMessage,s as isComplexObject,g as notAnyInstanceMessage,p as notComplexObjectMessage,w as notInstanceMessage,x as notInstanceOrObjectMessage,h as notObjectKeyMessage,l as notTypeMessage,m as nullMessage,r as throwEmpty,A as throwNotAnyInstance,b as throwNotComplexObject,U as throwNotInstance,K as throwNotInstanceOrObject,k as throwNotObjectKey,D as throwNotStringOrEmpty,c as throwNotType,i as throwNull,a as throwUndefined,M as throwUndefinedOrNull,y as undefinedMessage};
2
2
  //# sourceMappingURL=index.min.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/guardEmpty.ts", "../src/guardNull.ts", "../src/guardType.ts", "../src/guardUndefined.ts", "../src/guardAggregates.ts", "../src/guardComplexObject.ts", "../src/guardInstance.ts", "../src/guardObjectKey.ts"],
4
- "sourcesContent": ["/**\n * Generates an error message when a string is empty.\n * \n * @param guardName - The name of the variable being guarded.\n * @returns A formatted error message.\n */\nexport const emptyMessage = (guardName: string): string => `${guardName} is empty`;\n\n/**\n * Throws an error if the provided string is empty ('').\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The string to check for emptiness.\n * @throws {Error} Throws an error if `guard` is an empty string.\n */\nexport const throwEmpty = (guardName: string, guard: string): void => {\n const isEmpty = guard === '';\n if (isEmpty) throw new Error(emptyMessage(guardName));\n}", "/**\n * Generates an error message when a value is null.\n * \n * @param guardName - The name of the variable being guarded.\n * @returns A formatted error message.\n */\nexport const nullMessage = (guardName: string): string => `${guardName} is null`;\n\n/**\n * Throws an error if the provided value is null.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check for null.\n * @throws {Error} Throws an error if `guard` is strictly equal to `null`.\n */\nexport const throwNull = (guardName: string, guard: any): void => {\n const isNull = guard !== null;\n if (isNull === false) {\n throw new Error(nullMessage(guardName));\n }\n}", "/**\n * Supported JavaScript types for type guarding.\n */\nexport type Types = \"number\" | \"string\" | \"boolean\" | \"undefined\" | \"function\" | \"object\" | \"bigint\" | \"symbol\";\n\n\n/**\n * Generates a standard error message when a value is not of the expected type.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guardType - The expected type.\n * @returns A formatted error message.\n */\nexport const notTypeMessage = (guardName: string, guardType: Types): string => (\n `'${guardName}' must be a type of '${guardType}'`\n)\n\n/**\n * Throws an error if the provided value is not of the specified type.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @param guardType - The expected type.\n * @throws {Error} Throws an error if `typeof guard` does not equal `guardType`.\n */\nexport const throwNotType = (guardName: string, guard: any, guardType: Types): void => {\n const isType = typeof guard === guardType;\n if (isType === false) {\n throw new Error(notTypeMessage(guardName, guardType));\n }\n}", "/**\n * Generates an error message when a value is undefined.\n * \n * @param guardName - The name of the variable being guarded.\n * @returns A formatted error message.\n */\nexport const undefinedMessage = (guardName: string): string => `${guardName} is not defined`;\n\n/**\n * Throws an error if the provided value is undefined.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check for undefined.\n * @throws {Error} Throws an error if `guard` is strictly equal to `undefined`.\n */\nexport const throwUndefined = (guardName: string, guard: any): void => {\n const isDefined = guard !== undefined;\n if (isDefined === false) {\n throw new Error(undefinedMessage(guardName));\n }\n}", "import { throwEmpty } from \"./guardEmpty.js\";\nimport { throwNull } from \"./guardNull.js\";\nimport { throwNotType } from \"./guardType.js\";\nimport { throwUndefined } from \"./guardUndefined.js\";\n\n/**\n * Throws an error if the provided value is undefined or null.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @throws {Error} Throws an error if `guard` is `undefined` or `null`.\n */\nexport const throwUndefinedOrNull = (guardName: string, guard: any): void => {\n throwUndefined(guardName, guard);\n throwNull(guardName, guard);\n}\n\n/**\n * Throws an error if the provided value is not a string or is an empty string.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @throws {Error} Throws an error if `guard` is not a string or if it's empty ('').\n */\nexport const throwNotStringOrEmpty = (guardName: string, guard: any): void => {\n throwNotType(guardName, guard, 'string')\n throwEmpty(guardName, guard);\n}", "/**\n * Generates an error message when a value is not a complex object.\n * \n * @param guardName - The name of the variable being guarded.\n * @returns A formatted error message.\n */\nexport const notComplexObjectMessage = (guardName: string): string => (\n `'${guardName}' must be a complex object`\n)\n\n/**\n * Throws an error if the provided value is not a complex object.\n * \n * A complex object is defined as a non-null object that is not a primitive and whose constructor is `Object`.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @throws {Error} Throws an error if `isComplexObject(guard)` is false.\n */\nexport const throwNotComplexObject = (guardName: string, guard: any): void => {\n if (isComplexObject(guard) === false) {\n throw new Error(notComplexObjectMessage(guardName));\n }\n}\n\n/**\n * Determines if a value is a complex object.\n * \n * @param value - The value to test.\n * @returns `true` if the value is a defined, non-null object with a constructor matching `Object`.\n */\nexport const isComplexObject = (value: any): boolean => {\n const isDefined = value !== undefined && value !== null;\n return isDefined\n && value.constructor\n && value.constructor === Object;\n}", "/**\n * Represents a class constructor type.\n */\nexport type ClassInstance = new (...args: any[]) => void\n\n/**\n * Generates an error message when a value is not an instance of the specified class.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guardType - The expected class constructor.\n * @returns A formatted error message.\n */\nexport const notInstanceMessage = (guardName: string, guardType: ClassInstance): string => (\n `'${guardName}' must be an instance of '${guardType.name}'`\n)\n\n/**\n * Throws an error if the provided value is not an instance of the specified class.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @param guardType - The expected class constructor.\n * @throws {Error} Throws an error if `guard instanceof guardType` is false.\n */\nexport const throwNotInstance = (guardName: string, guard: any, guardType: ClassInstance): void => {\n const isInstance = guard instanceof guardType;\n if (isInstance === false) {\n throw new Error(notInstanceMessage(guardName, guardType));\n }\n}\n\n/**\n * Generates an error message when a value is not an instance of any of the specified classes.\n * \n * @param guardInstance - The value that failed the check.\n * @param guardTypes - The expected class constructors.\n * @returns A formatted error message.\n */\nexport const notAnyInstanceMessage = (guardInstance: any, ...guardTypes: ClassInstance[]): string => {\n const typeNames = guardTypes.map(t => t.name).join('|');\n return `'${guardInstance.constructor.name}' must be an instance of either '${typeNames}'`\n}\n\n/**\n * Throws an error if the provided value is not an instance of any of the specified classes.\n * \n * @param guardInstance - The value to check.\n * @param guardTypes - One or more expected class constructors.\n * @throws {Error} Throws an error if `guardInstance` is not an instance of any of the `guardTypes`.\n */\nexport const throwNotAnyInstance = (guardInstance: any, ...guardTypes: ClassInstance[]): void => {\n for (let i = 0; i < guardTypes.length; i++) {\n const isInstance = guardInstance instanceof guardTypes[i];\n if (isInstance) return;\n }\n throw new Error(notAnyInstanceMessage(guardInstance, ...guardTypes));\n}", "/**\n * Generates an error message when a value is not a key of the provided record.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The record containing valid keys.\n * @returns A formatted error message.\n */\nexport const notObjectKeyMessage = (guardName: string, guard: Record<any, any>): string => (\n `'${guardName}' must be one of '${Object.keys(guard).join(', ')}'`\n)\n\n/**\n * Throws an error if the provided key is not present in the given record.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The record to check against.\n * @param guardObjectKey - The key to check for existence in the record.\n * @throws {Error} Throws an error if `Object.hasOwn(guard, guardObjectKey)` is false.\n */\nexport const throwNotObjectKey = (guardName: string, guard: Record<any, any>, guardObjectKey: string): void => {\n const hasKey = Object.hasOwn(guard, guardObjectKey);\n if (hasKey === false) {\n throw new Error(notObjectKeyMessage(guardName, guard));\n }\n}"],
5
- "mappings": "AAMO,IAAMA,EAAgBC,GAA8B,GAAGA,CAAS,YAS1DC,EAAa,CAACD,EAAmBE,IAAwB,CAEpE,GADgBA,IAAU,GACb,MAAM,IAAI,MAAMH,EAAaC,CAAS,CAAC,CACtD,ECZO,IAAMG,EAAeC,GAA8B,GAAGA,CAAS,WASzDC,EAA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AIH,EAAc,YAAY,IAAI,oCAAoCE,CAAS,GACxF,EASaE,EAAsB,CAACJ,KAAuBC,IAAsC,CAC/F,QAASI,EAAI,EAAGA,EAAIJ,EAAW,OAAQI,IAErC,GADmBL,aAAyBC,EAAWI,CAAC,EACxC,OAElB,MAAM,IAAI,MAAMN,EAAsBC,EAAe,GAAGC,CAAU,CAAC,CACrE,ECjDO,IAAMK,EAAsB,CAACC,EAAmBC,IACrD,IAAID,CAAS,qBAAqB,OAAO,KAAKC,CAAK,EAAE,KAAK,IAAI,CAAC,IAWpDC,EAAoB,CAACF,EAAmBC,EAAyBE,IAAiC,CAE7G,GADe,OAAO,OAAOF,EAAOE,CAAc,IACnC,GACb,MAAM,IAAI,MAAMJ,EAAoBC,EAAWC,CAAK,CAAC,CAEzD",
6
- "names": ["emptyMessage", "guardName", "throwEmpty", "guard", "nullMessage", "guardName", "throwNull", "guard", "notTypeMessage", "guardName", "guardType", "throwNotType", "guard", "undefinedMessage", "guardName", "throwUndefined", "guard", "throwUndefinedOrNull", "guardName", "guard", "throwUndefined", "throwNull", "throwNotStringOrEmpty", "throwNotType", "throwEmpty", "notComplexObjectMessage", "guardName", "throwNotComplexObject", "guard", "isComplexObject", "value", "notInstanceMessage", "guardName", "guardType", "throwNotInstance", "guard", "notAnyInstanceMessage", "guardInstance", "guardTypes", "typeNames", "t", "throwNotAnyInstance", "i", "notObjectKeyMessage", "guardName", "guard", "throwNotObjectKey", "guardObjectKey"]
3
+ "sources": ["../src/guardComplexObject.ts", "../src/guardEmpty.ts", "../src/guardNull.ts", "../src/guardType.ts", "../src/guardUndefined.ts", "../src/guardAggregates.ts", "../src/guardInstance.ts", "../src/guardObjectKey.ts"],
4
+ "sourcesContent": ["/**\n * Generates an error message when a value is not a complex object.\n * \n * @param guardName - The name of the variable being guarded.\n * @returns A formatted error message.\n */\nexport const notComplexObjectMessage = (guardName: string): string => (\n `'${guardName}' must be a complex object`\n)\n\n/**\n * Throws an error if the provided value is not a complex object.\n * \n * A complex object is defined as a non-null object that is not a primitive and whose constructor is `Object`.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @throws {Error} Throws an error if `isComplexObject(guard)` is false.\n */\nexport const throwNotComplexObject = (guardName: string, guard: any): void => {\n if (isComplexObject(guard) === false) {\n throw new Error(notComplexObjectMessage(guardName));\n }\n}\n\n/**\n * Determines if a value is a complex object.\n * \n * @param value - The value to test.\n * @returns `true` if the value is a defined, non-null object with a constructor matching `Object`.\n */\nexport const isComplexObject = (value: any): boolean => {\n const isDefined = value !== undefined && value !== null;\n return isDefined\n && value.constructor\n && value.constructor === Object;\n}", "/**\n * Generates an error message when a string is empty.\n * \n * @param guardName - The name of the variable being guarded.\n * @returns A formatted error message.\n */\nexport const emptyMessage = (guardName: string): string => `${guardName} is empty`;\n\n/**\n * Throws an error if the provided string is empty ('').\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The string to check for emptiness.\n * @throws {Error} Throws an error if `guard` is an empty string.\n */\nexport const throwEmpty = (guardName: string, guard: string): void => {\n const isEmpty = guard === '';\n if (isEmpty) throw new Error(emptyMessage(guardName));\n}", "/**\n * Generates an error message when a value is null.\n * \n * @param guardName - The name of the variable being guarded.\n * @returns A formatted error message.\n */\nexport const nullMessage = (guardName: string): string => `${guardName} is null`;\n\n/**\n * Throws an error if the provided value is null.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check for null.\n * @throws {Error} Throws an error if `guard` is strictly equal to `null`.\n */\nexport const throwNull = (guardName: string, guard: any): void => {\n const isNull = guard !== null;\n if (isNull === false) {\n throw new Error(nullMessage(guardName));\n }\n}", "/**\n * Supported JavaScript types for type guarding.\n */\nexport type Types = \"number\" | \"string\" | \"boolean\" | \"undefined\" | \"function\" | \"object\" | \"bigint\" | \"symbol\";\n\n\n/**\n * Generates a standard error message when a value is not of the expected type.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guardType - The expected type.\n * @returns A formatted error message.\n */\nexport const notTypeMessage = (guardName: string, guardType: Types): string => (\n `'${guardName}' must be a type of '${guardType}'`\n)\n\n/**\n * Throws an error if the provided value is not of the specified type.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @param guardType - The expected type.\n * @throws {Error} Throws an error if `typeof guard` does not equal `guardType`.\n */\nexport const throwNotType = (guardName: string, guard: any, guardType: Types): void => {\n const isType = typeof guard === guardType;\n if (isType === false) {\n throw new Error(notTypeMessage(guardName, guardType));\n }\n}", "/**\n * Generates an error message when a value is undefined.\n * \n * @param guardName - The name of the variable being guarded.\n * @returns A formatted error message.\n */\nexport const undefinedMessage = (guardName: string): string => `${guardName} is not defined`;\n\n/**\n * Throws an error if the provided value is undefined.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check for undefined.\n * @throws {Error} Throws an error if `guard` is strictly equal to `undefined`.\n */\nexport const throwUndefined = (guardName: string, guard: any): void => {\n const isDefined = guard !== undefined;\n if (isDefined === false) {\n throw new Error(undefinedMessage(guardName));\n }\n}", "import { isComplexObject } from \"./guardComplexObject.js\";\nimport { throwEmpty } from \"./guardEmpty.js\";\nimport { ClassInstance } from \"./guardInstance.js\";\nimport { throwNull } from \"./guardNull.js\";\nimport { throwNotType } from \"./guardType.js\";\nimport { throwUndefined } from \"./guardUndefined.js\";\n\n/**\n * Throws an error if the provided value is undefined or null.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @throws {Error} Throws an error if `guard` is `undefined` or `null`.\n */\nexport const throwUndefinedOrNull = (guardName: string, guard: any): void => {\n throwUndefined(guardName, guard);\n throwNull(guardName, guard);\n}\n\n/**\n * Throws an error if the provided value is not a string or is an empty string.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @throws {Error} Throws an error if `guard` is not a string or if it's empty ('').\n */\nexport const throwNotStringOrEmpty = (guardName: string, guard: any): void => {\n throwNotType(guardName, guard, 'string')\n throwEmpty(guardName, guard);\n}\n\n/**\n * Generates an error message when a value is not an instance of the specified class or a complex object.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guardType - The expected class constructor.\n * @returns A formatted error message.\n */\nexport const notInstanceOrObjectMessage = (guardName: string, guardType: ClassInstance): string => (\n `'${guardName}' must be an instance of '${guardType.name}' or a complex object`\n)\n\n/**\n * Throws an error if the provided value is not an instance of the specified class or a complex object.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @param guardType - The expected class constructor.\n * @throws {Error} Throws an error if `guard` is not a complex object and not an instance of `guardType`.\n */\nexport const throwNotInstanceOrObject = (guardName: string, guard: any, guardType: ClassInstance): void => {\n if (isComplexObject(guard)) return;\n const isInstance = guard instanceof guardType;\n if (isInstance === false) {\n throw new Error(notInstanceOrObjectMessage(guardName, guardType));\n }\n}\n", "/**\n * Represents a class constructor type.\n */\nexport type ClassInstance = new (...args: any[]) => void\n\n/**\n * Generates an error message when a value is not an instance of the specified class.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guardType - The expected class constructor.\n * @returns A formatted error message.\n */\nexport const notInstanceMessage = (guardName: string, guardType: ClassInstance): string => (\n `'${guardName}' must be an instance of '${guardType.name}'`\n)\n\n/**\n * Throws an error if the provided value is not an instance of the specified class.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The value to check.\n * @param guardType - The expected class constructor.\n * @throws {Error} Throws an error if `guard instanceof guardType` is false.\n */\nexport const throwNotInstance = (guardName: string, guard: any, guardType: ClassInstance): void => {\n const isInstance = guard instanceof guardType;\n if (isInstance === false) {\n throw new Error(notInstanceMessage(guardName, guardType));\n }\n}\n\n/**\n * Generates an error message when a value is not an instance of any of the specified classes.\n * \n * @param guardInstance - The value that failed the check.\n * @param guardTypes - The expected class constructors.\n * @returns A formatted error message.\n */\nexport const notAnyInstanceMessage = (guardInstance: any, ...guardTypes: ClassInstance[]): string => {\n const typeNames = guardTypes.map(t => t.name).join('|');\n return `'${guardInstance.constructor.name}' must be an instance of either '${typeNames}'`\n}\n\n/**\n * Throws an error if the provided value is not an instance of any of the specified classes.\n * \n * @param guardInstance - The value to check.\n * @param guardTypes - One or more expected class constructors.\n * @throws {Error} Throws an error if `guardInstance` is not an instance of any of the `guardTypes`.\n */\nexport const throwNotAnyInstance = (guardInstance: any, ...guardTypes: ClassInstance[]): void => {\n for (let i = 0; i < guardTypes.length; i++) {\n const isInstance = guardInstance instanceof guardTypes[i];\n if (isInstance) return;\n }\n throw new Error(notAnyInstanceMessage(guardInstance, ...guardTypes));\n}", "/**\n * Generates an error message when a value is not a key of the provided record.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The record containing valid keys.\n * @returns A formatted error message.\n */\nexport const notObjectKeyMessage = (guardName: string, guard: Record<any, any>): string => (\n `'${guardName}' must be one of '${Object.keys(guard).join(', ')}'`\n)\n\n/**\n * Throws an error if the provided key is not present in the given record.\n * \n * @param guardName - The name of the variable being guarded.\n * @param guard - The record to check against.\n * @param guardObjectKey - The key to check for existence in the record.\n * @throws {Error} Throws an error if `Object.hasOwn(guard, guardObjectKey)` is false.\n */\nexport const throwNotObjectKey = (guardName: string, guard: Record<any, any>, guardObjectKey: string): void => {\n const hasKey = Object.hasOwn(guard, guardObjectKey);\n if (hasKey === false) {\n throw new Error(notObjectKeyMessage(guardName, guard));\n }\n}"],
5
+ "mappings": "AAMO,IAAMA,EAA2BC,GACtC,IAAIA,CAAS,6BAYFC,EAAwB,CAACD,EAAmBE,IAAqB,CAC5E,GAAIC,EAAgBD,CAAK,IAAM,GAC7B,MAAM,IAAI,MAAMH,EAAwBC,CAAS,CAAC,CAEtD,EAQaG,EAAmBC,GACWA,GAAU,MAE9CA,EAAM,aACNA,EAAM,cAAgB,OC7BtB,IAAMC,EAAgBC,GAA8B,GAAGA,CAAS,YAS1DC,EAAa,CAACD,EAAmBE,IAAwB,CAEpE,GADgBA,IAAU,GACb,MAAM,IAAI,MAAMH,EAAaC,CAAS,CAAC,CACtD,ECZO,IAAMG,EAAeC,GAA8B,GAAGA,CAAS,WASzDC,EAAY,CAACD,EA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AIH,EAAc,YAAY,IAAI,oCAAoCE,CAAS,GACxF,EASaE,EAAsB,CAACJ,KAAuBC,IAAsC,CAC/F,QAASI,EAAI,EAAGA,EAAIJ,EAAW,OAAQI,IAErC,GADmBL,aAAyBC,EAAWI,CAAC,EACxC,OAElB,MAAM,IAAI,MAAMN,EAAsBC,EAAe,GAAGC,CAAU,CAAC,CACrE,ECjDO,IAAMK,EAAsB,CAACC,EAAmBC,IACrD,IAAID,CAAS,qBAAqB,OAAO,KAAKC,CAAK,EAAE,KAAK,IAAI,CAAC,IAWpDC,EAAoB,CAACF,EAAmBC,EAAyBE,IAAiC,CAE7G,GADe,OAAO,OAAOF,EAAOE,CAAc,IACnC,GACb,MAAM,IAAI,MAAMJ,EAAoBC,EAAWC,CAAK,CAAC,CAEzD",
6
+ "names": ["notComplexObjectMessage", "guardName", "throwNotComplexObject", "guard", "isComplexObject", "value", "emptyMessage", "guardName", "throwEmpty", "guard", "nullMessage", "guardName", "throwNull", "guard", "notTypeMessage", "guardName", "guardType", "throwNotType", "guard", "undefinedMessage", "guardName", "throwUndefined", "guard", "throwUndefinedOrNull", "guardName", "guard", "throwUndefined", "throwNull", "throwNotStringOrEmpty", "throwNotType", "throwEmpty", "notInstanceOrObjectMessage", "guardType", "throwNotInstanceOrObject", "isComplexObject", "notInstanceMessage", "guardName", "guardType", "throwNotInstance", "guard", "notAnyInstanceMessage", "guardInstance", "guardTypes", "typeNames", "t", "throwNotAnyInstance", "i", "notObjectKeyMessage", "guardName", "guard", "throwNotObjectKey", "guardObjectKey"]
7
7
  }
@@ -1,3 +1,4 @@
1
+ import { ClassInstance } from "./guardInstance.js";
1
2
  /**
2
3
  * Throws an error if the provided value is undefined or null.
3
4
  *
@@ -14,4 +15,21 @@ export declare const throwUndefinedOrNull: (guardName: string, guard: any) => vo
14
15
  * @throws {Error} Throws an error if `guard` is not a string or if it's empty ('').
15
16
  */
16
17
  export declare const throwNotStringOrEmpty: (guardName: string, guard: any) => void;
18
+ /**
19
+ * Generates an error message when a value is not an instance of the specified class or a complex object.
20
+ *
21
+ * @param guardName - The name of the variable being guarded.
22
+ * @param guardType - The expected class constructor.
23
+ * @returns A formatted error message.
24
+ */
25
+ export declare const notInstanceOrObjectMessage: (guardName: string, guardType: ClassInstance) => string;
26
+ /**
27
+ * Throws an error if the provided value is not an instance of the specified class or a complex object.
28
+ *
29
+ * @param guardName - The name of the variable being guarded.
30
+ * @param guard - The value to check.
31
+ * @param guardType - The expected class constructor.
32
+ * @throws {Error} Throws an error if `guard` is not a complex object and not an instance of `guardType`.
33
+ */
34
+ export declare const throwNotInstanceOrObject: (guardName: string, guard: any, guardType: ClassInstance) => void;
17
35
  //# sourceMappingURL=guardAggregates.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"guardAggregates.d.ts","sourceRoot":"","sources":["../../../src/guardAggregates.ts"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,WAAW,MAAM,EAAE,OAAO,GAAG,KAAG,IAGpE,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,GAAI,WAAW,MAAM,EAAE,OAAO,GAAG,KAAG,IAGrE,CAAA"}
1
+ {"version":3,"file":"guardAggregates.d.ts","sourceRoot":"","sources":["../../../src/guardAggregates.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAKnD;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,WAAW,MAAM,EAAE,OAAO,GAAG,KAAG,IAGpE,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,GAAI,WAAW,MAAM,EAAE,OAAO,GAAG,KAAG,IAGrE,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,GAAI,WAAW,MAAM,EAAE,WAAW,aAAa,KAAG,MAExF,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,GAAI,WAAW,MAAM,EAAE,OAAO,GAAG,EAAE,WAAW,aAAa,KAAG,IAMlG,CAAA"}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.throwNotStringOrEmpty = exports.throwUndefinedOrNull = void 0;
3
+ exports.throwNotInstanceOrObject = exports.notInstanceOrObjectMessage = exports.throwNotStringOrEmpty = exports.throwUndefinedOrNull = void 0;
4
+ const guardComplexObject_js_1 = require("./guardComplexObject.cjs");
4
5
  const guardEmpty_js_1 = require("./guardEmpty.cjs");
5
6
  const guardNull_js_1 = require("./guardNull.cjs");
6
7
  const guardType_js_1 = require("./guardType.cjs");
@@ -29,4 +30,30 @@ const throwNotStringOrEmpty = (guardName, guard) => {
29
30
  (0, guardEmpty_js_1.throwEmpty)(guardName, guard);
30
31
  };
31
32
  exports.throwNotStringOrEmpty = throwNotStringOrEmpty;
33
+ /**
34
+ * Generates an error message when a value is not an instance of the specified class or a complex object.
35
+ *
36
+ * @param guardName - The name of the variable being guarded.
37
+ * @param guardType - The expected class constructor.
38
+ * @returns A formatted error message.
39
+ */
40
+ const notInstanceOrObjectMessage = (guardName, guardType) => (`'${guardName}' must be an instance of '${guardType.name}' or a complex object`);
41
+ exports.notInstanceOrObjectMessage = notInstanceOrObjectMessage;
42
+ /**
43
+ * Throws an error if the provided value is not an instance of the specified class or a complex object.
44
+ *
45
+ * @param guardName - The name of the variable being guarded.
46
+ * @param guard - The value to check.
47
+ * @param guardType - The expected class constructor.
48
+ * @throws {Error} Throws an error if `guard` is not a complex object and not an instance of `guardType`.
49
+ */
50
+ const throwNotInstanceOrObject = (guardName, guard, guardType) => {
51
+ if ((0, guardComplexObject_js_1.isComplexObject)(guard))
52
+ return;
53
+ const isInstance = guard instanceof guardType;
54
+ if (isInstance === false) {
55
+ throw new Error((0, exports.notInstanceOrObjectMessage)(guardName, guardType));
56
+ }
57
+ };
58
+ exports.throwNotInstanceOrObject = throwNotInstanceOrObject;
32
59
  //# sourceMappingURL=guardAggregates.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"guardAggregates.cjs","sourceRoot":"","sources":["../../src/guardAggregates.ts"],"names":[],"mappings":";;;AAAA,mDAA6C;AAC7C,iDAA2C;AAC3C,iDAA8C;AAC9C,2DAAqD;AAErD;;;;;;GAMG;AACI,MAAM,oBAAoB,GAAG,CAAC,SAAiB,EAAE,KAAU,EAAQ,EAAE;IAC1E,IAAA,kCAAc,EAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IACjC,IAAA,wBAAS,EAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC9B,CAAC,CAAA;AAHY,QAAA,oBAAoB,wBAGhC;AAED;;;;;;GAMG;AACI,MAAM,qBAAqB,GAAG,CAAC,SAAiB,EAAE,KAAU,EAAQ,EAAE;IAC3E,IAAA,2BAAY,EAAC,SAAS,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAA;IACxC,IAAA,0BAAU,EAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC/B,CAAC,CAAA;AAHY,QAAA,qBAAqB,yBAGjC"}
1
+ {"version":3,"file":"guardAggregates.cjs","sourceRoot":"","sources":["../../src/guardAggregates.ts"],"names":[],"mappings":";;;AAAA,mEAA0D;AAC1D,mDAA6C;AAE7C,iDAA2C;AAC3C,iDAA8C;AAC9C,2DAAqD;AAErD;;;;;;GAMG;AACI,MAAM,oBAAoB,GAAG,CAAC,SAAiB,EAAE,KAAU,EAAQ,EAAE;IAC1E,IAAA,kCAAc,EAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IACjC,IAAA,wBAAS,EAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC9B,CAAC,CAAA;AAHY,QAAA,oBAAoB,wBAGhC;AAED;;;;;;GAMG;AACI,MAAM,qBAAqB,GAAG,CAAC,SAAiB,EAAE,KAAU,EAAQ,EAAE;IAC3E,IAAA,2BAAY,EAAC,SAAS,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAA;IACxC,IAAA,0BAAU,EAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC/B,CAAC,CAAA;AAHY,QAAA,qBAAqB,yBAGjC;AAED;;;;;;GAMG;AACI,MAAM,0BAA0B,GAAG,CAAC,SAAiB,EAAE,SAAwB,EAAU,EAAE,CAAC,CACjG,IAAI,SAAS,6BAA6B,SAAS,CAAC,IAAI,uBAAuB,CAChF,CAAA;AAFY,QAAA,0BAA0B,8BAEtC;AAED;;;;;;;GAOG;AACI,MAAM,wBAAwB,GAAG,CAAC,SAAiB,EAAE,KAAU,EAAE,SAAwB,EAAQ,EAAE;IACxG,IAAI,IAAA,uCAAe,EAAC,KAAK,CAAC;QAAE,OAAO;IACnC,MAAM,UAAU,GAAG,KAAK,YAAY,SAAS,CAAC;IAC9C,IAAI,UAAU,KAAK,KAAK,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,IAAA,kCAA0B,EAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;IACpE,CAAC;AACH,CAAC,CAAA;AANY,QAAA,wBAAwB,4BAMpC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@esm-test/guards",
3
- "version": "1.0.0-beta.10",
3
+ "version": "1.0.0-beta.11",
4
4
  "description": "A js utility library for guarding objects, instances and types",
5
5
  "publishConfig": {
6
6
  "access": "public"