danholibraryjs 2.0.1 → 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.
Files changed (51) hide show
  1. package/README.md +1 -0
  2. package/dist/Extensions/Array/random.extension.d.ts +1 -2
  3. package/dist/Extensions/Array/random.extension.js +1 -22
  4. package/dist/Extensions/Array/string.extension.d.ts +3 -4
  5. package/dist/Extensions/Number.d.ts +2 -2
  6. package/dist/Extensions/Number.js +1 -1
  7. package/dist/Extensions/Object/arrays.extension.d.ts +14 -0
  8. package/dist/Extensions/Object/arrays.extension.js +7 -2
  9. package/dist/Extensions/Object/extracts.extension.d.ts +6 -6
  10. package/dist/Extensions/Object/extracts.extension.js +16 -9
  11. package/dist/Extensions/Object/properties.extension.js +1 -1
  12. package/dist/Extensions/String/case.extension.d.ts +1 -1
  13. package/dist/Extensions/String/case.extension.js +4 -1
  14. package/dist/Extensions/String/index.d.ts +1 -0
  15. package/dist/Extensions/String/index.js +1 -0
  16. package/dist/Extensions/String/string.extension.d.ts +6 -0
  17. package/dist/Extensions/String/string.extension.js +10 -0
  18. package/dist/Types/Able.d.ts +1 -1
  19. package/dist/Utils/NumberUtils.d.ts +5 -0
  20. package/dist/Utils/NumberUtils.js +25 -1
  21. package/dist/Utils/StringUtils.d.ts +5 -0
  22. package/dist/Utils/StringUtils.js +6 -1
  23. package/dist/Utils/TimeUtils/index.d.ts +3 -0
  24. package/dist/Utils/TimeUtils/index.js +2 -0
  25. package/dist/Utils/TimeUtils/string.util.d.ts +3 -0
  26. package/dist/Utils/TimeUtils/string.util.js +19 -0
  27. package/dist/index.d.ts +1 -0
  28. package/dist/index.js +1 -0
  29. package/docs/Classes.md +467 -467
  30. package/docs/Extensions.md +197 -164
  31. package/docs/Interfaces.md +12 -12
  32. package/docs/Types.md +24 -24
  33. package/docs/Utils.md +335 -0
  34. package/docs/index.md +1 -0
  35. package/package.json +1 -1
  36. package/src/Extensions/Array/random.extension.ts +2 -25
  37. package/src/Extensions/Array/string.extension.ts +3 -4
  38. package/src/Extensions/Number.ts +3 -3
  39. package/src/Extensions/Object/arrays.extension.ts +23 -2
  40. package/src/Extensions/Object/extracts.extension.ts +24 -15
  41. package/src/Extensions/Object/properties.extension.ts +1 -1
  42. package/src/Extensions/String/case.extension.ts +13 -9
  43. package/src/Extensions/String/index.ts +2 -1
  44. package/src/Extensions/String/string.extension.ts +11 -0
  45. package/src/Types/Able.ts +1 -1
  46. package/src/Utils/NumberUtils.ts +27 -0
  47. package/src/Utils/StringUtils.ts +7 -1
  48. package/src/Utils/TimeUtils/index.ts +2 -0
  49. package/src/Utils/TimeUtils/string.util.ts +13 -0
  50. package/src/index.ts +2 -1
  51. package/src/Extensions/Object/properties.ts +0 -51
package/README.md CHANGED
@@ -26,3 +26,4 @@ import { ... } from 'DanhoLibraryJS';
26
26
  * [Extensions](/docs/Extensions.md)
27
27
  * [Interfaces](/docs/Interfaces.md)
28
28
  * [Types](/docs/Types.md)
29
+ * [Utils](/docs/Utils.md)
@@ -15,9 +15,8 @@ declare global {
15
15
  * @param items An array of tuples where each tuple contains an item and its corresponding weight.
16
16
  * @returns A randomly selected item based on the provided weights.
17
17
  */
18
- randomWithPercentages(items: [item: T, weight: number][]): T;
18
+ randomWithPercentages(items: Array<[item: T, weight: number]>): T;
19
19
  }
20
20
  }
21
21
  export declare function random<T>(this: Array<T>): T;
22
22
  export declare function shuffle<T>(this: Array<T>): Array<T>;
