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