@esri/solution-workflow 5.2.1 → 5.2.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.
Files changed (53) hide show
  1. package/README.md +3 -6
  2. package/dist/esm/index.js.map +1 -0
  3. package/dist/esm/{workflow/src/workflow.d.ts → workflow.d.ts} +12 -3
  4. package/dist/esm/workflow.js +112 -0
  5. package/dist/esm/workflow.js.map +1 -0
  6. package/package.json +3 -3
  7. package/dist/cjs/common/src/generalHelpers.d.ts +0 -400
  8. package/dist/cjs/common/src/generalHelpers.js +0 -877
  9. package/dist/cjs/common/src/generalHelpers.js.map +0 -1
  10. package/dist/cjs/common/src/getItemTypeAbbrev.d.ts +0 -19
  11. package/dist/cjs/common/src/getItemTypeAbbrev.js +0 -186
  12. package/dist/cjs/common/src/getItemTypeAbbrev.js.map +0 -1
  13. package/dist/cjs/common/src/interfaces.d.ts +0 -1344
  14. package/dist/cjs/common/src/interfaces.js +0 -77
  15. package/dist/cjs/common/src/interfaces.js.map +0 -1
  16. package/dist/cjs/common/src/libConnectors.d.ts +0 -73
  17. package/dist/cjs/common/src/libConnectors.js +0 -115
  18. package/dist/cjs/common/src/libConnectors.js.map +0 -1
  19. package/dist/cjs/common/test/mocks/templates.d.ts +0 -71
  20. package/dist/cjs/common/test/mocks/templates.js +0 -1220
  21. package/dist/cjs/common/test/mocks/templates.js.map +0 -1
  22. package/dist/cjs/common/test/mocks/utils.d.ts +0 -605
  23. package/dist/cjs/common/test/mocks/utils.js +0 -1222
  24. package/dist/cjs/common/test/mocks/utils.js.map +0 -1
  25. package/dist/cjs/workflow/src/index.js +0 -25
  26. package/dist/cjs/workflow/src/index.js.map +0 -1
  27. package/dist/cjs/workflow/src/workflow.d.ts +0 -32
  28. package/dist/cjs/workflow/src/workflow.js +0 -51
  29. package/dist/cjs/workflow/src/workflow.js.map +0 -1
  30. package/dist/esm/common/src/generalHelpers.d.ts +0 -400
  31. package/dist/esm/common/src/generalHelpers.js +0 -829
  32. package/dist/esm/common/src/generalHelpers.js.map +0 -1
  33. package/dist/esm/common/src/getItemTypeAbbrev.d.ts +0 -19
  34. package/dist/esm/common/src/getItemTypeAbbrev.js +0 -182
  35. package/dist/esm/common/src/getItemTypeAbbrev.js.map +0 -1
  36. package/dist/esm/common/src/interfaces.d.ts +0 -1344
  37. package/dist/esm/common/src/interfaces.js +0 -72
  38. package/dist/esm/common/src/interfaces.js.map +0 -1
  39. package/dist/esm/common/src/libConnectors.d.ts +0 -73
  40. package/dist/esm/common/src/libConnectors.js +0 -105
  41. package/dist/esm/common/src/libConnectors.js.map +0 -1
  42. package/dist/esm/common/test/mocks/templates.d.ts +0 -71
  43. package/dist/esm/common/test/mocks/templates.js +0 -1195
  44. package/dist/esm/common/test/mocks/templates.js.map +0 -1
  45. package/dist/esm/common/test/mocks/utils.d.ts +0 -605
  46. package/dist/esm/common/test/mocks/utils.js +0 -1177
  47. package/dist/esm/common/test/mocks/utils.js.map +0 -1
  48. package/dist/esm/workflow/src/index.d.ts +0 -21
  49. package/dist/esm/workflow/src/index.js.map +0 -1
  50. package/dist/esm/workflow/src/workflow.js +0 -45
  51. package/dist/esm/workflow/src/workflow.js.map +0 -1
  52. /package/dist/{cjs/workflow/src → esm}/index.d.ts +0 -0
  53. /package/dist/esm/{workflow/src/index.js → index.js} +0 -0
