@oxog/vld 2.1.0 → 2.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/CHANGELOG.md +113 -0
- package/README.md +413 -1235
- package/dist/chunks/errors-core-CerMg2rq.js +1 -0
- package/dist/chunks/index-CkbfQQ4n.js +1 -0
- package/dist/chunks/index-yjpbvFgw.js +1 -0
- package/dist/chunks/ip-validation-g5LWgy9t.js +1 -0
- package/dist/chunks/security-Dy24T8N7.js +1 -0
- package/dist/cjs/chunks/errors-core-DrydF46s.cjs +1 -0
- package/dist/cjs/chunks/index-BEV9nG0N.cjs +1 -0
- package/dist/cjs/chunks/index-ZjHAxYdA.cjs +1 -0
- package/dist/cjs/chunks/ip-validation-BZqPamPC.cjs +1 -0
- package/dist/cjs/chunks/security-aXZVI7qW.cjs +1 -0
- package/dist/cjs/cli/bin.cjs +1 -505
- package/dist/cjs/cli/index.cjs +1 -209
- package/dist/cjs/codecs/index.cjs +1 -45
- package/dist/cjs/coercion/index.cjs +1 -588
- package/dist/cjs/compat/emitter.cjs +1 -184
- package/dist/cjs/compat/result.cjs +1 -189
- package/dist/cjs/errors.cjs +1 -180
- package/dist/cjs/index.cjs +1 -1894
- package/dist/cjs/kernel.cjs +1 -415
- package/dist/cjs/locales/af.cjs +1 -108
- package/dist/cjs/locales/ar.cjs +1 -108
- package/dist/cjs/locales/bn.cjs +1 -108
- package/dist/cjs/locales/da.cjs +1 -108
- package/dist/cjs/locales/de.cjs +1 -108
- package/dist/cjs/locales/en.cjs +1 -108
- package/dist/cjs/locales/es-MX.cjs +1 -107
- package/dist/cjs/locales/es.cjs +1 -96
- package/dist/cjs/locales/fi.cjs +1 -108
- package/dist/cjs/locales/fr.cjs +1 -96
- package/dist/cjs/locales/hi.cjs +1 -108
- package/dist/cjs/locales/id.cjs +1 -108
- package/dist/cjs/locales/index.cjs +1 -43
- package/dist/cjs/locales/it.cjs +1 -108
- package/dist/cjs/locales/ja.cjs +1 -108
- package/dist/cjs/locales/ko.cjs +1 -108
- package/dist/cjs/locales/lazy.cjs +1 -188
- package/dist/cjs/locales/nl.cjs +1 -108
- package/dist/cjs/locales/no.cjs +1 -108
- package/dist/cjs/locales/pl.cjs +1 -108
- package/dist/cjs/locales/pt-BR.cjs +1 -108
- package/dist/cjs/locales/pt.cjs +1 -108
- package/dist/cjs/locales/ru.cjs +1 -108
- package/dist/cjs/locales/runtime.cjs +1 -51
- package/dist/cjs/locales/sv.cjs +1 -108
- package/dist/cjs/locales/sw.cjs +1 -108
- package/dist/cjs/locales/th.cjs +1 -108
- package/dist/cjs/locales/tr.cjs +1 -108
- package/dist/cjs/locales/vi.cjs +1 -108
- package/dist/cjs/locales/zh.cjs +1 -108
- package/dist/cjs/mini.cjs +1 -242
- package/dist/cjs/pigment.cjs +1 -207
- package/dist/cjs/plugins/index.cjs +1 -12
- package/dist/cjs/registry.cjs +1 -37
- package/dist/cjs/v3/index.cjs +1 -425
- package/dist/cjs/v4/core/index.cjs +1 -894
- package/dist/cjs/v4/index.cjs +1 -425
- package/dist/cjs/v4/locales/index.cjs +1 -130
- package/dist/cjs/v4/mini/index.cjs +1 -509
- package/dist/cjs/v4-mini/index.cjs +1 -532
- package/dist/cjs/validators/any.cjs +1 -40
- package/dist/cjs/validators/array.cjs +1 -283
- package/dist/cjs/validators/base.cjs +1 -887
- package/dist/cjs/validators/base64.cjs +1 -73
- package/dist/cjs/validators/bigint.cjs +1 -232
- package/dist/cjs/validators/boolean.cjs +1 -146
- package/dist/cjs/validators/codec.cjs +1 -205
- package/dist/cjs/validators/custom.cjs +1 -71
- package/dist/cjs/validators/date.cjs +1 -278
- package/dist/cjs/validators/discriminated-union.cjs +1 -130
- package/dist/cjs/validators/enum.cjs +1 -154
- package/dist/cjs/validators/file.cjs +1 -130
- package/dist/cjs/validators/function.cjs +1 -63
- package/dist/cjs/validators/hex.cjs +1 -70
- package/dist/cjs/validators/index.cjs +1 -123
- package/dist/cjs/validators/intersection.cjs +1 -120
- package/dist/cjs/validators/json.cjs +1 -70
- package/dist/cjs/validators/lazy.cjs +1 -53
- package/dist/cjs/validators/literal.cjs +1 -64
- package/dist/cjs/validators/map.cjs +1 -218
- package/dist/cjs/validators/nan.cjs +1 -41
- package/dist/cjs/validators/never.cjs +1 -36
- package/dist/cjs/validators/null.cjs +1 -43
- package/dist/cjs/validators/number.cjs +1 -427
- package/dist/cjs/validators/object.cjs +1 -868
- package/dist/cjs/validators/promise.cjs +1 -115
- package/dist/cjs/validators/record.cjs +1 -262
- package/dist/cjs/validators/set.cjs +1 -203
- package/dist/cjs/validators/string-bool.cjs +1 -124
- package/dist/cjs/validators/string-formats.cjs +1 -168
- package/dist/cjs/validators/string.cjs +1 -12
- package/dist/cjs/validators/symbol.cjs +1 -62
- package/dist/cjs/validators/template-literal.cjs +1 -108
- package/dist/cjs/validators/tuple.cjs +1 -187
- package/dist/cjs/validators/uint8array.cjs +1 -106
- package/dist/cjs/validators/undefined.cjs +1 -43
- package/dist/cjs/validators/union.cjs +1 -191
- package/dist/cjs/validators/unknown.cjs +1 -40
- package/dist/cjs/validators/void.cjs +1 -48
- package/dist/cjs/validators/xor.cjs +1 -69
- package/dist/cli/bin.js +1 -406
- package/dist/cli/index.js +1 -205
- package/dist/codecs/index.js +1 -11
- package/dist/coercion/index.js +1 -579
- package/dist/compat/emitter.js +1 -180
- package/dist/compat/result.js +1 -170
- package/dist/errors-core.d.ts +32 -2
- package/dist/errors.js +1 -171
- package/dist/index.d.ts +315 -361
- package/dist/index.js +1 -1534
- package/dist/kernel.js +1 -409
- package/dist/locales/af.js +1 -106
- package/dist/locales/ar.js +1 -106
- package/dist/locales/bn.js +1 -106
- package/dist/locales/da.js +1 -106
- package/dist/locales/de.js +1 -106
- package/dist/locales/en.js +1 -106
- package/dist/locales/es-MX.js +1 -105
- package/dist/locales/es.js +1 -94
- package/dist/locales/fi.js +1 -106
- package/dist/locales/fr.js +1 -94
- package/dist/locales/hi.js +1 -106
- package/dist/locales/id.js +1 -106
- package/dist/locales/index.js +1 -29
- package/dist/locales/it.js +1 -106
- package/dist/locales/ja.js +1 -106
- package/dist/locales/ko.js +1 -106
- package/dist/locales/lazy.js +1 -177
- package/dist/locales/nl.js +1 -106
- package/dist/locales/no.js +1 -106
- package/dist/locales/pl.js +1 -106
- package/dist/locales/pt-BR.js +1 -106
- package/dist/locales/pt.js +1 -106
- package/dist/locales/ru.js +1 -106
- package/dist/locales/runtime.js +1 -41
- package/dist/locales/sv.js +1 -106
- package/dist/locales/sw.js +1 -106
- package/dist/locales/th.js +1 -106
- package/dist/locales/tr.js +1 -106
- package/dist/locales/vi.js +1 -106
- package/dist/locales/zh.js +1 -106
- package/dist/mini.d.ts +3 -3
- package/dist/mini.js +1 -156
- package/dist/pigment.js +1 -180
- package/dist/plugins/index.js +1 -1
- package/dist/registry.js +1 -34
- package/dist/utils/json-schema.d.ts +10 -1
- package/dist/v3/index.js +1 -46
- package/dist/v4/core/index.d.ts +28 -16
- package/dist/v4/core/index.js +1 -277
- package/dist/v4/index.js +1 -50
- package/dist/v4/locales/index.d.ts +4 -0
- package/dist/v4/locales/index.js +1 -77
- package/dist/v4/mini/index.js +1 -47
- package/dist/v4-mini/index.d.ts +1 -1
- package/dist/v4-mini/index.js +1 -75
- package/dist/validators/any.js +1 -37
- package/dist/validators/array.d.ts +7 -0
- package/dist/validators/array.js +1 -279
- package/dist/validators/base.d.ts +210 -17
- package/dist/validators/base.js +1 -868
- package/dist/validators/base64.js +1 -68
- package/dist/validators/bigint.d.ts +7 -0
- package/dist/validators/bigint.js +1 -228
- package/dist/validators/boolean.js +1 -142
- package/dist/validators/codec.js +1 -200
- package/dist/validators/custom.d.ts +20 -10
- package/dist/validators/custom.js +1 -67
- package/dist/validators/date.d.ts +2 -0
- package/dist/validators/date.js +1 -274
- package/dist/validators/discriminated-union.d.ts +4 -2
- package/dist/validators/discriminated-union.js +1 -123
- package/dist/validators/enum.d.ts +8 -2
- package/dist/validators/enum.js +1 -150
- package/dist/validators/file.js +1 -125
- package/dist/validators/function.js +1 -58
- package/dist/validators/hex.js +1 -65
- package/dist/validators/index.d.ts +1 -1
- package/dist/validators/index.js +1 -35
- package/dist/validators/intersection.js +1 -116
- package/dist/validators/json.js +1 -66
- package/dist/validators/lazy.js +1 -50
- package/dist/validators/literal.d.ts +8 -3
- package/dist/validators/literal.js +1 -60
- package/dist/validators/map.d.ts +7 -0
- package/dist/validators/map.js +1 -214
- package/dist/validators/nan.js +1 -38
- package/dist/validators/never.js +1 -32
- package/dist/validators/null.js +1 -40
- package/dist/validators/number.d.ts +25 -1
- package/dist/validators/number.js +1 -423
- package/dist/validators/object.d.ts +18 -2
- package/dist/validators/object.js +1 -864
- package/dist/validators/promise.d.ts +6 -7
- package/dist/validators/promise.js +1 -111
- package/dist/validators/record.d.ts +13 -8
- package/dist/validators/record.js +1 -258
- package/dist/validators/set.d.ts +5 -0
- package/dist/validators/set.js +1 -199
- package/dist/validators/string-bool.js +1 -120
- package/dist/validators/string-formats.d.ts +58 -62
- package/dist/validators/string-formats.js +1 -135
- package/dist/validators/string.d.ts +49 -10
- package/dist/validators/string.js +1 -4
- package/dist/validators/symbol.js +1 -58
- package/dist/validators/template-literal.js +1 -103
- package/dist/validators/tuple.d.ts +17 -13
- package/dist/validators/tuple.js +1 -183
- package/dist/validators/uint8array.js +1 -101
- package/dist/validators/undefined.js +1 -40
- package/dist/validators/union.d.ts +1 -0
- package/dist/validators/union.js +1 -187
- package/dist/validators/unknown.js +1 -37
- package/dist/validators/void.js +1 -44
- package/dist/validators/xor.d.ts +2 -2
- package/dist/validators/xor.js +1 -66
- package/package.json +240 -238
- package/dist/chunks/errors-core-BeiFUtZM.js +0 -81
- package/dist/chunks/index-0R2ntSud.js +0 -508
- package/dist/chunks/index-BvH403Yy.js +0 -88
- package/dist/chunks/security-B0Pd_vSY.js +0 -35
- package/dist/chunks/string-BJRQod3t.js +0 -530
- package/dist/cjs/chunks/errors-core-DWxLFs2f.cjs +0 -83
- package/dist/cjs/chunks/index-BkI37108.cjs +0 -538
- package/dist/cjs/chunks/index-Cc3bjUMX.cjs +0 -91
- package/dist/cjs/chunks/security-Dc3Jh13T.cjs +0 -37
- package/dist/cjs/chunks/string-BKZrc4hs.cjs +0 -533
|
@@ -1,184 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Event Emitter Implementation
|
|
5
|
-
*
|
|
6
|
-
* This module provides a type-safe event emitter,
|
|
7
|
-
* compatible with @oxog/emitter when available.
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Create a type-safe event emitter
|
|
11
|
-
*/
|
|
12
|
-
function createEmitter() {
|
|
13
|
-
const listeners = new Map();
|
|
14
|
-
const getListeners = (event) => {
|
|
15
|
-
if (!listeners.has(event)) {
|
|
16
|
-
listeners.set(event, []);
|
|
17
|
-
}
|
|
18
|
-
return listeners.get(event);
|
|
19
|
-
};
|
|
20
|
-
const sortListeners = (event) => {
|
|
21
|
-
const eventListeners = getListeners(event);
|
|
22
|
-
eventListeners.sort((a, b) => b.priority - a.priority);
|
|
23
|
-
};
|
|
24
|
-
const on = (event, handler, options = {}) => {
|
|
25
|
-
const entry = {
|
|
26
|
-
handler,
|
|
27
|
-
once: options.once ?? false,
|
|
28
|
-
priority: options.priority ?? 0
|
|
29
|
-
};
|
|
30
|
-
getListeners(event).push(entry);
|
|
31
|
-
sortListeners(event);
|
|
32
|
-
// Return unsubscribe function
|
|
33
|
-
return () => off(event, handler);
|
|
34
|
-
};
|
|
35
|
-
const once = (event, handler) => {
|
|
36
|
-
return on(event, handler, { once: true });
|
|
37
|
-
};
|
|
38
|
-
const off = (event, handler) => {
|
|
39
|
-
const eventListeners = listeners.get(event);
|
|
40
|
-
if (!eventListeners)
|
|
41
|
-
return;
|
|
42
|
-
const index = eventListeners.findIndex((entry) => entry.handler === handler);
|
|
43
|
-
if (index !== -1) {
|
|
44
|
-
eventListeners.splice(index, 1);
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const emit = (event, payload) => {
|
|
48
|
-
const eventListeners = listeners.get(event);
|
|
49
|
-
if (!eventListeners || eventListeners.length === 0)
|
|
50
|
-
return;
|
|
51
|
-
// Create a copy to allow modifications during iteration
|
|
52
|
-
const listenersToCall = [...eventListeners];
|
|
53
|
-
for (const entry of listenersToCall) {
|
|
54
|
-
if (entry.once) {
|
|
55
|
-
off(event, entry.handler);
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
entry.handler(payload);
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
61
|
-
// Log error but don't stop other handlers
|
|
62
|
-
console.error(`Error in event handler for "${String(event)}":`, error);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
const emitAsync = async (event, payload) => {
|
|
67
|
-
const eventListeners = listeners.get(event);
|
|
68
|
-
if (!eventListeners || eventListeners.length === 0)
|
|
69
|
-
return;
|
|
70
|
-
// Create a copy to allow modifications during iteration
|
|
71
|
-
const listenersToCall = [...eventListeners];
|
|
72
|
-
for (const entry of listenersToCall) {
|
|
73
|
-
if (entry.once) {
|
|
74
|
-
off(event, entry.handler);
|
|
75
|
-
}
|
|
76
|
-
try {
|
|
77
|
-
await entry.handler(payload);
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
// Log error but don't stop other handlers
|
|
81
|
-
console.error(`Error in async event handler for "${String(event)}":`, error);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
const removeAllListeners = (event) => {
|
|
86
|
-
if (event !== undefined) {
|
|
87
|
-
listeners.delete(event);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
listeners.clear();
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
const listenerCount = (event) => {
|
|
94
|
-
return listeners.get(event)?.length ?? 0;
|
|
95
|
-
};
|
|
96
|
-
const eventNames = () => {
|
|
97
|
-
return Array.from(listeners.keys()).filter((key) => {
|
|
98
|
-
const eventListeners = listeners.get(key);
|
|
99
|
-
return eventListeners !== undefined && eventListeners.length > 0;
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
|
-
return {
|
|
103
|
-
on,
|
|
104
|
-
once,
|
|
105
|
-
off,
|
|
106
|
-
emit,
|
|
107
|
-
emitAsync,
|
|
108
|
-
removeAllListeners,
|
|
109
|
-
listenerCount,
|
|
110
|
-
eventNames
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Mixin to add event emitter capabilities to a class
|
|
115
|
-
*/
|
|
116
|
-
function withEmitter() {
|
|
117
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
118
|
-
return function (Base) {
|
|
119
|
-
return class extends Base {
|
|
120
|
-
constructor() {
|
|
121
|
-
super(...arguments);
|
|
122
|
-
/** @internal */
|
|
123
|
-
this._emitter = createEmitter();
|
|
124
|
-
}
|
|
125
|
-
on(event, handler, options) {
|
|
126
|
-
return this._emitter.on(event, handler, options);
|
|
127
|
-
}
|
|
128
|
-
once(event, handler) {
|
|
129
|
-
return this._emitter.once(event, handler);
|
|
130
|
-
}
|
|
131
|
-
off(event, handler) {
|
|
132
|
-
this._emitter.off(event, handler);
|
|
133
|
-
}
|
|
134
|
-
emit(event, payload) {
|
|
135
|
-
this._emitter.emit(event, payload);
|
|
136
|
-
}
|
|
137
|
-
async emitAsync(event, payload) {
|
|
138
|
-
await this._emitter.emitAsync(event, payload);
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Create an event bus for global event handling
|
|
145
|
-
*/
|
|
146
|
-
function createEventBus() {
|
|
147
|
-
const emitter = createEmitter();
|
|
148
|
-
const createScope = () => {
|
|
149
|
-
const unsubscribers = [];
|
|
150
|
-
const scopedEmitter = {
|
|
151
|
-
on: (event, handler, options) => {
|
|
152
|
-
const unsubscribe = emitter.on(event, handler, options);
|
|
153
|
-
unsubscribers.push(unsubscribe);
|
|
154
|
-
return unsubscribe;
|
|
155
|
-
},
|
|
156
|
-
once: (event, handler) => {
|
|
157
|
-
const unsubscribe = emitter.once(event, handler);
|
|
158
|
-
unsubscribers.push(unsubscribe);
|
|
159
|
-
return unsubscribe;
|
|
160
|
-
},
|
|
161
|
-
off: emitter.off,
|
|
162
|
-
emit: emitter.emit,
|
|
163
|
-
emitAsync: emitter.emitAsync,
|
|
164
|
-
removeAllListeners: emitter.removeAllListeners,
|
|
165
|
-
listenerCount: emitter.listenerCount,
|
|
166
|
-
eventNames: emitter.eventNames,
|
|
167
|
-
dispose: () => {
|
|
168
|
-
for (const unsubscribe of unsubscribers) {
|
|
169
|
-
unsubscribe();
|
|
170
|
-
}
|
|
171
|
-
unsubscribers.length = 0;
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
return scopedEmitter;
|
|
175
|
-
};
|
|
176
|
-
return {
|
|
177
|
-
...emitter,
|
|
178
|
-
createScope
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
exports.createEmitter = createEmitter;
|
|
183
|
-
exports.createEventBus = createEventBus;
|
|
184
|
-
exports.withEmitter = withEmitter;
|
|
1
|
+
"use strict";function e(){const e=new Map,t=t=>(e.has(t)||e.set(t,[]),e.get(t)),n=(e,n,o={})=>{const s={handler:n,once:o.once??!1,priority:o.priority??0};return t(e).push(s),(e=>{t(e).sort((e,t)=>t.priority-e.priority)})(e),()=>r(e,n)},r=(t,n)=>{const r=e.get(t);if(!r)return;const o=r.findIndex(e=>e.handler===n);-1!==o&&r.splice(o,1)};return{on:n,once:(e,t)=>n(e,t,{once:!0}),off:r,emit:(t,n)=>{const o=e.get(t);if(!o||0===o.length)return;const s=[...o];for(const e of s){e.once&&r(t,e.handler);try{e.handler(n)}catch(e){console.error(`Error in event handler for "${String(t)}":`,e)}}},emitAsync:async(t,n)=>{const o=e.get(t);if(!o||0===o.length)return;const s=[...o];for(const e of s){e.once&&r(t,e.handler);try{await e.handler(n)}catch(e){console.error(`Error in async event handler for "${String(t)}":`,e)}}},removeAllListeners:t=>{void 0!==t?e.delete(t):e.clear()},listenerCount:t=>e.get(t)?.length??0,eventNames:()=>Array.from(e.keys()).filter(t=>{const n=e.get(t);return void 0!==n&&n.length>0})}}exports.createEmitter=e,exports.createEventBus=function(){const t=e();return{...t,createScope:()=>{const e=[];return{on:(n,r,o)=>{const s=t.on(n,r,o);return e.push(s),s},once:(n,r)=>{const o=t.once(n,r);return e.push(o),o},off:t.off,emit:t.emit,emitAsync:t.emitAsync,removeAllListeners:t.removeAllListeners,listenerCount:t.listenerCount,eventNames:t.eventNames,dispose:()=>{for(const t of e)t();e.length=0}}}}},exports.withEmitter=function(){return function(t){return class extends t{constructor(){super(...arguments),this._emitter=e()}on(e,t,n){return this._emitter.on(e,t,n)}once(e,t){return this._emitter.once(e,t)}off(e,t){this._emitter.off(e,t)}emit(e,t){this._emitter.emit(e,t)}async emitAsync(e,t){await this._emitter.emitAsync(e,t)}}}};
|
|
@@ -1,189 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Result Pattern Implementation
|
|
5
|
-
*
|
|
6
|
-
* This module provides a Result type for functional error handling,
|
|
7
|
-
* compatible with @oxog/types when available.
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Create a success result
|
|
11
|
-
*/
|
|
12
|
-
function Ok(data) {
|
|
13
|
-
return { success: true, data, value: data };
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Create an error result
|
|
17
|
-
*/
|
|
18
|
-
function Err(error) {
|
|
19
|
-
return { success: false, error };
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Create a success result (alias)
|
|
23
|
-
*/
|
|
24
|
-
const success = Ok;
|
|
25
|
-
/**
|
|
26
|
-
* Create a failure result (alias)
|
|
27
|
-
*/
|
|
28
|
-
const failure = Err;
|
|
29
|
-
/**
|
|
30
|
-
* Check if result is Ok
|
|
31
|
-
*/
|
|
32
|
-
function isOk(result) {
|
|
33
|
-
return result.success === true;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Check if result is Err
|
|
37
|
-
*/
|
|
38
|
-
function isErr(result) {
|
|
39
|
-
return result.success === false;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Check if value is a Result
|
|
43
|
-
*/
|
|
44
|
-
function isResult(value) {
|
|
45
|
-
return (typeof value === 'object' &&
|
|
46
|
-
value !== null &&
|
|
47
|
-
'success' in value &&
|
|
48
|
-
typeof value.success === 'boolean');
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Unwrap a result, throwing the error if it's an Err
|
|
52
|
-
*/
|
|
53
|
-
function unwrap(result) {
|
|
54
|
-
if (isOk(result)) {
|
|
55
|
-
return result.data;
|
|
56
|
-
}
|
|
57
|
-
throw result.error;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Unwrap a result with a default value
|
|
61
|
-
*/
|
|
62
|
-
function unwrapOr(result, defaultValue) {
|
|
63
|
-
if (isOk(result)) {
|
|
64
|
-
return result.data;
|
|
65
|
-
}
|
|
66
|
-
return defaultValue;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Map over a successful result
|
|
70
|
-
*/
|
|
71
|
-
function map(result, fn) {
|
|
72
|
-
if (isOk(result)) {
|
|
73
|
-
return Ok(fn(result.data));
|
|
74
|
-
}
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Map over an error result
|
|
79
|
-
*/
|
|
80
|
-
function mapErr(result, fn) {
|
|
81
|
-
if (isErr(result)) {
|
|
82
|
-
return Err(fn(result.error));
|
|
83
|
-
}
|
|
84
|
-
return result;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Chain results (flatMap)
|
|
88
|
-
*/
|
|
89
|
-
function flatMap(result, fn) {
|
|
90
|
-
if (isOk(result)) {
|
|
91
|
-
return fn(result.data);
|
|
92
|
-
}
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Pattern match on a result
|
|
97
|
-
*/
|
|
98
|
-
function match(result, handlers) {
|
|
99
|
-
if (isOk(result)) {
|
|
100
|
-
return handlers.ok(result.data);
|
|
101
|
-
}
|
|
102
|
-
return handlers.err(result.error);
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Try to execute a function and wrap the result
|
|
106
|
-
*/
|
|
107
|
-
function tryCatch(fn) {
|
|
108
|
-
try {
|
|
109
|
-
return Ok(fn());
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
return Err(error instanceof Error ? error : new Error(String(error)));
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Try to execute an async function and wrap the result
|
|
117
|
-
*/
|
|
118
|
-
async function tryCatchAsync(fn) {
|
|
119
|
-
try {
|
|
120
|
-
return Ok(await fn());
|
|
121
|
-
}
|
|
122
|
-
catch (error) {
|
|
123
|
-
return Err(error instanceof Error ? error : new Error(String(error)));
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Combine multiple results into a single result
|
|
128
|
-
* Returns Ok with array of values if all succeed, Err with first error otherwise
|
|
129
|
-
*/
|
|
130
|
-
function all(results) {
|
|
131
|
-
const values = [];
|
|
132
|
-
for (const result of results) {
|
|
133
|
-
if (isErr(result)) {
|
|
134
|
-
return result;
|
|
135
|
-
}
|
|
136
|
-
values.push(result.data);
|
|
137
|
-
}
|
|
138
|
-
return Ok(values);
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Convert a nullable value to a Result
|
|
142
|
-
*/
|
|
143
|
-
function fromNullable(value, error = new Error('Value is null or undefined')) {
|
|
144
|
-
if (value === null || value === undefined) {
|
|
145
|
-
return Err(error);
|
|
146
|
-
}
|
|
147
|
-
return Ok(value);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Result utilities namespace
|
|
151
|
-
*/
|
|
152
|
-
const ResultUtils = {
|
|
153
|
-
Ok,
|
|
154
|
-
Err,
|
|
155
|
-
success,
|
|
156
|
-
failure,
|
|
157
|
-
isOk,
|
|
158
|
-
isErr,
|
|
159
|
-
isResult,
|
|
160
|
-
unwrap,
|
|
161
|
-
unwrapOr,
|
|
162
|
-
map,
|
|
163
|
-
mapErr,
|
|
164
|
-
flatMap,
|
|
165
|
-
match,
|
|
166
|
-
tryCatch,
|
|
167
|
-
tryCatchAsync,
|
|
168
|
-
all,
|
|
169
|
-
fromNullable
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
exports.Err = Err;
|
|
173
|
-
exports.Ok = Ok;
|
|
174
|
-
exports.ResultUtils = ResultUtils;
|
|
175
|
-
exports.all = all;
|
|
176
|
-
exports.failure = failure;
|
|
177
|
-
exports.flatMap = flatMap;
|
|
178
|
-
exports.fromNullable = fromNullable;
|
|
179
|
-
exports.isErr = isErr;
|
|
180
|
-
exports.isOk = isOk;
|
|
181
|
-
exports.isResult = isResult;
|
|
182
|
-
exports.map = map;
|
|
183
|
-
exports.mapErr = mapErr;
|
|
184
|
-
exports.match = match;
|
|
185
|
-
exports.success = success;
|
|
186
|
-
exports.tryCatch = tryCatch;
|
|
187
|
-
exports.tryCatchAsync = tryCatchAsync;
|
|
188
|
-
exports.unwrap = unwrap;
|
|
189
|
-
exports.unwrapOr = unwrapOr;
|
|
1
|
+
"use strict";function r(r){return{success:!0,data:r,value:r}}function t(r){return{success:!1,error:r}}const n=r,e=t;function s(r){return!0===r.success}function o(r){return!1===r.success}function u(r){return"object"==typeof r&&null!==r&&"success"in r&&"boolean"==typeof r.success}function c(r){if(s(r))return r.data;throw r.error}function a(r,t){return s(r)?r.data:t}function i(t,n){return s(t)?r(n(t.data)):t}function p(r,n){return o(r)?t(n(r.error)):r}function f(r,t){return s(r)?t(r.data):r}function l(r,t){return s(r)?t.ok(r.data):t.err(r.error)}function x(n){try{return r(n())}catch(r){return t(r instanceof Error?r:new Error(String(r)))}}async function y(n){try{return r(await n())}catch(r){return t(r instanceof Error?r:new Error(String(r)))}}function E(t){const n=[];for(const r of t){if(o(r))return r;n.push(r.data)}return r(n)}function h(n,e=new Error("Value is null or undefined")){return null==n?t(e):r(n)}const d={Ok:r,Err:t,success:n,failure:e,isOk:s,isErr:o,isResult:u,unwrap:c,unwrapOr:a,map:i,mapErr:p,flatMap:f,match:l,tryCatch:x,tryCatchAsync:y,all:E,fromNullable:h};exports.Err=t,exports.Ok=r,exports.ResultUtils=d,exports.all=E,exports.failure=e,exports.flatMap=f,exports.fromNullable=h,exports.isErr=o,exports.isOk=s,exports.isResult=u,exports.map=i,exports.mapErr=p,exports.match=l,exports.success=n,exports.tryCatch=x,exports.tryCatchAsync=y,exports.unwrap=c,exports.unwrapOr=a;
|
package/dist/cjs/errors.cjs
CHANGED
|
@@ -1,180 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var pigment = require('./pigment.cjs');
|
|
4
|
-
var errorsCore = require('./chunks/errors-core-DWxLFs2f.cjs');
|
|
5
|
-
|
|
6
|
-
// VLD Error System - Advanced error formatting and utilities
|
|
7
|
-
/**
|
|
8
|
-
* Convert VldError to a nested tree structure
|
|
9
|
-
*/
|
|
10
|
-
function treeifyError(error) {
|
|
11
|
-
const tree = { errors: [] };
|
|
12
|
-
for (const issue of error.issues) {
|
|
13
|
-
let currentNode = tree;
|
|
14
|
-
const path = issue.path;
|
|
15
|
-
// Navigate to the correct position in the tree
|
|
16
|
-
for (let i = 0; i < path.length; i++) {
|
|
17
|
-
const segment = path[i];
|
|
18
|
-
const isLast = i === path.length - 1;
|
|
19
|
-
if (typeof segment === 'string') {
|
|
20
|
-
// Object property
|
|
21
|
-
if (!currentNode.properties) {
|
|
22
|
-
currentNode.properties = {};
|
|
23
|
-
}
|
|
24
|
-
if (!currentNode.properties[segment]) {
|
|
25
|
-
currentNode.properties[segment] = { errors: [] };
|
|
26
|
-
}
|
|
27
|
-
if (isLast) {
|
|
28
|
-
currentNode.properties[segment].errors.push(issue.message);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
currentNode = currentNode.properties[segment];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
else if (typeof segment === 'number') {
|
|
35
|
-
// Array index
|
|
36
|
-
if (!currentNode.items) {
|
|
37
|
-
currentNode.items = [];
|
|
38
|
-
}
|
|
39
|
-
while (currentNode.items.length <= segment) {
|
|
40
|
-
currentNode.items.push(undefined);
|
|
41
|
-
}
|
|
42
|
-
if (!currentNode.items[segment]) {
|
|
43
|
-
currentNode.items[segment] = { errors: [] };
|
|
44
|
-
}
|
|
45
|
-
if (isLast) {
|
|
46
|
-
currentNode.items[segment].errors.push(issue.message);
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
currentNode = currentNode.items[segment];
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
// If path is empty, add to root errors
|
|
54
|
-
if (path.length === 0) {
|
|
55
|
-
tree.errors.push(issue.message);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return tree;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Convert VldError to human-readable string
|
|
62
|
-
*/
|
|
63
|
-
function prettifyError(error, options = {}) {
|
|
64
|
-
const { colored = true, theme = pigment.vldTheme, includeCode = false, includeDetails = false } = options;
|
|
65
|
-
const lines = [];
|
|
66
|
-
for (const issue of error.issues) {
|
|
67
|
-
// Symbol and message
|
|
68
|
-
const symbol = colored ? theme.symbol('✖') : '✖';
|
|
69
|
-
const message = colored ? theme.error(issue.message) : issue.message;
|
|
70
|
-
let line = `${symbol} ${message}`;
|
|
71
|
-
// Error code
|
|
72
|
-
if (includeCode) {
|
|
73
|
-
const code = colored ? theme.muted(`[${issue.code}]`) : `[${issue.code}]`;
|
|
74
|
-
line += ` ${code}`;
|
|
75
|
-
}
|
|
76
|
-
// Path
|
|
77
|
-
if (issue.path.length > 0) {
|
|
78
|
-
const pathStr = issue.path
|
|
79
|
-
.map((segment, index) => {
|
|
80
|
-
if (typeof segment === 'string') {
|
|
81
|
-
const str = index === 0 ? segment : `.${segment}`;
|
|
82
|
-
return colored ? theme.path(str) : str;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
const str = `[${segment}]`;
|
|
86
|
-
return colored ? theme.path(str) : str;
|
|
87
|
-
}
|
|
88
|
-
})
|
|
89
|
-
.join('');
|
|
90
|
-
const arrow = colored ? theme.muted('→ at ') : '→ at ';
|
|
91
|
-
line += `\n ${arrow}${pathStr}`;
|
|
92
|
-
}
|
|
93
|
-
// Details
|
|
94
|
-
if (includeDetails) {
|
|
95
|
-
if (issue.expected) {
|
|
96
|
-
const expected = colored
|
|
97
|
-
? theme.muted('expected: ') + theme.success(issue.expected)
|
|
98
|
-
: `expected: ${issue.expected}`;
|
|
99
|
-
line += `\n ${expected}`;
|
|
100
|
-
}
|
|
101
|
-
if (issue.received) {
|
|
102
|
-
const received = colored
|
|
103
|
-
? theme.muted('received: ') + theme.warning(issue.received)
|
|
104
|
-
: `received: ${issue.received}`;
|
|
105
|
-
line += `\n ${received}`;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
lines.push(line);
|
|
109
|
-
}
|
|
110
|
-
return lines.join('\n');
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Convert VldError to human-readable string with full colors
|
|
114
|
-
* Shorthand for prettifyError with colored: true
|
|
115
|
-
*/
|
|
116
|
-
function prettifyErrorColored(error, options = {}) {
|
|
117
|
-
return prettifyError(error, { ...options, colored: true });
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Convert VldError to plain text string (no colors)
|
|
121
|
-
* Shorthand for prettifyError with colored: false
|
|
122
|
-
*/
|
|
123
|
-
function prettifyErrorPlain(error, options = {}) {
|
|
124
|
-
return prettifyError(error, { ...options, colored: false });
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Flatten VldError for simple form validation
|
|
128
|
-
*/
|
|
129
|
-
function flattenError(error) {
|
|
130
|
-
const formErrors = [];
|
|
131
|
-
const fieldErrors = {};
|
|
132
|
-
for (const issue of error.issues) {
|
|
133
|
-
if (issue.path.length === 0) {
|
|
134
|
-
formErrors.push(issue.message);
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
const firstPathSegment = issue.path[0];
|
|
138
|
-
const fieldName = firstPathSegment.toString();
|
|
139
|
-
if (!fieldErrors[fieldName]) {
|
|
140
|
-
fieldErrors[fieldName] = [];
|
|
141
|
-
}
|
|
142
|
-
fieldErrors[fieldName].push(issue.message);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return { formErrors, fieldErrors };
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Create a validation issue
|
|
149
|
-
*/
|
|
150
|
-
function createIssue(code, path, message, extra) {
|
|
151
|
-
return {
|
|
152
|
-
code,
|
|
153
|
-
path: [...path],
|
|
154
|
-
message,
|
|
155
|
-
...extra
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Helper to determine the type of a value for error messages
|
|
160
|
-
*/
|
|
161
|
-
function getValueType(value) {
|
|
162
|
-
if (value === null)
|
|
163
|
-
return 'null';
|
|
164
|
-
if (value === undefined)
|
|
165
|
-
return 'undefined';
|
|
166
|
-
if (Array.isArray(value))
|
|
167
|
-
return 'array';
|
|
168
|
-
if (value instanceof Date)
|
|
169
|
-
return 'date';
|
|
170
|
-
return typeof value;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
exports.VldError = errorsCore.VldError;
|
|
174
|
-
exports.createIssue = createIssue;
|
|
175
|
-
exports.flattenError = flattenError;
|
|
176
|
-
exports.getValueType = getValueType;
|
|
177
|
-
exports.prettifyError = prettifyError;
|
|
178
|
-
exports.prettifyErrorColored = prettifyErrorColored;
|
|
179
|
-
exports.prettifyErrorPlain = prettifyErrorPlain;
|
|
180
|
-
exports.treeifyError = treeifyError;
|
|
1
|
+
"use strict";var e=require("./pigment.cjs"),r=require("./chunks/errors-core-DrydF46s.cjs");function t(r,t={}){const{colored:s=!0,theme:o=e.vldTheme,includeCode:n=!1,includeDetails:i=!1}=t,p=[];for(const e of r.issues){let r=`${s?o.symbol("✖"):"✖"} ${s?o.error(e.message):e.message}`;if(n){r+=` ${s?o.muted(`[${e.code}]`):`[${e.code}]`}`}if(e.path.length>0){const t=e.path.map((e,r)=>{if("string"==typeof e){const t=0===r?e:`.${e}`;return s?o.path(t):t}{const r=`[${e}]`;return s?o.path(r):r}}).join("");r+=`\n ${s?o.muted("→ at "):"→ at "}${t}`}if(i){if(e.expected){r+=`\n ${s?o.muted("expected: ")+o.success(e.expected):`expected: ${e.expected}`}`}if(e.received){r+=`\n ${s?o.muted("received: ")+o.warning(e.received):`received: ${e.received}`}`}}p.push(r)}return p.join("\n")}exports.VldError=r.VldError,exports.createIssue=function(e,r,t,s){return{code:e,path:[...r],message:t,...s}},exports.flattenError=function(e){const r=[],t={};for(const s of e.issues)if(0===s.path.length)r.push(s.message);else{const e=s.path[0].toString();t[e]||(t[e]=[]),t[e].push(s.message)}return{formErrors:r,fieldErrors:t}},exports.getValueType=function(e){return null===e?"null":void 0===e?"undefined":Array.isArray(e)?"array":e instanceof Date?"date":typeof e},exports.prettifyError=t,exports.prettifyErrorColored=function(e,r={}){return t(e,{...r,colored:!0})},exports.prettifyErrorPlain=function(e,r={}){return t(e,{...r,colored:!1})},exports.treeifyError=function(e){const r={errors:[]};for(const t of e.issues){let e=r;const s=t.path;for(let r=0;r<s.length;r++){const o=s[r],n=r===s.length-1;if("string"==typeof o)e.properties||(e.properties={}),e.properties[o]||(e.properties[o]={errors:[]}),n?e.properties[o].errors.push(t.message):e=e.properties[o];else if("number"==typeof o){for(e.items||(e.items=[]);e.items.length<=o;)e.items.push(void 0);e.items[o]||(e.items[o]={errors:[]}),n?e.items[o].errors.push(t.message):e=e.items[o]}}0===s.length&&r.errors.push(t.message)}return r};
|