23
- export declare function randomWithPercentages<T>(items: [item: T, weight: number][]): T;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.randomWithPercentages = exports.shuffle = exports.random = void 0;
3
+ exports.shuffle = exports.random = void 0;
4
4
  function random() {
5
5
  const randomIndex = Math.floor(Math.random() * this.length);
6
6
  return this[randomIndex];
@@ -12,24 +12,3 @@ function shuffle() {
12
12
  }
13
13
  exports.shuffle = shuffle;
14
14
  Array.prototype.shuffle = shuffle;
15
- function randomWithPercentages(items) {
16
- if (items.length === 0)
17
- throw new Error('Items array cannot be empty');
18
- // Calculate total weight in case weights don't sum to 100
19
- const totalWeight = items.reduce((sum, [, weight]) => sum + weight, 0);
20
- if (totalWeight === 0)
21
- throw new Error('Total weight must be greater than zero');
22
- // Generate random number between 0 and totalWeight
23
- const random = Math.random() * totalWeight;
24
- // Find the item that corresponds to this random value
25
- let currentWeight = 0;
26
- for (const [item, weight] of items) {
27
- currentWeight += weight;
28
- if (random <= currentWeight) {
29
- return item;
30
- }
31
- }
32
- throw new Error('Unable to select an item based on weights');
33
- }
34
- exports.randomWithPercentages = randomWithPercentages;
35
- Array.prototype.randomWithPercentages = randomWithPercentages;
@@ -2,12 +2,11 @@ declare global {
2
2
  interface Array<T> {
3
3
  /**
4
4
  * Joins the elements of the array into a string, with optional custom separators.
5
- * @param args An array of strings or undefined values to be used as separators between elements.
6
- * @param separator The default separator to use between elements if args is not provided or undefined.
7
- * @param endSeparator The separator to use before the last element.
5
+ * @param separator The default separator to use between elements. Defaults to ','.
6
+ * @param endSeparator The separator to use before the last element. Default is '&'.
8
7
  * @returns A string with the joined elements.
9
8
  */
10
- join(args?: Array<string | undefined>, separator?: string, endSeparator?: string): string;
9
+ join(separator?: string, endSeparator?: string): string;
11
10
  }
12
11
  }
13
12
  export declare function join<T>(this: Array<T>, separator?: string, endSeparator?: string): string;
@@ -4,10 +4,10 @@ type Separators = {
4
4
  };
5
5
  declare global {
6
6
  interface Number {
7
- toSeparationString(separators: Partial<Separators>): string;
7
+ toSeparationString(separators?: Partial<Separators>): string;
8
8
  toRomanNumeral(): string;
9
9
  }
10
10
  }
11
- export declare function toSeparationString(this: number, separators: Partial<Separators>): string;
11
+ export declare function toSeparationString(this: number, separators?: Partial<Separators>): string;
12
12
  export declare function toRomanNumeral(this: number): string;
13
13
  export {};
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toRomanNumeral = exports.toSeparationString = void 0;
4
4
  function toSeparationString(separators) {
5
- const { thousand = '.', decimal = '.' } = separators;
5
+ const { thousand = '.', decimal = '.' } = separators || {};
6
6
  const [integerPart, decimalPart] = this.toString().split('.');
7
7
  const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, thousand);
8
8
  return decimalPart ? `${formattedInteger}${decimal}${decimalPart}` : formattedInteger;
@@ -6,6 +6,18 @@ declare global {
6
6
  * @param from Object to destruct
7
7
  */
8
8
  array<From extends {} = {}>(from: From): Array<[keyof From, ValueOf<From>]>;
9
+ /**
10
+ * Destructures object into array of property keys or values depending on selector
11
+ * @param from Object to destruct
12
+ * @param selector Selects whether to return keys or values
13
+ */
14
+ array<From extends {} = {}>(from: From, selector: 'keys'): Array<keyof From>;
15
+ /**
16
+ * Destructures object into array of property keys or values depending on selector
17
+ * @param from Object to destruct
18
+ * @param selector Selects whether to return keys or values
19
+ */
20
+ array<From extends {} = {}>(from: From, selector: 'values'): Array<ValueOf<From>>;
9
21
  /**
10
22
  * Destructures object into array of property keys
11
23
  * @param from Object to destruct
@@ -14,4 +26,6 @@ declare global {
14
26
  }
15
27
  }
16
28
  export declare function array<From extends {} = {}>(this: object, from: From): Array<[keyof From, ValueOf<From>]>;
29
+ export declare function array<From extends {} = {}>(this: object, from: From, selector: 'keys'): Array<keyof From>;
30
+ export declare function array<From extends {} = {}>(this: object, from: From, selector: 'values'): Array<ValueOf<From>>;
17
31
  export declare function keysOf<From extends {} = {}>(this: object, from: From): Array<keyof From>;
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.keysOf = exports.array = void 0;
4
- function array(from) {
5
- return Object.entries(from);
4
+ function array(from, selector) {
5
+ const entries = Object.entries(from);
6
+ switch (selector) {
7
+ case 'keys': return entries.map(([key]) => key);
8
+ case 'values': return entries.map(([, value]) => value);
9
+ default: return entries;
10
+ }
6
11
  }
7
12
  exports.array = array;
8
13
  Object.array = array;
@@ -13,24 +13,24 @@ declare global {
13
13
  */
14
14
  pick<From extends {}, Props extends keyof From>(from: From, ...props: Array<Props | Partial<From>>): Pick<From, Props>;
15
15
  /**
16
- * Receive an object with properties that are not in union of source and target objects
16
+ * Returns the difference between two objects (properties where values differ)
17
17
  * @param source Source object
18
18
  * @param target Target object
19
- * @param exclude Properties to exclude from difference
20
- * @returns Object with properties that are not in union of source and target objects, excluding specified properties
19
+ * @param exclude Properties to exclude from comparison
20
+ * @returns Object with properties where values differ between source and target, excluding specified properties
21
21
  */
22
- difference<T extends object>(source: T, target: T, ...exclude: Array<keyof T>): Omit<T, keyof T>;
22
+ difference<T extends object>(source: T, target: T, ...exclude: Array<keyof T>): Partial<T>;
23
23
  /**
24
24
  * Deeply combines objects, with later objects in parameters taking precedence over earlier ones. Does not combine arrays.
25
25
  * @param objects Objects to combine
26
26
  * @returns Combined object
27
27
  */
28
- combine<T extends Record<string, any | undefined>>(...objects: Array<Partial<T> | undefined>): T;
28
+ combine<T extends Record<string, any | undefined>>(...objects: Array<Combinable<T> | undefined>): T;
29
29
  }
30
30
  }
