@forklaunch/common 0.2.6 → 0.2.7

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/lib/index.d.mts CHANGED
@@ -1,3 +1,23 @@
1
+ /**
2
+ * Extracts the names of arguments from a function's string representation.
3
+ * This is useful for reflection and debugging purposes.
4
+ *
5
+ * @param {Object} func - A function or object with a toString method that returns the function definition
6
+ * @returns {string[]} An array of argument names
7
+ * @example
8
+ * function example(a, b, { c, d }) {}
9
+ * const names = extractArgumentNames(example);
10
+ * // Result: ['a', 'b', '{c,d}']
11
+ */
12
+ declare function extractArgumentNames(func: {
13
+ toString(): string;
14
+ }): string[];
15
+
16
+ /**
17
+ * Type guard that checks if a value is of type never
18
+ * @param value - The value to check
19
+ * @returns Always returns true since this is a type guard for the never type
20
+ */
1
21
  declare function isNever(value: never): value is never;
2
22
 
3
23
  /**
@@ -8,21 +28,100 @@ declare function isNever(value: never): value is never;
8
28
  */
9
29
  declare function isRecord(obj: unknown): obj is Record<string, unknown>;
10
30
 
31
+ /**
32
+ * Type guard that checks if a value is exactly true.
33
+ * This is useful for narrowing boolean types to the literal true value.
34
+ *
35
+ * @param {true} value - The value to check
36
+ * @returns {boolean} Always returns true since the parameter is already typed as true
37
+ * @example
38
+ * const value: boolean = true;
39
+ * if (isTrue(value)) {
40
+ * // value is now typed as true (not just boolean)
41
+ * }
42
+ */
11
43
  declare function isTrue(value: true): true;
12
44
 
45
+ /**
46
+ * A no-operation function that does nothing when called.
47
+ * This is commonly used as a default or placeholder function
48
+ * when a function parameter is optional but needs a default value.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * function withCallback(callback = noop) {
53
+ * // ... do something
54
+ * callback();
55
+ * }
56
+ * ```
57
+ */
58
+ declare function noop(): void;
59
+
60
+ /**
61
+ * Recursively sorts the keys of an object and its nested objects alphabetically.
62
+ * This is useful for consistent object serialization and comparison.
63
+ *
64
+ * @template T - The type of the object to sort
65
+ * @param {T} obj - The object whose keys should be sorted
66
+ * @returns {T} A new object with sorted keys
67
+ * @example
68
+ * const obj = { b: 2, a: 1, c: { f: 6, e: 5 } };
69
+ * const sorted = sortObjectKeys(obj);
70
+ * // Result: { a: 1, b: 2, c: { e: 5, f: 6 } }
71
+ */
72
+ declare function sortObjectKeys<T extends Record<string, unknown>>(obj: T): T;
73
+
74
+ /**
75
+ * Removes all properties with undefined values from an object. Note: this does NOT strip null values.
76
+ * @param obj The object to strip undefined properties from
77
+ * @returns A new object with all undefined properties removed
78
+ */
79
+ declare function stripUndefinedProperties<T extends Record<string, unknown>>(obj: T): Partial<T>;
80
+
81
+ /**
82
+ * Type representing a DTO (Data Transfer Object) with an id field.
83
+ */
13
84
  type IdDto = {
14
85
  id: string;
15
86
  };
87
+ /**
88
+ * Type representing a DTO with an array of ids.
89
+ */
16
90
  type IdsDto = {
17
91
  ids: string[];
18
92
  };
93
+ /**
94
+ * Type representing a DTO with timing information (created and updated timestamps).
95
+ */
19
96
  type RecordTimingDto = {
20
97
  createdAt: Date;
21
98
  updatedAt: Date;
22
99
  };
100
+ /**
101
+ * Type that creates a record of return types from a record of functions.
102
+ *
103
+ * @template T - A record type where each value is a function
104
+ * @example
105
+ * type Functions = {
106
+ * getName: () => string;
107
+ * getAge: () => number;
108
+ * };
109
+ * type ReturnTypes = ReturnTypeRecord<Functions>; // { getName: string; getAge: number; }
110
+ */
23
111
  type ReturnTypeRecord<T extends Record<string, (...args: never[]) => unknown>> = {
24
112
  [K in keyof T]: ReturnType<T[K]>;
25
113
  };
