complete-common 1.1.1 → 1.2.0

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.
@@ -3,16 +3,8 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- declare const TYPES_TO_VALIDATION_FUNCTIONS: {
7
- readonly string: (val: unknown) => val is string;
8
- readonly number: (val: unknown) => val is number;
9
- readonly boolean: (val: unknown) => val is boolean;
10
- readonly object: (val: unknown) => val is object;
11
- readonly array: (val: unknown) => val is unknown[];
12
- readonly null: (val: unknown) => val is null;
13
- readonly undefined: (val: unknown) => val is undefined;
14
- readonly function: (val: unknown) => val is Function;
15
- };
6
+ /** Helper function to throw an error if the provided value is not a boolean. */
7
+ export declare function assertBoolean(value: unknown, msg: string): asserts value is boolean;
16
8
  /**
17
9
  * Helper function to throw an error if the provided value is equal to `undefined`.
18
10
  *
@@ -29,6 +21,8 @@ export declare function assertDefined<T>(value: T, ...[msg]: [undefined] extends
29
21
  export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T] ? [string] : [
30
22
  "The assertion is useless because the provided value does not contain null."
31
23
  ]): asserts value is Exclude<T, null>;
24
+ /** Helper function to throw an error if the provided value is not a number. */
25
+ export declare function assertNumber(value: unknown, msg: string): asserts value is number;
32
26
  /**
33
27
  * Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
34
28
  * record).
@@ -39,7 +33,6 @@ export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T]
39
33
  * Under the hood, this function uses the `isObject` helper function.
40
34
  */
41
35
  export declare function assertObject(value: unknown, msg: string): asserts value is Record<string, unknown>;
42
- /** Helper function to throw an error if the provided value is not of the provided type. */
43
- export declare function assertType(value: unknown, type: keyof typeof TYPES_TO_VALIDATION_FUNCTIONS, msg: string): asserts value is unknown;
44
- export {};
36
+ /** Helper function to throw an error if the provided value is not a string. */
37
+ export declare function assertString(value: unknown, msg: string): asserts value is string;
45
38
  //# sourceMappingURL=assert.d.ts.map
@@ -3,16 +3,8 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- declare const TYPES_TO_VALIDATION_FUNCTIONS: {
7
- readonly string: (val: unknown) => val is string;
8
- readonly number: (val: unknown) => val is number;
9
- readonly boolean: (val: unknown) => val is boolean;
10
- readonly object: (val: unknown) => val is object;
11
- readonly array: (val: unknown) => val is unknown[];
12
- readonly null: (val: unknown) => val is null;
13
- readonly undefined: (val: unknown) => val is undefined;
14
- readonly function: (val: unknown) => val is Function;
15
- };
6
+ /** Helper function to throw an error if the provided value is not a boolean. */
7
+ export declare function assertBoolean(value: unknown, msg: string): asserts value is boolean;
16
8
  /**
17
9
  * Helper function to throw an error if the provided value is equal to `undefined`.
18
10
  *
@@ -29,6 +21,8 @@ export declare function assertDefined<T>(value: T, ...[msg]: [undefined] extends
29
21
  export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T] ? [string] : [
30
22
  "The assertion is useless because the provided value does not contain null."
31
23
  ]): asserts value is Exclude<T, null>;
24
+ /** Helper function to throw an error if the provided value is not a number. */
25
+ export declare function assertNumber(value: unknown, msg: string): asserts value is number;
32
26
  /**
33
27
  * Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
34
28
  * record).
@@ -39,7 +33,6 @@ export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T]
39
33
  * Under the hood, this function uses the `isObject` helper function.
40
34
  */
41
35
  export declare function assertObject(value: unknown, msg: string): asserts value is Record<string, unknown>;
42
- /** Helper function to throw an error if the provided value is not of the provided type. */
43
- export declare function assertType(value: unknown, type: keyof typeof TYPES_TO_VALIDATION_FUNCTIONS, msg: string): asserts value is unknown;
44
- export {};
36
+ /** Helper function to throw an error if the provided value is not a string. */
37
+ export declare function assertString(value: unknown, msg: string): asserts value is string;
45
38
  //# sourceMappingURL=assert.d.ts.map
