-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstring-utils.ts
More file actions
91 lines (86 loc) · 2.31 KB
/
Copy pathstring-utils.ts
File metadata and controls
91 lines (86 loc) · 2.31 KB
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
/*!
* Convert JS SDK
* Version 1.0.0
* Copyright(c) 2020 Convert Insights, Inc
* License Apache-2.0
*/
import Murmurhash from 'murmurhash';
/**
* String formatter tool. Use %s for string %d for digit and %j for JSON formatting
* @param {string} template
* @param {Array<string>} args
* @return {string}
*/
export function stringFormat(template: string, ...args: any[]): string {
if (args.length) {
let i = 0;
return template
.replace(
/(%?)(%([sj]))/g,
(match: string, p1: string, p2: string, p3: string): string => {
if (p1) {
return match;
}
const arg = args[i++];
const val = typeof arg === 'function' ? arg() : arg;
switch (p3) {
case 's':
return String(val);
case 'j':
return JSON.stringify(val);
}
}
)
.replace('%%', '%');
}
return String(template);
}
/**
* String formatter tool. Transforms a space-separated string into camelCase
* @param {string} input
* @return {string}
*/
export function camelCase(input: string): string {
return input
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
})
.replace(/\s+/g, '');
}
/**
* Generate numeric hash based on seed
* @param {string} value
* @param {number=} seed
* @return {number}
*/
export function generateHash(value: string, seed = 9999): number {
return Murmurhash.v3(String(value), seed);
}
/**
* Check if a value is numeric
* @param {string | number} value
* @returns {boolean}
*/
export function isNumeric(value: string | number): boolean {
const regex = /^-?(?:(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?|\.\d+)$/;
if (typeof value === 'number') return Number.isFinite(value);
if (typeof value !== 'string' || !regex.test(value)) return false;
const num = parseFloat(value.replace(/,/g, ''));
return Number.isFinite(num);
}
/**
* Convert a string to a number
* @param {string | number} value
* @returns {number}
*/
export function toNumber(value: string | number): number {
if (typeof value === 'number') {
return value;
}
const parts = String(value).split(',');
return parseFloat(
parts[0] == '0'
? String(value).replace(/,/g, '.')
: String(value).replace(/,/g, '')
);
}