danholibraryjs 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/Utils.md CHANGED
@@ -11,55 +11,55 @@ Utility functions and classes for common tasks.
11
11
  * API utility class for making HTTP requests
12
12
  */
13
13
  class ApiUtils<ApiEndpoints extends string> {
14
- /**
15
- * @param options Configuration options
16
- * @param options.baseEndpointDev Base URL for development environment
17
- * @param options.baseEndpoint Base URL for production environment (optional)
18
- * @param options.log Enable request logging (default: false)
19
- */
20
- constructor(options: {
21
- baseEndpointDev: string;
22
- baseEndpoint?: string;
23
- log?: boolean;
24
- });
25
-
26
- /**
27
- * Make a request to the API
28
- * @param path The path to the endpoint
29
- * @param options Request options (method, body, headers, etc.)
30
- * @returns The response from the API
31
- */
32
- public async request<TData>(
33
- path: ApiEndpoints,
34
- options?: RequestOptions
35
- ): Promise<TData>;
36
-
37
- /**
38
- * Get the base endpoint URL based on environment
39
- */
40
- public get baseEndpoint(): string;
14
+ /**
15
+ * @param options Configuration options
16
+ * @param options.baseEndpointDev Base URL for development environment
17
+ * @param options.baseEndpoint Base URL for production environment (optional)
18
+ * @param options.log Enable request logging (default: false)
19
+ */
20
+ constructor(options: {
21
+ baseEndpointDev: string;
22
+ baseEndpoint?: string;
23
+ log?: boolean;
24
+ });
25
+
26
+ /**
27
+ * Make a request to the API
28
+ * @param path The path to the endpoint
29
+ * @param options Request options (method, body, headers, etc.)
30
+ * @returns The response from the API
31
+ */
32
+ public async request<TData>(
33
+ path: ApiEndpoints,
34
+ options?: RequestOptions
35
+ ): Promise<TData>;
36
+
37
+ /**
38
+ * Get the base endpoint URL based on environment
39
+ */
40
+ public get baseEndpoint(): string;
41
41
  }
42
42
 
43
43
  /**
44
44
  * Request options type
45
45
  */
46
46
  type RequestOptions<TBody = any> = Omit<RequestInit, 'method' | 'body'> & {
47
- method?: HttpMethods;
48
- body?: TBody;
49
- params?: Record<string, string | number | boolean>;
47
+ method?: HttpMethods;
48
+ body?: TBody;
49
+ params?: Record<string, string | number | boolean>;
50
50
  };
51
51
 
52
52
  /**
53
53
  * HTTP methods
54
54
  */
55
55
  type HttpMethods =
56
- | 'GET'
57
- | 'POST'
58
- | 'PUT'
59
- | 'PATCH'
60
- | 'DELETE'
61
- | 'HEAD'
62
- | 'OPTIONS';
56
+ | 'GET'
57
+ | 'POST'
58
+ | 'PUT'
59
+ | 'PATCH'
60
+ | 'DELETE'
61
+ | 'HEAD'
62
+ | 'OPTIONS';
63
63
  ```
64
64
 
65
65
  ### ColorUtils
@@ -100,8 +100,8 @@ function generateRandomColor(): Hex;
100
100
  * Color utilities object
101
101
  */
102
102
  const ColorUtils = {
103
- convert,
104
- generateRandomColor
103
+ convert,
104
+ generateRandomColor
105
105
  };
106
106
  ```
107
107
 
@@ -140,8 +140,8 @@ function randomWithPercentages<T>(items: [item: T, weight: number][]): T;
140
140
  * Number utilities object
141
141
  */
142
142
  const NumberUtils = {
143
- between,
144
- randomWithPercentages,
143
+ between,
144
+ randomWithPercentages,
145
145
  };
