-
Notifications
You must be signed in to change notification settings - Fork 13
/
strings.ts
665 lines (562 loc) · 17.1 KB
/
strings.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import validator from 'validator';
import lTrim from 'lodash/trim';
import lTrimStart from 'lodash/trimStart';
import lTrimEnd from 'lodash/trimEnd';
import lCamelCase from 'lodash/camelCase';
import lSnakeCase from 'lodash/snakeCase';
import lKebabCase from 'lodash/kebabCase';
import lStartCase from 'lodash/startCase';
import { MACDelimiter } from '@terascope/types';
import { isArrayLike } from './arrays';
import { getTypeOf } from './deps';
import { bigIntToJSON } from './numbers';
/** A simplified implementation of lodash isString */
export function isString(val: unknown): val is string {
return typeof val === 'string';
}
/**
* Safely convert any input to a string
*
* @example
*
* toString(1); // '1'
* toString(0.01); // '0.01'
* toString(true); // 'true'
* toString(BigInt(2) ** BigInt(64)); // '18,446,744,073,709,551,616'
* toString(new Date('2020-09-23T14:54:21.020Z')) // '2020-09-23T14:54:21.020Z'
*/
export function toString(val: unknown): string {
if (val == null) return '';
if (typeof val === 'string') return val;
if (typeof val === 'number' || typeof val === 'symbol' || typeof val === 'boolean') {
return String(val);
}
if (typeof val === 'bigint') {
const res = bigIntToJSON(val);
if (typeof res === 'string') return res;
return `${res}`;
}
if (typeof val === 'function') {
return val.toString();
}
if (isArrayLike(val)) {
return val.map(toString).join(',');
}
if (val instanceof Date) {
return val.toISOString();
}
if (typeof val === 'object' && val != null) {
if (val[Symbol.iterator]) {
return [...val as any].map(toString).join(',');
}
// is error
if ('message' in val && 'stack' in val) {
return val.toString();
}
if (val[Symbol.toPrimitive]) {
return `${val}`;
}
if (typeof (val as any).toJSON === 'function') {
return toString((val as any).toJSON());
}
}
// fall back to this
return JSON.stringify(val);
}
/**
* Check if a value is a JavaScript primitive value OR
* it is object with Symbol.toPrimitive
*/
export function isPrimitiveValue(value: unknown): boolean {
const type = typeof value;
if (type === 'string') return true;
if (type === 'boolean') return true;
if (type === 'number') return true;
if (type === 'bigint') return true;
if (type === 'symbol') return true;
if (type === 'object' && typeof (value as any)[Symbol.toPrimitive] === 'function') return true;
return false;
}
/**
* Convert a JavaScript primitive value to a string.
* (Does not covert object like entities unless Symbol.toPrimitive is specified)
*/
export function primitiveToString(value: unknown): string {
if (value == null) return '';
if (!isPrimitiveValue(value)) {
throw new Error(`Expected ${value} (${getTypeOf(value)}) to be in a string like format`);
}
return `${value}`;
}
/** safely trims whitespace from an input */
export function trim(input: unknown, char?: string): string {
return lTrim(primitiveToString(input), char);
}
export function trimFP(char?: string) {
return function _trim(input: unknown): string {
return trim(input, char);
};
}
export function trimStart(input: unknown, char?: string): string {
return lTrimStart(primitiveToString(input), char);
}
export function trimStartFP(char?: string) {
return function _trimStart(input: unknown): string {
return trimStart(input, char);
};
}
export function trimEnd(input: unknown, char?: string): string {
return lTrimEnd(primitiveToString(input), char);
}
export function trimEndFP(char?: string) {
return function _trimEnd(input: unknown): string {
return trimEnd(input, char);
};
}
/** safely trim and to lower a input, useful for string comparison */
export function trimAndToLower(input?: string): string {
return trim(input).toLowerCase();
}
/** safely trim and to lower a input, useful for string comparison */
export function trimAndToUpper(input?: string): string {
return trim(input).toUpperCase();
}
/**
* Converts a value to upper case
*
* @example
*
* toUpperCase('lowercase'); // 'LOWERCASE'
* toUpperCase('MixEd'); // 'MIXED'
* toUpperCase('UPPERCASE'); // 'UPPERCASE'
*/
export function toUpperCase(input: unknown): string {
if (typeof input !== 'string') return '';
return input.toUpperCase();
}
/**
* Converts a value to lower case
*
* @example
*
* toLowerCase('lowercase'); // 'lowercase'
* toLowerCase('MixEd'); // 'mixed'
* toLowerCase('UPPERCASE'); // 'uppercase'
*/
export function toLowerCase(input: unknown): string {
if (typeof input !== 'string') return '';
return input.toLowerCase();
}
/** Unescape characters in string and avoid double escaping */
export function unescapeString(str = ''): string {
const len = str.length;
let unescaped = '';
for (let i = 0; i < len; i++) {
const char = str.charAt(i);
const next = i < len ? str.charAt(i + 1) : '';
if (char === '\\' && next) {
unescaped += next;
i++;
} else {
unescaped += char;
}
}
return unescaped;
}
/** A native implementation of lodash startsWith */
export function startsWith(str: unknown, val: unknown): boolean {
if (!isString(str)) return false;
if (!isString(val)) return false;
return str.startsWith(val);
}
/** A function version of startsWith */
export function startsWithFP(val: string): (input: unknown) => boolean {
if (!isString(val)) {
throw new Error(`Invalid argument "val", must be of type string, got ${getTypeOf(val)}`);
}
return function _startsWithFP(str: unknown): boolean {
return startsWith(str, val);
};
}
/** A native implementation of lodash endsWith */
export function endsWith(str: unknown, val: unknown): boolean {
if (!isString(str)) return false;
if (!isString(val)) return false;
return str.endsWith(val);
}
/** A function version of startsWith */
export function endsWithFP(val: string): (input: unknown) => boolean {
if (!isString(val)) {
throw new Error(`Invalid argument "val", must be of type string, got ${getTypeOf(val)}`);
}
return function _startsWithFP(str: unknown): boolean {
return endsWith(str, val);
};
}
/**
* Truncate a string value, by default it will add an ellipsis (...) if truncated.
*/
export function truncate(value: unknown, len: number, ellipsis = true): string {
if (value == null || value === '') return '';
if (!isString(value)) {
throw new SyntaxError(`Expected string value to truncate, got ${getTypeOf(value)}`);
}
if (!ellipsis) return value.slice(0, len);
const sliceLen = len - 4 > 0 ? len - 4 : len;
return value.length >= len ? `${value.slice(0, sliceLen)} ...` : value;
}
/**
* A functional version of truncate
*/
export function truncateFP(len: number, ellipsis = true) {
return function _truncateFP(value: unknown): string {
return truncate(value, len, ellipsis);
};
}
export const LOWER_CASE_CHARS = {
a: true,
b: true,
c: true,
d: true,
e: true,
f: true,
g: true,
h: true,
i: true,
j: true,
k: true,
l: true,
m: true,
n: true,
o: true,
p: true,
q: true,
r: true,
s: true,
t: true,
u: true,
v: true,
w: true,
x: true,
y: true,
z: true,
} as const;
export const UPPER_CASE_CHARS = {
A: true,
B: true,
C: true,
D: true,
E: true,
F: true,
G: true,
H: true,
I: true,
J: true,
K: true,
L: true,
M: true,
N: true,
O: true,
P: true,
Q: true,
R: true,
S: true,
T: true,
U: true,
V: true,
W: true,
X: true,
Y: true,
Z: true,
} as const;
export const NUM_CHARS = {
0: true,
1: true,
2: true,
3: true,
4: true,
5: true,
6: true,
7: true,
8: true,
9: true,
} as const;
export const WORD_SEPARATOR_CHARS = {
' ': true,
_: true,
'-': true,
} as const;
export const WORD_CHARS = {
...LOWER_CASE_CHARS,
...UPPER_CASE_CHARS,
...NUM_CHARS,
} as const;
/**
* Split a string and get the word parts
*/
export function getWordParts(input: string): string[] {
if (!isString(input)) {
throw new Error(`Expected string, got ${getTypeOf(input)}`);
}
const parts: string[] = [];
let word = '';
let started = false;
for (let i = 0; i < input.length; i++) {
const char = input.charAt(i);
const nextChar = input.charAt(i + 1);
if (!started && char === '_') {
if (nextChar === '_' || WORD_CHARS[nextChar]) {
word += char;
continue;
}
}
started = true;
if (char && WORD_CHARS[char]) {
word += char;
}
if (WORD_SEPARATOR_CHARS[nextChar]) {
parts.push(word);
word = '';
}
if (UPPER_CASE_CHARS[nextChar]) {
const nextNextChar = input.charAt(i + 2);
if (LOWER_CASE_CHARS[nextNextChar]) {
parts.push(word);
word = '';
}
}
}
return parts.concat(word).filter(Boolean);
}
export function toCamelCase(input: string): string {
return lCamelCase(input);
}
export function toPascalCase(input: string): string {
return firstToUpper(lCamelCase(input));
}
export function toKebabCase(input: string): string {
return lKebabCase(input);
}
export function toSnakeCase(input: string): string {
return lSnakeCase(input);
}
export function toTitleCase(input: string): string {
return lStartCase(input);
}
/**
* Make a string url/elasticsearch safe.
* safeString converts the string to lower case,
* removes any invalid characters,
* and replaces whitespace with _ (if it exists in the string) or -
* Warning this may reduce the str length
*/
export function toSafeString(input: string): string {
let s = trimAndToLower(input);
const startReg = /^[_\-+]+/;
while (startReg.test(s)) {
s = s.replace(startReg, '');
}
const whitespaceChar = s.includes('_') ? '_' : '-';
s = s.replace(/\s/g, whitespaceChar);
const reg = /[.+#*?"<>|/\\\\]/g;
s = s.replace(reg, '');
return s;
}
function _replaceFirstWordChar(str: string, fn: (char: string) => string): string {
let found = false;
return str.split('').map((s) => {
if (!found && WORD_CHARS[s]) {
found = true;
return fn(s);
}
return s;
}).join('');
}
/** Change first character in string to upper case */
export function firstToUpper(str: string): string {
if (!str) return '';
return _replaceFirstWordChar(str, (char) => char.toUpperCase());
}
/** Change first character in string to lower case */
export function firstToLower(str: string): string {
if (!str) return '';
return _replaceFirstWordChar(str, (char) => char.toLowerCase());
}
export function getFirstChar(input: string): string {
if (!input) return '';
return trim(input).charAt(0);
}
// http://www.regular-expressions.info/email.html
// not an exhaustive email regex, which is impossible, but will catch obvious errors
// is more lenient in most cases
const EmailRegex = /^[A-Z0-9._%+-@]{1,64}@(?:[A-Z0-9-]{1,63}\.){1,8}[A-Z]{2,63}$/i;
export function isEmail(input: unknown): input is string {
return isString(input) && EmailRegex.test(input);
}
const macAddressDelimiters = {
colon: /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/,
space: /^([0-9a-fA-F][0-9a-fA-F]\s){5}([0-9a-fA-F][0-9a-fA-F])$/,
dash: /^([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])$/,
dot: /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/,
none: /^([0-9a-fA-F]){12}$/
} as const;
export function isMACAddress(
input: unknown, delimiter?: MACDelimiter
): input is string {
if (!isString(input)) return false;
if (!delimiter || delimiter === 'any') {
return Object.values(macAddressDelimiters).some((d) => d.test(input));
}
return macAddressDelimiters[delimiter].test(input);
}
/**
* A functional version of isMacAddress
*/
export function isMACAddressFP(args?: MACDelimiter) {
return function _isMacAddressFP(input: unknown): input is string {
return isMACAddress(input, args);
};
}
export function isURL(input: unknown): boolean {
return isString(input) && validator.isURL(input);
}
export function isUUID(input: unknown): boolean {
return isString(input) && validator.isUUID(input);
}
/**
* Check whether a string includes another string
*/
export function contains(input: unknown, substring: string): input is string {
return isString(input) && input.includes(substring);
}
/**
* A function version of contains
*/
export function containsFP(substring: string) {
return function _contains(input: unknown): input is string {
return isString(input) && input.includes(substring);
};
}
export function isBase64(input: unknown): boolean {
if (!isString(input)) return false;
const validatorValid = validator.isBase64(input);
if (validatorValid) {
const decode = Buffer.from(input, 'base64').toString('utf8');
const encode = Buffer.from(decode, 'utf8').toString('base64');
return input === encode;
}
return false;
}
export function isFQDN(input: unknown): boolean {
return isString(input) && validator.isFQDN(input);
}
export function isCountryCode(input: unknown): boolean {
return isString(input) && validator.isISO31661Alpha2(input);
}
export function isPostalCode(input: unknown, locale: validator.PostalCodeLocale | 'any' = 'any'): boolean {
return validator.isPostalCode(toString(input), locale);
}
export function isPort(input: unknown): boolean {
return validator.isPort(toString(input));
}
export function isAlpha(input: unknown, locale?: validator.AlphaLocale): boolean {
return isString(input) && validator.isAlpha(input, locale);
}
export function isAlphaNumeric(input: unknown, locale?: validator.AlphanumericLocale): boolean {
return isString(input) && validator.isAlphanumeric(input, locale);
}
export function isMIMEType(input: unknown): boolean {
return validator.isMimeType(toString(input));
}
/**
* Maps an array of strings and and trims the result, or
* parses a comma separated list and trims the result
*/
export function parseList(input: unknown): string[] {
let strings: string[] = [];
if (isString(input)) {
strings = input.split(',');
} else if (isArrayLike(input)) {
strings = input.map((val) => {
if (!val) return '';
return toString(val);
});
} else {
return [];
}
return strings.map((s) => s.trim()).filter((s) => !!s);
}
type JoinListType = string|number|boolean|symbol|null|undefined;
/**
* Create a sentence from a list (all items will be unique, empty values will be skipped)
*/
export function joinList(
input: (JoinListType)[]|readonly (JoinListType)[],
sep = ',',
join = 'and'
): string {
if (!Array.isArray(input)) {
throw new Error('joinList requires input to be a array');
}
const list = [
...new Set(input
.filter((str) => str != null && str !== '')
.map((str) => toString(str).trim()))
];
if (list.length === 0) {
throw new Error('joinList requires at least one string');
}
if (list.length === 1) return `${list[0]}`;
return list.reduce((acc, curr, index, arr) => {
if (!acc) return curr;
const isLast = (index + 1) === arr.length;
if (isLast) {
return `${acc} ${join} ${curr}`;
}
return `${acc}${sep} ${curr}`;
}, '');
}
export type StringEntropyFN = (input: unknown) => number
// inspired from https://gist.github.com/jabney/5018b4adc9b2bf488696
/** Performs a Shannon entropy calculation on string inputs */
export function shannonEntropy(input: unknown): number {
if (!isString(input)) {
throw new Error(`Invalid input ${input}, must be of type String`);
}
let sum = 0;
const len = input.length;
const dict: Record<string, number> = Object.create(null);
// get number of chars per string
for (const char of input) {
if (dict[char] != null) {
dict[char]++;
} else {
dict[char] = 1;
}
}
for (const num of Object.values(dict)) {
const p = num / len;
const pLogCalc = p * Math.log(p);
sum -= pLogCalc / Math.log(2);
}
return sum;
}
export enum StringEntropy {
shannon = 'shannon'
}
const StringEntropyDict: Record<StringEntropy, StringEntropyFN> = {
[StringEntropy.shannon]: shannonEntropy
};
/** returns a function to perform entropy calculations, currently only supports
* the "shannon" algorithm
* */
export function stringEntropy(
algo: StringEntropy = StringEntropy.shannon
): StringEntropyFN {
const fn = StringEntropyDict[algo];
if (fn == null) {
const keys = Object.keys(StringEntropyDict);
throw new Error(`Unsupported algorithm ${algo}, please use the available algorithms ${joinList(keys, ', ')}`);
}
return fn;
}