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