@kwiz/common 1.0.132 → 1.0.134

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.
Files changed (108) hide show
  1. package/lib/cjs/helpers/promises.js +4 -3
  2. package/lib/cjs/helpers/promises.js.map +1 -1
  3. package/lib/cjs/utils/sharepoint.rest/web.js +10 -2
  4. package/lib/cjs/utils/sharepoint.rest/web.js.map +1 -1
  5. package/lib/esm/helpers/promises.js +4 -3
  6. package/lib/esm/helpers/promises.js.map +1 -1
  7. package/lib/esm/utils/sharepoint.rest/web.js +7 -0
  8. package/lib/esm/utils/sharepoint.rest/web.js.map +1 -1
  9. package/lib/types/helpers/promises.d.ts +1 -1
  10. package/lib/types/utils/sharepoint.rest/web.d.ts +1 -0
  11. package/package.json +3 -2
  12. package/.github/workflows/npm-publish.yml +0 -24
  13. package/src/_dependencies.ts +0 -13
  14. package/src/config.ts +0 -19
  15. package/src/exports-index.ts +0 -4
  16. package/src/helpers/Guid.ts +0 -182
  17. package/src/helpers/base64.ts +0 -174
  18. package/src/helpers/browser.test.js +0 -13
  19. package/src/helpers/browser.ts +0 -1505
  20. package/src/helpers/browserinfo.ts +0 -293
  21. package/src/helpers/collections.base.test.js +0 -26
  22. package/src/helpers/collections.base.ts +0 -438
  23. package/src/helpers/collections.ts +0 -108
  24. package/src/helpers/color.ts +0 -55
  25. package/src/helpers/cookies.ts +0 -60
  26. package/src/helpers/date.test.js +0 -120
  27. package/src/helpers/date.ts +0 -189
  28. package/src/helpers/debug.ts +0 -187
  29. package/src/helpers/diagrams.ts +0 -44
  30. package/src/helpers/emails.ts +0 -7
  31. package/src/helpers/eval.ts +0 -5
  32. package/src/helpers/exports-index.ts +0 -30
  33. package/src/helpers/file.test.js +0 -51
  34. package/src/helpers/file.ts +0 -64
  35. package/src/helpers/flatted.ts +0 -150
  36. package/src/helpers/functions.ts +0 -17
  37. package/src/helpers/graph/calendar.types.ts +0 -11
  38. package/src/helpers/graph/exports-index.ts +0 -1
  39. package/src/helpers/http.ts +0 -70
  40. package/src/helpers/images.ts +0 -23
  41. package/src/helpers/json.ts +0 -45
  42. package/src/helpers/md5.ts +0 -190
  43. package/src/helpers/objects.test.js +0 -34
  44. package/src/helpers/objects.ts +0 -275
  45. package/src/helpers/promises.test.js +0 -38
  46. package/src/helpers/promises.ts +0 -166
  47. package/src/helpers/random.ts +0 -27
  48. package/src/helpers/scheduler/exports-index.ts +0 -1
  49. package/src/helpers/scheduler/scheduler.test.js +0 -104
  50. package/src/helpers/scheduler/scheduler.ts +0 -132
  51. package/src/helpers/sharepoint.ts +0 -797
  52. package/src/helpers/strings.test.js +0 -123
  53. package/src/helpers/strings.ts +0 -338
  54. package/src/helpers/typecheckers.test.js +0 -35
  55. package/src/helpers/typecheckers.ts +0 -267
  56. package/src/helpers/url.test.js +0 -44
  57. package/src/helpers/url.ts +0 -208
  58. package/src/helpers/urlhelper.ts +0 -112
  59. package/src/index.ts +0 -6
  60. package/src/types/auth.ts +0 -63
  61. package/src/types/common.types.ts +0 -16
  62. package/src/types/exports-index.ts +0 -15
  63. package/src/types/flatted.types.ts +0 -60
  64. package/src/types/globals.types.ts +0 -7
  65. package/src/types/graph/calendar.types.ts +0 -81
  66. package/src/types/graph/exports-index.ts +0 -1
  67. package/src/types/knownscript.types.ts +0 -19
  68. package/src/types/libs/datajs.types.ts +0 -29
  69. package/src/types/libs/exports-index.ts +0 -3
  70. package/src/types/libs/ics.types.ts +0 -31
  71. package/src/types/libs/msal.types.ts +0 -58
  72. package/src/types/locales.ts +0 -126
  73. package/src/types/localstoragecache.types.ts +0 -9
  74. package/src/types/location.types.ts +0 -28
  75. package/src/types/moment.ts +0 -12
  76. package/src/types/regex.types.ts +0 -17
  77. package/src/types/rest.types.ts +0 -96
  78. package/src/types/sharepoint.types.ts +0 -1467
  79. package/src/types/sharepoint.utils.types.ts +0 -307
  80. package/src/utils/auth/common.ts +0 -119
  81. package/src/utils/auth/discovery.test.js +0 -13
  82. package/src/utils/auth/discovery.ts +0 -132
  83. package/src/utils/auth/exports-index.ts +0 -2
  84. package/src/utils/base64.ts +0 -28
  85. package/src/utils/consolelogger.ts +0 -334
  86. package/src/utils/date.ts +0 -173
  87. package/src/utils/emails.ts +0 -25
  88. package/src/utils/exports-index.ts +0 -11
  89. package/src/utils/knownscript.ts +0 -287
  90. package/src/utils/localstoragecache.ts +0 -447
  91. package/src/utils/rest.ts +0 -502
  92. package/src/utils/script.ts +0 -171
  93. package/src/utils/sharepoint.rest/common.ts +0 -160
  94. package/src/utils/sharepoint.rest/date.ts +0 -63
  95. package/src/utils/sharepoint.rest/exports-index.ts +0 -11
  96. package/src/utils/sharepoint.rest/file.folder.ts +0 -685
  97. package/src/utils/sharepoint.rest/item.ts +0 -547
  98. package/src/utils/sharepoint.rest/list.ts +0 -1572
  99. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +0 -775
  100. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +0 -275
  101. package/src/utils/sharepoint.rest/listutils/common.ts +0 -207
  102. package/src/utils/sharepoint.rest/listutils/exports-index.ts +0 -3
  103. package/src/utils/sharepoint.rest/location.ts +0 -142
  104. package/src/utils/sharepoint.rest/navigation-links.ts +0 -87
  105. package/src/utils/sharepoint.rest/user-search.ts +0 -253
  106. package/src/utils/sharepoint.rest/user.ts +0 -559
  107. package/src/utils/sharepoint.rest/web.ts +0 -1385
  108. package/src/utils/sod.ts +0 -195