146
146
  ```
147
147
 
@@ -162,15 +162,15 @@ type PatchEvent = 'before' | 'instead' | 'after';
162
162
  * @returns Function to unpatch
163
163
  */
164
164
  function patch<
165
- TTarget extends object,
166
- TProperty extends keyof TTarget,
167
- TPatchEvent extends PatchEvent,
168
- TPatchReplacement extends PatcherReplacement<TTarget, TProperty, TPatchEvent>
165
+ TTarget extends object,
166
+ TProperty extends keyof TTarget,
167
+ TPatchEvent extends PatchEvent,
168
+ TPatchReplacement extends PatcherReplacement<TTarget, TProperty, TPatchEvent>
169
169
  >(
170
- target: TTarget,
171
- property: TProperty,
172
- event: TPatchEvent,
173
- replacement: TPatchReplacement
170
+ target: TTarget,
171
+ property: TProperty,
172
+ event: TPatchEvent,
173
+ replacement: TPatchReplacement
174
174
  ): (() => void) | undefined;
175
175
 
176
176
  /**
@@ -179,8 +179,8 @@ function patch<
179
179
  * @param property Property to unpatch
180
180
  */
181
181
  function unpatch<TTarget extends object, TProperty extends keyof TTarget>(
182
- target: TTarget,
183
- property: TProperty
182
+ target: TTarget,
183
+ property: TProperty
184
184
  ): void;
185
185
 
186
186
  /**
@@ -216,9 +216,9 @@ function randomId(length?: number): string;
216
216
  * @returns Singular or plural form based on count
217
217
  */
218
218
  function pluralize(
219
- countable: number | ArrayLike<any> | Map<any, any>,
220
- singular: string,
221
- plural?: string
219
+ countable: number | ArrayLike<any> | Map<any, any>,
220
+ singular: string,
221
+ plural?: string
222
222
  ): string;
223
223
  ```
224
224
 
@@ -248,10 +248,10 @@ function getUnixTime(timestamp: number): number;
248
248
  * @returns Promise that resolves with callback result or rejects if cancelled
249
249
  */
250
250
  function debounce<T>(
251
- debounceId: string,
252
- callback: () => T,
253
- delay: number,
254
- signal?: AbortSignal
251
+ debounceId: string,
252
+ callback: () => T,
253
+ delay: number,
254
+ signal?: AbortSignal
255
255
  ): Promise<T>;
256
256
 
257
257
  /**
@@ -261,8 +261,8 @@ function debounce<T>(
261
261
  * @returns Debounced function
262
262
  */
263
263
  function wrapInDebounce<T>(
264
- callback: (...args: T[]) => void,
265
- delay: number
264
+ callback: (...args: T[]) => void,
265
+ delay: number
266
266
  ): (...args: T[]) => void;
267
267
 
268
268
  /**
@@ -273,9 +273,9 @@ function wrapInDebounce<T>(
273
273
  * @returns Callback result or undefined if on cooldown
274
274
  */
275
275
  function throttle<T>(
276
- throttleId: string,
277
- callback: () => T,
278
- cooldown: number
276
+ throttleId: string,
277
+ callback: () => T,
278
+ cooldown: number
279
279
  ): T | undefined;
280
280
 
281
281
  /**
@@ -285,8 +285,8 @@ function throttle<T>(
285
285
  * @returns Throttled function
286
286
  */
287
287
  function wrapInThrottle<T>(
288
- callback: (...args: T[]) => void,
289
- cooldown: number
288
+ callback: (...args: T[]) => void,
289
+ cooldown: number
290
290
  ): (...args: T[]) => void;
291
291
 
292
292
  /**
@@ -321,15 +321,15 @@ function get24HourFormat(hour: number): string;
321
321
  * Time utilities object containing all time-related functions
322
322
  */
323
323
  const TimeUtils = {
324
- wait,
325
- getUnixTime,
326
- debounce,
327
- wrapInDebounce,
328
- throttle,
329
- wrapInThrottle,
330
- isThrottleOnCooldown,
331
- ensureStartZero,
332
- get12HourFormat,
333
- get24HourFormat
324
+ wait,
325
+ getUnixTime,
326
+ debounce,
327
+ wrapInDebounce,
328
+ throttle,
329
+ wrapInThrottle,
330
+ isThrottleOnCooldown,
331
+ ensureStartZero,
332
+ get12HourFormat,
333
+ get24HourFormat
334
334
  };
