@develia/commons 0.4.9 → 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.
- package/dist/cjs/array.js +5 -4
- package/dist/cjs/array.js.map +1 -1
- package/dist/cjs/browser/form-utilities.js +29 -23
- package/dist/cjs/browser/form-utilities.js.map +1 -1
- package/dist/cjs/browser/geo-location.js +61 -37
- package/dist/cjs/browser/geo-location.js.map +1 -1
- package/dist/cjs/browser/index.js +46 -1
- package/dist/cjs/browser/index.js.map +1 -1
- package/dist/cjs/event-bus.js +70 -0
- package/dist/cjs/event-bus.js.map +1 -0
- package/dist/cjs/from.js +1 -1
- package/dist/cjs/from.js.map +1 -1
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/math.js +3 -2
- package/dist/cjs/math.js.map +1 -1
- package/dist/cjs/parse.js +4 -3
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/patch.js +3 -2
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/string.js +43 -3
- package/dist/cjs/string.js.map +1 -1
- package/dist/cjs/utilities.js +25 -24
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/browser/form-utilities.js +23 -18
- package/dist/esm/browser/form-utilities.js.map +1 -1
- package/dist/esm/browser/geo-location.js +60 -36
- package/dist/esm/browser/geo-location.js.map +1 -1
- package/dist/esm/browser/index.js +44 -0
- package/dist/esm/browser/index.js.map +1 -1
- package/dist/esm/event-bus.js +65 -0
- package/dist/esm/event-bus.js.map +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/string.js +34 -0
- package/dist/esm/string.js.map +1 -1
- package/dist/types/browser/form-utilities.d.ts +2 -2
- package/dist/types/browser/form-utilities.d.ts.map +1 -1
- package/dist/types/browser/geo-location.d.ts.map +1 -1
- package/dist/types/browser/index.d.ts +7 -0
- package/dist/types/browser/index.d.ts.map +1 -1
- package/dist/types/event-bus.d.ts +21 -0
- package/dist/types/event-bus.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/string.d.ts +5 -0
- package/dist/types/string.d.ts.map +1 -1
- package/dist/web/browser/form-utilities.js +23 -18
- package/dist/web/browser/form-utilities.js.map +1 -1
- package/dist/web/browser/geo-location.js +60 -36
- package/dist/web/browser/geo-location.js.map +1 -1
- package/dist/web/browser/index.js +44 -0
- package/dist/web/browser/index.js.map +1 -1
- package/dist/web/event-bus.js +65 -0
- package/dist/web/event-bus.js.map +1 -0
- package/dist/web/index.js +1 -0
- package/dist/web/index.js.map +1 -1
- package/dist/web/string.js +34 -0
- package/dist/web/string.js.map +1 -1
- package/package.json +56 -56
- package/src/array.ts +0 -0
- package/src/async-pool-executor.ts +0 -0
- package/src/browser/form-utilities.ts +179 -173
- package/src/browser/geo-location.ts +113 -79
- package/src/browser/index.ts +78 -4
- package/src/browser/native-bridge.ts +116 -116
- package/src/cancellable-promise.ts +76 -76
- package/src/clean-url-search-params.ts +0 -0
- package/src/collections/cache-dictionary.ts +0 -0
- package/src/collections/default-map.ts +0 -0
- package/src/collections/default-weak-map.ts +0 -0
- package/src/collections/index.ts +2 -2
- package/src/event-bus.ts +84 -0
- package/src/from.ts +0 -0
- package/src/index.ts +37 -36
- package/src/key-value-pair.ts +0 -0
- package/src/lazy.ts +0 -0
- package/src/math.ts +0 -0
- package/src/order.ts +0 -0
- package/src/parse.ts +240 -240
- package/src/patch.ts +0 -0
- package/src/stopwatch.ts +0 -0
- package/src/string.ts +82 -2
- package/src/timer.ts +0 -0
- package/src/timespan.ts +0 -0
- package/src/type-code.ts +15 -15
- package/src/type.ts +371 -371
- package/src/types.ts +30 -30
- package/src/utilities.ts +404 -404
|
File without changes
|
package/src/collections/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {default as DefaultWeakMap} from './default-weak-map.js'
|
|
2
|
-
export {default as DefaultMap} from './default-map.js'
|
|
1
|
+
export {default as DefaultWeakMap} from './default-weak-map.js'
|
|
2
|
+
export {default as DefaultMap} from './default-map.js'
|
|
3
3
|
export {default as CacheDictionary} from './cache-dictionary.js';
|
package/src/event-bus.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// event.ts
|
|
2
|
+
|
|
3
|
+
export abstract class Event {
|
|
4
|
+
readonly timestamp: Date;
|
|
5
|
+
|
|
6
|
+
protected constructor() {
|
|
7
|
+
this.timestamp = new Date();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
toString(): string {
|
|
11
|
+
return `${this.constructor.name}(${this.timestamp.toISOString()})`;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type EventClass<T extends Event> = new (...args: any[]) => T;
|
|
16
|
+
type Handler<T extends Event> = (event: T) => void;
|
|
17
|
+
|
|
18
|
+
interface HandlerWrapper {
|
|
19
|
+
original: Function;
|
|
20
|
+
wrapped: (event: Event) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class EventBus {
|
|
24
|
+
private static _instance: EventBus | null = null;
|
|
25
|
+
|
|
26
|
+
static get shared(): EventBus {
|
|
27
|
+
return this._instance ??= new EventBus();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private _handlers = new Map<Function, HandlerWrapper[]>();
|
|
31
|
+
|
|
32
|
+
on<T extends Event>(type: EventClass<T>, handler: Handler<T>): void {
|
|
33
|
+
if (!this._handlers.has(type)) {
|
|
34
|
+
this._handlers.set(type, []);
|
|
35
|
+
}
|
|
36
|
+
this._handlers.get(type)!.push({
|
|
37
|
+
original: handler,
|
|
38
|
+
wrapped: (event: Event) => handler(event as T),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
off<T extends Event>(type: EventClass<T>, handler: Handler<T>): void {
|
|
43
|
+
const handlers = this._handlers.get(type);
|
|
44
|
+
if (!handlers) return;
|
|
45
|
+
|
|
46
|
+
let index = handlers.findIndex((h) => h.original === handler);
|
|
47
|
+
if (index !== -1) {
|
|
48
|
+
handlers.splice(index, 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
emit<T extends Event>(event: T): void {
|
|
53
|
+
const handlers = this._handlers.get(event.constructor);
|
|
54
|
+
if (!handlers) return;
|
|
55
|
+
|
|
56
|
+
for (const handler of [...handlers]) {
|
|
57
|
+
try {
|
|
58
|
+
handler.wrapped(event);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.error(`[EventBus] Error en handler de ${event.constructor.name}:`, e);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
once<T extends Event>(type: EventClass<T>, handler: Handler<T>): void {
|
|
66
|
+
const wrapper: Handler<T> = (event: T) => {
|
|
67
|
+
handler(event);
|
|
68
|
+
this.off(type, wrapper);
|
|
69
|
+
};
|
|
70
|
+
this.on(type, wrapper);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
clear<T extends Event>(type: EventClass<T>): void {
|
|
74
|
+
this._handlers.delete(type);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
clearAll(): void {
|
|
78
|
+
this._handlers.clear();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
hasListeners<T extends Event>(type: EventClass<T>): boolean {
|
|
82
|
+
return (this._handlers.get(type)?.length ?? 0) > 0;
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/from.ts
CHANGED
|
File without changes
|
package/src/index.ts
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
|
|
3
|
-
// ─── Types ───────────────────────────────────────────
|
|
4
|
-
export * from './types.js';
|
|
5
|
-
export {default as Type} from './type.js';
|
|
6
|
-
export {default as TypeCode} from './type-code.js';
|
|
7
|
-
|
|
8
|
-
// ─── Utilities ───────────────────────────────────────
|
|
9
|
-
export * from './utilities.js';
|
|
10
|
-
export * from './string.js';
|
|
11
|
-
export * from './math.js';
|
|
12
|
-
export {default as CancellablePromise} from "./cancellable-promise.js";
|
|
13
|
-
|
|
14
|
-
// ─── Data Structures ────────────────────────────────
|
|
15
|
-
export {default as KeyValuePair} from './key-value-pair.js';
|
|
16
|
-
export {default as Order} from './order.js';
|
|
17
|
-
|
|
18
|
-
// ─── Async & Timing ─────────────────────────────────
|
|
19
|
-
export {default as Timer} from './timer.js';
|
|
20
|
-
export {default as TimeSpan} from './timespan.js';
|
|
21
|
-
export {default as Stopwatch} from './stopwatch.js';
|
|
22
|
-
export {default as AsyncPoolExecutor} from './async-pool-executor.js';
|
|
23
|
-
|
|
24
|
-
// ─── Patterns ────────────────────────────────────────
|
|
25
|
-
export {default as Lazy} from './lazy.js';
|
|
26
|
-
export {default as from, From} from './from.js';
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
|
|
3
|
+
// ─── Types ───────────────────────────────────────────
|
|
4
|
+
export * from './types.js';
|
|
5
|
+
export {default as Type} from './type.js';
|
|
6
|
+
export {default as TypeCode} from './type-code.js';
|
|
7
|
+
|
|
8
|
+
// ─── Utilities ───────────────────────────────────────
|
|
9
|
+
export * from './utilities.js';
|
|
10
|
+
export * from './string.js';
|
|
11
|
+
export * from './math.js';
|
|
12
|
+
export {default as CancellablePromise} from "./cancellable-promise.js";
|
|
13
|
+
|
|
14
|
+
// ─── Data Structures ────────────────────────────────
|
|
15
|
+
export {default as KeyValuePair} from './key-value-pair.js';
|
|
16
|
+
export {default as Order} from './order.js';
|
|
17
|
+
|
|
18
|
+
// ─── Async & Timing ─────────────────────────────────
|
|
19
|
+
export {default as Timer} from './timer.js';
|
|
20
|
+
export {default as TimeSpan} from './timespan.js';
|
|
21
|
+
export {default as Stopwatch} from './stopwatch.js';
|
|
22
|
+
export {default as AsyncPoolExecutor} from './async-pool-executor.js';
|
|
23
|
+
|
|
24
|
+
// ─── Patterns ────────────────────────────────────────
|
|
25
|
+
export {default as Lazy} from './lazy.js';
|
|
26
|
+
export {default as from, From} from './from.js';
|
|
27
|
+
export {EventBus, Event} from './event-bus.js';
|
|
28
|
+
|
|
29
|
+
// ─── Web ─────────────────────────────────────────────
|
|
30
|
+
export {default as CleanURLSearchParams} from './clean-url-search-params.js';
|
|
31
|
+
/** @deprecated */
|
|
32
|
+
export {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';
|
|
33
|
+
|
|
34
|
+
// ─── Deprecated ──────────────────────────────────────
|
|
35
|
+
/** @deprecated */
|
|
36
|
+
export * from './array.js';
|
|
37
|
+
/** @deprecated */
|
|
37
38
|
export * from './collections/index.js';
|
package/src/key-value-pair.ts
CHANGED
|
File without changes
|
package/src/lazy.ts
CHANGED
|
File without changes
|
package/src/math.ts
CHANGED
|
File without changes
|
package/src/order.ts
CHANGED
|
File without changes
|
package/src/parse.ts
CHANGED
|
@@ -1,241 +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;
|
|
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
241
|
}
|
package/src/patch.ts
CHANGED
|
File without changes
|