31
31
  export declare function omit<From extends {}, Props extends keyof From>(from: From, ...props: Array<Props | Partial<From>>): Omit<From, Props>;
32
32
  export declare function pick<From extends {}, Props extends keyof From>(from: From, ...props: Array<Props | Partial<From>>): Pick<From, Props>;
33
- export declare function difference<T extends object>(source: T, target: T, ...exclude: Array<keyof T>): Omit<T, keyof T>;
33
+ export declare function difference<T extends object>(source: T, target: T, ...exclude: Array<keyof T>): Partial<T>;
34
34
  type Combinable<T extends Record<string, any>> = {
35
35
  [key in keyof T]?: T[key] extends Record<string, any> ? Combinable<T[key]> : T[key];
36
36
  };
@@ -19,9 +19,13 @@ function pick(from, ...props) {
19
19
  return props.reduce((result, prop) => {
20
20
  if (typeof prop === "object") {
21
21
  const keys = Object.keysOf(prop);
22
- keys.forEach(key => result[key] = from[key]);
22
+ keys.forEach(key => {
23
+ if (key in from) {
24
+ result[key] = from[key];
25
+ }
26
+ });
23
27
  }
24
- else {
28
+ else if (prop in from) {
25
29
  result[prop] = from[prop];
26
30
  }
27
31
  return result;
@@ -30,13 +34,16 @@ function pick(from, ...props) {
30
34
  exports.pick = pick;
31
35
  Object.pick = pick;
32
36
  function difference(source, target, ...exclude) {
33
- const diffKeys = new Set([...Object.keysOf(source), ...Object.keysOf(target)]);
34
- exclude?.forEach(key => diffKeys.delete(key));
35
- return [...diffKeys.values()].reduce((acc, key, i, arr) => {
36
- const sourceValue = JSON.stringify(source[key]);
37
- const targetValue = JSON.stringify(target[key]);
38
- if (sourceValue !== targetValue)
39
- acc[key] = target[key];
37
+ const excludeSet = new Set(exclude);
38
+ const allKeys = new Set([...Object.keysOf(source), ...Object.keysOf(target)]);
39
+ return [...allKeys].reduce((acc, key) => {
40
+ if (excludeSet.has(key))
41
+ return acc;
42
+ const sourceValue = source[key];
43
+ const targetValue = target[key];
44
+ if (JSON.stringify(sourceValue) !== JSON.stringify(targetValue)) {
45
+ acc[key] = targetValue;
46
+ }
40
47
  return acc;
41
48
  }, {});
42
49
  }
@@ -7,7 +7,7 @@ exports.properties = [
7
7
  'object', 'function', 'any',
8
8
  'Date', 'RegExp', 'Promise', 'Array', 'Map', 'Set'
9
9
  ].reduce((result, primitive) => {
10
- result[`get${(0, case_extension_1.convertCase)('camel', 'pascal')}s`] = function (source, withFunctions = false) {
10
+ result[`get${case_extension_1.convertCase.call(primitive, 'camel', 'pascal')}s`] = function (source, withFunctions = false) {
11
11
  return Object.keysOf(source).reduce((result, key) => {
12
12
  if (source[key].constructor.name === primitive ||
13
13
  (withFunctions && typeof source[key] === 'function' && source[key]).constructor.name === primitive) {
@@ -9,4 +9,4 @@ declare global {
9
9
  convertCase<To extends Array<Case>, Return extends To extends [...Array<Case>, infer To] ? To : Case>(from: Case, ...to: To): (Return extends 'upper' ? Uppercase<string> : Return extends 'lower' ? Lowercase<string> : Return extends 'pascal' ? Capitalize<string> : Return extends 'camel' ? Uncapitalize<string> : string);
10
10
  }
11
11
  }
12
- export declare const convertCase: <TValue extends string, To extends Case[], Return extends To extends [...Case[], infer To_1] ? To_1 : Case>(value: TValue, from: Case, ...to: To) => Return extends "upper" ? Uppercase<TValue> : Return extends "lower" ? Lowercase<TValue> : Return extends "pascal" ? Capitalize<TValue> : Return extends "camel" ? Uncapitalize<TValue> : string;
12
+ export declare function convertCase<TValue extends string, To extends Array<Case>, Return extends To extends [...Array<Case>, infer To] ? To : Case>(this: TValue, from: Case, ...to: To): (Return extends 'upper' ? Uppercase<TValue> : Return extends 'lower' ? Lowercase<TValue> : Return extends 'pascal' ? Capitalize<TValue> : Return extends 'camel' ? Uncapitalize<TValue> : string);
@@ -51,5 +51,8 @@ const caseMap = {
51
51
  lower: (str) => str.toLowerCase(),
52
52
  }
53
53
  };
54
- const convertCase = (value, from, ...to) => to.reduce((str, toCase) => caseMap[from][toCase](str), value);
54
+ function convertCase(from, ...to) {
55
+ return to.reduce((str, toCase) => caseMap[from][toCase](str), this);
56
+ }
55
57
  exports.convertCase = convertCase;
58
+ String.prototype.convertCase = convertCase;
@@ -1 +1,2 @@
1
1
  export * from './case.extension';
2
+ export * from './string.extension';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./case.extension"), exports);
18
+ __exportStar(require("./string.extension"), exports);
@@ -0,0 +1,6 @@
1
+ declare global {
2
+ interface String {
3
+ truncate(length: number, ellipsis?: string): string;
4
+ }
5
+ }
6
+ export declare function truncate(this: string, length: number, ellipsis?: string): string;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.truncate = void 0;
4
+ function truncate(length, ellipsis = "...") {
5
+ if (this.length <= length)
6
+ return this;
7
+ return this.slice(0, length - ellipsis.length) + ellipsis;
8
+ }
9
+ exports.truncate = truncate;
10
+ String.prototype.truncate = truncate;
@@ -5,7 +5,7 @@ export type Functionable<T, Args extends any[] = []> = T | ((...args: Args) => T
5
5
  /**
6
6
  * Item is Promise<T> or T
7
7
  */
8
- export type Promisable<T> = T | Promise<T>;
8
+ export type Promiseable<T> = T | Promise<T>;
9
9
  /**
10
10
  * Item is T or null
11
11
  */
@@ -1 +1,6 @@
1
1
  export declare function between(min: number, max: number): number;
2
+ export declare function randomWithPercentages<T>(items: [item: T, weight: number][]): T;
3
+ export declare const NumberUtils: {
4
+ between: typeof between;
5
+ randomWithPercentages: typeof randomWithPercentages;
6
+ };
@@ -1,7 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.between = void 0;
3
+ exports.NumberUtils = exports.randomWithPercentages = exports.between = void 0;
4
4
  function between(min, max) {
5
5
  return Math.floor(Math.random() * (max - min + 1)) + min;
6
6
  }
7
7
  exports.between = between;
8
+ function randomWithPercentages(items) {
9
+ if (items.length === 0)
10
+ throw new Error('Items array cannot be empty');
11
+ // Calculate total weight in case weights don't sum to 100
12
+ const totalWeight = items.reduce((sum, [, weight]) => sum + weight, 0);
13
+ if (totalWeight === 0)
14
+ throw new Error('Total weight must be greater than zero');
15
+ // Generate random number between 0 and totalWeight
16
+ const random = Math.random() * totalWeight;
17
+ // Find the item that corresponds to this random value
18
+ let currentWeight = 0;
19
+ for (const [item, weight] of items) {
20
+ currentWeight += weight;
21
+ if (random <= currentWeight) {
22
+ return item;
23
+ }
24
+ }
25
+ throw new Error('Unable to select an item based on weights');
26
+ }
27
+ exports.randomWithPercentages = randomWithPercentages;
28
+ exports.NumberUtils = {
29
+ between,
30
+ randomWithPercentages,
31
+ };
@@ -1,3 +1,8 @@
1
1
  export declare function classNames(...args: Array<any>): string;
2
2
  export declare function randomId(length?: number): string;
3
3
  export declare function pluralize(countable: number | ArrayLike<any> | Map<any, any>, singular: string, plural?: string): string;
4
+ export declare const StringUtils: {
5
+ classNames: typeof classNames;
6
+ randomId: typeof randomId;
7
+ pluralize: typeof pluralize;
8
+ };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pluralize = exports.randomId = exports.classNames = void 0;
3
+ exports.StringUtils = exports.pluralize = exports.randomId = exports.classNames = void 0;
4
4
  function classNames(...args) {
5
5
  return args.reduce((acc, arg) => {
6
6
  if (!arg)
@@ -45,3 +45,8 @@ function pluralize(countable, singular, plural) {
45
45
  return `${singular}s`;
46
46
  }
47
47
  exports.pluralize = pluralize;
48
+ exports.StringUtils = {
49
+ classNames,
50
+ randomId,
51
+ pluralize
52
+ };
@@ -1,4 +1,7 @@
1
1
  export declare const TimeUtils: {
2
+ ensureStartZero(num: number): string;
3
+ get12HourFormat(hour: number): string;
4
+ get24HourFormat(hour: number): string;
2
5
  throttle<T>(throttleId: string, callback: () => T, cooldown: number): T;
3
6
  wrapInThrottle<T_1>(callback: (...args: T_1[]) => void, cooldown: number): (...args: T_1[]) => void;
4
7
  isThrottleOnCooldown(throttleId: string): boolean;
@@ -27,8 +27,10 @@ exports.TimeUtils = void 0;
27
27
  const Debounce = __importStar(require("./debounce.util"));
28
28
  const Functions = __importStar(require("./functions.util"));
29
29
  const Throttle = __importStar(require("./throttle.util"));
30
+ const StringUtils = __importStar(require("./string.util"));
30
31
  exports.TimeUtils = {
31
32
  ...Functions,
32
33
  ...Debounce,
33
34
  ...Throttle,
35
+ ...StringUtils,
34
36
  };
@@ -0,0 +1,3 @@
1
+ export declare function ensureStartZero(num: number): string;
2
+ export declare function get12HourFormat(hour: number): string;
3
+ export declare function get24HourFormat(hour: number): string;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.get24HourFormat = exports.get12HourFormat = exports.ensureStartZero = void 0;
4
+ function ensureStartZero(num) {
5
+ return num < 10 ? `0${num}` : num.toString();
6
+ }
7
+ exports.ensureStartZero = ensureStartZero;
8
+ function get12HourFormat(hour) {
9
+ if (hour === 0)
10
+ return '12am';
11
+ if (hour > 12)
12
+ return `${hour - 12}pm`;
13
+ return `${hour}am`;
14
+ }
15
+ exports.get12HourFormat = get12HourFormat;
16
+ function get24HourFormat(hour) {
17
+ return ensureStartZero(hour);
18
+ }
19
+ exports.get24HourFormat = get24HourFormat;
package/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export * from './Classes';
2
2
  export * from './Extensions';
3
3
  export * from './Interfaces';
4
4
  export * from './Types';
5
+ export * from './Utils';
package/dist/index.js CHANGED
@@ -18,3 +18,4 @@ __exportStar(require("./Classes"), exports);
18
18
  __exportStar(require("./Extensions"), exports);
19
19
  __exportStar(require("./Interfaces"), exports);
20
20
  __exportStar(require("./Types"), exports);
21
+ __exportStar(require("./Utils"), exports);