@hichchi/utils 0.0.1-alpha.1 → 0.0.1-beta.1
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 +9 -1
- package/README.md +6236 -1032
- package/constants/constants.d.ts +223 -0
- package/constants/constants.js +227 -0
- package/constants/constants.js.map +1 -0
- package/constants/english-inflection-rules.d.ts +53 -0
- package/constants/english-inflection-rules.js +360 -0
- package/constants/english-inflection-rules.js.map +1 -0
- package/constants/index.d.ts +2 -0
- package/constants/index.js +6 -0
- package/constants/index.js.map +1 -0
- package/enums/index.d.ts +1 -0
- package/{src → enums}/index.js +1 -1
- package/enums/index.js.map +1 -0
- package/enums/template-tag.enum.d.ts +33 -0
- package/enums/template-tag.enum.js +39 -0
- package/enums/template-tag.enum.js.map +1 -0
- package/index.d.ts +5 -0
- package/index.js +9 -0
- package/index.js.map +1 -0
- package/interfaces/index.d.ts +3 -0
- package/interfaces/index.js +7 -0
- package/interfaces/index.js.map +1 -0
- package/interfaces/infinite-object.interface.d.ts +45 -0
- package/interfaces/infinite-object.interface.js +3 -0
- package/interfaces/infinite-object.interface.js.map +1 -0
- package/interfaces/inflection-rule.interfaces.d.ts +142 -0
- package/interfaces/inflection-rule.interfaces.js +3 -0
- package/interfaces/inflection-rule.interfaces.js.map +1 -0
- package/interfaces/path-value-set.interface.d.ts +79 -0
- package/interfaces/path-value-set.interface.js +3 -0
- package/interfaces/path-value-set.interface.js.map +1 -0
- package/package.json +4 -4
- package/readme-top.md +182 -6
- package/types/deep-partial.type.d.ts +47 -0
- package/types/deep-partial.type.js +3 -0
- package/types/deep-partial.type.js.map +1 -0
- package/types/index.d.ts +34 -0
- package/types/index.js +38 -0
- package/types/index.js.map +1 -0
- package/types/is-already-in-path.type.d.ts +24 -0
- package/types/is-already-in-path.type.js +3 -0
- package/types/is-already-in-path.type.js.map +1 -0
- package/types/is-empty.type.d.ts +25 -0
- package/types/is-empty.type.js +3 -0
- package/types/is-empty.type.js.map +1 -0
- package/types/is-primitive.type.d.ts +27 -0
- package/types/is-primitive.type.js +4 -0
- package/types/is-primitive.type.js.map +1 -0
- package/types/literal-object.type.d.ts +49 -0
- package/types/literal-object.type.js +3 -0
- package/types/literal-object.type.js.map +1 -0
- package/{src/lib/types.d.ts → types/loose-autocomplete.type.d.ts} +18 -6
- package/types/loose-autocomplete.type.js +3 -0
- package/types/loose-autocomplete.type.js.map +1 -0
- package/types/partial-with-null.type.d.ts +27 -0
- package/types/partial-with-null.type.js +3 -0
- package/types/partial-with-null.type.js.map +1 -0
- package/types/prettify.type.d.ts +23 -0
- package/types/prettify.type.js +3 -0
- package/types/prettify.type.js.map +1 -0
- package/types/type.type.d.ts +21 -0
- package/{src/lib/interfaces.js → types/type.type.js} +1 -1
- package/types/type.type.js.map +1 -0
- package/utils/assertions.utils.d.ts +150 -0
- package/utils/assertions.utils.js +163 -0
- package/utils/assertions.utils.js.map +1 -0
- package/utils/file.utils.d.ts +87 -0
- package/{src/lib → utils}/file.utils.js +78 -26
- package/utils/file.utils.js.map +1 -0
- package/{src/lib → utils}/index.d.ts +3 -5
- package/{src/lib → utils}/index.js +3 -5
- package/utils/index.js.map +1 -0
- package/utils/object.utils.d.ts +912 -0
- package/utils/object.utils.js +1069 -0
- package/utils/object.utils.js.map +1 -0
- package/utils/string-template.utils.d.ts +169 -0
- package/utils/string-template.utils.js +269 -0
- package/utils/string-template.utils.js.map +1 -0
- package/utils/string.utils.d.ts +932 -0
- package/utils/string.utils.js +1255 -0
- package/utils/string.utils.js.map +1 -0
- package/utils/url.utils.d.ts +99 -0
- package/utils/url.utils.js +112 -0
- package/utils/url.utils.js.map +1 -0
- package/src/index.d.ts +0 -1
- package/src/index.js.map +0 -1
- package/src/lib/assertions.utils.d.ts +0 -55
- package/src/lib/assertions.utils.js +0 -68
- package/src/lib/assertions.utils.js.map +0 -1
- package/src/lib/file.utils.d.ts +0 -25
- package/src/lib/file.utils.js.map +0 -1
- package/src/lib/index.js.map +0 -1
- package/src/lib/interfaces.d.ts +0 -6
- package/src/lib/interfaces.js.map +0 -1
- package/src/lib/object.utils.d.ts +0 -265
- package/src/lib/object.utils.js +0 -414
- package/src/lib/object.utils.js.map +0 -1
- package/src/lib/string-template.utils.d.ts +0 -25
- package/src/lib/string-template.utils.js +0 -40
- package/src/lib/string-template.utils.js.map +0 -1
- package/src/lib/string.utils.d.ts +0 -217
- package/src/lib/string.utils.js +0 -314
- package/src/lib/string.utils.js.map +0 -1
- package/src/lib/types.js +0 -4
- package/src/lib/types.js.map +0 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type Primitive = string | number | boolean | symbol | bigint | null | undefined;
|
|
2
|
+
export type NonNullPrimitive = string | number | boolean | symbol | bigint;
|
|
3
|
+
/**
|
|
4
|
+
* Type predicate that determines if a type is a primitive JavaScript value.
|
|
5
|
+
*
|
|
6
|
+
* The `IsPrimitive` type evaluates to `true` if the provided type T is a primitive
|
|
7
|
+
* JavaScript value (string, number, boolean, symbol, bigint, null, or undefined),
|
|
8
|
+
* and `false` otherwise (objects, arrays, functions, etc.).
|
|
9
|
+
*
|
|
10
|
+
* This utility type is useful for creating conditional types that need to
|
|
11
|
+
* behave differently for primitive vs. non-primitive values.
|
|
12
|
+
*
|
|
13
|
+
* @template T The type to check
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Example with conditional types
|
|
18
|
+
* type SerializeValue<T> = IsPrimitive<T> extends true
|
|
19
|
+
* ? T // For primitives, keep as-is
|
|
20
|
+
* : string; // For non-primitives, convert to string
|
|
21
|
+
*
|
|
22
|
+
* // Usage
|
|
23
|
+
* type SerializedNumber = SerializeValue<number>; // number
|
|
24
|
+
* type SerializedObject = SerializeValue<{foo: string}>; // string
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export type IsPrimitive<T> = T extends Primitive ? true : false;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-primitive.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/is-primitive.type.ts"],"names":[],"mappings":";AAAA,qCAAqC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic interface for objects with string keys and values of a specified type.
|
|
3
|
+
*
|
|
4
|
+
* `LiteralObject` represents a simple key-value dictionary where all keys are strings
|
|
5
|
+
* and all values are of the same type T. It provides a type-safe way to work with
|
|
6
|
+
* dynamic objects that have arbitrary string keys.
|
|
7
|
+
*
|
|
8
|
+
* Key features:
|
|
9
|
+
* - Allows any string as a key using an index signature
|
|
10
|
+
* - Generic type parameter for values with `any` as the default
|
|
11
|
+
* - Useful for representing dynamic objects with unknown structure
|
|
12
|
+
* - Commonly used in object transformation and manipulation utilities
|
|
13
|
+
*
|
|
14
|
+
* Common use cases:
|
|
15
|
+
* - Working with JSON data
|
|
16
|
+
* - Creating dictionaries or maps with string keys
|
|
17
|
+
* - Representing configuration objects
|
|
18
|
+
* - Handling data with dynamic property names
|
|
19
|
+
*
|
|
20
|
+
* @template T The type of values in the object (defaults to `any`)
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Basic usage with default any type
|
|
25
|
+
* const config: LiteralObject = {
|
|
26
|
+
* apiUrl: 'https://api.example.com',
|
|
27
|
+
* timeout: 5000,
|
|
28
|
+
* retryCount: 3
|
|
29
|
+
* };
|
|
30
|
+
*
|
|
31
|
+
* // With a specific value type
|
|
32
|
+
* const stringMap: LiteralObject<string> = {
|
|
33
|
+
* en: 'Hello',
|
|
34
|
+
* fr: 'Bonjour',
|
|
35
|
+
* es: 'Hola'
|
|
36
|
+
* };
|
|
37
|
+
*
|
|
38
|
+
* // Accessing properties dynamically
|
|
39
|
+
* function getValue(obj: LiteralObject, key: string): any {
|
|
40
|
+
* return obj[key];
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @see {@link objectToPathValueSet} Utility that uses LiteralObject for object flattening
|
|
45
|
+
* @see {@link PathValueSet} Related interface for flattened object representation
|
|
46
|
+
*/
|
|
47
|
+
export interface LiteralObject<T = any> {
|
|
48
|
+
[key: string]: T;
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal-object.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/literal-object.type.ts"],"names":[],"mappings":""}
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
export type LiteralObject<T = any> = {
|
|
2
|
-
[key: string]: T;
|
|
3
|
-
};
|
|
4
|
-
export type PartialWithNull<T> = {
|
|
5
|
-
[p in keyof T]?: T[p] | null;
|
|
6
|
-
};
|
|
7
1
|
/**
|
|
8
2
|
* Represents a type for autocompleting string values.
|
|
9
3
|
*
|
|
@@ -15,6 +9,24 @@ export type PartialWithNull<T> = {
|
|
|
15
9
|
*
|
|
16
10
|
* @template T Extends string - The set of predefined literal string options for autocompletion.
|
|
17
11
|
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Define a set of color options
|
|
15
|
+
* type ColorOption = 'red' | 'green' | 'blue';
|
|
16
|
+
*
|
|
17
|
+
* // Function that accepts predefined colors but also allows custom colors
|
|
18
|
+
* function setColor(color: LooseAutocomplete<ColorOption>): void {
|
|
19
|
+
* console.log(`Setting color to: ${color}`);
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* // Usage with predefined options (gets autocomplete)
|
|
23
|
+
* setColor('red'); // Works, with autocomplete
|
|
24
|
+
* setColor('green'); // Works, with autocomplete
|
|
25
|
+
*
|
|
26
|
+
* // Usage with custom string (allowed, but no autocomplete)
|
|
27
|
+
* setColor('purple'); // Also works, even though not in predefined options
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
18
30
|
* @author Matt Pocock (https://www.totaltypescript.com/tips/create-autocomplete-helper-which-allows-for-arbitrary-values)
|
|
19
31
|
*/
|
|
20
32
|
export type LooseAutocomplete<T extends string> = T | Omit<string, T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loose-autocomplete.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/loose-autocomplete.type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes all properties of type T optional and allows them to be null.
|
|
3
|
+
*
|
|
4
|
+
* This type is similar to TypeScript's built-in `Partial<T>` type, but it also
|
|
5
|
+
* allows properties to be null in addition to being undefined or their original type.
|
|
6
|
+
*
|
|
7
|
+
* @template T The type to make partial with null.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```TypeScript
|
|
11
|
+
* interface User {
|
|
12
|
+
* id: number;
|
|
13
|
+
* name: string;
|
|
14
|
+
* email: string;
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* // All properties are optional and can be null
|
|
18
|
+
* const partialUser: PartialWithNull<User> = {
|
|
19
|
+
* id: 1,
|
|
20
|
+
* name: null,
|
|
21
|
+
* // email is omitted (undefined)
|
|
22
|
+
* };
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type PartialWithNull<T> = {
|
|
26
|
+
[p in keyof T]?: T[p] | null;
|
|
27
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"partial-with-null.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/partial-with-null.type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility type for creating a clean object type from a complex type.
|
|
3
|
+
*
|
|
4
|
+
* The `Prettify` type takes a potentially complex type (with intersections, unions,
|
|
5
|
+
* mapped types, etc.) and converts it into a simple object type with all properties
|
|
6
|
+
* explicitly defined. This makes the resulting type easier to read in IDE tooltips
|
|
7
|
+
* and type errors.
|
|
8
|
+
*
|
|
9
|
+
* @template T The complex type to simplify
|
|
10
|
+
*
|
|
11
|
+
* @author Matt Pocock (https://www.totaltypescript.com/tips/create-autocomplete-helper-which-allows-for-arbitrary-values)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* // Instead of seeing: { a: number } & { b: string }
|
|
16
|
+
* // You'll see: { a: number, b: string }
|
|
17
|
+
* type ComplexType = { a: number } & { b: string };
|
|
18
|
+
* type SimpleType = Prettify<ComplexType>;
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export type Prettify<T> = {
|
|
22
|
+
[K in keyof T]: T[K];
|
|
23
|
+
} & {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prettify.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/prettify.type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic constructor type for creating class instances.
|
|
3
|
+
*
|
|
4
|
+
* The `Type` type represents a constructor function that can create instances of a class.
|
|
5
|
+
* It's particularly useful when working with dependency injection systems, factories,
|
|
6
|
+
* or any code that needs to work with class constructors in a generic way.
|
|
7
|
+
*
|
|
8
|
+
* @template T The type of object that will be instantiated (defaults to unknown)
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* function createInstance<T>(ctor: Type<T>): T {
|
|
13
|
+
* return new ctor();
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* // Usage
|
|
17
|
+
* class MyService {}
|
|
18
|
+
* const instance = createInstance(MyService);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export type Type<T = unknown> = new (...args: unknown[]) => T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.type.js","sourceRoot":"","sources":["../../../../libs/utils/src/types/type.type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe utility to check if a value is an array of a specific type.
|
|
3
|
+
*
|
|
4
|
+
* This function acts as a type guard that not only checks if the provided value
|
|
5
|
+
* is an array (using the native Array.isArray method), but also narrows the TypeScript
|
|
6
|
+
* type to an array of the generic type T. This enables safer handling of potentially
|
|
7
|
+
* array-typed values with full type inference in the conditional branches.
|
|
8
|
+
*
|
|
9
|
+
* @template T The expected element type of the array
|
|
10
|
+
* @param {T | T[] | undefined} value The value to check, which could be a single item,
|
|
11
|
+
* an array of items, or undefined
|
|
12
|
+
* @returns {value is T[]} Type predicate that narrows the type to T[] when true
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* While this function essentially wraps Array.isArray, its value comes from the
|
|
16
|
+
* TypeScript type narrowing it provides, making it especially useful in code that
|
|
17
|
+
* needs to handle both single items and collections of items with type safety.
|
|
18
|
+
*
|
|
19
|
+
* The function properly handles undefined values by returning false, making it safe
|
|
20
|
+
* to use with optional parameters or potentially undefined values.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Function that can accept either a single user or multiple users
|
|
25
|
+
* async function createUser(userOrUsers: UserDto | UserDto[] | undefined): Promise<User | User[]> {
|
|
26
|
+
* // TypeScript knows userOrUsers is UserDto[] in this branch
|
|
27
|
+
* if (isArray<UserDto>(userOrUsers)) {
|
|
28
|
+
* return Promise.all(userOrUsers.map(user => userService.createUser(user)));
|
|
29
|
+
* }
|
|
30
|
+
* // TypeScript knows userOrUsers is UserDto or undefined in this branch
|
|
31
|
+
* else if (userOrUsers) {
|
|
32
|
+
* return userService.createUser(userOrUsers);
|
|
33
|
+
* }
|
|
34
|
+
* else {
|
|
35
|
+
* throw new BadRequestException('User data is required');
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function isArray<T>(value: T | T[] | undefined): value is T[];
|
|
41
|
+
/**
|
|
42
|
+
* Type-safe utility to check if a value is a non-array object of a specific type.
|
|
43
|
+
*
|
|
44
|
+
* This function serves as a type guard that checks if the provided value is an object
|
|
45
|
+
* (but not an array) and narrows the TypeScript type to the generic type T. It properly
|
|
46
|
+
* excludes null, arrays, and primitive values, focusing only on object instances.
|
|
47
|
+
*
|
|
48
|
+
* @template T The expected object type to narrow to when the check passes
|
|
49
|
+
* @param {T | T[] | undefined} [value] The value to check, which could be a single object,
|
|
50
|
+
* an array of objects, or undefined
|
|
51
|
+
* @returns {value is T} Type predicate that narrows the type to T when true
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* This function performs three checks to ensure the value is a proper object:
|
|
55
|
+
* 1. It's not an array (using Array.isArray)
|
|
56
|
+
* 2. It's of type "object" according to JavaScript's typeof operator
|
|
57
|
+
* 3. It's not null (which would pass the typeof check but isn't a valid object)
|
|
58
|
+
*
|
|
59
|
+
* This is particularly useful when handling parameters that could be either an object
|
|
60
|
+
* or a simpler identifier (like an ID), allowing for type-safe conditional logic.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // Function that can accept either a user ID or a user object
|
|
65
|
+
* async function getUserInfo(userIdOrUser: number | User | undefined): Promise<UserInfo> {
|
|
66
|
+
* // TypeScript knows userIdOrUser is User in this branch
|
|
67
|
+
* if (isObject<User>(userIdOrUser)) {
|
|
68
|
+
* // Can safely access object properties
|
|
69
|
+
* return await userService.getUserInfo(userIdOrUser.id);
|
|
70
|
+
* }
|
|
71
|
+
* // TypeScript knows userIdOrUser is number or undefined in this branch
|
|
72
|
+
* else {
|
|
73
|
+
* return await userService.getUserInfo(userIdOrUser);
|
|
74
|
+
* }
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* // Works with optional parameters too
|
|
78
|
+
* function formatUserName(user?: User | string): string {
|
|
79
|
+
* if (isObject<User>(user)) {
|
|
80
|
+
* return `${user.firstName} ${user.lastName}`;
|
|
81
|
+
* }
|
|
82
|
+
* return user || 'Guest';
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function isObject<T>(value?: T | T[] | undefined): value is T;
|
|
87
|
+
/**
|
|
88
|
+
* Type-safe utility to check if a value is an object with a specific property.
|
|
89
|
+
*
|
|
90
|
+
* This function acts as a type guard that determines if an unknown value is an object
|
|
91
|
+
* that contains a specified property, and narrows the TypeScript type to the generic
|
|
92
|
+
* type T when true. This provides a robust way to implement duck typing in TypeScript,
|
|
93
|
+
* allowing for safe property access in the conditional branches.
|
|
94
|
+
*
|
|
95
|
+
* @template T The expected object type that should contain the property
|
|
96
|
+
* @param {unknown} value Any value to check, with no type constraints
|
|
97
|
+
* @param {keyof T} propertyName The name of the property that should exist on the object
|
|
98
|
+
* @returns {value is T} Type predicate that narrows the type to T when true
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* This function uses Object.prototype.hasOwnProperty.call to safely check for property
|
|
102
|
+
* existence, even if the object has a custom implementation of hasOwnProperty or if the
|
|
103
|
+
* property is named 'hasOwnProperty'.
|
|
104
|
+
*
|
|
105
|
+
* Unlike the simpler isObject utility, this function can distinguish between different
|
|
106
|
+
* object types based on their properties, making it ideal for discriminating between
|
|
107
|
+
* different interface implementations or handling union types.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // Check if an object has the properties of a User interface
|
|
112
|
+
* interface User {
|
|
113
|
+
* id: number;
|
|
114
|
+
* name: string;
|
|
115
|
+
* }
|
|
116
|
+
*
|
|
117
|
+
* interface Order {
|
|
118
|
+
* orderNumber: string;
|
|
119
|
+
* items: string[];
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* // Function that can handle different object types
|
|
123
|
+
* function processEntity(entity: unknown): void {
|
|
124
|
+
* // Check if entity has 'id' property, indicating it's a User
|
|
125
|
+
* if (isObjectWith<User>(entity, 'id')) {
|
|
126
|
+
* // TypeScript knows entity is User in this branch
|
|
127
|
+
* console.log(`Processing user: ${entity.name}`);
|
|
128
|
+
* }
|
|
129
|
+
* // Check if entity has 'orderNumber' property, indicating it's an Order
|
|
130
|
+
* else if (isObjectWith<Order>(entity, 'orderNumber')) {
|
|
131
|
+
* // TypeScript knows entity is Order in this branch
|
|
132
|
+
* console.log(`Processing order: ${entity.orderNumber} with ${entity.items.length} items`);
|
|
133
|
+
* }
|
|
134
|
+
* else {
|
|
135
|
+
* console.log('Unknown entity type');
|
|
136
|
+
* }
|
|
137
|
+
* }
|
|
138
|
+
*
|
|
139
|
+
* // Useful for API responses where types may vary
|
|
140
|
+
* async function handleResponse(response: unknown): Promise<void> {
|
|
141
|
+
* if (isObjectWith<ErrorResponse>(response, 'errorCode')) {
|
|
142
|
+
* throw new ApiException(response.errorCode, response.message);
|
|
143
|
+
* }
|
|
144
|
+
* else if (isObjectWith<SuccessResponse>(response, 'data')) {
|
|
145
|
+
* await processData(response.data);
|
|
146
|
+
* }
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export declare function isObjectWith<T extends object>(value: unknown, propertyName: keyof T): value is T;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// noinspection JSUnusedGlobalSymbols
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isArray = isArray;
|
|
5
|
+
exports.isObject = isObject;
|
|
6
|
+
exports.isObjectWith = isObjectWith;
|
|
7
|
+
/**
|
|
8
|
+
* Type-safe utility to check if a value is an array of a specific type.
|
|
9
|
+
*
|
|
10
|
+
* This function acts as a type guard that not only checks if the provided value
|
|
11
|
+
* is an array (using the native Array.isArray method), but also narrows the TypeScript
|
|
12
|
+
* type to an array of the generic type T. This enables safer handling of potentially
|
|
13
|
+
* array-typed values with full type inference in the conditional branches.
|
|
14
|
+
*
|
|
15
|
+
* @template T The expected element type of the array
|
|
16
|
+
* @param {T | T[] | undefined} value The value to check, which could be a single item,
|
|
17
|
+
* an array of items, or undefined
|
|
18
|
+
* @returns {value is T[]} Type predicate that narrows the type to T[] when true
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* While this function essentially wraps Array.isArray, its value comes from the
|
|
22
|
+
* TypeScript type narrowing it provides, making it especially useful in code that
|
|
23
|
+
* needs to handle both single items and collections of items with type safety.
|
|
24
|
+
*
|
|
25
|
+
* The function properly handles undefined values by returning false, making it safe
|
|
26
|
+
* to use with optional parameters or potentially undefined values.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // Function that can accept either a single user or multiple users
|
|
31
|
+
* async function createUser(userOrUsers: UserDto | UserDto[] | undefined): Promise<User | User[]> {
|
|
32
|
+
* // TypeScript knows userOrUsers is UserDto[] in this branch
|
|
33
|
+
* if (isArray<UserDto>(userOrUsers)) {
|
|
34
|
+
* return Promise.all(userOrUsers.map(user => userService.createUser(user)));
|
|
35
|
+
* }
|
|
36
|
+
* // TypeScript knows userOrUsers is UserDto or undefined in this branch
|
|
37
|
+
* else if (userOrUsers) {
|
|
38
|
+
* return userService.createUser(userOrUsers);
|
|
39
|
+
* }
|
|
40
|
+
* else {
|
|
41
|
+
* throw new BadRequestException('User data is required');
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
function isArray(value) {
|
|
47
|
+
return Array.isArray(value);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Type-safe utility to check if a value is a non-array object of a specific type.
|
|
51
|
+
*
|
|
52
|
+
* This function serves as a type guard that checks if the provided value is an object
|
|
53
|
+
* (but not an array) and narrows the TypeScript type to the generic type T. It properly
|
|
54
|
+
* excludes null, arrays, and primitive values, focusing only on object instances.
|
|
55
|
+
*
|
|
56
|
+
* @template T The expected object type to narrow to when the check passes
|
|
57
|
+
* @param {T | T[] | undefined} [value] The value to check, which could be a single object,
|
|
58
|
+
* an array of objects, or undefined
|
|
59
|
+
* @returns {value is T} Type predicate that narrows the type to T when true
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* This function performs three checks to ensure the value is a proper object:
|
|
63
|
+
* 1. It's not an array (using Array.isArray)
|
|
64
|
+
* 2. It's of type "object" according to JavaScript's typeof operator
|
|
65
|
+
* 3. It's not null (which would pass the typeof check but isn't a valid object)
|
|
66
|
+
*
|
|
67
|
+
* This is particularly useful when handling parameters that could be either an object
|
|
68
|
+
* or a simpler identifier (like an ID), allowing for type-safe conditional logic.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* // Function that can accept either a user ID or a user object
|
|
73
|
+
* async function getUserInfo(userIdOrUser: number | User | undefined): Promise<UserInfo> {
|
|
74
|
+
* // TypeScript knows userIdOrUser is User in this branch
|
|
75
|
+
* if (isObject<User>(userIdOrUser)) {
|
|
76
|
+
* // Can safely access object properties
|
|
77
|
+
* return await userService.getUserInfo(userIdOrUser.id);
|
|
78
|
+
* }
|
|
79
|
+
* // TypeScript knows userIdOrUser is number or undefined in this branch
|
|
80
|
+
* else {
|
|
81
|
+
* return await userService.getUserInfo(userIdOrUser);
|
|
82
|
+
* }
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* // Works with optional parameters too
|
|
86
|
+
* function formatUserName(user?: User | string): string {
|
|
87
|
+
* if (isObject<User>(user)) {
|
|
88
|
+
* return `${user.firstName} ${user.lastName}`;
|
|
89
|
+
* }
|
|
90
|
+
* return user || 'Guest';
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
function isObject(value) {
|
|
95
|
+
return !Array.isArray(value) && typeof value === "object" && value !== null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Type-safe utility to check if a value is an object with a specific property.
|
|
99
|
+
*
|
|
100
|
+
* This function acts as a type guard that determines if an unknown value is an object
|
|
101
|
+
* that contains a specified property, and narrows the TypeScript type to the generic
|
|
102
|
+
* type T when true. This provides a robust way to implement duck typing in TypeScript,
|
|
103
|
+
* allowing for safe property access in the conditional branches.
|
|
104
|
+
*
|
|
105
|
+
* @template T The expected object type that should contain the property
|
|
106
|
+
* @param {unknown} value Any value to check, with no type constraints
|
|
107
|
+
* @param {keyof T} propertyName The name of the property that should exist on the object
|
|
108
|
+
* @returns {value is T} Type predicate that narrows the type to T when true
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* This function uses Object.prototype.hasOwnProperty.call to safely check for property
|
|
112
|
+
* existence, even if the object has a custom implementation of hasOwnProperty or if the
|
|
113
|
+
* property is named 'hasOwnProperty'.
|
|
114
|
+
*
|
|
115
|
+
* Unlike the simpler isObject utility, this function can distinguish between different
|
|
116
|
+
* object types based on their properties, making it ideal for discriminating between
|
|
117
|
+
* different interface implementations or handling union types.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // Check if an object has the properties of a User interface
|
|
122
|
+
* interface User {
|
|
123
|
+
* id: number;
|
|
124
|
+
* name: string;
|
|
125
|
+
* }
|
|
126
|
+
*
|
|
127
|
+
* interface Order {
|
|
128
|
+
* orderNumber: string;
|
|
129
|
+
* items: string[];
|
|
130
|
+
* }
|
|
131
|
+
*
|
|
132
|
+
* // Function that can handle different object types
|
|
133
|
+
* function processEntity(entity: unknown): void {
|
|
134
|
+
* // Check if entity has 'id' property, indicating it's a User
|
|
135
|
+
* if (isObjectWith<User>(entity, 'id')) {
|
|
136
|
+
* // TypeScript knows entity is User in this branch
|
|
137
|
+
* console.log(`Processing user: ${entity.name}`);
|
|
138
|
+
* }
|
|
139
|
+
* // Check if entity has 'orderNumber' property, indicating it's an Order
|
|
140
|
+
* else if (isObjectWith<Order>(entity, 'orderNumber')) {
|
|
141
|
+
* // TypeScript knows entity is Order in this branch
|
|
142
|
+
* console.log(`Processing order: ${entity.orderNumber} with ${entity.items.length} items`);
|
|
143
|
+
* }
|
|
144
|
+
* else {
|
|
145
|
+
* console.log('Unknown entity type');
|
|
146
|
+
* }
|
|
147
|
+
* }
|
|
148
|
+
*
|
|
149
|
+
* // Useful for API responses where types may vary
|
|
150
|
+
* async function handleResponse(response: unknown): Promise<void> {
|
|
151
|
+
* if (isObjectWith<ErrorResponse>(response, 'errorCode')) {
|
|
152
|
+
* throw new ApiException(response.errorCode, response.message);
|
|
153
|
+
* }
|
|
154
|
+
* else if (isObjectWith<SuccessResponse>(response, 'data')) {
|
|
155
|
+
* await processData(response.data);
|
|
156
|
+
* }
|
|
157
|
+
* }
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
function isObjectWith(value, propertyName) {
|
|
161
|
+
return Object.prototype.hasOwnProperty.call(value, propertyName);
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=assertions.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assertions.utils.js","sourceRoot":"","sources":["../../../../libs/utils/src/utils/assertions.utils.ts"],"names":[],"mappings":";AAAA,qCAAqC;;AAyCrC,0BAEC;AA+CD,4BAEC;AAiED,oCAEC;AA7JD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,SAAgB,OAAO,CAAI,KAA0B;IACjD,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,SAAgB,QAAQ,CAAI,KAA2B;IACnD,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC;AAChF,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8DG;AACH,SAAgB,YAAY,CAAmB,KAAc,EAAE,YAAqB;IAChF,OAAO,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;AACrE,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comprehensive mapping of file extensions to their corresponding MIME types.
|
|
3
|
+
*
|
|
4
|
+
* This map provides a bidirectional mapping between file extensions and MIME types,
|
|
5
|
+
* enabling applications to determine the appropriate content type for files or
|
|
6
|
+
* to identify the likely file extension for a given MIME type.
|
|
7
|
+
*
|
|
8
|
+
* The map includes entries for common file formats across various categories:
|
|
9
|
+
* - Documents (PDF, DOC, DOCX, XLS, XLSX, etc.)
|
|
10
|
+
* - Images (JPG, PNG, GIF, SVG, WebP, etc.)
|
|
11
|
+
* - Audio (MP3, WAV, OGG, etc.)
|
|
12
|
+
* - Video (MP4, WebM, AVI, etc.)
|
|
13
|
+
* - Archives (ZIP, RAR, 7Z, etc.)
|
|
14
|
+
* - Web resources (HTML, CSS, JS, etc.)
|
|
15
|
+
* - And many more specialized formats
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* The map is indexed by file extension (without the dot prefix) and contains
|
|
19
|
+
* the corresponding MIME type as the value. Use utility functions like
|
|
20
|
+
* getMimeTypeFromExtension and getExtensionFromMimeType to perform lookups
|
|
21
|
+
* rather than accessing this map directly.
|
|
22
|
+
*
|
|
23
|
+
* Extensions are stored in lowercase to ensure case-insensitive matching.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // Get MIME type for a file extension
|
|
28
|
+
* const mimeType = getMimeTypeFromExtension('pdf'); // 'application/pdf'
|
|
29
|
+
*
|
|
30
|
+
* // Get file extension for a MIME type
|
|
31
|
+
* const extension = getExtensionFromMimeType('image/jpeg'); // 'jpg'
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare const mimeTypes: Map<string, string>;
|
|
35
|
+
/**
|
|
36
|
+
* Get the file extension of the given mime type.
|
|
37
|
+
* @param {string} mimeType - Mime type.
|
|
38
|
+
* @param {Map<string, string>} [allowedMimeTypes] - Allowed mime types. If not provided, the default mime types map will be used.
|
|
39
|
+
* @returns {string | undefined} File extension or undefined if the mime type is not found.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```TypeScript
|
|
43
|
+
* // Using the default mime types map
|
|
44
|
+
* const extension = getFileExt('application/pdf');
|
|
45
|
+
* // Output: 'pdf'
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```TypeScript
|
|
50
|
+
* // Using a custom mime types map
|
|
51
|
+
* const customMimeTypes = new Map([
|
|
52
|
+
* ['jpg', 'image/jpeg'],
|
|
53
|
+
* ['png', 'image/png']
|
|
54
|
+
* ]);
|
|
55
|
+
* const extension = getFileExt('image/jpeg', customMimeTypes);
|
|
56
|
+
* // Output: 'jpg'
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare const getFileExt: (mimeType: string, allowedMimeTypes?: Map<string, string>) => string | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Get file size in human-readable format.
|
|
62
|
+
* @param {number} size - File size in bytes.
|
|
63
|
+
* @param {boolean} [round] - Whether to round the size to whole numbers. If true, decimals will be removed.
|
|
64
|
+
* @returns {string} File size in human-readable format (B, KB, MB, or GB).
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```TypeScript
|
|
68
|
+
* // Get file size with decimals
|
|
69
|
+
* const readableSize = getFileSize(1536);
|
|
70
|
+
* // Output: '1.50 KB'
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```TypeScript
|
|
75
|
+
* // Get rounded file size
|
|
76
|
+
* const roundedSize = getFileSize(1536, true);
|
|
77
|
+
* // Output: '2 KB'
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```TypeScript
|
|
82
|
+
* // Get size for larger files
|
|
83
|
+
* const largeFileSize = getFileSize(1073741824);
|
|
84
|
+
* // Output: '1.00 GB'
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare const getFileSize: (size: number, round?: boolean) => string;
|