335
335
  ```
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "registry": "https://registry.npmjs.org/"
5
5
  },
6
- "version": "2.0.2",
6
+ "version": "2.0.3",
7
7
  "description": "Library for Javascript.",
8
8
  "main": "dist/index.js",
9
9
  "types": "dist/index.d.ts",
@@ -51,8 +51,12 @@ export function pick<From extends {}, Props extends keyof From>(from: From, ...p
51
51
  return props.reduce((result, prop) => {
52
52
  if (typeof prop === "object") {
53
53
  const keys = Object.keysOf(prop);
54
- keys.forEach(key => (result as Partial<From>)[key] = from[key]);
55
- } else {
54
+ keys.forEach(key => {
55
+ if (key in from) {
56
+ (result as Partial<From>)[key] = from[key];
57
+ }
58
+ });
59
+ } else if (prop in from) {
56
60
  (result as Partial<From>)[prop] = from[prop];
57
61
  }
58
62
  return result;
@@ -44,7 +44,7 @@ export const properties: Properties = [
44
44
  'object', 'function', 'any',
45
45
  'Date', 'RegExp', 'Promise', 'Array', 'Map', 'Set'
46
46
  ].reduce((result, primitive) => {
47
- result[`get${convertCase('camel', 'pascal')}s` as keyof Properties] = function <Source extends {}, AllowFunctions extends boolean = false>(source: Source, withFunctions: AllowFunctions = false as AllowFunctions) {
47
+ result[`get${convertCase.call(primitive, 'camel', 'pascal')}s` as keyof Properties] = function <Source extends {}, AllowFunctions extends boolean = false>(source: Source, withFunctions: AllowFunctions = false as AllowFunctions) {
48
48
  return Object.keysOf<Source>(source).reduce((result, key) => {
49
49
  if ((source[key] as any).constructor.name === primitive ||
50
50
  (withFunctions && typeof source[key] === 'function' && source[key] as any).constructor.name === primitive) {
@@ -11,12 +11,12 @@ declare global {
11
11
  from: Case,
12
12
  ...to: To
13
13
  ): (
14
- Return extends 'upper' ? Uppercase<string>
15
- : Return extends 'lower' ? Lowercase<string>
16
- : Return extends 'pascal' ? Capitalize<string>
17
- : Return extends 'camel' ? Uncapitalize<string>
18
- : string
19
- );
14
+ Return extends 'upper' ? Uppercase<string>
15
+ : Return extends 'lower' ? Lowercase<string>
16
+ : Return extends 'pascal' ? Capitalize<string>
17
+ : Return extends 'camel' ? Uncapitalize<string>
18
+ : string
19
+ );
20
20
  }
21
21
  }
22
22
 
@@ -78,12 +78,12 @@ const caseMap: Record<Case, Record<Case, (str: string) => string>> = {
78
78
  }
79
79
  };
80
80
 
81
- export const convertCase = <
81
+ export function convertCase<
82
82
  TValue extends string,
83
83
  To extends Array<Case>,
84
84
  Return extends To extends [...Array<Case>, infer To] ? To : Case
85
85
  >(
86
- value: TValue,
86
+ this: TValue,
87
87
  from: Case,
88
88
  ...to: To
89
89
  ): (
@@ -92,4 +92,8 @@ export const convertCase = <
92
92
  : Return extends 'pascal' ? Capitalize<TValue>
93
93
  : Return extends 'camel' ? Uncapitalize<TValue>
94
94
  : string
95
- ) => to.reduce((str, toCase) => caseMap[from][toCase](str), value as string) as any;
95
+ ) {
96
+ return to.reduce((str, toCase) => caseMap[from][toCase](str), this as string) as any;
97
+ }
98
+
99
+ String.prototype.convertCase = convertCase;