114
+ /**
115
+ * Type that creates a record of instance types from a record of constructors.
116
+ *
117
+ * @template T - A record type where each value is a constructor
118
+ * @example
119
+ * type Constructors = {
120
+ * User: new () => User;
121
+ * Post: new () => Post;
122
+ * };
123
+ * type InstanceTypes = InstanceTypeRecord<Constructors>; // { User: User; Post: Post; }
124
+ */
26
125
  type InstanceTypeRecord<T extends Record<string, new (...args: never[]) => unknown>> = {
27
126
  [K in keyof T]: InstanceType<T[K]>;
28
127
  };
@@ -54,7 +153,28 @@ type Flatten<T> = T extends object ? {
54
153
  [K in keyof T]: Flatten<T[K]>;
55
154
  } : T;
56
155
 
156
+ /**
157
+ * Helper type that checks if all properties of a type are optional.
158
+ *
159
+ * @template T - The type to check
160
+ */
57
161
  type AllPropertiesOptional<T> = T extends Partial<T> ? (Partial<T> extends T ? true : false) : false;
162
+ /**
163
+ * Type that makes properties optional if all their children are optional.
164
+ * This is useful for creating types where properties are only optional if their nested properties are all optional.
165
+ *
166
+ * @template T - The type to transform
167
+ * @example
168
+ * type User = {
169
+ * name: string;
170
+ * address?: {
171
+ * street?: string;
172
+ * city?: string;
173
+ * };
174
+ * };
175
+ * type Transformed = MakePropertyOptionalIfChildrenOptional<User>;
176
+ * // Result: { name: string; address?: { street?: string; city?: string; } }
177
+ */
58
178
  type MakePropertyOptionalIfChildrenOptional<T> = {
59
179
  [K in keyof T as AllPropertiesOptional<T[K]> extends true ? K : never]?: T[K];
60
180
  } & {
@@ -74,19 +194,14 @@ type Prettify<T> = {
74
194
  [K in keyof T]: T[K];
75
195
  } & {};
76
196
 
77
- type RemoveTrailingSlash<T extends string> = T extends `${infer Route}/` ? Route : T;
78
-
79
- declare function extractArgumentNames(func: {
80
- toString(): string;
81
- }): string[];
82
-
83
- declare function sortObjectKeys<T extends Record<string, unknown>>(obj: T): T;
84
-
85
197
  /**
86
- * Removes all properties with undefined values from an object. Note: this does NOT strip null values.
87
- * @param obj The object to strip undefined properties from
88
- * @returns A new object with all undefined properties removed
198
+ * Type that removes a trailing slash from a string type if present.
199
+ *
200
+ * @template T - The string type to process
201
+ * @example
202
+ * type Route1 = RemoveTrailingSlash<'/users/'>; // '/users'
203
+ * type Route2 = RemoveTrailingSlash<'/users'>; // '/users'
89
204
  */
90
- declare function stripUndefinedProperties<T extends Record<string, unknown>>(obj: T): Partial<T>;
205
+ type RemoveTrailingSlash<T extends string> = T extends `${infer Route}/` ? Route : T;
91
206
 
92
- export { type Flatten, type FlattenKeys, type FlattenValues, type IdDto, type IdsDto, type InstanceTypeRecord, type MakePropertyOptionalIfChildrenOptional, type Prettify, type RecordTimingDto, type RemoveTrailingSlash, type ReturnTypeRecord, extractArgumentNames, isNever, isRecord, isTrue, sortObjectKeys, stripUndefinedProperties };
207
+ export { type Flatten, type FlattenKeys, type FlattenValues, type IdDto, type IdsDto, type InstanceTypeRecord, type MakePropertyOptionalIfChildrenOptional, type Prettify, type RecordTimingDto, type RemoveTrailingSlash, type ReturnTypeRecord, extractArgumentNames, isNever, isRecord, isTrue, noop, sortObjectKeys, stripUndefinedProperties };
package/lib/index.d.ts CHANGED
@@ -1,3 +1,23 @@
1
+ /**
2
+ * Extracts the names of arguments from a function's string representation.
3
+ * This is useful for reflection and debugging purposes.
4
+ *
5
+ * @param {Object} func - A function or object with a toString method that returns the function definition
6
+ * @returns {string[]} An array of argument names
7
+ * @example
8
+ * function example(a, b, { c, d }) {}
9
+ * const names = extractArgumentNames(example);
10
+ * // Result: ['a', 'b', '{c,d}']
11
+ */
12
+ declare function extractArgumentNames(func: {
13
+ toString(): string;
14
+ }): string[];
15
+
16
+ /**
17
+ * Type guard that checks if a value is of type never
18
+ * @param value - The value to check
19
+ * @returns Always returns true since this is a type guard for the never type
20
+ */
1
21
  declare function isNever(value: never): value is never;
2
22
 
3
23
  /**
@@ -8,21 +28,100 @@ declare function isNever(value: never): value is never;
8
28
  */
9
29
  declare function isRecord(obj: unknown): obj is Record<string, unknown>;
10
30
 
31
+ /**
32
+ * Type guard that checks if a value is exactly true.
33
+ * This is useful for narrowing boolean types to the literal true value.
34
+ *
35
+ * @param {true} value - The value to check
36
+ * @returns {boolean} Always returns true since the parameter is already typed as true
37
+ * @example
38
+ * const value: boolean = true;
39
+ * if (isTrue(value)) {
40
+ * // value is now typed as true (not just boolean)
41
+ * }
42
+ */
11
43
  declare function isTrue(value: true): true;
12
44
 
45
+ /**
46
+ * A no-operation function that does nothing when called.
47
+ * This is commonly used as a default or placeholder function
48
+ * when a function parameter is optional but needs a default value.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * function withCallback(callback = noop) {
53
+ * // ... do something
54
+ * callback();
55
+ * }
56
+ * ```
57
+ */
58
+ declare function noop(): void;
59
+
60
+ /**
61
+ * Recursively sorts the keys of an object and its nested objects alphabetically.
62
+ * This is useful for consistent object serialization and comparison.
63
+ *
64
+ * @template T - The type of the object to sort
65
+ * @param {T} obj - The object whose keys should be sorted
66
+ * @returns {T} A new object with sorted keys
67
+ * @example
68
+ * const obj = { b: 2, a: 1, c: { f: 6, e: 5 } };
69
+ * const sorted = sortObjectKeys(obj);
70
+ * // Result: { a: 1, b: 2, c: { e: 5, f: 6 } }
71
+ */
72
+ declare function sortObjectKeys<T extends Record<string, unknown>>(obj: T): T;
73
+
74
+ /**
75
+ * Removes all properties with undefined values from an object. Note: this does NOT strip null values.
76
+ * @param obj The object to strip undefined properties from
77
+ * @returns A new object with all undefined properties removed
78
+ */
79
+ declare function stripUndefinedProperties<T extends Record<string, unknown>>(obj: T): Partial<T>;
80
+
81
+ /**
82
+ * Type representing a DTO (Data Transfer Object) with an id field.
83
+ */
13
84
  type IdDto = {
14
85
  id: string;
15
86
  };
87
+ /**
88
+ * Type representing a DTO with an array of ids.
89
+ */
16
90
  type IdsDto = {
17
91
  ids: string[];
18
92
  };
93
+ /**
94
+ * Type representing a DTO with timing information (created and updated timestamps).
95
+ */
19
96
  type RecordTimingDto = {
20
97
  createdAt: Date;
21
98
  updatedAt: Date;
22
99
  };
100
+ /**
101
+ * Type that creates a record of return types from a record of functions.
102
+ *
103
+ * @template T - A record type where each value is a function
104
+ * @example
105
+ * type Functions = {
106
+ * getName: () => string;
107
+ * getAge: () => number;
108
+ * };
109
+ * type ReturnTypes = ReturnTypeRecord<Functions>; // { getName: string; getAge: number; }
110
+ */
23
111
  type ReturnTypeRecord<T extends Record<string, (...args: never[]) => unknown>> = {
24
112
  [K in keyof T]: ReturnType<T[K]>;
25
113
  };
114
+ /**
115
+ * Type that creates a record of instance types from a record of constructors.
116
+ *
117
+ * @template T - A record type where each value is a constructor
118
+ * @example
119
+ * type Constructors = {
120
+ * User: new () => User;
121
+ * Post: new () => Post;
122
+ * };
123
+ * type InstanceTypes = InstanceTypeRecord<Constructors>; // { User: User; Post: Post; }
124
+ */
26
125
  type InstanceTypeRecord<T extends Record<string, new (...args: never[]) => unknown>> = {
27
126
  [K in keyof T]: InstanceType<T[K]>;
28
127
  };
@@ -54,7 +153,28 @@ type Flatten<T> = T extends object ? {
54
153
  [K in keyof T]: Flatten<T[K]>;
55
154
  } : T;
56
155
 
156
+ /**
157
+ * Helper type that checks if all properties of a type are optional.
158
+ *
159
+ * @template T - The type to check
160
+ */
57
161
  type AllPropertiesOptional<T> = T extends Partial<T> ? (Partial<T> extends T ? true : false) : false;
162
+ /**
163
+ * Type that makes properties optional if all their children are optional.
164
+ * This is useful for creating types where properties are only optional if their nested properties are all optional.
165
+ *
166
+ * @template T - The type to transform
167
+ * @example
168
+ * type User = {
169
+ * name: string;
170
+ * address?: {
171
+ * street?: string;
172
+ * city?: string;
173
+ * };
174
+ * };
175
+ * type Transformed = MakePropertyOptionalIfChildrenOptional<User>;
176
+ * // Result: { name: string; address?: { street?: string; city?: string; } }
177
+ */
58
178
  type MakePropertyOptionalIfChildrenOptional<T> = {
59
179
  [K in keyof T as AllPropertiesOptional<T[K]> extends true ? K : never]?: T[K];
60
180
  } & {
@@ -74,19 +194,14 @@ type Prettify<T> = {
74
194
  [K in keyof T]: T[K];
75
195
  } & {};
76
196
 
77
- type RemoveTrailingSlash<T extends string> = T extends `${infer Route}/` ? Route : T;
78
-
79
- declare function extractArgumentNames(func: {
80
- toString(): string;
81
- }): string[];
82
-
83
- declare function sortObjectKeys<T extends Record<string, unknown>>(obj: T): T;
84
-
85
197
  /**
86
- * Removes all properties with undefined values from an object. Note: this does NOT strip null values.
87
- * @param obj The object to strip undefined properties from
88
- * @returns A new object with all undefined properties removed
198
+ * Type that removes a trailing slash from a string type if present.
199
+ *
200
+ * @template T - The string type to process
201
+ * @example
202
+ * type Route1 = RemoveTrailingSlash<'/users/'>; // '/users'
203
+ * type Route2 = RemoveTrailingSlash<'/users'>; // '/users'
89
204
  */
90
- declare function stripUndefinedProperties<T extends Record<string, unknown>>(obj: T): Partial<T>;
205
+ type RemoveTrailingSlash<T extends string> = T extends `${infer Route}/` ? Route : T;
91
206
 
92
- export { type Flatten, type FlattenKeys, type FlattenValues, type IdDto, type IdsDto, type InstanceTypeRecord, type MakePropertyOptionalIfChildrenOptional, type Prettify, type RecordTimingDto, type RemoveTrailingSlash, type ReturnTypeRecord, extractArgumentNames, isNever, isRecord, isTrue, sortObjectKeys, stripUndefinedProperties };
207
+ export { type Flatten, type FlattenKeys, type FlattenValues, type IdDto, type IdsDto, type InstanceTypeRecord, type MakePropertyOptionalIfChildrenOptional, type Prettify, type RecordTimingDto, type RemoveTrailingSlash, type ReturnTypeRecord, extractArgumentNames, isNever, isRecord, isTrue, noop, sortObjectKeys, stripUndefinedProperties };
package/lib/index.js CHANGED
@@ -24,27 +24,13 @@ __export(index_exports, {
24
24
  isNever: () => isNever,
25
25
  isRecord: () => isRecord,
26
26
  isTrue: () => isTrue,
27
+ noop: () => noop,
27
28
  sortObjectKeys: () => sortObjectKeys,
28
29
  stripUndefinedProperties: () => stripUndefinedProperties
29
30
  });
30
31
  module.exports = __toCommonJS(index_exports);
31
32
 
32
- // src/guards/isNever.ts
33
- function isNever(value) {
34
- return true;
35
- }
36
-
37
- // src/guards/isRecord.ts
38
- function isRecord(obj) {
39
- return obj !== null && typeof obj === "object" && !Array.isArray(obj);
40
- }
41
-
42
- // src/guards/isTrue.ts
43
- function isTrue(value) {
44
- return value;
45
- }
46
-
47
- // src/utils/extractArgumentNames.ts
33
+ // src/extractArgumentNames.ts
48
34
  function extractArgumentNames(func) {
49
35
  const fnStr = func.toString();
50
36
  const args = fnStr.match(/\(([^)]*)\)/);
@@ -72,7 +58,26 @@ function extractArgumentNames(func) {
72
58
  return result;
73
59
  }
74
60
 
75
- // src/utils/sortObjectKeys.ts
61
+ // src/guards/isNever.ts
62
+ function isNever(value) {
63
+ return true;
64
+ }
65
+
66
+ // src/guards/isRecord.ts
67
+ function isRecord(obj) {
68
+ return obj !== null && typeof obj === "object" && !Array.isArray(obj);
69
+ }
70
+
71
+ // src/guards/isTrue.ts
72
+ function isTrue(value) {
73
+ return value;
74
+ }
75
+
76
+ // src/noop.ts
77
+ function noop() {
78
+ }
79
+
80
+ // src/sortObjectKeys.ts
76
81
  function sortObjectKeys(obj) {
77
82
  if (typeof obj !== "object" || obj === null) {
78
83
  return obj;
@@ -87,7 +92,7 @@ function sortObjectKeys(obj) {
87
92
  );
88
93
  }
89
94
 
90
- // src/utils/stripUndefinedProperties.ts
95
+ // src/stripUndefinedProperties.ts
91
96
  function stripUndefinedProperties(obj) {
92
97
  return Object.fromEntries(
93
98
  Object.entries(obj).filter(([, value]) => value !== void 0)
@@ -99,6 +104,7 @@ function stripUndefinedProperties(obj) {
99
104
  isNever,
100
105
  isRecord,
101
106
  isTrue,
107
+ noop,
102
108
  sortObjectKeys,
103
109
  stripUndefinedProperties
104
110
  });
package/lib/index.mjs CHANGED
@@ -1,19 +1,4 @@
1
- // src/guards/isNever.ts
2
- function isNever(value) {
3
- return true;
4
- }
5
-
6
- // src/guards/isRecord.ts
7
- function isRecord(obj) {
8
- return obj !== null && typeof obj === "object" && !Array.isArray(obj);
9
- }
10
-
11
- // src/guards/isTrue.ts
12
- function isTrue(value) {
13
- return value;
14
- }
15
-
16
- // src/utils/extractArgumentNames.ts
1
+ // src/extractArgumentNames.ts
17
2
  function extractArgumentNames(func) {
18
3
  const fnStr = func.toString();
19
4
  const args = fnStr.match(/\(([^)]*)\)/);
@@ -41,7 +26,26 @@ function extractArgumentNames(func) {
41
26
  return result;
42
27
  }
43
28
 
44
- // src/utils/sortObjectKeys.ts
29
+ // src/guards/isNever.ts
30
+ function isNever(value) {
31
+ return true;
32
+ }
33
+
34
+ // src/guards/isRecord.ts
35
+ function isRecord(obj) {
36
+ return obj !== null && typeof obj === "object" && !Array.isArray(obj);
37
+ }
38
+
39
+ // src/guards/isTrue.ts
40
+ function isTrue(value) {
41
+ return value;
42
+ }
43
+
44
+ // src/noop.ts
45
+ function noop() {
46
+ }
47
+
48
+ // src/sortObjectKeys.ts
45
49
  function sortObjectKeys(obj) {
46
50
  if (typeof obj !== "object" || obj === null) {
47
51
  return obj;
@@ -56,7 +60,7 @@ function sortObjectKeys(obj) {
56
60
  );
57
61
  }
58
62
 
59
- // src/utils/stripUndefinedProperties.ts
63
+ // src/stripUndefinedProperties.ts
60
64
  function stripUndefinedProperties(obj) {
61
65
  return Object.fromEntries(
62
66
  Object.entries(obj).filter(([, value]) => value !== void 0)
@@ -67,6 +71,7 @@ export {
67
71
  isNever,
68
72
  isRecord,
69
73
  isTrue,
74
+ noop,
70
75
  sortObjectKeys,
71
76
  stripUndefinedProperties
72
77
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forklaunch/common",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Common package for base types, interfaces, implementations.",
5
5
  "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
6
6
  "bugs": {