@@ -3,16 +3,8 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- declare const TYPES_TO_VALIDATION_FUNCTIONS: {
7
- readonly string: (val: unknown) => val is string;
8
- readonly number: (val: unknown) => val is number;
9
- readonly boolean: (val: unknown) => val is boolean;
10
- readonly object: (val: unknown) => val is object;
11
- readonly array: (val: unknown) => val is unknown[];
12
- readonly null: (val: unknown) => val is null;
13
- readonly undefined: (val: unknown) => val is undefined;
14
- readonly function: (val: unknown) => val is Function;
15
- };
6
+ /** Helper function to throw an error if the provided value is not a boolean. */
7
+ export declare function assertBoolean(value: unknown, msg: string): asserts value is boolean;
16
8
  /**
17
9
  * Helper function to throw an error if the provided value is equal to `undefined`.
18
10
  *
@@ -29,6 +21,8 @@ export declare function assertDefined<T>(value: T, ...[msg]: [undefined] extends
29
21
  export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T] ? [string] : [
30
22
  "The assertion is useless because the provided value does not contain null."
31
23
  ]): asserts value is Exclude<T, null>;
24
+ /** Helper function to throw an error if the provided value is not a number. */
25
+ export declare function assertNumber(value: unknown, msg: string): asserts value is number;
32
26
  /**
33
27
  * Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
34
28
  * record).
@@ -39,7 +33,6 @@ export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T]
39
33
  * Under the hood, this function uses the `isObject` helper function.
40
34
  */
41
35
  export declare function assertObject(value: unknown, msg: string): asserts value is Record<string, unknown>;
42
- /** Helper function to throw an error if the provided value is not of the provided type. */
43
- export declare function assertType(value: unknown, type: keyof typeof TYPES_TO_VALIDATION_FUNCTIONS, msg: string): asserts value is unknown;
44
- export {};
36
+ /** Helper function to throw an error if the provided value is not a string. */
37
+ export declare function assertString(value: unknown, msg: string): asserts value is string;
45
38
  //# sourceMappingURL=assert.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/functions/assert.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,QAAA,MAAM,6BAA6B;2BACnB,OAAO,KAAG,GAAG,IAAI,MAAM;2BACvB,OAAO,KAAG,GAAG,IAAI,MAAM;4BACtB,OAAO,KAAG,GAAG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O,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,KAAK,IAAI,OAAO,CAI1B;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,KAAK,EAAE,CAAC,EACR,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,GAC7B,CAAC,MAAM,CAAC,GACR;IACE,iFAAiF;CAClF,GACJ,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,CAIxC;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,KAAK,EAAE,CAAC,EACR,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GACxB,CAAC,MAAM,CAAC,GACR;IACE,4EAA4E;CAC7E,GACJ,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,CAInC;AAED,+EAA+E;AAC/E,wBAAgB,YAAY,CAC1B,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,KAAK,IAAI,MAAM,CAIzB;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAI1C;AAED,+EAA+E;AAC/E,wBAAgB,YAAY,CAC1B,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,KAAK,IAAI,MAAM,CAIzB"}
package/dist/index.cjs CHANGED
@@ -10,17 +10,11 @@ function isObject(variable) {
10
10
  return typeof variable === "object" && variable !== null && !Array.isArray(variable);
11
11
  }
12
12
 