@@ -1,123 +0,0 @@
1
- import assert from 'assert/strict';
2
- import test from 'node:test';
3
- import { ReplaceTokensInDictionary, capitalizeFirstLetter, escapeXml, maskString, replaceAll, replaceRegex, splitString } from './strings';
4
-
5
- test('replaceAll', t => {
6
- assert.strictEqual(replaceAll("hello old#@! world old#@! !", "old#@!", "new!@$"), "hello new!@$ world new!@$ !");
7
-
8
- //this failed since the input find included protected regex characters.
9
- //escapeRegExp needs to fix that
10
- let string = "string with special regex[:(s:) chars";
11
- let find = "regex[:(s:)";
12
- let replace = "regex[~~(s~~)";
13
- let result = "string with special regex[~~(s~~) chars";
14
- assert.strictEqual(replaceAll(string, find, replace), result);
15
-
16
- string = `To activate the KWIZ Forms feature in your list you should click the "KWIZ Forms Display Mode" button:
17
-
18
- ![](https://kwizcom.sharepoint.com/sites/Docs/CMSResources/CMS365/Screenshot 2024-06-18 123120.jpg)
19
-
20
- Now select one "Side Panel" or "Full Page" for the display mode:`;
21
- find = "https://kwizcom.sharepoint.com/sites/Docs/CMSResources/CMS365/Screenshot 2024-06-18 123120.jpg";
22
- replace = "/sites/Docs/CMSResources/CMS365/Screenshot 2024-06-18 123120.jpg";
23
- result = `To activate the KWIZ Forms feature in your list you should click the "KWIZ Forms Display Mode" button:
24
-
25
- ![](/sites/Docs/CMSResources/CMS365/Screenshot 2024-06-18 123120.jpg)
26
-
27
- Now select one "Side Panel" or "Full Page" for the display mode:`;
28
- assert.strictEqual(replaceAll(string, find, replace, true), result);
29
-
30
- });
31
-
32
- test('capitalizeFirstLetter', async t => {
33
- assert.strictEqual(capitalizeFirstLetter(), "");
34
- assert.strictEqual(capitalizeFirstLetter("a"), "A");
35
- assert.strictEqual(capitalizeFirstLetter("hello"), "Hello");
36
- assert.strictEqual(capitalizeFirstLetter("hello world!"), "Hello world!");
37
- assert.strictEqual(capitalizeFirstLetter("helloworld"), "Helloworld");
38
- assert.strictEqual(capitalizeFirstLetter(""), "");
39
- });
40
-
41
- test('escapeXml', async t => {
42
- assert.strictEqual(escapeXml(), "");
43
- assert.strictEqual(escapeXml(""), "");
44
- assert.strictEqual(escapeXml(" "), " ");
45
- assert.strictEqual(escapeXml("<test />"), "&lt;test /&gt;");
46
- assert.strictEqual(escapeXml(`full scope: <>&'"`), `full scope: &lt;&gt;&amp;'"`);
47
- assert.strictEqual(escapeXml(undefined, true), "");
48
- assert.strictEqual(escapeXml("", true), "");
49
- assert.strictEqual(escapeXml(" ", true), " ");
50
- assert.strictEqual(escapeXml("<test />", true), "&lt;test /&gt;");
51
- assert.strictEqual(escapeXml(`full scope: <>&'"`, true), `full scope: &lt;&gt;&amp;&apos;&quot;`);
52
- });
53
-
54
- test('replaceRegex', t => {
55
- /** Match anything between {zone:*} */
56
- var match = /{zone:\w+}/gi;
57
-
58
- var source = `text {zone:one} {zone:two}
59
- more text
60
- {zone:three}`;
61
- var result = `text ONE TWO
62
- more text
63
- THREE`;
64
-
65
- assert.strictEqual(replaceRegex(source, match, m => m.slice(1, m.length - 1).split(':')[1].toLocaleUpperCase()), result);
66
-
67
- /** match: "](______)" */
68
- match = /]\(.+\)/gi;
69
- source = '[name:root](https://kwizcom.sharepoint.com/:fl:/g/contentstorage)';
70
- result = '[name:root](https~~~~//kwizcom.sharepoint.com/~~~~fl~~~~/g/contentstorage)';
71
- assert.strictEqual(replaceRegex(source, match, m => m.replace(/:/gi, "~~~~")), result);
72
- });
73
-
74
- test('maskString', async t => {
75
- assert.strictEqual(maskString("abcdefg"), "ab*****fg");
76
- assert.strictEqual(maskString("ab"), "ab*****ab");
77
- assert.strictEqual(maskString(""), "*****");
78
- assert.strictEqual(maskString("abcdefg", { mask: "..." }), "ab...fg");
79
- assert.strictEqual(maskString("abcdefg", { mask: "...", start: 0, end: 0 }), "...");
80
- assert.strictEqual(maskString("abcdefg", { mask: "...", start: 1, end: 1 }), "a...g");
81
- });
82
-
83
- test('ReplaceTokensInDictionary', async t => {
84
- const tokens = { "t1": "token1", "t2": "token 2" };
85
- let dic = {};
86
- let expected = {};
87
-
88
- dic = { a: "hello" };
89
- expected = { a: "hello" };
90
- ReplaceTokensInDictionary(dic, tokens);
91
- assert.strictEqual(JSON.stringify(dic), JSON.stringify(expected));
92
-
93
- dic = { a: "hello", b: "hello [t1]" };
94
- expected = { a: "hello", b: "hello token1" };
95
- ReplaceTokensInDictionary(dic, tokens);
96
- assert.strictEqual(JSON.stringify(dic), JSON.stringify(expected));
97
-
98
- dic = { a: "hello", b: "hello [t1] [t2]", c: "[t2]" };
99
- expected = { a: "hello", b: "hello token1 token 2", c: "token 2" };
100
- ReplaceTokensInDictionary(dic, tokens);
101
- assert.strictEqual(JSON.stringify(dic), JSON.stringify(expected));
102
- });
103
-
104
- test('splitString', async t => {
105
- const str = "abcd|1234|efgh|5678";
106
- let expected = [
107
- 'abc', 'd|1',
108
- '234', '|ef',
109
- 'gh|', '567',
110
- '8'
111
- ];
112
- assert.strictEqual(JSON.stringify(splitString(str, { maxLength: 3 })), JSON.stringify(expected));
113
- expected = [
114
- 'abcd', '1234',
115
- 'efgh', '5678'
116
- ];
117
- assert.strictEqual(JSON.stringify(splitString(str, { marker: "|" })), JSON.stringify(expected));
118
- expected = [
119
- 'ab', 'cd', '12', '34',
120
- 'ef', 'gh', '56', '78'
121
- ];
122
- assert.strictEqual(JSON.stringify(splitString(str, { marker: "|", maxLength: 2 })), JSON.stringify(expected));
123
- });
@@ -1,338 +0,0 @@
1
- import { IDictionary } from "../types/common.types";
2
- import { filterEmptyEntries, 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
- /** calls replace tokens on every dictionary value, or set null keys to "". This function ignores null/empty dictionaries */
111
- export function ReplaceTokensInDictionary(Dictionary: IDictionary<string>, TokenValues: IDictionary<string>) {
112
- if (!isNullOrUndefined(Dictionary)) {
113
- Object.keys(Dictionary).forEach(key => {
114
- Dictionary[key] = isNullOrEmptyString(Dictionary[key]) ? "" : ReplaceTokens(Dictionary[key], TokenValues);
115
- });
116
- }
117
- }
118
-
119
- /** Normalizes a guid string, lower case and removes {} */
120
- export function normalizeGuid(text: string, removeDashes?: boolean): string {
121
- if (isNullOrEmptyString(text) || !isString(text)) {
122
- return text;
123
- }
124
- var guid = text.toLowerCase().trim();
125
-
126
- if (guid.startsWith("{")) {
127
- guid = guid.substr(1);
128
- }
129
-
130
- if (guid.endsWith("}")) {
131
- guid = guid.substr(0, guid.length - 1);
132
- }
133
-
134
- if (removeDashes) {
135
- guid = guid.replace(/-/g, '');
136
- }
137
-
138
- return guid;
139
- }
140
-
141
- export function isEmptyGuid(guid: { toString(): string; }) {
142
- if (isNullOrEmptyString(guid)) return true;
143
- else if (Number(normalizeGuid(guid.toString(), true)) === 0) return true;
144
- return false;
145
- }
146
-
147
- export function escapeRegExp(text: string) {
148
- return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
149
- }
150
-
151
- export function isValidDomainLogin(login: string) {
152
- return /^[A-Za-z0-9\\._-]{7,}$/.test(login);
153
- }
154
-
155
- export function stripRichTextWhitespace(value: string) {
156
- // richText fields in have random markup even when field is empty
157
- // \u200B zero width space
158
- // \u200C zero width non-joiner Unicode code point
159
- // \u200D zero width joiner Unicode code point
160
- // \uFEFF zero width no-break space Unicode code point
161
- return isString(value) ? value.replace(/[\u200B-\u200D\uFEFF]/g, "") : value;
162
- }
163
-
164
- /** allows min length 1, letters, numbers underscore only */
165
- export function isValidVarName(text: string) {
166
- return /^[A-Za-z0-9_]{1,}$/.test(text);
167
- }
168
-
169
- /** allows min length 1, letters, numbers underscore and hyphen only */
170
- export function isValidHeaderName(text: string) {
171
- return /^[A-Za-z0-9_-]{1,}$/.test(text);
172
- }
173
-
174
- /** returns token info with format */
175
- export function GetTokenInfo(text: string) {
176
- let split = text.split('??');
177
- let hasFormat = split.length > 0 && !isNullOrEmptyString(split[1]);
178
- let formatSplit = hasFormat ? split[1].split('::') : [];
179
- let valueIfEmpty = formatSplit.length > 1 ? formatSplit[1] : "";
180
- let formatIfNotEmpty = formatSplit[0];
181
- let info = {
182
- tokenName: hasFormat ? split[0] : text,
183
- hasFormat: hasFormat,
184
- getValue: (value: string) => {
185
- if (!hasFormat) return value;
186
- else {
187
- if (isNullOrEmptyString(value)) return valueIfEmpty;
188
- else return formatIfNotEmpty.replace('{0}', value);
189
- }
190
- }
191
- };
192
- return info;
193
- }
194
-
195
- /** return true if both strings are the same, or both are empty/null/undefined */
196
- export function stringEqualsOrEmpty(str1: string, str2: string, ignoreCase?: boolean) {
197
- if (isNullOrEmptyString(str1) && isNullOrEmptyString(str2)) return true;
198
- if (ignoreCase) {
199
- if (!isNullOrEmptyString(str1)) str1 = str1.toLowerCase();
200
- if (!isNullOrEmptyString(str2)) str2 = str2.toLowerCase();
201
- }
202
-
203
- return str1 === str2;
204
- }
205
-
206
- /** return true if str1 contains str2 */
207
- export function stringContains(str1: string, str2: string, ignoreCase?: boolean) {
208
- if (isNullOrEmptyString(str1) && isNullOrEmptyString(str2)) return true;
209
-
210
- if (isNullOrEmptyString(str1))
211
- str1 = "";
212
- if (isNullOrEmptyString(str2))
213
- str2 = "";
214
-
215
- if (ignoreCase) {
216
- str1 = str1.toLowerCase();
217
- str2 = str2.toLowerCase();
218
- }
219
-
220
- return str1.indexOf(str2) >= 0;
221
- }
222
-
223
- export function cleanupString(str: string, options: {
224
- replaceNewLines?: string;
225
- collapseMultipleSpaces?: string;
226
- collapseMultipleDashes?: string;
227
- collapseMultipleUnderscore?: string;
228
- }) {
229
- if (isString(options.replaceNewLines))
230
- str = str.replace(/\r/g, '')//no returns
231
- .replace(/\n/g, options.replaceNewLines);//no line breaks
232
- if (isString(options.collapseMultipleDashes))
233
- str = str.replace(/-+/g, options.collapseMultipleSpaces);//no extra spaces
234
- if (isString(options.collapseMultipleUnderscore))
235
- str = str.replace(/_+/g, options.collapseMultipleUnderscore);//no extra spaces
236
-
237
- // do this last, so it will collapse spaces added by previous options
238
- if (isString(options.collapseMultipleSpaces)) {
239
- str = str.replace(new RegExp(String.fromCharCode(160), "g"), '')//get rid of non-breaking spaces
240
- .replace(/ +/g, options.collapseMultipleSpaces);//no extra spaces
241
- }
242
-
243
- return str;
244
- }
245
-
246
- /** normalizes &#160; to &nbsp; see Issue 752 */
247
- export function normalizeHtmlSpace(html: string) {
248
- if (isNullOrEmptyString(html)) return html;
249
- return html.replace(/&#160;/i, "&nbsp;");
250
- }
251
-
252
- export function replaceAll(str: string, find: string, replace: string, ignoreCase = false) {
253
- //must call escapeRegExp on find, to make sure it works when there are protected regex characters
254
- return str.replace(new RegExp(escapeRegExp(find), `g${ignoreCase ? 'i' : ''}`), replace);
255
- }
256
-
257
- export function capitalizeFirstLetter(str: string) {
258
- return isNullOrEmptyString(str)
259
- ? ""
260
- : `${str.charAt(0).toUpperCase()}${str.substring(1)}`;
261
- }
262
-
263
- export function escapeXml(unsafe: string, isAttribute = false) {
264
- if (isNullOrEmptyString(unsafe)) return "";
265
- return isAttribute
266
- ? unsafe.replace(/[<>&'"]/g, (c) => {
267
- switch (c) {
268
- case '<': return '&lt;';
269
- case '>': return '&gt;';
270
- case '&': return '&amp;';
271
- case '\'': return '&apos;';
272
- case '"': return '&quot;';
273
- }
274
- return c;
275
- })
276
- : unsafe.replace(/[<>&]/g, (c) => {
277
- switch (c) {
278
- case '<': return '&lt;';
279
- case '>': return '&gt;';
280
- case '&': return '&amp;';
281
- }
282
- return c;
283
- });
284
- }
285
-
286
- /** uses regex str.match to replace each match by calling the replacer function (imported from CMS) */
287
- export function replaceRegex(str: string, regex: RegExp, replacer: (match: string) => string | null) {
288
- let matches = str.match(regex);
289
- if (!matches || matches.length < 1) return str;
290
- //replace each found token only once
291
- let unique = makeUniqueArray(matches);
292
- //todo: this has a bug where tokens matched contain each other, example, match numbers and prefix with #: 10, 100 will match both and produce #10 ##100
293
- unique.forEach(m => {
294
- let replacement = replacer(m);
295
- if (!isNullOrUndefined(replacement))//ignore nulls
296
- str = replaceAll(str, m, replacement);
297
- });
298
- return str;
299
- }
300
-
301
- /** masks a long string, keeping X number for characters at the start/end and replacing the middle with the mask string (default: CC*****CCC) */
302
- export function maskString(str: string, options?: {
303
- /** mask string, default ***** */
304
- mask?: string;
305
- /** characters to keep at start, default 2 */
306
- start?: number;
307
- /** characters to keep at end, default 2 */
308
- end?: number;
309
- }) {
310
- const mask = options && options.mask || "*****";
311
- const start = options && isNumber(options.start) ? options.start : 2;
312
- const end = options && isNumber(options.end) ? options.end : 2;
313
- const prefix = start >= 0 ? str.slice(0, start) : str;
314
- const sliceEnd = str.length - end;
315
- const suffix = sliceEnd >= 0 ? str.slice(sliceEnd) : str;
316
-
317
- return `${prefix}${mask}${suffix}`;
318
- }
319
-
320
- export function splitString(str: string, options: { maxLength?: number; marker?: string | RegExp; clearEmptyEntries?: boolean }) {
321
- let strArr: string[] = [str];
322
- if (!isNullOrUndefined(options.marker)) strArr = str.split(options.marker);
323
- if (isNumber(options.maxLength) && options.maxLength > 0) {
324
- let splitted: string[] = [];
325
- strArr.forEach(currentStr => {
326
- while (currentStr.length > 0) {
327
- splitted.push(currentStr.slice(0, options.maxLength));
328
- currentStr = currentStr.slice(options.maxLength);
329
- }
330
- });
331
-
332
- strArr = splitted;
333
- }
334
-
335
- if (options.clearEmptyEntries)
336
- strArr = filterEmptyEntries(strArr);
337
- return strArr;
338
- }
@@ -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
- });