@@ -1,829 +0,0 @@
1
- /** @license
2
- * Copyright 2018 Esri
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- /**
17
- * Provides general helper functions.
18
- *
19
- * @module generalHelpers
20
- */
21
- import { createId } from "@esri/hub-common";
22
- import { sanitizeJSON } from "./libConnectors";
23
- // ------------------------------------------------------------------------------------------------------------------ //
24
- /**
25
- * Returns a URL with a query parameter appended
26
- *
27
- * @param url URL to append to
28
- * @param parameter Query parameter to append, prefixed with "?" or "&" as appropriate to what url already has
29
- * @returns New URL combining url and parameter
30
- */
31
- export function appendQueryParam(url, parameter) {
32
- return url + (url.indexOf("?") === -1 ? "?" : "&") + parameter;
33
- }
34
- /**
35
- * Extracts JSON from a Blob.
36
- *
37
- * @param blob Blob to use as source
38
- * @returns A promise that will resolve with JSON or null
39
- */
40
- export function blobToJson(blob) {
41
- return new Promise(resolve => {
42
- blobToText(blob).then(blobContents => {
43
- try {
44
- resolve(JSON.parse(blobContents));
45
- }
46
- catch (err) {
47
- resolve(null);
48
- }
49
- }, () => resolve(null));
50
- });
51
- }
52
- /**
53
- * Converts a Blob to a File.
54
- *
55
- * @param blob Blob to use as source
56
- * @param filename Name to use for file
57
- * @param mimeType MIME type to override blob's MIME type
58
- * @returns File created out of Blob and filename
59
- */
60
- export function blobToFile(blob, filename, mimeType) {
61
- return new File([blob], filename ? filename : "", {
62
- type: mimeType ?? blob.type // Blobs default to type=""
63
- });
64
- }
65
- /**
66
- * Extracts text from a Blob.
67
- *
68
- * @param blob Blob to use as source
69
- * @returns A promise that will resolve with text read from blob
70
- */
71
- export function blobToText(blob) {
72
- return new Promise(resolve => {
73
- const reader = new FileReader();
74
- reader.onload = function (evt) {
75
- // Disable needed because Node requires cast
76
- const blobContents = evt.target.result;
77
- resolve(blobContents ? blobContents : ""); // not handling ArrayContents variant
78
- };
79
- reader.readAsText(blob);
80
- });
81
- }
82
- /**
83
- * Checks that a URL path ends with a slash.
84
- *
85
- * @param url URL to check
86
- * @returns URL, appended with slash if missing
87
- */
88
- export function checkUrlPathTermination(url) {
89
- return url ? (url.endsWith("/") ? url : url + "/") : url;
90
- }
91
- /**
92
- * Converts a hub-style item into a solutions-style item, the difference being handling of resources.
93
- *
94
- * @param hubModel Hub-style item
95
- * @return solutions-style item
96
- */
97
- export function convertIModel(hubModel) {
98
- const item = {
99
- ...hubModel
100
- };
101
- item.resources = hubModel?.resources ? Object.values(hubModel.resources) : [];
102
- return item;
103
- }
104
- /**
105
- * Creates a random 32-character alphanumeric string.
106
- *
107
- * @returns A lowercase 32-char alphanumeric string
108
- * @internal
109
- */
110
- export function createLongId() {
111
- // createId gets a random number, converts it to base 36 representation, then grabs chars 2-8
112
- return createId("") + createId("") + createId("") + createId("");
113
- }
114
- /**
115
- * Creates a random 8-character alphanumeric string that begins with an alphabetic character.
116
- *
117
- * @returns An alphanumeric string in the range [a0000000..zzzzzzzz]
118
- */
119
- export function createShortId() {
120
- // Return a random number, but beginning with an alphabetic character so that it can be used as a valid
121
- // dotable property name. Used for unique identifiers that do not require the rigor of a full UUID -
122
- // i.e. node ids, process ids, etc.
123
- const min = 0.2777777777777778; // 0.a in base 36
124
- const max = 0.9999999999996456; // 0.zzzzzzzz in base 36
125
- return (_getRandomNumberInRange(min, max).toString(36) + "0000000").substr(2, 8);
126
- }
127
- /**
128
- * Copies an input list removing duplicates.
129
- *
130
- * @param input List to be deduped
131
- * @returns Deduped list; order of items in input is not maintained
132
- */
133
- export function dedupe(input = []) {
134
- if (input.length === 0) {
135
- return [];
136
- }
137
- const dedupedList = new Set(input);
138
- const output = [];
139
- dedupedList.forEach((value) => output.push(value));
140
- return output;
141
- }
142
- /**
143
- * Flags a failure to create an item from a template.
144
- *
145
- * @param itemType The AGO item type
146
- * @param id Item id to include in response
147
- * @returns Empty creation response
148
- */
149
- export function generateEmptyCreationResponse(itemType, id = "") {
150
- return {
151
- item: null,
152
- id,
153
- type: itemType,
154
- postProcess: false
155
- };
156
- }
157
- /**
158
- * Converts JSON to a Blob.
159
- *
160
- * @param json JSON to use as source
161
- * @returns A blob from the source JSON
162
- */
163
- export function jsonToBlob(json) {
164
- const uint8array = new TextEncoder().encode(JSON.stringify(json));
165
- const blobOptions = { type: "application/octet-stream" };
166
- return new Blob([uint8array], blobOptions);
167
- }
168
- /**
169
- * Converts JSON to a File.
170
- *
171
- * @param json JSON to use as source
172
- * @param filename Name to use for file
173
- * @param mimeType MIME type to override blob's MIME type
174
- * @returns File created out of JSON and filename
175
- */
176
- export function jsonToFile(json, filename, mimeType = "application/json") {
177
- return blobToFile(jsonToBlob(json), filename, mimeType);
178
- }
179
- /**
180
- * Makes a unique copy of JSON by stringifying and parsing.
181
- *
182
- * @param json JSON to use as source
183
- * @returns A JSON object from the source JSON
184
- */
185
- export function jsonToJson(json) {
186
- return JSON.parse(JSON.stringify(json));
187
- }
188
- /**
189
- * Saves a blob to a file.
190
- *
191
- * @param filename Name to give file
192
- * @param blob Blob to save
193
- * @returns Promise resolving when operation is complete
194
- */
195
- // Function is only used for live testing, so excluding it from coverage for now
196
- /* istanbul ignore next */
197
- export function saveBlobAsFile(filename, blob) {
198
- return new Promise(resolve => {
199
- const dataUrl = URL.createObjectURL(blob);
200
- const linkElement = document.createElement("a");
201
- linkElement.setAttribute("href", dataUrl);
202
- linkElement.setAttribute("download", filename);
203
- linkElement.style.display = "none";
204
- document.body.appendChild(linkElement);
205
- linkElement.click();
206
- document.body.removeChild(linkElement);
207
- setTimeout(() => {
208
- URL.revokeObjectURL(dataUrl);
209
- resolve(null);
210
- }, 500);
211
- });
212
- }
213
- /**
214
- * Makes a deep clone, including arrays but not functions.
215
- *
216
- * @param obj Object to be cloned
217
- * @returns Clone of obj
218
- * @example
219
- * ```js
220
- * import { cloneObject } from "utils/object-helpers";
221
- * const original = { foo: "bar" }
222
- * const copy = cloneObject(original)
223
- * copy.foo // "bar"
224
- * copy === original // false
225
- * ```
226
- */
227
- export function cloneObject(obj) {
228
- let clone = {};
229
- // first check array
230
- if (Array.isArray(obj)) {
231
- clone = obj.map(cloneObject);
232
- }
233
- else if (typeof obj === "object") {
234
- if (obj instanceof File) {
235
- const fileOptions = obj.type ? { type: obj.type } : undefined;
236
- clone = new File([obj], obj.name, fileOptions);
237
- }
238
- else {
239
- for (const i in obj) {
240
- if (obj[i] != null && typeof obj[i] === "object") {
241
- clone[i] = cloneObject(obj[i]);
242
- }
243
- else {
244
- clone[i] = obj[i];
245
- }
246
- }
247
- }
248
- }
249
- else {
250
- clone = obj;
251
- }
252
- return clone;
253
- }
254
- /**
255
- * Compares two JSON objects using JSON.stringify.
256
- *
257
- * @param json1 First object
258
- * @param json2 Second object
259
- * @returns True if objects are the same
260
- */
261
- export function compareJSON(json1, json2) {
262
- return JSON.stringify(json1) === JSON.stringify(json2);
263
- }
264
- /**
265
- * Compares two JSON objects using JSON.stringify, converting empty strings to nulls.
266
- *
267
- * @param json1 First object
268
- * @param json2 Second object
269
- * @returns True if objects are the same
270
- */
271
- export function compareJSONNoEmptyStrings(json1, json2) {
272
- const jsonStr1 = JSON.stringify(json1).replace(/":""/g, '":null');
273
- const jsonStr2 = JSON.stringify(json2).replace(/":""/g, '":null');
274
- return jsonStr1 === jsonStr2;
275
- }
276
- /**
277
- * Compares two JSON objects property by property and reports each mismatch.
278
- *
279
- * @param json1 First object
280
- * @param json2 Second object
281
- * @returns A list of mismatch report strings
282
- */
283
- export function compareJSONProperties(json1, json2) {
284
- let mismatches = [];
285
- const type1 = _typeof_null(json1);
286
- const type2 = _typeof_null(json2);
287
- if (type1 !== type2) {
288
- // Ignore "undefined" vs. "null" and vice versa
289
- /* istanbul ignore else */
290
- if ((type1 !== "undefined" && type1 !== "null") ||
291
- (type2 !== "null" && type2 !== "undefined")) {
292
- mismatches.push("Type difference: " + type1 + " vs. " + type2);
293
- }
294
- }
295
- else {
296
- if (json1 !== json2) {
297
- switch (type1) {
298
- case "boolean":
299
- mismatches.push("Value difference: " + json1 + " vs. " + json2);
300
- break;
301
- case "number":
302
- mismatches.push("Value difference: " + json1 + " vs. " + json2);
303
- break;
304
- case "string":
305
- mismatches.push('String difference: "' + json1 + '" vs. "' + json2 + '"');
306
- break;
307
- case "object":
308
- const keys1 = Object.keys(json1);
309
- const keys2 = Object.keys(json2);
310
- if (keys1.length !== keys2.length ||
311
- JSON.stringify(keys1) !== JSON.stringify(keys2)) {
312
- if (Array.isArray(json1) && Array.isArray(json2)) {
313
- mismatches.push("Array length difference: [" +
314
- keys1.length +
315
- "] vs. [" +
316
- keys2.length +
317
- "]");
318
- }
319
- else {
320
- mismatches.push("Props difference: " +
321
- JSON.stringify(keys1) +
322
- " vs. " +
323
- JSON.stringify(keys2));
324
- }
325
- }
326
- else {
327
- for (let k = 0; k < keys1.length; ++k) {
328
- const submismatches = compareJSONProperties(json1[keys1[k]], json2[keys2[k]]);
329
- if (submismatches.length > 0) {
330
- mismatches = mismatches.concat(submismatches);
331
- }
332
- }
333
- }
334
- break;
335
- }
336
- }
337
- }
338
- return mismatches;
339
- }
340
- /**
341
- * Sanitizes JSON and echoes changes to console.
342
- *
343
- * @param json JSON to sanitize
344
- * @param sanitizer Instance of Sanitizer class
345
- * @returns Sanitized version of `json`
346
- * @see https://github.com/esri/arcgis-html-sanitizer#sanitize-json
347
- */
348
- export function sanitizeJSONAndReportChanges(json, sanitizer) {
349
- const sanitizedJSON = sanitizeJSON(json, sanitizer);
350
- const mismatches = compareJSONProperties(json, sanitizedJSON);
351
- if (mismatches.length > 0) {
352
- console.warn("Changed " +
353
- mismatches.length +
354
- (mismatches.length === 1 ? " property" : " properties"));
355
- mismatches.forEach(mismatch => console.warn(" " + mismatch));
356
- }
357
- return sanitizedJSON;
358
- }
359
- export function deleteItemProps(itemTemplate) {
360
- const propsToRetain = [
361
- "accessInformation",
362
- "appCategories",
363
- "banner",
364
- "categories",
365
- "culture",
366
- "description",
367
- "documentation",
368
- "extent",
369
- "groupDesignations",
370
- "industries",
371
- "languages",
372
- "licenseInfo",
373
- "listed",
374
- "name",
375
- "properties",
376
- "proxyFilter",
377
- "screenshots",
378
- "size",
379
- "snippet",
380
- "spatialReference",
381
- "tags",
382
- "title",
383
- "type",
384
- "typeKeywords",
385
- "url"
386
- ];
387
- const propsToDelete = Object.keys(itemTemplate).filter(k => propsToRetain.indexOf(k) < 0);
388
- deleteProps(itemTemplate, propsToDelete);
389
- return itemTemplate;
390
- }
391
- /**
392
- * Deletes a property from an object.
393
- *
394
- * @param obj Object with property to delete
395
- * @param path Path into an object to property, e.g., "data.values.webmap", where "data" is a top-level property
396
- * in obj
397
- */
398
- export function deleteProp(obj, path) {
399
- const pathParts = path.split(".");
400
- if (Array.isArray(obj)) {
401
- obj.forEach((child) => deleteProp(child, path));
402
- }
403
- else {
404
- const subpath = pathParts.slice(1).join(".");
405
- if (typeof obj[pathParts[0]] !== "undefined") {
406
- if (pathParts.length === 1) {
407
- delete obj[path];
408
- }
409
- else {
410
- deleteProp(obj[pathParts[0]], subpath);
411
- }
412
- }
413
- }
414
- }
415
- /**
416
- * Deletes properties from an object.
417
- *
418
- * @param obj Object with properties to delete
419
- * @param props Array of properties on object that should be deleted
420
- */
421
- export function deleteProps(obj, props) {
422
- props.forEach(prop => {
423
- deleteProp(obj, prop);
424
- });
425
- }
426
- /**
427
- * Creates an AGO-style JSON failure response with success property.
428
- *
429
- * @param e Optional error information
430
- * @returns JSON structure with property success set to false and optionally including `e`
431
- */
432
- export function fail(e) {
433
- if (e) {
434
- return { success: false, error: e.response?.error || e.error || e };
435
- }
436
- else {
437
- return { success: false };
438
- }
439
- }
440
- /**
441
- * Creates an AGO-style JSON failure response with success property and extended with ids list.
442
- *
443
- * @param ids List of ids
444
- * @param e Optional error information
445
- * @returns JSON structure with property success set to false and optionally including `e`
446
- */
447
- export function failWithIds(itemIds, e) {
448
- if (e) {
449
- return { success: false, itemIds, error: e.error || e };
450
- }
451
- else {
452
- return { success: false, itemIds };
453
- }
454
- }
455
- /**
456
- * Extracts subgroup ids from item tags
457
- *
458
- * @param tags Tags in an item
459
- * @returns List of subgroup ids; subgroups are identified using tags that begin with "group." and end with a group id,
460
- * e.g., "group.8d515625ee9f49d7b4f6c6cb2a389151"; non-matching tags are ignored
461
- */
462
- export function getSubgroupIds(tags) {
463
- if (tags) {
464
- const containedGroupPrefix = "group.";
465
- return tags
466
- .filter(tag => tag.startsWith(containedGroupPrefix))
467
- .map(tag => tag.substring(containedGroupPrefix.length));
468
- }
469
- else {
470
- return [];
471
- }
472
- }
473
- /**
474
- * Extracts the ids from a string
475
- *
476
- * @param v String to examine
477
- * @returns List of id strings found
478
- * @example
479
- * get id from
480
- * bad3483e025c47338d43df308c117308
481
- * {bad3483e025c47338d43df308c117308
482
- * =bad3483e025c47338d43df308c117308
483
- * do not get id from
484
- * http: *something/name_bad3483e025c47338d43df308c117308
485
- * {{bad3483e025c47338d43df308c117308.itemId}}
486
- * bad3483e025c47338d43df308c117308bad3483e025c47338d43df308c117308
487
- */
488
- export function getIDs(v) {
489
- // lookbehind is not supported in safari
490
- // cannot use /(?<!_)(?<!{{)\b[0-9A-F]{32}/gi
491
- // use groups and filter out the ids that start with {{
492
- return regExTest(v, /({*)(\b[0-9A-F]{32}\b)/gi).reduce(function (acc, _v) {
493
- /* istanbul ignore else */
494
- if (_v.indexOf("{{") < 0) {
495
- acc.push(_v.replace("{", ""));
496
- }
497
- return acc;
498
- }, []);
499
- }
500
- /**
501
- * Gets a property out of a deeply nested object.
502
- * Does not handle anything but nested object graph
503
- *
504
- * @param obj Object to retrieve value from
505
- * @param path Path into an object, e.g., "data.values.webmap", where "data" is a top-level property
506
- * in obj
507
- * @returns Value at end of path
508
- */
509
- export function getProp(obj, path) {
510
- return path.split(".").reduce(function (prev, curr) {
511
- /* istanbul ignore next no need to test undefined scenario */
512
- return prev ? prev[curr] : undefined;
513
- }, obj);
514
- }
515
- /**
516
- * Returns an array of values from an object based on an array of property paths.
517
- *
518
- * @param obj Object to retrieve values from
519
- * @param props Array of paths into the object e.g., "data.values.webmap", where "data" is a top-level property
520
- * @returns Array of the values plucked from the object; only defined values are returned
521
- */
522
- export function getProps(obj, props) {
523
- return props.reduce((a, p) => {
524
- const v = getProp(obj, p);
525
- if (v) {
526
- a.push(v);
527
- }
528
- return a;
529
- }, []);
530
- }
531
- /**
532
- * Get a property out of a deeply nested object
533
- * Does not handle anything but nested object graph
534
- *
535
- * @param obj Object to retrieve value from
536
- * @param path Path into an object, e.g., "data.values.webmap", where "data" is a top-level property
537
- * in obj
538
- * @param defaultV Optional value to use if any part of path--including final value--is undefined
539
- * @returns Value at end of path
540
- */
541
- export function getPropWithDefault(obj, path, defaultV) {
542
- const value = path.split(".").reduce(function (prev, curr) {
543
- /* istanbul ignore next no need to test undefined scenario */
544
- return prev ? prev[curr] : undefined;
545
- }, obj);
546
- if (typeof value === "undefined") {
547
- return defaultV;
548
- }
549
- else {
550
- return value;
551
- }
552
- }
553
- /**
554
- * Updates a list of the items dependencies if more are found in the
555
- * provided value.
556
- *
557
- * @param v a string value to check for ids
558
- * @param deps a list of the items dependencies
559
- */
560
- export function idTest(v, deps) {
561
- const ids = getIDs(v);
562
- ids.forEach(id => {
563
- /* istanbul ignore else */
564
- if (deps.indexOf(id) === -1) {
565
- deps.push(id);
566
- }
567
- });
568
- }
569
- /**
570
- * Sets a deeply nested property of an object.
571
- * Creates the full path if it does not exist.
572
- *
573
- * @param obj Object to set value of
574
- * @param path Path into an object, e.g., "data.values.webmap", where "data" is a top-level property in obj
575
- * @param value The value to set at the end of the path
576
- */
577
- export function setCreateProp(obj, path, value) {
578
- const pathParts = path.split(".");
579
- pathParts.reduce((a, b, c) => {
580
- if (c === pathParts.length - 1) {
581
- a[b] = value;
582
- return value;
583
- }
584
- else {
585
- if (!a[b]) {
586
- a[b] = {};
587
- }
588
- return a[b];
589
- }
590
- }, obj);
591
- }
592
- /**
593
- * Sets a deeply nested property of an object.
594
- * Does nothing if the full path does not exist.
595
- *
596
- * @param obj Object to set value of
597
- * @param path Path into an object, e.g., "data.values.webmap", where "data" is a top-level property in obj
598
- * @param value The value to set at the end of the path
599
- */
600
- export function setProp(obj, path, value) {
601
- if (getProp(obj, path)) {
602
- const pathParts = path.split(".");
603
- pathParts.reduce((a, b, c) => {
604
- if (c === pathParts.length - 1) {
605
- a[b] = value;
606
- return value;
607
- }
608
- else {
609
- return a[b];
610
- }
611
- }, obj);
612
- }
613
- }
614
- /**
615
- * Creates a timestamp string using the current UTC date and time.
616
- *
617
- * @returns Timestamp formatted as YYYYMMDD_hhmm_ssmmm, with month one-based and all values padded with zeroes on the
618
- * left as needed (`ssmmm` stands for seconds from 0..59 and milliseconds from 0..999)
619
- * @private
620
- */
621
- export function getUTCTimestamp() {
622
- const now = new Date();
623
- return (_padPositiveNum(now.getUTCFullYear(), 4) +
624
- _padPositiveNum(now.getUTCMonth() + 1, 2) +
625
- _padPositiveNum(now.getUTCDate(), 2) +
626
- "_" +
627
- _padPositiveNum(now.getUTCHours(), 2) +
628
- _padPositiveNum(now.getUTCMinutes(), 2) +
629
- "_" +
630
- _padPositiveNum(now.getUTCSeconds(), 2) +
631
- _padPositiveNum(now.getUTCMilliseconds(), 3));
632
- }
633
- /**
634
- * Tests if an object's `item.typeKeywords` or `typeKeywords` properties has any of a set of keywords.
635
- *
636
- * @param jsonObj Object to test
637
- * @param keywords List of keywords to look for in jsonObj
638
- * @returns Boolean indicating result
639
- */
640
- export function hasAnyKeyword(jsonObj, keywords) {
641
- const typeKeywords = getProp(jsonObj, "item.typeKeywords") || jsonObj.typeKeywords || [];
642
- return keywords.reduce((a, kw) => {
643
- if (!a) {
644
- a = typeKeywords.includes(kw);
645
- }
646
- return a;
647
- }, false);
648
- }
649
- /**
650
- * Tests if an object's `item.typeKeywords` or `typeKeywords` properties has a specific keyword.
651
- *
652
- * @param jsonObj Object to test
653
- * @param keyword Keyword to look for in jsonObj
654
- * @returns Boolean indicating result
655
- */
656
- export function hasTypeKeyword(jsonObj, keyword) {
657
- const typeKeywords = getProp(jsonObj, "item.typeKeywords") || jsonObj.typeKeywords || [];
658
- return typeKeywords.includes(keyword);
659
- }
660
- /**
661
- * Will return the provided title if it does not exist as a property
662
- * in one of the objects at the defined path. Otherwise the title will
663
- * have a numerical value attached.
664
- *
665
- * @param title The root title to test
666
- * @param templateDictionary Hash of the facts
667
- * @param path to the objects to evaluate for potantial name clashes
668
- * @returns string The unique title to use
669
- */
670
- export function getUniqueTitle(title, templateDictionary, path) {
671
- title = title ? title.trim() : "_";
672
- const objs = getProp(templateDictionary, path) || [];
673
- const titles = objs.map(obj => {
674
- return obj.title;
675
- });
676
- let newTitle = title;
677
- let i = 0;
678
- while (titles.indexOf(newTitle) > -1) {
679
- i++;
680
- newTitle = title + " " + i;
681
- }
682
- return newTitle;
683
- }
684
- /**
685
- * Performs string replacement on every string in an object.
686
- *
687
- * @param obj Object to scan and to modify
688
- * @param pattern Search pattern in each string
689
- * @param replacement Replacement for matches to search pattern
690
- * @returns Modified obj is returned
691
- */
692
- export function globalStringReplace(obj, pattern, replacement) {
693
- if (obj) {
694
- Object.keys(obj).forEach(prop => {
695
- const propObj = obj[prop];
696
- if (propObj) {
697
- /* istanbul ignore else */
698
- if (typeof propObj === "object") {
699
- globalStringReplace(propObj, pattern, replacement);
700
- }
701
- else if (typeof propObj === "string") {
702
- obj[prop] = obj[prop].replace(pattern, replacement);
703
- }
704
- }
705
- });
706
- }
707
- return obj;
708
- }
709
- /**
710
- * Tests if an array of DatasourceInfos has a given item and layer id already.
711
- *
712
- * @param datasourceInfos Array of DatasourceInfos to evaluate
713
- * @param itemId The items id to check for
714
- * @param layerId The layers id to check for
715
- * @returns Boolean indicating result
716
- */
717
- export function hasDatasource(datasourceInfos, itemId, layerId) {
718
- return datasourceInfos.some(ds => ds.itemId === itemId && ds.layerId === layerId);
719
- }
720
- /**
721
- * remove templatization from item id to compare
722
- *
723
- * @example
724
- * \{\{934a9ef8efa7448fa8ddf7b13cef0240.itemId\}\}
725
- * returns 934a9ef8efa7448fa8ddf7b13cef0240
726
- */
727
- export function cleanItemId(id) {
728
- return id ? id.replace("{{", "").replace(".itemId}}", "") : id;
729
- }
730
- /**
731
- * remove templatization from layer based item id to compare
732
- *
733
- * @example
734
- * \{\{934a9ef8efa7448fa8ddf7b13cef0240.layer0.itemId\}\}
735
- * returns 934a9ef8efa7448fa8ddf7b13cef0240
736
- */
737
- export function cleanLayerBasedItemId(id) {
738
- return id
739
- ? id
740
- .replace("{{", "")
741
- .replace(/([.]layer([0-9]|[1-9][0-9])[.](item|layer)Id)[}]{2}/, "")
742
- : id;
743
- }
744
- /**
745
- * remove templatization from layer id to compare
746
- *
747
- * @example
748
- * \{\{934a9ef8efa7448fa8ddf7b13cef0240.layer0.layerId\}\}
749
- * returns 0
750
- */
751
- export function cleanLayerId(id) {
752
- return id?.toString()
753
- ? parseInt(id
754
- .toString()
755
- .replace(/[{]{2}.{32}[.]layer/, "")
756
- .replace(/[.]layerId[}]{2}/, ""), 10)
757
- : id;
758
- }
759
- /**
760
- * Get template from list of templates by ID
761
- *
762
- * @param templates Array of item templates to search
763
- * @param id of template we are searching for
764
- *
765
- * @returns Template associated with the user provided id argument
766
- */
767
- export function getTemplateById(templates, id) {
768
- let template;
769
- templates.some(_template => {
770
- if (_template.itemId === id) {
771
- template = _template;
772
- return true;
773
- }
774
- return false;
775
- });
776
- return template;
777
- }
778
- /**
779
- * Evaluates a value with a regular expression
780
- *
781
- * @param v a string value to test with the expression
782
- * @param ex the regular expresion to test with
783
- * @returns an array of matches
784
- */
785
- export function regExTest(v, ex) {
786
- return v && ex.test(v) ? v.match(ex) : [];
787
- }
788
- // ------------------------------------------------------------------------------------------------------------------ //
789
- /**
790
- * Creates a random number between two values.
791
- *
792
- * @param min Inclusive minimum desired value
793
- * @param max Non-inclusive maximum desired value
794
- * @returns Random number in the range [min, max)
795
- */
796
- export function _getRandomNumberInRange(min, max) {
797
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_number_between_two_values
798
- // © 2006 IvanWills
799
- // MIT license https://opensource.org/licenses/mit-license.php
800
- return Math.random() * (max - min) + min;
801
- }
802
- /**
803
- * Pads the string representation of a number to a minimum width. Requires modern browser.
804
- *
805
- * @param n Number to pad
806
- * @param totalSize Desired *minimum* width of number after padding with zeroes
807
- * @returns Number converted to string and padded on the left as needed
808
- * @private
809
- */
810
- export function _padPositiveNum(n, totalSize) {
811
- let numStr = n.toString();
812
- const numPads = totalSize - numStr.length;
813
- if (numPads > 0) {
814
- numStr = "0".repeat(numPads) + numStr; // TODO IE11 does not support repeat()
815
- }
816
- return numStr;
817
- }
818
- /**
819
- * Implements rejected ECMA proposal to change `typeof null` from "object" to "null".
820
- *
821
- * @param value Value whose type is sought
822
- * @returns "null" if `value` is null; `typeof value` otherwise
823
- * @see https://web.archive.org/web/20160331031419/http://wiki.ecmascript.org:80/doku.php?id=harmony:typeof_null
824
- * @private
825
- */
826
- function _typeof_null(value) {
827
- return value === null ? "null" : typeof value;
828
- }
829
- //# sourceMappingURL=generalHelpers.js.map