@develia/commons 0.4.8 → 0.4.12

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 (103) hide show
  1. package/dist/cjs/array.js +5 -4
  2. package/dist/cjs/array.js.map +1 -1
  3. package/dist/cjs/browser/form-utilities.js +29 -23
  4. package/dist/cjs/browser/form-utilities.js.map +1 -1
  5. package/dist/cjs/browser/geo-location.js +61 -37
  6. package/dist/cjs/browser/geo-location.js.map +1 -1
  7. package/dist/cjs/browser/index.js +46 -1
  8. package/dist/cjs/browser/index.js.map +1 -1
  9. package/dist/cjs/cancellable-promise.js +60 -0
  10. package/dist/cjs/cancellable-promise.js.map +1 -0
  11. package/dist/cjs/event-bus.js +70 -0
  12. package/dist/cjs/event-bus.js.map +1 -0
  13. package/dist/cjs/from.js +1 -1
  14. package/dist/cjs/from.js.map +1 -1
  15. package/dist/cjs/index.js +6 -1
  16. package/dist/cjs/index.js.map +1 -1
  17. package/dist/cjs/math.js +3 -2
  18. package/dist/cjs/math.js.map +1 -1
  19. package/dist/cjs/parse.js +70 -0
  20. package/dist/cjs/parse.js.map +1 -0
  21. package/dist/cjs/patch.js +3 -2
  22. package/dist/cjs/patch.js.map +1 -1
  23. package/dist/cjs/string.js +43 -3
  24. package/dist/cjs/string.js.map +1 -1
  25. package/dist/cjs/utilities.js +25 -24
  26. package/dist/cjs/utilities.js.map +1 -1
  27. package/dist/esm/browser/form-utilities.js +23 -18
  28. package/dist/esm/browser/form-utilities.js.map +1 -1
  29. package/dist/esm/browser/geo-location.js +60 -36
  30. package/dist/esm/browser/geo-location.js.map +1 -1
  31. package/dist/esm/browser/index.js +44 -0
  32. package/dist/esm/browser/index.js.map +1 -1
  33. package/dist/esm/cancellable-promise.js +57 -0
  34. package/dist/esm/cancellable-promise.js.map +1 -0
  35. package/dist/esm/event-bus.js +65 -0
  36. package/dist/esm/event-bus.js.map +1 -0
  37. package/dist/esm/index.js +2 -0
  38. package/dist/esm/index.js.map +1 -1
  39. package/dist/esm/parse.js +64 -0
  40. package/dist/esm/parse.js.map +1 -0
  41. package/dist/esm/string.js +34 -0
  42. package/dist/esm/string.js.map +1 -1
  43. package/dist/types/browser/form-utilities.d.ts +2 -2
  44. package/dist/types/browser/form-utilities.d.ts.map +1 -1
  45. package/dist/types/browser/geo-location.d.ts.map +1 -1
  46. package/dist/types/browser/index.d.ts +7 -0
  47. package/dist/types/browser/index.d.ts.map +1 -1
  48. package/dist/types/cancellable-promise.d.ts +13 -0
  49. package/dist/types/cancellable-promise.d.ts.map +1 -0
  50. package/dist/types/event-bus.d.ts +21 -0
  51. package/dist/types/event-bus.d.ts.map +1 -0
  52. package/dist/types/index.d.ts +2 -0
  53. package/dist/types/index.d.ts.map +1 -1
  54. package/dist/types/parse.d.ts +16 -0
  55. package/dist/types/parse.d.ts.map +1 -0
  56. package/dist/types/string.d.ts +5 -0
  57. package/dist/types/string.d.ts.map +1 -1
  58. package/dist/web/browser/form-utilities.js +23 -18
  59. package/dist/web/browser/form-utilities.js.map +1 -1
  60. package/dist/web/browser/geo-location.js +60 -36
  61. package/dist/web/browser/geo-location.js.map +1 -1
  62. package/dist/web/browser/index.js +44 -0
  63. package/dist/web/browser/index.js.map +1 -1
  64. package/dist/web/cancellable-promise.js +57 -0
  65. package/dist/web/cancellable-promise.js.map +1 -0
  66. package/dist/web/event-bus.js +65 -0
  67. package/dist/web/event-bus.js.map +1 -0
  68. package/dist/web/index.js +2 -0
  69. package/dist/web/index.js.map +1 -1
  70. package/dist/web/parse.js +64 -0
  71. package/dist/web/parse.js.map +1 -0
  72. package/dist/web/string.js +34 -0
  73. package/dist/web/string.js.map +1 -1
  74. package/package.json +56 -56
  75. package/src/array.ts +0 -0
  76. package/src/async-pool-executor.ts +0 -0
  77. package/src/browser/form-utilities.ts +179 -173
  78. package/src/browser/geo-location.ts +113 -79
  79. package/src/browser/index.ts +78 -4
  80. package/src/browser/native-bridge.ts +116 -116
  81. package/src/cancellable-promise.ts +77 -0
  82. package/src/clean-url-search-params.ts +0 -0
  83. package/src/collections/cache-dictionary.ts +0 -0
  84. package/src/collections/default-map.ts +0 -0
  85. package/src/collections/default-weak-map.ts +0 -0
  86. package/src/collections/index.ts +2 -2
  87. package/src/event-bus.ts +84 -0
  88. package/src/from.ts +0 -0
  89. package/src/index.ts +37 -35
  90. package/src/key-value-pair.ts +0 -0
  91. package/src/lazy.ts +0 -0
  92. package/src/math.ts +0 -0
  93. package/src/order.ts +0 -0
  94. package/src/parse.ts +241 -0
  95. package/src/patch.ts +0 -0
  96. package/src/stopwatch.ts +0 -0
  97. package/src/string.ts +82 -2
  98. package/src/timer.ts +0 -0
  99. package/src/timespan.ts +0 -0
  100. package/src/type-code.ts +15 -15
  101. package/src/type.ts +371 -371
  102. package/src/types.ts +30 -30
  103. package/src/utilities.ts +404 -404
