-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.ts
201 lines (169 loc) · 6.44 KB
/
data.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
// JAUL: data.ts
/** @hidden */
const _ = require("lodash")
/** Data Utilities class. */
class DataUtils {
/**
* Removes all the specified characters from a string. For example you can cleanup
* a phone number by using removeFromString(phone, [" ", "-", "(", ")", "/"]).
* @param value The original value to be cleaned.
* @param charsToRemove Array of characters or single string to be removed from the original string.
* @returns Value with the characters removed.
*/
static removeFromString(value: string, charsToRemove: any[] | string): string {
if (!value) {
return value
}
// Make sure value is a valid string.
let result = value.toString()
if (!Array.isArray(charsToRemove)) {
charsToRemove = Array.from(charsToRemove)
}
for (let c of charsToRemove) {
result = result.split(c).join("")
}
return result
}
/**
* Masks the specified string. For eaxmple to mask a phone number but leave the
* last 4 digits visible you could use maskString(phone, "X", 4).
* @param value The original value to be masked.
* @param maskChar Optional character to be used on the masking, default is *.
* @param leaveLast Optional, leave last X positions of the string unmasked, default is 0.
* @returns The masked string.
*/
static maskString(value: string, maskChar?: string, leaveLast?: number): string {
if (!value) {
return value
}
// Make sure value is a valid string.
value = value.toString()
const separators = [" ", "-", "_", "+", "=", "/"]
if (maskChar == null || maskChar == "") {
maskChar = "*"
}
if (leaveLast == null || leaveLast < 1) {
leaveLast = 0
}
let result = ""
let i = 0
// First split characters, then iterate to replace.
const arr = value.split("")
while (i < arr.length - leaveLast) {
const char = arr[i]
if (separators.indexOf(char) < 0) {
result += maskChar
} else {
result += char
}
i++
}
// Leave last characters?
if (leaveLast > 0) {
result += value.substr(value.length - leaveLast)
}
return result
}
/**
* Minify the passed JSON value. Removes comments, unecessary white spaces etc.
* @param source The JSON string or object to be minified.
* @param asString If true, return as string instead of JSON object, default is false.
* @returns The minified JSON as object or string, depending on asString.
*/
static minifyJson(source: string, asString?: boolean): any {
if (_.isObject(source)) {
source = JSON.stringify(source, null, 0)
}
let index = 0
const {length} = source
let result = ""
let symbol = undefined
let position = undefined
// Main iterator.
while (index < length) {
symbol = source.charAt(index)
switch (symbol) {
// Ignore whitespace tokens. According to ES 5.1 section 15.12.1.1,
// whitespace tokens include tabs, carriage returns, line feeds, and
// space characters.
/* istanbul ignore next */
case "\t":
/* istanbul ignore next */
case "\r":
case "\n":
case " ":
index += 1
break
// Ignore line and block comments.
case "/":
symbol = source.charAt((index += 1))
switch (symbol) {
// Line comments.
case "/":
position = source.indexOf("\n", index)
// Check for CR-style line endings.
if (position < 0) {
position = source.indexOf("\r", index)
}
index = position > -1 ? position : length
break
// Block comments.
case "*":
position = source.indexOf("*/", index)
if (position > -1) {
// Advance the scanner's position past the end of the comment.
index = position += 2
break
}
throw new Error("Unterminated block comment.")
default:
throw new Error("Invalid comment.")
}
break
// Parse strings separately to ensure that any whitespace characters and
// JavaScript-style comments within them are preserved.
case '"':
position = index
while (index < length) {
symbol = source.charAt((index += 1))
if (symbol === "\\") {
// Skip past escaped characters.
index += 1
} else if (symbol === '"') {
break
}
}
if (source.charAt(index) === '"') {
result += source.slice(position, (index += 1))
break
}
throw new Error("Unterminated string.")
// Preserve all other characters.
default:
result += symbol
index += 1
}
}
// Check if should return as string or JSON.
if (asString) {
return result
} else {
return JSON.parse(result)
}
}
/**
* Generates a RFC4122-compliant unique ID using random numbers.
* @returns A unique ID.
*/
static uuid(): string {
const baseStr = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
const generator = function(c) {
const r = (Math.random() * 16) | 0
const v = c === "x" ? r : (r & 0x3) | 0x8
return v.toString(16)
}
return baseStr.replace(/[xy]/g, generator)
}
}
// Exports...
export = DataUtils