13
- const TYPES_TO_VALIDATION_FUNCTIONS = {
14
- string: (val) => typeof val === "string",
15
- number: (val) => typeof val === "number",
16
- boolean: (val) => typeof val === "boolean",
17
- object: (val) => val !== null && typeof val === "object",
18
- array: (val) => Array.isArray(val),
19
- null: (val) => val === null,
20
- undefined: (val) => val === void 0,
21
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
22
- function: (val) => typeof val === "function"
23
- };
13
+ function assertBoolean(value, msg) {
14
+ if (typeof value !== "boolean") {
15
+ throw new TypeError(msg);
16
+ }
17
+ }
24
18
  function assertDefined(value, ...[msg]) {
25
19
  if (value === void 0) {
26
20
  throw new TypeError(msg);
@@ -31,14 +25,18 @@ function assertNotNull(value, ...[msg]) {
31
25
  throw new TypeError(msg);
32
26
  }
33
27
  }
28
+ function assertNumber(value, msg) {
29
+ if (typeof value !== "number") {
30
+ throw new TypeError(msg);
31
+ }
32
+ }
34
33
  function assertObject(value, msg) {
35
34
  if (!isObject(value)) {
36
35
  throw new TypeError(msg);
37
36
  }
38
37
  }
39
- function assertType(value, type, msg) {
40
- const validationFunction = TYPES_TO_VALIDATION_FUNCTIONS[type];
41
- if (!validationFunction(value)) {
38
+ function assertString(value, msg) {
39
+ if (typeof value !== "string") {
42
40
  throw new TypeError(msg);
43
41
  }
44
42
  }
@@ -512,10 +510,12 @@ exports.arrayCopyTwoDimensional = arrayCopyTwoDimensional;
512
510
  exports.arrayEquals = arrayEquals;
513
511
  exports.arrayRemove = arrayRemove;
514
512
  exports.arrayRemoveInPlace = arrayRemoveInPlace;
513
+ exports.assertBoolean = assertBoolean;
515
514
  exports.assertDefined = assertDefined;
516
515
  exports.assertNotNull = assertNotNull;
516
+ exports.assertNumber = assertNumber;
517
517
  exports.assertObject = assertObject;
518
- exports.assertType = assertType;
518
+ exports.assertString = assertString;
519
519
  exports.capitalizeFirstLetter = capitalizeFirstLetter;
520
520
  exports.clamp = clamp;
521
521
  exports.combineSets = combineSets;
package/dist/index.mjs CHANGED
@@ -8,17 +8,11 @@ function isObject(variable) {
8
8
  return typeof variable === "object" && variable !== null && !Array.isArray(variable);
9
9
  }
10
10
 
11
- const TYPES_TO_VALIDATION_FUNCTIONS = {
12
- string: (val) => typeof val === "string",
13
- number: (val) => typeof val === "number",
14
- boolean: (val) => typeof val === "boolean",
15
- object: (val) => val !== null && typeof val === "object",
16
- array: (val) => Array.isArray(val),
17
- null: (val) => val === null,
18
- undefined: (val) => val === void 0,
19
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
20
- function: (val) => typeof val === "function"
21
- };
11
+ function assertBoolean(value, msg) {
12
+ if (typeof value !== "boolean") {
13
+ throw new TypeError(msg);
14
+ }
15
+ }
22
16
  function assertDefined(value, ...[msg]) {
23
17
  if (value === void 0) {
24
18
  throw new TypeError(msg);
@@ -29,14 +23,18 @@ function assertNotNull(value, ...[msg]) {
29
23
  throw new TypeError(msg);
30
24
  }
31
25
  }
26
+ function assertNumber(value, msg) {
27
+ if (typeof value !== "number") {
28
+ throw new TypeError(msg);
29
+ }
30
+ }
32
31
  function assertObject(value, msg) {
33
32
  if (!isObject(value)) {
34
33
  throw new TypeError(msg);
35
34
  }
36
35
  }
37
- function assertType(value, type, msg) {
38
- const validationFunction = TYPES_TO_VALIDATION_FUNCTIONS[type];
39
- if (!validationFunction(value)) {
36
+ function assertString(value, msg) {
37
+ if (typeof value !== "string") {
40
38
  throw new TypeError(msg);
41
39
  }
42
40
  }
@@ -500,4 +498,4 @@ function* tupleKeys(tuple) {
500
498
 
501
499
  const ReadonlyMap = Map;
502
500
 
503
- export { HOUR_IN_MILLISECONDS, MINUTE_IN_MILLISECONDS, ReadonlyMap, ReadonlySet, SECOND_IN_MILLISECONDS, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveInPlace, assertDefined, assertNotNull, assertObject, assertType, capitalizeFirstLetter, clamp, combineSets, copySet, eRange, emptyArray, escapeHTMLCharacters, filterMap, getElapsedSeconds, getEnumEntries, getEnumKeys, getEnumValues, getNumConsecutiveDiacritics, getRandomArrayElement, getRandomArrayIndex, getRandomInt, hasDiacritic, hasEmoji, hasWhitespace, iRange, includes, interfaceSatisfiesEnum, isArray, isEnumValue, isFirstLetterCapitalized, isKebabCase, isKeyOf, isObject, isSemanticVersion, kebabCaseToCamelCase, mapFilter, mapFind, newArray, noop, normalizeString, objectFilter, objectKeysToReadonlySet, objectKeysToSet, objectToMap, objectToReadonlyMap, objectToReverseMap, objectToReverseReadonlyMap, objectValuesToReadonlySet, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
501
+ export { HOUR_IN_MILLISECONDS, MINUTE_IN_MILLISECONDS, ReadonlyMap, ReadonlySet, SECOND_IN_MILLISECONDS, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveInPlace, assertBoolean, assertDefined, assertNotNull, assertNumber, assertObject, assertString, capitalizeFirstLetter, clamp, combineSets, copySet, eRange, emptyArray, escapeHTMLCharacters, filterMap, getElapsedSeconds, getEnumEntries, getEnumKeys, getEnumValues, getNumConsecutiveDiacritics, getRandomArrayElement, getRandomArrayIndex, getRandomInt, hasDiacritic, hasEmoji, hasWhitespace, iRange, includes, interfaceSatisfiesEnum, isArray, isEnumValue, isFirstLetterCapitalized, isKebabCase, isKeyOf, isObject, isSemanticVersion, kebabCaseToCamelCase, mapFilter, mapFind, newArray, noop, normalizeString, objectFilter, objectKeysToReadonlySet, objectKeysToSet, objectToMap, objectToReadonlyMap, objectToReverseMap, objectToReverseReadonlyMap, objectValuesToReadonlySet, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "complete-common",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Helper functions for TypeScript projects.",
5
5
  "keywords": [],
6
6
  "homepage": "https://complete-ts.github.io/",
@@ -36,11 +36,11 @@
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "22.13.5",
39
- "complete-node": "3.0.0",
39
+ "complete-node": "3.1.6",
40
40
  "eslint-plugin-sort-exports": "0.9.1",
41
41
  "glob": "11.0.1",
42
42
  "typescript": "5.7.3",
43
- "typescript-eslint": "8.24.1",
44
- "unbuild": "3.3.1"
43
+ "typescript-eslint": "8.25.0",
44
+ "unbuild": "3.5.0"
45
45
  }
46
46
  }
@@ -6,18 +6,15 @@
6
6
 
7
7
  import { isObject } from "./types.js";
8
8
 
9
- const TYPES_TO_VALIDATION_FUNCTIONS = {
10
- string: (val: unknown): val is string => typeof val === "string",
11
- number: (val: unknown): val is number => typeof val === "number",
12
- boolean: (val: unknown): val is boolean => typeof val === "boolean",
13
- object: (val: unknown): val is object =>
14
- val !== null && typeof val === "object",
15
- array: (val: unknown): val is unknown[] => Array.isArray(val),
16
- null: (val: unknown): val is null => val === null,
17
- undefined: (val: unknown): val is undefined => val === undefined,
18
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
19
- function: (val: unknown): val is Function => typeof val === "function",
20
- } as const;
9
+ /** Helper function to throw an error if the provided value is not a boolean. */
10
+ export function assertBoolean(
11
+ value: unknown,
12
+ msg: string,
13
+ ): asserts value is boolean {
14
+ if (typeof value !== "boolean") {
15
+ throw new TypeError(msg);
16
+ }
17
+ }
21
18
 
22
19
  /**
23
20
  * Helper function to throw an error if the provided value is equal to `undefined`.
@@ -55,6 +52,16 @@ export function assertNotNull<T>(
55
52
  }
56
53
  }
57
54
 
55
+ /** Helper function to throw an error if the provided value is not a number. */
56
+ export function assertNumber(
57
+ value: unknown,
58
+ msg: string,
59
+ ): asserts value is number {
60
+ if (typeof value !== "number") {
61
+ throw new TypeError(msg);
62
+ }
63
+ }
64
+
58
65
  /**
59
66
  * Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
60
67
  * record).
@@ -73,14 +80,12 @@ export function assertObject(
73
80
  }
74
81
  }
75
82
 
76
- /** Helper function to throw an error if the provided value is not of the provided type. */
77
- export function assertType(
83
+ /** Helper function to throw an error if the provided value is not a string. */
84
+ export function assertString(
78
85
  value: unknown,
79
- type: keyof typeof TYPES_TO_VALIDATION_FUNCTIONS,
80
86
  msg: string,
81
- ): asserts value is unknown {
82
- const validationFunction = TYPES_TO_VALIDATION_FUNCTIONS[type];
83
- if (!validationFunction(value)) {
87
+ ): asserts value is string {
88
+ if (typeof value !== "string") {
84
89
  throw new TypeError(msg);
85
90
  }
86
91
  }
@@ -199,9 +199,9 @@ export function parseSemanticVersion(versionString: string):
199
199
  const patchVersion = parseIntSafe(patch);
200
200
 
201
201
  if (
202
- majorVersion === undefined ||
203
- minorVersion === undefined ||
204
- patchVersion === undefined
202
+ majorVersion === undefined
203
+ || minorVersion === undefined
204
+ || patchVersion === undefined
205
205
  ) {
206
206
  return undefined;
207
207
  }
@@ -14,8 +14,8 @@ export function isObject(
14
14
  variable: unknown,
15
15
  ): variable is Record<string, unknown> {
16
16
  return (
17
- typeof variable === "object" &&
18
- variable !== null &&
19
- !Array.isArray(variable)
17
+ typeof variable === "object"
18
+ && variable !== null
19
+ && !Array.isArray(variable)
20
20
  );
21
21
  }