package/src/parse.ts ADDED
@@ -0,0 +1,241 @@
1
+ // parse.ts
2
+ /**
3
+ * Options for {@link parseBoolean}.
4
+ */
5
+ export interface ParseBooleanOptions {
6
+ /**
7
+ * Additional strings to consider as `true`.
8
+ * These are matched case-insensitively.
9
+ * @example ["oui", "ja"]
10
+ */
11
+ extraTruthy?: string[];
12
+
13
+ /**
14
+ * Additional strings to consider as `false`.
15
+ * These are matched case-insensitively.
16
+ * @example ["non", "nein"]
17
+ */
18
+ extraFalsy?: string[];
19
+ }
20
+
21
+ /**
22
+ * Options for {@link parseInteger}.
23
+ */
24
+ export interface ParseIntegerOptions {
25
+ /**
26
+ * Numeric base for conversion.
27
+ * @default 10
28
+ */
29
+ radix?: number;
30
+
31
+ /**
32
+ * Thousands separator to strip before parsing.
33
+ * @example "." for "1.234.567" → 1234567
34
+ * @example "," for "1,234,567" → 1234567
35
+ */
36
+ thousandsSeparator?: string;
37
+ }
38
+
39
+ /**
40
+ * Options for {@link parseDecimal}.
41
+ */
42
+ export interface ParseDecimalOptions {
43
+ /**
44
+ * Character used as decimal point.
45
+ * @default "."
46
+ */
47
+ decimalSeparator?: "." | ",";
48
+
49
+ /**
50
+ * Thousands separator to strip before parsing.
51
+ * @example "." for "1.234,56" (EU format)
52
+ * @example "," for "1,234.56" (US format)
53
+ */
54
+ thousandsSeparator?: string;
55
+ }
56
+
57
+ // ============================================
58
+ // HELPERS
59
+ // ============================================
60
+
61
+ function stripThousands(value: string, separator?: string): string {
62
+ if (!separator) return value;
63
+ return value.split(separator).join("");
64
+ }
65
+
66
+ // ============================================
67
+ // parseInteger
68
+ // ============================================
69
+
70
+ /**
71
+ * Safely parses a value into an integer.
72
+ * Returns `null` if the value cannot be parsed.
73
+ *
74
+ * - `null` or `undefined` → `null`
75
+ * - `number` → returned as-is (floats are truncated)
76
+ * - `string` → parsed with the given options
77
+ *
78
+ * @param value - The value to parse.
79
+ * @param options - Optional parsing configuration.
80
+ * @returns The parsed integer, or `null` on failure.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * parseInteger(42) // 42
85
+ * parseInteger(3.7) // 3
86
+ * parseInteger(null) // null
87
+ * parseInteger(undefined) // null
88
+ * parseInteger("42") // 42
89
+ * parseInteger("1.234.567", { thousandsSeparator: "." }) // 1234567
90
+ * parseInteger("FF", { radix: 16 }) // 255
91
+ * parseInteger("abc") // null
92
+ * ```
93
+ */
94
+ export function parseInteger(
95
+ value: string | number | null | undefined,
96
+ options: ParseIntegerOptions = {}
97
+ ): number | null {
98
+ if (value === null || value === undefined) return null;
99
+
100
+ if (typeof value === "number") {
101
+ return Number.isFinite(value) ? Math.trunc(value) : null;
102
+ }
103
+
104
+ const { radix = 10, thousandsSeparator } = options;
105
+
106
+ const cleaned = stripThousands(value.trim(), thousandsSeparator);
107
+
108
+ if (cleaned === "") return null;
109
+ if (radix === 10 && !/^[+-]?\d+$/.test(cleaned)) return null;
110
+
111
+ const parsed = Number.parseInt(cleaned, radix);
112
+
113
+ return Number.isNaN(parsed) ? null : parsed;
114
+ }
115
+
116
+ // ============================================
117
+ // parseDecimal
118
+ // ============================================
119
+
120
+ /**
121
+ * Safely parses a value into a floating-point number.
122
+ * Returns `null` if the value cannot be parsed.
123
+ *
124
+ * - `null` or `undefined` → `null`
125
+ * - `number` → returned as-is (if finite)
126
+ * - `string` → parsed with the given options
127
+ *
128
+ * Supports custom decimal and thousands separators, making it suitable
129
+ * for both US (`1,234.56`) and EU (`1.234,56`) number formats.
130
+ *
131
+ * @param value - The value to parse.
132
+ * @param options - Optional parsing configuration.
133
+ * @returns The parsed float, or `null` on failure.
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * parseDecimal(3.14) // 3.14
138
+ * parseDecimal(null) // null
139
+ * parseDecimal(undefined) // null
140
+ * parseDecimal("3.14") // 3.14
141
+ * parseDecimal("3,14", { decimalSeparator: "," }) // 3.14
142
+ * parseDecimal("1.234,56", { decimalSeparator: ",", thousandsSeparator: "." }) // 1234.56
143
+ * parseDecimal("abc") // null
144
+ * ```
145
+ */
146
+ export function parseDecimal(
147
+ value: string | number | null | undefined,
148
+ options: ParseDecimalOptions = {}
149
+ ): number | null {
150
+ if (value === null || value === undefined) return null;
151
+
152
+ if (typeof value === "number") {
153
+ return Number.isFinite(value) ? value : null;
154
+ }
155
+
156
+ const { decimalSeparator = ".", thousandsSeparator } = options;
157
+
158
+ let cleaned = stripThousands(value.trim(), thousandsSeparator);
159
+
160
+ if (cleaned === "") return null;
161
+
162
+ if (decimalSeparator === ",") {
163
+ const commaCount = (cleaned.match(/,/g) || []).length;
164
+ if (commaCount > 1) return null;
165
+ cleaned = cleaned.replace(",", ".");
166
+ }
167
+
168
+ if (!/^[+-]?\d+(\.\d+)?$/.test(cleaned)) return null;
169
+
170
+ const parsed = Number.parseFloat(cleaned);
171
+
172
+ return Number.isNaN(parsed) || !Number.isFinite(parsed) ? null : parsed;
173
+ }
174
+
175
+ // ============================================
176
+ // parseBoolean
177
+ // ============================================
178
+
179
+ const DEFAULT_TRUTHY = ["true", "1", "yes", "si", "sí", "on"];
180
+ const DEFAULT_FALSY = ["false", "0", "no", "off"];
181
+
182
+ /**
183
+ * Safely parses a value into a boolean.
184
+ * Returns `null` if the value cannot be interpreted as boolean.
185
+ *
186
+ * - `null` or `undefined` → `null`
187
+ * - `boolean` → returned as-is
188
+ * - `number` → `0` is `false`, any other finite number is `true`
189
+ * - `string` → matched case-insensitively against known truthy/falsy values
190
+ *
191
+ * Default truthy: `"true"`, `"1"`, `"yes"`, `"si"`, `"sí"`, `"on"`
192
+ * Default falsy: `"false"`, `"0"`, `"no"`, `"off"`
193
+ *
194
+ * @param value - The value to parse.
195
+ * @param options - Optional parsing configuration.
196
+ * @returns The parsed boolean, or `null` on failure.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * parseBoolean(true) // true
201
+ * parseBoolean(false) // false
202
+ * parseBoolean(1) // true
203
+ * parseBoolean(0) // false
204
+ * parseBoolean(null) // null
205
+ * parseBoolean(undefined) // null
206
+ * parseBoolean("yes") // true
207
+ * parseBoolean("no") // false
208
+ * parseBoolean("sí") // true
209
+ * parseBoolean("TRUE") // true
210
+ * parseBoolean("oui") // null
211
+ * parseBoolean("oui", { extraTruthy: ["oui"] }) // true
212
+ * parseBoolean("nein", { extraFalsy: ["nein"] }) // false
213
+ * parseBoolean("whatever") // null
214
+ * ```
215
+ */
216
+ export function parseBoolean(
217
+ value: string | number | boolean | null | undefined,
218
+ options: ParseBooleanOptions = {}
219
+ ): boolean | null {
220
+ if (value === null || value === undefined) return null;
221
+
222
+ if (typeof value === "boolean") return value;
223
+
224
+ if (typeof value === "number") {
225
+ return Number.isFinite(value) ? value !== 0 : null;
226
+ }
227
+
228
+ const { extraTruthy = [], extraFalsy = [] } = options;
229
+
230
+ const normalized = value.trim().toLowerCase();
231
+
232
+ if (normalized === "") return null;
233
+
234
+ const truthy = [...DEFAULT_TRUTHY, ...extraTruthy.map((s) => s.toLowerCase())];
235
+ const falsy = [...DEFAULT_FALSY, ...extraFalsy.map((s) => s.toLowerCase())];
236
+
237
+ if (truthy.includes(normalized)) return true;
238
+ if (falsy.includes(normalized)) return false;
239
+
240
+ return null;
241
+ }
package/src/patch.ts CHANGED
File without changes
package/src/stopwatch.ts CHANGED
File without changes
package/src/string.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  * @param {string} prefix - The prefix to ensure on the string.
7
7
  * @return {string} - The resulting string with the ensured prefix.
