/
index.ts
323 lines (267 loc) · 7.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { ValidationFlags } from '../types';
import { ValidationClassMap } from '../config';
export const isNaN = (value: unknown): boolean => {
// NaN is the one value that does not equal itself.
// eslint-disable-next-line
return value !== value;
};
/**
* Checks if the values are either null or undefined.
*/
export const isNullOrUndefined = (value: unknown): value is undefined | null => {
return value === null || value === undefined;
};
/**
* Creates the default flags object.
*/
export const createFlags = (): ValidationFlags => ({
untouched: true,
touched: false,
dirty: false,
pristine: true,
valid: false,
invalid: false,
validated: false,
pending: false,
required: false,
changed: false
});
/**
* Checks if the value is an object.
*/
export const isObject = (obj: unknown): obj is { [x: string]: any } =>
obj !== null && obj && typeof obj === 'object' && !Array.isArray(obj);
export function identity<T>(x: T): T {
return x;
}
/**
* Shallow object comparison.
*/
export const isEqual = (lhs: any, rhs: any): boolean => {
if (lhs instanceof RegExp && rhs instanceof RegExp) {
return isEqual(lhs.source, rhs.source) && isEqual(lhs.flags, rhs.flags);
}
if (Array.isArray(lhs) && Array.isArray(rhs)) {
if (lhs.length !== rhs.length) return false;
for (let i = 0; i < lhs.length; i++) {
if (!isEqual(lhs[i], rhs[i])) {
return false;
}
}
return true;
}
// if both are objects, compare each key recursively.
if (isObject(lhs) && isObject(rhs)) {
return (
Object.keys(lhs).every(key => {
return isEqual(lhs[key], rhs[key]);
}) &&
Object.keys(rhs).every(key => {
return isEqual(lhs[key], rhs[key]);
})
);
}
if (isNaN(lhs) && isNaN(rhs)) {
return true;
}
return lhs === rhs;
};
export const includes = (collection: any[] | string, item: any): boolean => {
return collection.indexOf(item) !== -1;
};
/**
* Parses a rule string expression.
*/
export const parseRule = (rule: string) => {
let params: string[] = [];
const name = rule.split(':')[0];
if (includes(rule, ':')) {
params = rule
.split(':')
.slice(1)
.join(':')
.split(',');
}
return { name, params };
};
/**
* Debounces a function.
*/
export const debounce = (fn: Function, wait = 0, token = { cancelled: false }) => {
if (wait === 0) {
return fn;
}
let timeout: ReturnType<typeof setTimeout> | undefined;
return (...args: any[]) => {
const later = () => {
timeout = undefined;
// check if the fn call was cancelled.
if (!token.cancelled) fn(...args);
};
// because we might want to use Node.js setTimout for SSR.
clearTimeout(timeout as any);
timeout = setTimeout(later, wait) as any;
};
};
/**
* Emits a warning to the console.
*/
export const warn = (message: string) => {
console.warn(`[vee-validate] ${message}`);
};
/**
* Normalizes the given rules expression.
*/
export const normalizeRules = (rules: any) => {
// if falsy value return an empty object.
const acc: { [x: string]: any[] } = {};
Object.defineProperty(acc, '_$$isNormalized', {
value: true,
writable: false,
enumerable: false,
configurable: false
});
if (!rules) {
return acc;
}
// Object is already normalized, skip.
if (isObject(rules) && rules._$$isNormalized) {
return rules as typeof acc;
}
if (isObject(rules)) {
return Object.keys(rules).reduce((prev, curr) => {
let params = [];
if (rules[curr] === true) {
params = [];
} else if (Array.isArray(rules[curr])) {
params = rules[curr];
} else if (isObject(rules[curr])) {
params = rules[curr];
} else {
params = [rules[curr]];
}
if (rules[curr] !== false) {
prev[curr] = params;
}
return prev;
}, acc);
}
/* istanbul ignore if */
if (typeof rules !== 'string') {
warn('rules must be either a string or an object.');
return acc;
}
return rules.split('|').reduce((prev, rule) => {
const parsedRule = parseRule(rule);
if (!parsedRule.name) {
return prev;
}
prev[parsedRule.name] = parsedRule.params;
return prev;
}, acc);
};
/**
* Checks if a function is callable.
*/
export const isCallable = (func: unknown): func is Function => typeof func === 'function';
export function computeClassObj(names: ValidationClassMap, flags: ValidationFlags) {
const acc: Record<string, boolean> = {};
const keys = Object.keys(flags);
const length = keys.length;
for (let i = 0; i < length; i++) {
const flag = keys[i];
const className = (names && names[flag]) || flag;
const value = flags[flag];
if (isNullOrUndefined(value)) {
continue;
}
if ((flag === 'valid' || flag === 'invalid') && !flags.validated) {
continue;
}
if (typeof className === 'string') {
acc[className] = value;
} else if (Array.isArray(className)) {
className.forEach(cls => {
acc[cls] = value;
});
}
}
return acc;
}
/* istanbul ignore next */
function _copyArray<T>(arrayLike: ArrayLike<T>): T[] {
const array = [];
const length = arrayLike.length;
for (let i = 0; i < length; i++) {
array.push(arrayLike[i]);
}
return array;
}
/**
* Converts an array-like object to array, provides a simple polyfill for Array.from
*/
export function toArray<T>(arrayLike: ArrayLike<T>): T[] {
if (isCallable(Array.from)) {
return Array.from(arrayLike);
}
/* istanbul ignore next */
return _copyArray(arrayLike);
}
export function findIndex<T>(arrayLike: ArrayLike<T>, predicate: (item: T, idx: number) => boolean): number {
const array = Array.isArray(arrayLike) ? arrayLike : toArray(arrayLike);
if (isCallable(array.findIndex)) {
return array.findIndex(predicate);
}
/* istanbul ignore next */
for (let i = 0; i < array.length; i++) {
if (predicate(array[i], i)) {
return i;
}
}
/* istanbul ignore next */
return -1;
}
/**
* finds the first element that satisfies the predicate callback, polyfills array.find
*/
export function find<T>(arrayLike: ArrayLike<T>, predicate: (item: T, idx: number) => boolean): T | undefined {
const array = Array.isArray(arrayLike) ? arrayLike : toArray(arrayLike);
const idx = findIndex(array, predicate);
return idx === -1 ? undefined : array[idx];
}
export function merge(target: any, source: any) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (!target[key]) {
target[key] = {};
}
merge(target[key], source[key]);
return;
}
target[key] = source[key];
});
return target;
}
export function values<T>(obj: { [x: string]: T }): T[] {
if (isCallable(Object.values)) {
return Object.values(obj);
}
// fallback to keys()
/* istanbul ignore next */
return Object.keys(obj).map(k => obj[k]);
}
export const isEmptyArray = (arr: any[]): boolean => {
return Array.isArray(arr) && arr.length === 0;
};
export const interpolate = (template: string, values: Record<string, any>): string => {
return template.replace(/\{([^}]+)\}/g, (_, p): string => {
return p in values ? values[p] : `{${p}}`;
});
};
// Checks if a given value is not an empty string or null or undefined.
export const isSpecified = (val: string | null | undefined): boolean => {
if (val === '') {
return false;
}
return !isNullOrUndefined(val);
};