@esri/solution-common 4.1.2 → 5.0.0

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