8
8
  */
9
- export function ensurePrefix(str: string, prefix: string) {
9
+ export function ensurePrefix(str: string, prefix: string): string {
10
10
  if (!str.startsWith(prefix))
11
11
  return prefix + str
12
12
  return str
@@ -20,7 +20,7 @@ export function ensurePrefix(str: string, prefix: string) {
20
20
  * @param {string} suffix - The suffix to ensure.
21
21
  * @return {string} - The modified string with the suffix added if it was not already present.
22
22
  */
23
- export function ensureSuffix(str: string, suffix: string) {
23
+ export function ensureSuffix(str: string, suffix: string): string {
24
24
  if (!str.endsWith(suffix))
25
25
  return str + suffix
26
26
  return str
@@ -46,4 +46,84 @@ export function format(template: string, ...args: any[]): string {
46
46
  return template.replace(regex, (match, key) => {
47
47
  return typeof args[key] !== 'undefined' ? args[key] : "";
48
48
  });
49
+ }
50
+
51
+ /**
52
+ * Splits a string into its constituent words, regardless of the original case
53
+ * style (camelCase, PascalCase, snake_case, kebab-case, CONSTANT_CASE,
54
+ * space-separated, etc.).
55
+ *
56
+ * @param {string} str - The string to split into words.
57
+ * @return {string[]} - The list of lowercase words extracted from the input.
58
+ */
59
+ function toWords(str: string): string[] {
60
+ if (!str) return [];
61
+
62
+ return str
63
+ // Insert a space between a lowercase/digit and an uppercase letter (camelCase -> camel Case)
64
+ .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
65
+ // Insert a space between consecutive uppercase letters followed by a lowercase letter (XMLParser -> XML Parser)
66
+ .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
67
+ // Replace any non-alphanumeric character with a space
68
+ .replace(/[^a-zA-Z0-9]+/g, ' ')
69
+ .trim()
70
+ .toLowerCase()
71
+ .split(/\s+/)
72
+ .filter(Boolean);
73
+ }
74
+
75
+ /**
76
+ * Converts a string to snake_case.
77
+ *
78
+ * @param {string} str - The string to convert.
79
+ * @return {string} - The string converted to snake_case.
80
+ */
81
+ export function toSnakeCase(str: string): string {
82
+ return toWords(str).join('_');
83
+ }
84
+
85
+ /**
86
+ * Converts a string to kebab-case.
87
+ *
88
+ * @param {string} str - The string to convert.
89
+ * @return {string} - The string converted to kebab-case.
90
+ */
91
+ export function toKebabCase(str: string): string {
92
+ return toWords(str).join('-');
93
+ }
94
+
95
+ /**
96
+ * Converts a string to camelCase.
97
+ *
98
+ * @param {string} str - The string to convert.
99
+ * @return {string} - The string converted to camelCase.
100
+ */
101
+ export function toCamelCase(str: string): string {
102
+ const words = toWords(str);
103
+ if (words.length === 0) return '';
104
+ return words
105
+ .map((word, i) => i === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1))
106
+ .join('');
107
+ }
108
+
109
+ /**
110
+ * Converts a string to PascalCase.
111
+ *
112
+ * @param {string} str - The string to convert.
113
+ * @return {string} - The string converted to PascalCase.
114
+ */
115
+ export function toPascalCase(str: string): string {
116
+ return toWords(str)
117
+ .map(word => word.charAt(0).toUpperCase() + word.slice(1))
118
+ .join('');
119
+ }
120
+
121
+ /**
122
+ * Converts a string to CONSTANT_CASE (a.k.a. SCREAMING_SNAKE_CASE).
123
+ *
124
+ * @param {string} str - The string to convert.
125
+ * @return {string} - The string converted to CONSTANT_CASE.
126
+ */
127
+ export function toConstantCase(str: string): string {
128
+ return toWords(str).join('_').toUpperCase();
49
129
  }
package/src/timer.ts CHANGED
File without changes
package/src/timespan.ts CHANGED
File without changes
package/src/type-code.ts CHANGED
@@ -1,16 +1,16 @@
1
- enum TypeCode {
2
- String = 'string',
3
- Number = 'number',
4
- Boolean = 'boolean',
5
- Symbol = 'symbol',
6
- BigInt = 'bigint',
7
- Null = 'null',
8
- Undefined = 'undefined',
9
- Object = 'object',
10
- Function = 'function',
11
- Array = 'array',
12
- Unknown = 'unknown',
13
- }
14
-
15
-
1
+ enum TypeCode {
2
+ String = 'string',
3
+ Number = 'number',
4
+ Boolean = 'boolean',
5
+ Symbol = 'symbol',
6
+ BigInt = 'bigint',
7
+ Null = 'null',
8
+ Undefined = 'undefined',
9
+ Object = 'object',
10
+ Function = 'function',
11
+ Array = 'array',
12
+ Unknown = 'unknown',
13
+ }
14
+
15
+
16
16
  export default TypeCode;