@kwiz/common 1.0.2 → 1.0.3
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/package.json +1 -1
- package/src/_dependencies.ts +0 -2
- package/src/helpers/Guid.ts +0 -182
- package/src/helpers/_dependencies.ts +0 -3
- package/src/helpers/base64.ts +0 -174
- package/src/helpers/browser.test.js +0 -9
- package/src/helpers/browser.ts +0 -1299
- package/src/helpers/browserinfo.ts +0 -293
- package/src/helpers/collections.base.test.js +0 -26
- package/src/helpers/collections.base.ts +0 -439
- package/src/helpers/collections.ts +0 -108
- package/src/helpers/color.ts +0 -55
- package/src/helpers/cookies.ts +0 -55
- package/src/helpers/date.test.js +0 -97
- package/src/helpers/date.ts +0 -163
- package/src/helpers/debug.ts +0 -187
- package/src/helpers/emails.ts +0 -7
- package/src/helpers/eval.ts +0 -5
- package/src/helpers/file.test.js +0 -51
- package/src/helpers/file.ts +0 -59
- package/src/helpers/flatted.ts +0 -150
- package/src/helpers/functions.ts +0 -17
- package/src/helpers/graph/calendar.types.ts +0 -11
- package/src/helpers/http.ts +0 -70
- package/src/helpers/images.ts +0 -23
- package/src/helpers/index.ts +0 -29
- package/src/helpers/json.ts +0 -39
- package/src/helpers/md5.ts +0 -190
- package/src/helpers/objects.test.js +0 -27
- package/src/helpers/objects.ts +0 -256
- package/src/helpers/promises.test.js +0 -18
- package/src/helpers/promises.ts +0 -102
- package/src/helpers/random.ts +0 -27
- package/src/helpers/scheduler/scheduler.test.js +0 -104
- package/src/helpers/scheduler/scheduler.ts +0 -132
- package/src/helpers/sharepoint.ts +0 -681
- package/src/helpers/strings.test.js +0 -43
- package/src/helpers/strings.ts +0 -288
- package/src/helpers/typecheckers.test.js +0 -35
- package/src/helpers/typecheckers.ts +0 -263
- package/src/helpers/url.test.js +0 -18
- package/src/helpers/url.ts +0 -202
- package/src/helpers/urlhelper.ts +0 -104
- package/src/index.ts +0 -24
- package/src/types/common.types.ts +0 -16
- package/src/types/flatted.types.ts +0 -60
- package/src/types/globals.types.ts +0 -7
- package/src/types/graph/calendar.types.ts +0 -81
- package/src/types/graph/index.ts +0 -1
- package/src/types/index.ts +0 -12
- package/src/types/knownscript.types.ts +0 -19
- package/src/types/libs/datajs.types.ts +0 -29
- package/src/types/libs/ics.types.ts +0 -31
- package/src/types/libs/index.ts +0 -4
- package/src/types/libs/msal.types.ts +0 -79
- package/src/types/locales.ts +0 -125
- package/src/types/localstoragecache.types.ts +0 -9
- package/src/types/moment.ts +0 -7
- package/src/types/regex.types.ts +0 -17
- package/src/types/rest.types.ts +0 -94
- package/src/types/sharepoint.types.ts +0 -1413
- package/src/types/sharepoint.utils.types.ts +0 -246
- package/src/utils/_dependencies.ts +0 -4
- package/src/utils/base64.ts +0 -27
- package/src/utils/consolelogger.ts +0 -315
- package/src/utils/date.ts +0 -36
- package/src/utils/emails.ts +0 -25
- package/src/utils/index.ts +0 -13
- package/src/utils/knownscript.ts +0 -280
- package/src/utils/localstoragecache.ts +0 -419
- package/src/utils/rest.ts +0 -465
- package/src/utils/script.ts +0 -168
- package/src/utils/sharepoint.rest/common.ts +0 -149
- package/src/utils/sharepoint.rest/date.ts +0 -61
- package/src/utils/sharepoint.rest/file.folder.ts +0 -368
- package/src/utils/sharepoint.rest/index.ts +0 -11
- package/src/utils/sharepoint.rest/item.ts +0 -456
- package/src/utils/sharepoint.rest/list.ts +0 -1144
- package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +0 -750
- package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +0 -265
- package/src/utils/sharepoint.rest/listutils/common.ts +0 -202
- package/src/utils/sharepoint.rest/user-search.ts +0 -254
- package/src/utils/sharepoint.rest/user.ts +0 -447
- package/src/utils/sharepoint.rest/web.ts +0 -1031
- package/src/utils/sod.ts +0 -193
- package/tsconfig.json +0 -21
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import assert from 'assert/strict';
|
|
2
|
-
import test from 'node:test';
|
|
3
|
-
import { capitalizeFirstLetter, escapeXml, replaceAll, replaceRegex } from './strings';
|
|
4
|
-
|
|
5
|
-
test('replaceAll', t => {
|
|
6
|
-
assert.strictEqual(replaceAll("hello old#@! world old#@! !", "old#@!", "new!@$"), "hello new!@$ world new!@$ !");
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
test('capitalizeFirstLetter', async t => {
|
|
10
|
-
assert.strictEqual(capitalizeFirstLetter(), "");
|
|
11
|
-
assert.strictEqual(capitalizeFirstLetter("a"), "A");
|
|
12
|
-
assert.strictEqual(capitalizeFirstLetter("hello"), "Hello");
|
|
13
|
-
assert.strictEqual(capitalizeFirstLetter("hello world!"), "Hello world!");
|
|
14
|
-
assert.strictEqual(capitalizeFirstLetter("helloworld"), "Helloworld");
|
|
15
|
-
assert.strictEqual(capitalizeFirstLetter(""), "");
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test('escapeXml', async t => {
|
|
19
|
-
assert.strictEqual(escapeXml(), "");
|
|
20
|
-
assert.strictEqual(escapeXml(""), "");
|
|
21
|
-
assert.strictEqual(escapeXml(" "), " ");
|
|
22
|
-
assert.strictEqual(escapeXml("<test />"), "<test />");
|
|
23
|
-
assert.strictEqual(escapeXml(`full scope: <>&'"`), `full scope: <>&'"`);
|
|
24
|
-
assert.strictEqual(escapeXml(undefined, true), "");
|
|
25
|
-
assert.strictEqual(escapeXml("", true), "");
|
|
26
|
-
assert.strictEqual(escapeXml(" ", true), " ");
|
|
27
|
-
assert.strictEqual(escapeXml("<test />", true), "<test />");
|
|
28
|
-
assert.strictEqual(escapeXml(`full scope: <>&'"`, true), `full scope: <>&'"`);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('replaceRegex', t => {
|
|
32
|
-
/** Match anything between {zone:*} */
|
|
33
|
-
var matchZones = /{zone:\w+}/gi;
|
|
34
|
-
|
|
35
|
-
var source = `text {zone:one} {zone:two}
|
|
36
|
-
more text
|
|
37
|
-
{zone:three}`;
|
|
38
|
-
var result = `text ONE TWO
|
|
39
|
-
more text
|
|
40
|
-
THREE`;
|
|
41
|
-
|
|
42
|
-
assert.strictEqual(replaceRegex(source, matchZones, m => m.slice(1, m.length - 1).split(':')[1].toLocaleUpperCase()), result);
|
|
43
|
-
});
|
package/src/helpers/strings.ts
DELETED
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
import { IDictionary } from "./_dependencies";
|
|
2
|
-
import { makeUniqueArray } from "./collections.base";
|
|
3
|
-
import { isNullOrEmptyString, isNullOrUndefined, isNumber, isString } from "./typecheckers";
|
|
4
|
-
|
|
5
|
-
export function endsWith(str: string, value: string, ignoreCase?: boolean): boolean {
|
|
6
|
-
let str1 = str;
|
|
7
|
-
let find = value;
|
|
8
|
-
if (ignoreCase) {
|
|
9
|
-
str1 = str1.toLowerCase();
|
|
10
|
-
find = find.toLowerCase();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return str1.substr(str1.length - find.length) === find;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function startsWith(str: string, value: string, ignoreCase?: boolean): boolean {
|
|
17
|
-
let str1 = str;
|
|
18
|
-
let find = value;
|
|
19
|
-
if (ignoreCase) {
|
|
20
|
-
str1 = str1.toLowerCase();
|
|
21
|
-
find = find.toLowerCase();
|
|
22
|
-
}
|
|
23
|
-
return str1.substr(0, find.length) === find;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** remove space at start or end */
|
|
27
|
-
export function trim(str: string): string {
|
|
28
|
-
return str.replace(/^\s+|\s+$/g, '');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function trimEnd(str: string): string {
|
|
32
|
-
return str.replace(/\s+$/, "");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function trimStart(str: string): string {
|
|
36
|
-
return str.replace(/^\s+/, "");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function splice(str: string, start: number, delCount: number, insert: string) {
|
|
40
|
-
return str.slice(0, start) + insert + str.slice(start + Math.abs(delCount));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function padRight(value: string | number, length: number, fillString: string = "0") {
|
|
44
|
-
if (isNumber(value))
|
|
45
|
-
value = value.toString(10);
|
|
46
|
-
let pad = isNullOrEmptyString(fillString) ? "0" : fillString[0];
|
|
47
|
-
return value + Array(length - value.length + 1).join(pad);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function padLeft(value: string | number, length: number, fillString: string = "0") {
|
|
51
|
-
if (isNumber(value))
|
|
52
|
-
value = value.toString(10);
|
|
53
|
-
let pad = isNullOrEmptyString(fillString) ? "0" : fillString[0];
|
|
54
|
-
return Array(length - String(value).length + 1).join(pad) + value;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** returns array of [token] found inside the string
|
|
58
|
-
* supports token formats [Author??Created by {0}::Unknown author] will return "Author" as the token
|
|
59
|
-
* */
|
|
60
|
-
export function GetTokens(StringFormat: string): string[] {
|
|
61
|
-
let tokensResult: string[] = [];
|
|
62
|
-
if (isNullOrEmptyString(StringFormat)) return tokensResult;
|
|
63
|
-
|
|
64
|
-
let tokens = StringFormat.match(/\[[^\]]*\]/g);
|
|
65
|
-
if (tokens && tokens.length > 0) {
|
|
66
|
-
tokens.forEach(token => {
|
|
67
|
-
let key = token.slice(1, token.length - 1);
|
|
68
|
-
key = GetTokenInfo(key).tokenName;
|
|
69
|
-
if (tokensResult.indexOf(key) < 0)
|
|
70
|
-
tokensResult.push(key);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return tokensResult;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** replaces a string with [token] and [otherToken] with their matched provided values
|
|
78
|
-
* supports token formats [Author??Created by {0}::Unknown author]
|
|
79
|
-
*/
|
|
80
|
-
export function ReplaceTokens(StringFormat: string, TokenValues: IDictionary<string>, options?: {
|
|
81
|
-
/**set to true if you want to keep "[token]" in the string when a token value wasn't provided */
|
|
82
|
-
keepMissingTokens?: boolean;
|
|
83
|
-
}): string {
|
|
84
|
-
|
|
85
|
-
let skipMissingTokens = options && options.keepMissingTokens;
|
|
86
|
-
|
|
87
|
-
if (isNullOrUndefined(StringFormat)) return null;
|
|
88
|
-
if (StringFormat !== '') {
|
|
89
|
-
let tokens = StringFormat.match(/\[[^\]]*\]/g);
|
|
90
|
-
if (tokens && tokens.length > 0) {
|
|
91
|
-
if (isNullOrUndefined(TokenValues)) TokenValues = {};
|
|
92
|
-
|
|
93
|
-
tokens.forEach(token => {
|
|
94
|
-
let key = token.slice(1, token.length - 1);
|
|
95
|
-
let tokenInfo = GetTokenInfo(key);
|
|
96
|
-
let value = TokenValues[tokenInfo.tokenName];
|
|
97
|
-
let skip = false;
|
|
98
|
-
if (isNullOrUndefined(value)) {
|
|
99
|
-
value = "";
|
|
100
|
-
skip = skipMissingTokens;//if true we won't replace this one
|
|
101
|
-
}
|
|
102
|
-
if (!skip || tokenInfo.hasFormat)
|
|
103
|
-
StringFormat = StringFormat.replace(token, tokenInfo.getValue(value));
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return StringFormat;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** Normalizes a guid string, lower case and removes {} */
|
|
111
|
-
export function normalizeGuid(text: string, removeDashes?: boolean): string {
|
|
112
|
-
if (isNullOrEmptyString(text) || !isString(text)) {
|
|
113
|
-
return text;
|
|
114
|
-
}
|
|
115
|
-
var guid = text.toLowerCase().trim();
|
|
116
|
-
|
|
117
|
-
if (guid.startsWith("{")) {
|
|
118
|
-
guid = guid.substr(1);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (guid.endsWith("}")) {
|
|
122
|
-
guid = guid.substr(0, guid.length - 1);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (removeDashes) {
|
|
126
|
-
guid = guid.replace(/-/g, '');
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return guid;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function isEmptyGuid(guid: { toString(): string; }) {
|
|
133
|
-
if (isNullOrEmptyString(guid)) return true;
|
|
134
|
-
else if (Number(normalizeGuid(guid.toString(), true)) === 0) return true;
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function escapeRegExp(text: string) {
|
|
139
|
-
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function isValidDomainLogin(login: string) {
|
|
143
|
-
return /^[A-Za-z0-9\\._-]{7,}$/.test(login);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function stripRichTextWhitespace(value: string) {
|
|
147
|
-
// richText fields in have random markup even when field is empty
|
|
148
|
-
// \u200B zero width space
|
|
149
|
-
// \u200C zero width non-joiner Unicode code point
|
|
150
|
-
// \u200D zero width joiner Unicode code point
|
|
151
|
-
// \uFEFF zero width no-break space Unicode code point
|
|
152
|
-
return isString(value) ? value.replace(/[\u200B-\u200D\uFEFF]/g, "") : value;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** allows min length 1, letters, numbers underscore only */
|
|
156
|
-
export function isValidVarName(text: string) {
|
|
157
|
-
return /^[A-Za-z0-9_]{1,}$/.test(text);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/** allows min length 1, letters, numbers underscore and hyphen only */
|
|
161
|
-
export function isValidHeaderName(text: string) {
|
|
162
|
-
return /^[A-Za-z0-9_-]{1,}$/.test(text);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/** returns token info with format */
|
|
166
|
-
export function GetTokenInfo(text: string) {
|
|
167
|
-
let split = text.split('??');
|
|
168
|
-
let hasFormat = split.length > 0 && !isNullOrEmptyString(split[1]);
|
|
169
|
-
let formatSplit = hasFormat ? split[1].split('::') : [];
|
|
170
|
-
let valueIfEmpty = formatSplit.length > 1 ? formatSplit[1] : "";
|
|
171
|
-
let formatIfNotEmpty = formatSplit[0];
|
|
172
|
-
let info = {
|
|
173
|
-
tokenName: hasFormat ? split[0] : text,
|
|
174
|
-
hasFormat: hasFormat,
|
|
175
|
-
getValue: (value: string) => {
|
|
176
|
-
if (!hasFormat) return value;
|
|
177
|
-
else {
|
|
178
|
-
if (isNullOrEmptyString(value)) return valueIfEmpty;
|
|
179
|
-
else return formatIfNotEmpty.replace('{0}', value);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
return info;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/** return true if both strings are the same, or both are empty/null/undefined */
|
|
187
|
-
export function stringEqualsOrEmpty(str1: string, str2: string, ignoreCase?: boolean) {
|
|
188
|
-
if (isNullOrEmptyString(str1) && isNullOrEmptyString(str2)) return true;
|
|
189
|
-
if (ignoreCase) {
|
|
190
|
-
if (!isNullOrEmptyString(str1)) str1 = str1.toLowerCase();
|
|
191
|
-
if (!isNullOrEmptyString(str2)) str2 = str2.toLowerCase();
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return str1 === str2;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/** return true if str1 contains str2 */
|
|
198
|
-
export function stringContains(str1: string, str2: string, ignoreCase?: boolean) {
|
|
199
|
-
if (isNullOrEmptyString(str1) && isNullOrEmptyString(str2)) return true;
|
|
200
|
-
|
|
201
|
-
if (isNullOrEmptyString(str1))
|
|
202
|
-
str1 = "";
|
|
203
|
-
if (isNullOrEmptyString(str2))
|
|
204
|
-
str2 = "";
|
|
205
|
-
|
|
206
|
-
if (ignoreCase) {
|
|
207
|
-
str1 = str1.toLowerCase();
|
|
208
|
-
str2 = str2.toLowerCase();
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return str1.indexOf(str2) >= 0;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export function cleanupString(str: string, options: {
|
|
215
|
-
replaceNewLines?: string;
|
|
216
|
-
collapseMultipleSpaces?: string;
|
|
217
|
-
collapseMultipleDashes?: string;
|
|
218
|
-
collapseMultipleUnderscore?: string;
|
|
219
|
-
}) {
|
|
220
|
-
if (isString(options.replaceNewLines))
|
|
221
|
-
str = str.replace(/\r/g, '')//no returns
|
|
222
|
-
.replace(/\n/g, options.replaceNewLines);//no line breaks
|
|
223
|
-
if (isString(options.collapseMultipleDashes))
|
|
224
|
-
str = str.replace(/-+/g, options.collapseMultipleSpaces);//no extra spaces
|
|
225
|
-
if (isString(options.collapseMultipleUnderscore))
|
|
226
|
-
str = str.replace(/_+/g, options.collapseMultipleUnderscore);//no extra spaces
|
|
227
|
-
|
|
228
|
-
// do this last, so it will collapse spaces added by previous options
|
|
229
|
-
if (isString(options.collapseMultipleSpaces)) {
|
|
230
|
-
str = str.replace(new RegExp(String.fromCharCode(160), "g"), '')//get rid of non-breaking spaces
|
|
231
|
-
.replace(/ +/g, options.collapseMultipleSpaces);//no extra spaces
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return str;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/** normalizes   to see Issue 752 */
|
|
238
|
-
export function normalizeHtmlSpace(html: string) {
|
|
239
|
-
if (isNullOrEmptyString(html)) return html;
|
|
240
|
-
return html.replace(/ /i, " ");
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export function replaceAll(str: string, find: string, replace: string, ignoreCase = false) {
|
|
244
|
-
return str.replace(new RegExp(find, `g${ignoreCase ? 'i' : ''}`), replace);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
export function capitalizeFirstLetter(str: string) {
|
|
248
|
-
return isNullOrEmptyString(str)
|
|
249
|
-
? ""
|
|
250
|
-
: `${str.charAt(0).toUpperCase()}${str.substring(1)}`;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
export function escapeXml(unsafe: string, isAttribute = false) {
|
|
254
|
-
if (isNullOrEmptyString(unsafe)) return "";
|
|
255
|
-
return isAttribute
|
|
256
|
-
? unsafe.replace(/[<>&'"]/g, (c) => {
|
|
257
|
-
switch (c) {
|
|
258
|
-
case '<': return '<';
|
|
259
|
-
case '>': return '>';
|
|
260
|
-
case '&': return '&';
|
|
261
|
-
case '\'': return ''';
|
|
262
|
-
case '"': return '"';
|
|
263
|
-
}
|
|
264
|
-
return c;
|
|
265
|
-
})
|
|
266
|
-
: unsafe.replace(/[<>&]/g, (c) => {
|
|
267
|
-
switch (c) {
|
|
268
|
-
case '<': return '<';
|
|
269
|
-
case '>': return '>';
|
|
270
|
-
case '&': return '&';
|
|
271
|
-
}
|
|
272
|
-
return c;
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/** uses regex str.match to replace each match by calling the replacer function (imported from CMS) */
|
|
277
|
-
export function replaceRegex(str: string, regex: RegExp, replacer: (match: string) => string | null) {
|
|
278
|
-
let matches = str.match(regex);
|
|
279
|
-
if (!matches || matches.length < 1) return str;
|
|
280
|
-
//replace each found token only once
|
|
281
|
-
let unique = makeUniqueArray(matches);
|
|
282
|
-
unique.forEach(m => {
|
|
283
|
-
let replacement = replacer(m);
|
|
284
|
-
if (!isNullOrUndefined(replacement))//ignore nulls
|
|
285
|
-
str = replaceAll(str, m, replacement);
|
|
286
|
-
});
|
|
287
|
-
return str;
|
|
288
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import assert from 'assert/strict';
|
|
2
|
-
import test from 'node:test';
|
|
3
|
-
import { isEmptyObject, isNullOrEmptyString, isPrimitiveValue } from './typecheckers';
|
|
4
|
-
|
|
5
|
-
test('isNullOrEmptyString', async t => {
|
|
6
|
-
// This test passes because it does not throw an exception.
|
|
7
|
-
await t.test("null", t => assert.strictEqual(isNullOrEmptyString(null), true));
|
|
8
|
-
await t.test("undefined", t => assert.strictEqual(isNullOrEmptyString(undefined), true));
|
|
9
|
-
await t.test("empty string", t => assert.strictEqual(isNullOrEmptyString(""), true));
|
|
10
|
-
await t.test("space", t => assert.strictEqual(isNullOrEmptyString(" "), false));
|
|
11
|
-
await t.test("0", t => assert.strictEqual(isNullOrEmptyString(0), false));
|
|
12
|
-
await t.test("1", t => assert.strictEqual(isNullOrEmptyString(1), false));
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test('isEmptyObject', async t => {
|
|
16
|
-
await t.test("null", t => assert.strictEqual(isEmptyObject(null), true));
|
|
17
|
-
await t.test("undefined", t => assert.strictEqual(isEmptyObject(undefined), true));
|
|
18
|
-
await t.test("[]", t => assert.strictEqual(isEmptyObject([]), true));
|
|
19
|
-
await t.test("{}", t => assert.strictEqual(isEmptyObject({}), true));
|
|
20
|
-
await t.test("ignore keys", t => assert.strictEqual(isEmptyObject({ test: 1 }, { ignoreKeys: ["test"] }), true));
|
|
21
|
-
await t.test("[1]", t => assert.strictEqual(isEmptyObject([1]), false));
|
|
22
|
-
await t.test("non empty object", t => assert.strictEqual(isEmptyObject({ test: 1 }), false));
|
|
23
|
-
await t.test("non empty object with ignored key", t => assert.strictEqual(isEmptyObject({ test: 1, test2: 2 }, { ignoreKeys: ["test"] }), false));
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('isPrimitiveValue', async t => {
|
|
27
|
-
assert.strictEqual(isPrimitiveValue(1.42), true);
|
|
28
|
-
assert.strictEqual(isPrimitiveValue(new Date()), true);
|
|
29
|
-
assert.strictEqual(isPrimitiveValue({ title: "hello" }), false);
|
|
30
|
-
assert.strictEqual(isPrimitiveValue("hello"), true);
|
|
31
|
-
assert.strictEqual(isPrimitiveValue(() => { }), false);
|
|
32
|
-
assert.strictEqual(isPrimitiveValue([1, 2, 3]), false);
|
|
33
|
-
assert.strictEqual(isPrimitiveValue([]), false);
|
|
34
|
-
assert.strictEqual(isPrimitiveValue(), true);
|
|
35
|
-
});
|
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
var _objectTypes = {
|
|
2
|
-
"Function": typeof (() => {/*empty*/ }),
|
|
3
|
-
"Undefined": typeof (undefined),
|
|
4
|
-
"String": typeof (""),
|
|
5
|
-
"Number": typeof (1),
|
|
6
|
-
"Object": typeof ({}),
|
|
7
|
-
"Boolean": typeof (true)
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
/** check if a global object in that full name exists and return its type or "undefined" */
|
|
11
|
-
export function typeofFullName(fullName: string, windowOrParent?: Window | any) {
|
|
12
|
-
//todo: possible to merge with getFromFullName, but we need to distinguish between null and undefined
|
|
13
|
-
|
|
14
|
-
if (!fullName) {
|
|
15
|
-
return _objectTypes.Undefined;
|
|
16
|
-
}
|
|
17
|
-
try {
|
|
18
|
-
let names = fullName.split(".");
|
|
19
|
-
let len = names.length;
|
|
20
|
-
let obj = windowOrParent || window;
|
|
21
|
-
|
|
22
|
-
for (var i = 0; i < len; i++) {
|
|
23
|
-
obj = obj[names[i]];
|
|
24
|
-
if (typeof obj === _objectTypes.Undefined)
|
|
25
|
-
return _objectTypes.Undefined;
|
|
26
|
-
if (obj === null && i < len)//one of the chained objects (not the leaf) is null - so return undefined
|
|
27
|
-
return _objectTypes.Undefined;
|
|
28
|
-
}
|
|
29
|
-
return typeof obj;
|
|
30
|
-
} catch (ex) {
|
|
31
|
-
return _objectTypes.Undefined;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** get the value by full name of property */
|
|
36
|
-
export function getFromFullName<T>(fullName: string, windowOrParent?: Window | any) {
|
|
37
|
-
try {
|
|
38
|
-
if (isNullOrEmptyString(fullName)) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
try {
|
|
42
|
-
var names = fullName.split(".");
|
|
43
|
-
var len = names.length;
|
|
44
|
-
var obj = windowOrParent || window;
|
|
45
|
-
|
|
46
|
-
for (var i = 0; i < len; i++) {
|
|
47
|
-
obj = obj[names[i]];
|
|
48
|
-
if (typeof obj === _objectTypes.Undefined || obj === null) {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return <T>obj;
|
|
53
|
-
} catch (ex) {
|
|
54
|
-
}
|
|
55
|
-
} catch (e) { }
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function isTypeofFullNameObject(fullName: string, windowOrParent?: Window | any): boolean {
|
|
60
|
-
return typeofFullName(fullName, windowOrParent) === _objectTypes.Object;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function isTypeofFullNameString(fullName: string, windowOrParent?: Window | any): boolean {
|
|
64
|
-
return typeofFullName(fullName, windowOrParent) === _objectTypes.String;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function isTypeofFullNameNumber(fullName: string, windowOrParent?: Window | any): boolean {
|
|
68
|
-
return typeofFullName(fullName, windowOrParent) === _objectTypes.Number;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function isTypeofFullNameNullOrUndefined(fullName: string, windowOrParent?: Window | any): boolean {
|
|
72
|
-
if (typeofFullName(fullName, windowOrParent) === _objectTypes.Undefined) {
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
var names = fullName.split(".");
|
|
78
|
-
var len = names.length;
|
|
79
|
-
var obj = windowOrParent || window;
|
|
80
|
-
|
|
81
|
-
for (var i = 0; i < len && obj !== null; i++) {
|
|
82
|
-
obj = obj[names[i]];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return obj === null || obj === undefined;
|
|
86
|
-
} catch (ex) {
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function isTypeofFullNameUndefined(fullName: string, windowOrParent?: Window | any): boolean {
|
|
92
|
-
return typeofFullName(fullName, windowOrParent) === _objectTypes.Undefined;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function isTypeofFullNameFunction(fullName: string, windowOrParent?: Window | any): boolean {
|
|
96
|
-
return typeofFullName(fullName, windowOrParent) === _objectTypes.Function;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function isTypeofFullNameBoolean(fullName: string, windowOrParent?: Window | any): boolean {
|
|
100
|
-
return typeofFullName(fullName, windowOrParent) === _objectTypes.Boolean;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function isType(obj: any, str: string) {
|
|
104
|
-
return typeof (obj) === str;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
108
|
-
export function isObject(obj: any): obj is object {
|
|
109
|
-
return isType(obj, _objectTypes.Object);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Checks if obj is empty - as primitive, array or object
|
|
113
|
-
* If an object, pass an optional array of keys to ignore
|
|
114
|
-
*/
|
|
115
|
-
export function isEmptyObject(obj: any, options?: { ignoreKeys?: string[] }) {
|
|
116
|
-
let ignoreKeys = options && options.ignoreKeys || [];
|
|
117
|
-
return isNullOrUndefined(obj) || (Array.isArray(obj) && obj.length === 0) ||
|
|
118
|
-
(isObject(obj) && Object.keys(obj)
|
|
119
|
-
//if options.ignoreKeys is not empty - only include keys that are NOT in this array
|
|
120
|
-
.filter(key => SafeIfElse(() => ignoreKeys.indexOf(key) < 0, true))
|
|
121
|
-
.length === 0);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function isNullOrUndefined(obj: any) {
|
|
125
|
-
return isUndefined(obj) || obj === null;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** return true if o is undefined, null or not a number */
|
|
129
|
-
export function isNullOrNaN(o: any): boolean {
|
|
130
|
-
return isNullOrEmptyString(o) || isNaN(o);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** return true if o is undefined, null or empty string */
|
|
134
|
-
export function isNullOrEmptyString(o: any): o is null | undefined | "" {
|
|
135
|
-
return isNullOrUndefined(o) || o === '';
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/** o is an array that is not empty (length > 0) */
|
|
139
|
-
export function isNotEmptyArray(o: any): o is any[] {
|
|
140
|
-
return Array.isArray(o) && o.length > 0;
|
|
141
|
-
}
|
|
142
|
-
/** o is undefined, null or an empty array */
|
|
143
|
-
export function isNullOrEmptyArray(o: any): boolean {
|
|
144
|
-
return isNullOrUndefined(o) || (Array.isArray(o) && o.length < 1);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export function isString(obj: any): obj is string {
|
|
148
|
-
return isType(obj, _objectTypes.String);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** true if object is a Date object */
|
|
152
|
-
export function isDate(obj: any): obj is Date {
|
|
153
|
-
return !isNullOrUndefined(obj) && isFunction(obj.getTime) && !isNullOrNaN((obj as Date).getTime());
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/** true if obj is a number or a numeric string */
|
|
157
|
-
export function isNumeric(obj: any): obj is number | string {
|
|
158
|
-
return !isNullOrEmptyString(obj) && !Array.isArray(obj) &&//[14] will return true, since [14].toString() is "14"
|
|
159
|
-
!isNaN(parseFloat(obj as string)) && isFinite(obj as number) && isType(Number(obj), _objectTypes.Number);
|
|
160
|
-
}
|
|
161
|
-
/** true if obj is a number */
|
|
162
|
-
export function isNumber(obj?: any): obj is number {
|
|
163
|
-
return !isNullOrNaN(obj) && isType(obj, _objectTypes.Number);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function isNumberArray(obj: any[]): obj is number[] {
|
|
167
|
-
return !isNullOrUndefined(obj) && Array.isArray(obj) && obj.every((entry) => {
|
|
168
|
-
return isNumber(entry);
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export function isUndefined(obj: any): obj is undefined {
|
|
173
|
-
return isType(obj, _objectTypes.Undefined);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
//eslint-disable-next-line @typescript-eslint/ban-types
|
|
177
|
-
export function isFunction(obj: any): obj is Function {
|
|
178
|
-
return isType(obj, _objectTypes.Function);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export function isBoolean(obj: any): obj is boolean {
|
|
182
|
-
return isType(obj, _objectTypes.Boolean);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export function isPromise<t>(obj: any): obj is Promise<t> {
|
|
186
|
-
return obj && isFunction(obj["then"]);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
export type primitiveTypes = string | number | Date | boolean | null;
|
|
190
|
-
/**returns true if object is string, number, date, boolean value or null*/
|
|
191
|
-
export function isPrimitiveValue(obj: any): obj is primitiveTypes {
|
|
192
|
-
return isNullOrUndefined(obj) || isString(obj) || isNumber(obj) || isDate(obj) || isBoolean(obj);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export function isValidGuid(str: string) {
|
|
196
|
-
var a = new RegExp("^[{|\\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\\)|}]?$");
|
|
197
|
-
return !!a.exec(str);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
export var BoolTrueStrings = ["true", "1", "on", "yes"];
|
|
201
|
-
export var BoolFalseStrings = ["false", "0", "off", "no"];
|
|
202
|
-
export function isTrueString(str: string, options?: { allowPositiveNumbers?: boolean; }) {
|
|
203
|
-
if (isNullOrEmptyString(str)) return false;
|
|
204
|
-
else if (BoolTrueStrings.includes(str.toLowerCase()))
|
|
205
|
-
return true;
|
|
206
|
-
else if (options && options.allowPositiveNumbers && isNumeric(str))
|
|
207
|
-
return Number(str) > 0;//any number greater than 0 is considered true.
|
|
208
|
-
else return false;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
export function newGuid() {
|
|
212
|
-
var S4 = () => {
|
|
213
|
-
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
|
214
|
-
};
|
|
215
|
-
// then to call it, plus stitch in '4' in the third group
|
|
216
|
-
return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0, 3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/** check if object has all members (they are not undefined) */
|
|
220
|
-
export function hasMembers(o: any, ...members: string[]) {
|
|
221
|
-
if (!isNullOrUndefined(o)) {
|
|
222
|
-
for (let i = 0; i < members.length; i++)
|
|
223
|
-
if (isUndefined(o[members[i]]))
|
|
224
|
-
return false;
|
|
225
|
-
return true;//not null and all members exist
|
|
226
|
-
}
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/** compares 2 versions. if v1 is bigger return 1, if v2 is bigger returns -1, if equals return 0 */
|
|
231
|
-
export function CompareVersion(v1: string, v2: string) {
|
|
232
|
-
let v1Split = v1 && v1.split('.').map(s => parseInt(s, 10)) || [];
|
|
233
|
-
let v2Split = v2 && v2.split('.').map(s => parseInt(s, 10)) || [];
|
|
234
|
-
|
|
235
|
-
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
|
|
236
|
-
let p1 = v1Split[i];
|
|
237
|
-
let p2 = v2Split[i];
|
|
238
|
-
if (isNaN(p1)) p1 = -1;
|
|
239
|
-
if (isNaN(p2)) p2 = -1;
|
|
240
|
-
|
|
241
|
-
if (p1 > p2) return 1;
|
|
242
|
-
else if (p1 < p2) return -1;
|
|
243
|
-
}
|
|
244
|
-
//finished while equal? return 0
|
|
245
|
-
return 0;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/** pass along a list of unsafe tests to get a value, the first one that doesn't throw an exception and doesnt return null will get returned */
|
|
249
|
-
export function SafeIfElse<T>(...list: ((() => T) | T)[]) {
|
|
250
|
-
for (let i = 0; i < list.length; i++) {
|
|
251
|
-
let v: T = null;
|
|
252
|
-
let getter = list[i];
|
|
253
|
-
if (isFunction(getter)) {
|
|
254
|
-
try {
|
|
255
|
-
v = getter();
|
|
256
|
-
} catch (e) { v = null; }
|
|
257
|
-
}
|
|
258
|
-
else v = getter;
|
|
259
|
-
|
|
260
|
-
if (!isNullOrUndefined(v)) return v;
|
|
261
|
-
}
|
|
262
|
-
return null;
|
|
263
|
-
}
|
package/src/helpers/url.test.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import assert from 'assert/strict';
|
|
2
|
-
import test from 'node:test';
|
|
3
|
-
import { parseHash, parseQueryString } from './url';
|
|
4
|
-
|
|
5
|
-
test('parseQueryString', t => {
|
|
6
|
-
assert.deepEqual(parseQueryString(), {});
|
|
7
|
-
assert.deepEqual(parseQueryString("http://foo"), {});
|
|
8
|
-
assert.deepEqual(parseQueryString("http://foo?p1=1&p2=2"), { p1: '1', p2: '2' });
|
|
9
|
-
assert.deepEqual(parseQueryString("http://foo?p1=1&p2=2#someAnchor"), { p1: '1', p2: '2' });
|
|
10
|
-
});
|
|
11
|
-
test('parseHash', t => {
|
|
12
|
-
assert.deepEqual(parseHash(), {});
|
|
13
|
-
assert.deepEqual(parseHash("http://foo"), {});
|
|
14
|
-
assert.deepEqual(parseHash("http://foo?p1=1&p2=2"), {});
|
|
15
|
-
assert.deepEqual(parseHash("http://foo?p1=1&p2=2#someAnchor"), { someAnchor: '' });
|
|
16
|
-
assert.deepEqual(parseHash("http://foo?p1=3&p2=4#p1=1&p2=2"), { p1: '1', p2: '2' });
|
|
17
|
-
assert.deepEqual(parseHash("http://foo#p1=1&p2=2"), { p1: '1', p2: '2' });
|
|
18
|
-
});
|