@wener/utils 1.1.26 → 1.1.29

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 (351) hide show
  1. package/README.md +4 -0
  2. package/lib/arrays/MaybeArray.js +36 -25
  3. package/lib/arrays/MaybeArray.js.map +1 -1
  4. package/lib/arrays/arrayFromAsync.js +5 -7
  5. package/lib/arrays/arrayFromAsync.js.map +1 -1
  6. package/lib/asyncs/AsyncInterval.js +11 -12
  7. package/lib/asyncs/AsyncInterval.js.map +1 -1
  8. package/lib/asyncs/MaybePromise.js +3 -0
  9. package/lib/asyncs/MaybePromise.js.map +1 -0
  10. package/lib/asyncs/createAsyncIterator.js +37 -35
  11. package/lib/asyncs/createAsyncIterator.js.map +1 -1
  12. package/lib/asyncs/createLazyPromise.js +59 -60
  13. package/lib/asyncs/createLazyPromise.js.map +1 -1
  14. package/lib/asyncs/firstOfAsyncIterator.js +7 -9
  15. package/lib/asyncs/firstOfAsyncIterator.js.map +1 -1
  16. package/lib/asyncs/generatorOfStream.js +16 -0
  17. package/lib/asyncs/generatorOfStream.js.map +1 -0
  18. package/lib/asyncs/isIterator.js +3 -4
  19. package/lib/asyncs/isIterator.js.map +1 -1
  20. package/lib/asyncs/isPromise.js +3 -4
  21. package/lib/asyncs/isPromise.js.map +1 -1
  22. package/lib/asyncs/isThenable.js +6 -0
  23. package/lib/asyncs/isThenable.js.map +1 -0
  24. package/lib/asyncs/nextOfAsyncIterator.js +20 -14
  25. package/lib/asyncs/nextOfAsyncIterator.js.map +1 -1
  26. package/lib/asyncs/promiseOfCallback.js +17 -0
  27. package/lib/asyncs/promiseOfCallback.js.map +1 -0
  28. package/lib/asyncs/sleep.js +2 -3
  29. package/lib/asyncs/sleep.js.map +1 -1
  30. package/lib/asyncs/timeout.js +22 -26
  31. package/lib/asyncs/timeout.js.map +1 -1
  32. package/lib/browsers/copy.js +47 -45
  33. package/lib/browsers/copy.js.map +1 -1
  34. package/lib/browsers/download.js +38 -27
  35. package/lib/browsers/download.js.map +1 -1
  36. package/lib/browsers/getFileFromDataTransfer.js +41 -28
  37. package/lib/browsers/getFileFromDataTransfer.js.map +1 -1
  38. package/lib/browsers/loaders.js +43 -43
  39. package/lib/browsers/loaders.js.map +1 -1
  40. package/lib/crypto/base.js +5 -4
  41. package/lib/crypto/base.js.map +1 -1
  42. package/lib/crypto/getNodeCrypto.js +14 -13
  43. package/lib/crypto/getNodeCrypto.js.map +1 -1
  44. package/lib/crypto/getRandomValues.js +22 -26
  45. package/lib/crypto/getRandomValues.js.map +1 -1
  46. package/lib/crypto/hashing.js +42 -50
  47. package/lib/crypto/hashing.js.map +1 -1
  48. package/lib/crypto/md5.bench.js +21 -0
  49. package/lib/crypto/md5.bench.js.map +1 -0
  50. package/lib/crypto/md5.d.js +3 -0
  51. package/lib/crypto/md5.d.js.map +1 -0
  52. package/lib/crypto/md5.js +119 -121
  53. package/lib/crypto/md5.js.map +1 -1
  54. package/lib/crypto/pem/pem.js +32 -35
  55. package/lib/crypto/pem/pem.js.map +1 -1
  56. package/lib/crypto/randomUUID.js +12 -11
  57. package/lib/crypto/randomUUID.js.map +1 -1
  58. package/lib/crypto/ulid.js +123 -112
  59. package/lib/crypto/ulid.js.map +1 -1
  60. package/lib/errors/Errors.js +167 -149
  61. package/lib/errors/Errors.js.map +1 -1
  62. package/lib/fetch/createFetchWith.js +20 -23
  63. package/lib/fetch/createFetchWith.js.map +1 -1
  64. package/lib/fetch/createFetchWithLogging.js +20 -16
  65. package/lib/fetch/createFetchWithLogging.js.map +1 -1
  66. package/lib/fetch/createFetchWithRetry.js +61 -65
  67. package/lib/fetch/createFetchWithRetry.js.map +1 -1
  68. package/lib/fetch/dumpRequest.js +39 -42
  69. package/lib/fetch/dumpRequest.js.map +1 -1
  70. package/lib/fetch/dumpResponse.js +13 -20
  71. package/lib/fetch/dumpResponse.js.map +1 -1
  72. package/lib/fetch/index.js +7 -0
  73. package/lib/fetch/index.js.map +1 -0
  74. package/lib/fetch/types.js +3 -0
  75. package/lib/fetch/types.js.map +1 -0
  76. package/lib/http/HttpStatus.js +89 -80
  77. package/lib/http/HttpStatus.js.map +1 -1
  78. package/lib/i18n/createTranslate.js +48 -48
  79. package/lib/i18n/createTranslate.js.map +1 -1
  80. package/lib/index.js +30 -14
  81. package/lib/index.js.map +1 -1
  82. package/lib/io/AbstractEncoding.js +7 -0
  83. package/lib/io/AbstractEncoding.js.map +1 -0
  84. package/lib/io/ArrayBuffer.test-d.js +7 -0
  85. package/lib/io/ArrayBuffer.test-d.js.map +1 -0
  86. package/lib/io/ArrayBuffers.js +198 -172
  87. package/lib/io/ArrayBuffers.js.map +1 -1
  88. package/lib/io/Buffer.js +21 -20
  89. package/lib/io/Buffer.js.map +1 -1
  90. package/lib/io/base64.js +49 -48
  91. package/lib/io/base64.js.map +1 -1
  92. package/lib/io/isBuffer.js +7 -5
  93. package/lib/io/isBuffer.js.map +1 -1
  94. package/lib/io/isTransferable.js +23 -19
  95. package/lib/io/isTransferable.js.map +1 -1
  96. package/lib/isomorphics/getGlobalThis.js +17 -12
  97. package/lib/isomorphics/getGlobalThis.js.map +1 -1
  98. package/lib/isomorphics/structuredClone.js +71 -63
  99. package/lib/isomorphics/structuredClone.js.map +1 -1
  100. package/lib/langs/MaybeFunction.js +7 -7
  101. package/lib/langs/MaybeFunction.js.map +1 -1
  102. package/lib/langs/classOf.js +3 -4
  103. package/lib/langs/classOf.js.map +1 -1
  104. package/lib/langs/deepEqual.js +77 -85
  105. package/lib/langs/deepEqual.js.map +1 -1
  106. package/lib/langs/deepFreeze.js +20 -0
  107. package/lib/langs/deepFreeze.js.map +1 -0
  108. package/lib/langs/hashCode.ignored.js +112 -0
  109. package/lib/langs/hashCode.ignored.js.map +1 -0
  110. package/lib/langs/isClass.js +3 -4
  111. package/lib/langs/isClass.js.map +1 -1
  112. package/lib/langs/isDefined.js +3 -4
  113. package/lib/langs/isDefined.js.map +1 -1
  114. package/lib/langs/isEmptyObject.js +3 -5
  115. package/lib/langs/isEmptyObject.js.map +1 -1
  116. package/lib/langs/isFunction.js +5 -0
  117. package/lib/langs/isFunction.js.map +1 -0
  118. package/lib/langs/isPlainObject.js +7 -8
  119. package/lib/langs/isPlainObject.js.map +1 -1
  120. package/lib/langs/memoize.js +19 -21
  121. package/lib/langs/memoize.js.map +1 -1
  122. package/lib/langs/parseBoolean.js +26 -27
  123. package/lib/langs/parseBoolean.js.map +1 -1
  124. package/lib/langs/shallowClone.js +12 -12
  125. package/lib/langs/shallowClone.js.map +1 -1
  126. package/lib/langs/shallowEqual.js +21 -18
  127. package/lib/langs/shallowEqual.js.map +1 -1
  128. package/lib/libs/ms.js +123 -103
  129. package/lib/libs/ms.js.map +1 -1
  130. package/lib/logging/Logger.js +5 -0
  131. package/lib/logging/Logger.js.map +1 -0
  132. package/lib/logging/createChildLogger.js +12 -14
  133. package/lib/logging/createChildLogger.js.map +1 -1
  134. package/lib/logging/createLogger.js +59 -33
  135. package/lib/logging/createLogger.js.map +1 -1
  136. package/lib/logging/createNoopLogger.js +11 -12
  137. package/lib/logging/createNoopLogger.js.map +1 -1
  138. package/lib/logging/slog.js +143 -0
  139. package/lib/logging/slog.js.map +1 -0
  140. package/lib/maths/clamp.js +21 -0
  141. package/lib/maths/clamp.js.map +1 -0
  142. package/lib/maths/random.js +14 -15
  143. package/lib/maths/random.js.map +1 -1
  144. package/lib/modules/isModule.js +5 -4
  145. package/lib/modules/isModule.js.map +1 -1
  146. package/lib/modules/parseModuleId.js +36 -31
  147. package/lib/modules/parseModuleId.js.map +1 -1
  148. package/lib/objects/get.js +13 -11
  149. package/lib/objects/get.js.map +1 -1
  150. package/lib/objects/get.test-d.js +33 -0
  151. package/lib/objects/get.test-d.js.map +1 -0
  152. package/lib/objects/merge/index.js +3 -0
  153. package/lib/objects/merge/index.js.map +1 -0
  154. package/lib/objects/merge/isMergeableObject.js +11 -10
  155. package/lib/objects/merge/isMergeableObject.js.map +1 -1
  156. package/lib/objects/merge/merge.js +65 -66
  157. package/lib/objects/merge/merge.js.map +1 -1
  158. package/lib/objects/parseObjectPath.js +35 -23
  159. package/lib/objects/parseObjectPath.js.map +1 -1
  160. package/lib/objects/set.js +33 -34
  161. package/lib/objects/set.js.map +1 -1
  162. package/lib/schema/typebox/gen/codegen/common/encoder.js +100 -0
  163. package/lib/schema/typebox/gen/codegen/common/encoder.js.map +1 -0
  164. package/lib/schema/typebox/gen/codegen/common/formatter.js +33 -0
  165. package/lib/schema/typebox/gen/codegen/common/formatter.js.map +1 -0
  166. package/lib/schema/typebox/gen/codegen/common/index.js +5 -0
  167. package/lib/schema/typebox/gen/codegen/common/index.js.map +1 -0
  168. package/lib/schema/typebox/gen/codegen/common/jsdoc.js +117 -0
  169. package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +1 -0
  170. package/lib/schema/typebox/gen/codegen/expression/compiler.js +367 -0
  171. package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +1 -0
  172. package/lib/schema/typebox/gen/codegen/expression/errors.js +259 -0
  173. package/lib/schema/typebox/gen/codegen/expression/errors.js.map +1 -0
  174. package/lib/schema/typebox/gen/codegen/expression/evaluator.js +254 -0
  175. package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +1 -0
  176. package/lib/schema/typebox/gen/codegen/expression/expression.js +384 -0
  177. package/lib/schema/typebox/gen/codegen/expression/expression.js.map +1 -0
  178. package/lib/schema/typebox/gen/codegen/expression/index.js +6 -0
  179. package/lib/schema/typebox/gen/codegen/expression/index.js.map +1 -0
  180. package/lib/schema/typebox/gen/codegen/index.js +5 -0
  181. package/lib/schema/typebox/gen/codegen/index.js.map +1 -0
  182. package/lib/schema/typebox/gen/codegen/model/index.js +16 -0
  183. package/lib/schema/typebox/gen/codegen/model/index.js.map +1 -0
  184. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +239 -0
  185. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +1 -0
  186. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +359 -0
  187. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +1 -0
  188. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +217 -0
  189. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +1 -0
  190. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +253 -0
  191. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +1 -0
  192. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +47 -0
  193. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +1 -0
  194. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +171 -0
  195. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +1 -0
  196. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +33 -0
  197. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +1 -0
  198. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +164 -0
  199. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +1 -0
  200. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +218 -0
  201. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +1 -0
  202. package/lib/schema/typebox/gen/codegen/model/model-to-value.js +43 -0
  203. package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +1 -0
  204. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +206 -0
  205. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +1 -0
  206. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +204 -0
  207. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +1 -0
  208. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +227 -0
  209. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +1 -0
  210. package/lib/schema/typebox/gen/codegen/model/model.js +27 -0
  211. package/lib/schema/typebox/gen/codegen/model/model.js.map +1 -0
  212. package/lib/schema/typebox/gen/codegen/typescript/index.js +4 -0
  213. package/lib/schema/typebox/gen/codegen/typescript/index.js.map +1 -0
  214. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +48 -0
  215. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +1 -0
  216. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +596 -0
  217. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +1 -0
  218. package/lib/schema/typebox/gen/index.js +3 -0
  219. package/lib/schema/typebox/gen/index.js.map +1 -0
  220. package/lib/schema/typebox/index.js +3 -0
  221. package/lib/schema/typebox/index.js.map +1 -0
  222. package/lib/schema/typebox/typebox.js +23 -0
  223. package/lib/schema/typebox/typebox.js.map +1 -0
  224. package/lib/server.js +2 -1
  225. package/lib/server.js.map +1 -1
  226. package/lib/servers/crypto/md5.js +3 -5
  227. package/lib/servers/crypto/md5.js.map +1 -1
  228. package/lib/servers/fetch/createFetchWithProxy.js +2 -4
  229. package/lib/servers/fetch/createFetchWithProxy.js.map +1 -1
  230. package/lib/servers/fetch/createFetchWithProxyByNodeFetch.js +34 -34
  231. package/lib/servers/fetch/createFetchWithProxyByNodeFetch.js.map +1 -1
  232. package/lib/servers/fetch/createFetchWithProxyByUndici.js +59 -45
  233. package/lib/servers/fetch/createFetchWithProxyByUndici.js.map +1 -1
  234. package/lib/servers/getPackageDir.js +8 -10
  235. package/lib/servers/getPackageDir.js.map +1 -1
  236. package/lib/servers/jsdom.js +3 -2
  237. package/lib/servers/jsdom.js.map +1 -1
  238. package/lib/servers/node-fetch.js +4 -3
  239. package/lib/servers/node-fetch.js.map +1 -1
  240. package/lib/servers/polyfill/polyfillBrowser.js +17 -0
  241. package/lib/servers/polyfill/polyfillBrowser.js.map +1 -0
  242. package/lib/servers/polyfill/polyfillCrypto.js +8 -10
  243. package/lib/servers/polyfill/polyfillCrypto.js.map +1 -1
  244. package/lib/servers/polyfill/polyfillFetch.js +23 -22
  245. package/lib/servers/polyfill/polyfillFetch.js.map +1 -1
  246. package/lib/servers/polyfill/polyfillJsDom.js +59 -52
  247. package/lib/servers/polyfill/polyfillJsDom.js.map +1 -1
  248. package/lib/servers/polyfill/polyfillWebSocket.js +13 -13
  249. package/lib/servers/polyfill/polyfillWebSocket.js.map +1 -1
  250. package/lib/servers/ws.js +3 -2
  251. package/lib/servers/ws.js.map +1 -1
  252. package/lib/strings/camelCase.js +51 -49
  253. package/lib/strings/camelCase.js.map +1 -1
  254. package/lib/strings/formatBytes.js +41 -15
  255. package/lib/strings/formatBytes.js.map +1 -1
  256. package/lib/strings/renderTemplate.js +27 -20
  257. package/lib/strings/renderTemplate.js.map +1 -1
  258. package/lib/typedoc.js +4 -0
  259. package/lib/typedoc.js.map +1 -0
  260. package/lib/types/global.d.js +2 -0
  261. package/lib/types/global.d.js.map +1 -0
  262. package/lib/types/index.d.js +6 -0
  263. package/lib/types/index.d.js.map +1 -0
  264. package/lib/validations/asserts.js +14 -0
  265. package/lib/validations/asserts.js.map +1 -0
  266. package/lib/validations/isUUID.js +3 -4
  267. package/lib/validations/isUUID.js.map +1 -1
  268. package/lib/validations/parseTimestamp.js +25 -21
  269. package/lib/validations/parseTimestamp.js.map +1 -1
  270. package/package.json +59 -44
  271. package/src/index.ts +3 -3
  272. package/src/langs/deepFreeze.ts +19 -0
  273. package/src/langs/hashCode.ignored.ts +125 -0
  274. package/src/libs/README.md +2 -0
  275. package/src/logging/slog.ts +121 -0
  276. package/src/maths/clamp.test.ts +19 -0
  277. package/src/maths/clamp.ts +23 -1
  278. package/src/objects/get.ts +14 -14
  279. package/src/objects/set.ts +5 -5
  280. package/src/schema/typebox/gen/codegen/common/encoder.ts +99 -0
  281. package/src/schema/typebox/gen/codegen/common/formatter.ts +31 -0
  282. package/src/schema/typebox/gen/codegen/common/index.ts +29 -0
  283. package/src/schema/typebox/gen/codegen/common/jsdoc.ts +93 -0
  284. package/src/schema/typebox/gen/codegen/expression/compiler.ts +377 -0
  285. package/src/schema/typebox/gen/codegen/expression/errors.ts +302 -0
  286. package/src/schema/typebox/gen/codegen/expression/evaluator.ts +268 -0
  287. package/src/schema/typebox/gen/codegen/expression/expression.ts +538 -0
  288. package/src/schema/typebox/gen/codegen/expression/index.ts +32 -0
  289. package/src/schema/typebox/gen/codegen/index.ts +29 -0
  290. package/src/schema/typebox/gen/codegen/model/index.ts +40 -0
  291. package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +266 -0
  292. package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +378 -0
  293. package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +244 -0
  294. package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +294 -0
  295. package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +42 -0
  296. package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +166 -0
  297. package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +32 -0
  298. package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +189 -0
  299. package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +236 -0
  300. package/src/schema/typebox/gen/codegen/model/model-to-value.ts +42 -0
  301. package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +232 -0
  302. package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +226 -0
  303. package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +251 -0
  304. package/src/schema/typebox/gen/codegen/model/model.ts +33 -0
  305. package/src/schema/typebox/gen/codegen/typescript/index.ts +28 -0
  306. package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +61 -0
  307. package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +647 -0
  308. package/src/schema/typebox/gen/gen.test.ts +12 -0
  309. package/src/schema/typebox/gen/index.ts +1 -0
  310. package/src/schema/typebox/index.ts +1 -0
  311. package/src/schema/typebox/typebox.ts +34 -0
  312. package/src/servers/fetch/createFetchWithProxyByUndici.ts +16 -1
  313. package/src/types/index.d.ts +1 -0
  314. package/dist/LICENSE.txt +0 -1
  315. package/dist/cjs/getGlobalThis-304f74e0.js +0 -2
  316. package/dist/cjs/getGlobalThis-304f74e0.js.map +0 -1
  317. package/dist/cjs/index.cjs +0 -21
  318. package/dist/cjs/index.cjs.map +0 -1
  319. package/dist/cjs/server.cjs +0 -2
  320. package/dist/cjs/server.cjs.map +0 -1
  321. package/dist/cjs/servers/jsdom.cjs +0 -2
  322. package/dist/cjs/servers/jsdom.cjs.map +0 -1
  323. package/dist/cjs/servers/node-fetch.cjs +0 -2
  324. package/dist/cjs/servers/node-fetch.cjs.map +0 -1
  325. package/dist/cjs/servers/ws.cjs +0 -2
  326. package/dist/cjs/servers/ws.cjs.map +0 -1
  327. package/dist/esm/getGlobalThis-b7ad0cf9.js +0 -2
  328. package/dist/esm/getGlobalThis-b7ad0cf9.js.map +0 -1
  329. package/dist/esm/index.js +0 -21
  330. package/dist/esm/index.js.map +0 -1
  331. package/dist/esm/server.js +0 -2
  332. package/dist/esm/server.js.map +0 -1
  333. package/dist/esm/servers/jsdom.js +0 -2
  334. package/dist/esm/servers/jsdom.js.map +0 -1
  335. package/dist/esm/servers/node-fetch.js +0 -2
  336. package/dist/esm/servers/node-fetch.js.map +0 -1
  337. package/dist/esm/servers/ws.js +0 -2
  338. package/dist/esm/servers/ws.js.map +0 -1
  339. package/dist/system/getGlobalThis-8951eb0e.js +0 -2
  340. package/dist/system/getGlobalThis-8951eb0e.js.map +0 -1
  341. package/dist/system/index.js +0 -21
  342. package/dist/system/index.js.map +0 -1
  343. package/dist/system/server.js +0 -2
  344. package/dist/system/server.js.map +0 -1
  345. package/dist/system/servers/jsdom.js +0 -2
  346. package/dist/system/servers/jsdom.js.map +0 -1
  347. package/dist/system/servers/node-fetch.js +0 -2
  348. package/dist/system/servers/node-fetch.js.map +0 -1
  349. package/dist/system/servers/ws.js +0 -2
  350. package/dist/system/servers/ws.js.map +0 -1
  351. package/src/langs/hashCode.ts +0 -37
@@ -0,0 +1,99 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-codegen
4
+
5
+ The MIT License (MIT)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
25
+ ---------------------------------------------------------------------------*/
26
+
27
+ // --------------------------------------------------------------------------
28
+ // Character
29
+ // --------------------------------------------------------------------------
30
+ namespace Character {
31
+ export function DollarSign(code: number) {
32
+ return code === 36;
33
+ }
34
+ export function IsUnderscore(code: number) {
35
+ return code === 95;
36
+ }
37
+ export function IsAlpha(code: number) {
38
+ return (code >= 64 && code <= 90) || (code >= 97 && code <= 122);
39
+ }
40
+ export function IsNumeric(code: number) {
41
+ return code >= 48 && code <= 57;
42
+ }
43
+ }
44
+ // --------------------------------------------------------------------------
45
+ // MemberExpressionEncoder
46
+ // --------------------------------------------------------------------------
47
+ export namespace MemberExpressionEncoder {
48
+ function IsFirstCharacterNumeric(value: string) {
49
+ if (value.length === 0) return false;
50
+ return Character.IsNumeric(value.charCodeAt(0));
51
+ }
52
+ function IsAccessor(value: string) {
53
+ if (IsFirstCharacterNumeric(value)) return false;
54
+ for (let i = 0; i < value.length; i++) {
55
+ const code = value.charCodeAt(i);
56
+ const check =
57
+ Character.IsAlpha(code) ||
58
+ Character.IsNumeric(code) ||
59
+ Character.DollarSign(code) ||
60
+ Character.IsUnderscore(code);
61
+ if (!check) return false;
62
+ }
63
+ return true;
64
+ }
65
+ function EscapeHyphen(key: string) {
66
+ return key.replace(/'/g, "\\'");
67
+ }
68
+ export function Encode(object: string, key: string) {
69
+ return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`;
70
+ }
71
+ }
72
+ // --------------------------------------------------------------------------
73
+ // PropertyEncoder
74
+ // --------------------------------------------------------------------------
75
+ export namespace PropertyEncoder {
76
+ function IsFirstCharacterNumeric(value: string) {
77
+ if (value.length === 0) return false;
78
+ return Character.IsNumeric(value.charCodeAt(0));
79
+ }
80
+ function IsSafeProperty(value: string) {
81
+ if (IsFirstCharacterNumeric(value)) return false;
82
+ for (let i = 0; i < value.length; i++) {
83
+ const code = value.charCodeAt(i);
84
+ const check =
85
+ Character.IsAlpha(code) ||
86
+ Character.IsNumeric(code) ||
87
+ Character.DollarSign(code) ||
88
+ Character.IsUnderscore(code);
89
+ if (!check) return false;
90
+ }
91
+ return true;
92
+ }
93
+ function Quoted(value: string) {
94
+ return `'${value}'`;
95
+ }
96
+ export function Encode(value: string) {
97
+ return IsSafeProperty(value) ? value : Quoted(value);
98
+ }
99
+ }
@@ -0,0 +1,31 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-codegen
4
+
5
+ The MIT License (MIT)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
25
+ ---------------------------------------------------------------------------*/
26
+
27
+ export namespace Formatter {
28
+ export function Format(code: string): string {
29
+ return code;
30
+ }
31
+ }
@@ -0,0 +1,29 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-codegen
4
+
5
+ The MIT License (MIT)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
25
+ ---------------------------------------------------------------------------*/
26
+
27
+ export * from './encoder';
28
+ export * from './formatter';
29
+ export * from './jsdoc';
@@ -0,0 +1,93 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-codegen
4
+
5
+ The MIT License (MIT)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+
25
+ ---------------------------------------------------------------------------*/
26
+
27
+ /** JSDoc Property Parser */
28
+ export namespace JsDoc {
29
+ // ----------------------------------------------------------------
30
+ // Quotes
31
+ // ----------------------------------------------------------------
32
+ function ConsumeQuote(content: string): [consumed: string, rest: string] {
33
+ if (!IsQuote(content)) return ['', content];
34
+ const quote = content[0];
35
+ for (let i = 1; i < content.length; i++) {
36
+ if (content[i] === quote) return [content.slice(1, i), content.slice(i)];
37
+ if (content[i] === '\n') return [content.slice(1, i), content.slice(i)];
38
+ }
39
+ return [content.slice(1), ''];
40
+ }
41
+ function IsQuote(content: string) {
42
+ return content[0] === '"' || content[0] === "'";
43
+ }
44
+ // ----------------------------------------------------------------
45
+ // Decode
46
+ // ----------------------------------------------------------------
47
+ function DecodeWithNonQuotedProperties(content: string): any {
48
+ const parseFunction = new Function(`return (${content});`);
49
+ return parseFunction();
50
+ }
51
+ function Decode(content: string) {
52
+ try {
53
+ return DecodeWithNonQuotedProperties(content);
54
+ } catch {
55
+ return content;
56
+ }
57
+ }
58
+ function* ParseValue(key: string, content: string): IterableIterator<[string, any]> {
59
+ if (IsQuote(content)) {
60
+ const [consumed, rest] = ConsumeQuote(content);
61
+ yield [key, Decode(consumed)];
62
+ return yield* ParseContent(rest);
63
+ }
64
+ for (let i = 0; i < content.length; i++) {
65
+ if (content[i] === '\n' || content[i] === '-') {
66
+ const value = content.slice(0, i).trim();
67
+ const rest = content.slice(i);
68
+ yield [key, Decode(value)];
69
+ return yield* ParseContent(rest);
70
+ }
71
+ }
72
+ }
73
+ function* ParseKey(content: string): IterableIterator<[string, any]> {
74
+ for (let i = 1; i < content.length; i++) {
75
+ if (content[i] === ' ') {
76
+ const key = content.slice(1, i);
77
+ const rest = content.slice(i).trimStart();
78
+ return yield* ParseValue(key, rest);
79
+ }
80
+ }
81
+ }
82
+ function* ParseContent(content: string): IterableIterator<[string, any]> {
83
+ for (let i = 0; i < content.length; i++) {
84
+ if (content[i] === '@') {
85
+ return yield* ParseKey(content.slice(i));
86
+ }
87
+ }
88
+ }
89
+ export function Parse(content: string): Record<string, any> {
90
+ const properties = [...ParseContent(content)];
91
+ return properties.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
92
+ }
93
+ }
@@ -0,0 +1,377 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ @sinclair/typebox-expression
4
+
5
+ The MIT License (MIT)
6
+
7
+ Copyright (c) 2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in
17
+ all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ THE SOFTWARE.
26
+
27
+ ---------------------------------------------------------------------------*/
28
+ import { Hash } from '@sinclair/typebox/value';
29
+ import * as Expr from './expression';
30
+
31
+ // --------------------------------------------------------------------------
32
+ // PropertyFormatter
33
+ // --------------------------------------------------------------------------
34
+
35
+ namespace MemberAccessor {
36
+ function DollarSign(code: number) {
37
+ return code === 36;
38
+ }
39
+ function Underscore(code: number) {
40
+ return code === 95;
41
+ }
42
+ function Numeric(code: number) {
43
+ return code >= 48 && code <= 57;
44
+ }
45
+ function Alpha(code: number) {
46
+ return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
47
+ }
48
+ export function IsDot(propertyName: string) {
49
+ if (propertyName.length === 0) return false;
50
+ {
51
+ const code = propertyName.charCodeAt(0);
52
+ if (!(DollarSign(code) || Underscore(code) || Alpha(code))) {
53
+ return false;
54
+ }
55
+ }
56
+ for (let i = 1; i < propertyName.length; i++) {
57
+ const code = propertyName.charCodeAt(i);
58
+ if (!(DollarSign(code) || Underscore(code) || Alpha(code) || Numeric(code))) {
59
+ return false;
60
+ }
61
+ }
62
+ return true;
63
+ }
64
+ }
65
+
66
+ // --------------------------------------------------------------------------
67
+ // Errors
68
+ // --------------------------------------------------------------------------
69
+
70
+ export class UnknownExpressionError extends Error {
71
+ constructor(public readonly expression: unknown) {
72
+ super('Unknown Expression Detected');
73
+ }
74
+ }
75
+ export class ReferencedExpressionNotFound extends Error {
76
+ constructor(public readonly expression: unknown) {
77
+ super('Referenced Expression Not Found');
78
+ }
79
+ }
80
+ // --------------------------------------------------------------------------
81
+ // ExpressionCheck
82
+ // --------------------------------------------------------------------------
83
+
84
+ export class ExpressionCheck {
85
+ constructor(
86
+ private readonly check: (value: unknown) => boolean,
87
+ private readonly locals: Map<string, unknown>,
88
+ private readonly code: string,
89
+ ) {}
90
+ public Code(): string {
91
+ return this.code;
92
+ }
93
+ public Check(value: unknown): boolean {
94
+ return this.check(value);
95
+ }
96
+ public Locals(): Map<string, unknown> {
97
+ return this.locals;
98
+ }
99
+ }
100
+
101
+ // --------------------------------------------------------------------------
102
+ // ExpressionCheckCompiler
103
+ // --------------------------------------------------------------------------
104
+
105
+ export namespace ExpressionCompiler {
106
+ function And(expression: Expr.AndExpression, value: string): string {
107
+ return `(${expression.expr.map((expression) => Visit(expression, `${value}`)).join(' && ')})`;
108
+ }
109
+ function Call(expression: Expr.CallExpression, value: string): string {
110
+ return Visit(expression.expr, `${value}.${expression.target}(...${SetExtern(expression.parameters)})`);
111
+ }
112
+ function ElementsDistinct(expression: Expr.ElementsDistinctExpression, value: string): string {
113
+ return `((array) => { const set = new Set(); for (const value of array) { const hash = __hash(value); if (set.has(hash)) return false; set.add(hash) } return true })(${value})`;
114
+ }
115
+ function Elements(expression: Expr.ElementsExpression, value: string): string {
116
+ return `${value}.every((value) => ${Visit(expression.expr, 'value')})`;
117
+ }
118
+ function Equals(expression: Expr.EqualsExpression, value: string): string {
119
+ return typeof expression.value === 'string'
120
+ ? `${value} === '${expression.value}'`
121
+ : `${value} === ${expression.value}`;
122
+ }
123
+ function False(expression: Expr.FalseExpression, value: string): string {
124
+ return `false`;
125
+ }
126
+ function Function(expression: Expr.FunctionExpression, value: string): string {
127
+ return `${SetExtern(expression.callback)}(${value})`;
128
+ }
129
+ function GreaterThan(expression: Expr.GreaterThanExpression, value: string): string {
130
+ return `${value} > ${expression.value}`;
131
+ }
132
+ function GreaterThanEqual(expression: Expr.GreaterThanEqualExpression, value: string): string {
133
+ return `${value} >= ${expression.value}`;
134
+ }
135
+ function IfThenElse(expression: Expr.IfThenElseExpression, value: string): string {
136
+ return `(${Visit(expression.if, `${value}`)}) ? (${Visit(expression.then, `${value}`)}) : (${Visit(
137
+ expression.else,
138
+ `${value}`,
139
+ )})`;
140
+ }
141
+ function Index(expression: Expr.IndexExpression, value: string): string {
142
+ return Visit(expression.expr, `${value}[${expression.index}]`);
143
+ }
144
+ function InstanceOf(expression: Expr.InstanceOfExpression, value: string): string {
145
+ return `${value} instanceof ${expression.value}`;
146
+ }
147
+ function IsArray(expression: Expr.IsArrayExpression, value: string): string {
148
+ return `Array.isArray(${value})`;
149
+ }
150
+ function IsFinite(expression: Expr.IsFiniteExpression, value: string): string {
151
+ return `Number.isFinite(${value})`;
152
+ }
153
+ function IsInteger(expression: Expr.IsIntegerExpression, value: string): string {
154
+ return `Number.isInteger(${value})`;
155
+ }
156
+ function IsNaN(expression: Expr.IsNaNExpression, value: string): string {
157
+ return `Number.isNaN(${value})`;
158
+ }
159
+ function IsSafeInteger(expression: Expr.IsSafeIntegerExpression, value: string): string {
160
+ return `${value} === undefined`;
161
+ }
162
+ function IsUndefined(expression: Expr.IsUndefinedExpression, value: string): string {
163
+ return `Number.isSafeInteger(${value})`;
164
+ }
165
+ function LessThan(expression: Expr.LessThanExpression, value: string): string {
166
+ return `${value} < ${expression.value}`;
167
+ }
168
+ function LessThanEqual(expression: Expr.LessThanEqualExpression, value: string): string {
169
+ return `${value} <= ${expression.value}`;
170
+ }
171
+ function MultipleOf(expression: Expr.MultipleOfExpression, value: string): string {
172
+ return `${value} % ${expression.value} === 0`;
173
+ }
174
+ function Not(expression: Expr.NotExpression, value: string): string {
175
+ return `!(${Visit(expression.expr, value)})`;
176
+ }
177
+ function Or(expression: Expr.OrExpression, value: string): string {
178
+ return `(${expression.expr.map((expression) => Visit(expression, `${value}`)).join(' || ')})`;
179
+ }
180
+ function PropertiesExclude(expression: Expr.PropertiesExcludeExpression, value: string): string {
181
+ return `Object.getOwnPropertyNames(${value}).every((key) => !${SetExtern(expression.keys)}.includes(key) ? ${Visit(
182
+ expression.expr,
183
+ `${value}[key]`,
184
+ )} : true)`;
185
+ }
186
+ function PropetiesExcludePattern(expression: Expr.PropertiesExcludePatternExpression, value: string): string {
187
+ const local = SetLocal(`new RegExp(/${expression.pattern}/)`);
188
+ return `Object.getOwnPropertyNames(${value}).every((key) => !${local}.test(key) ? ${Visit(
189
+ expression.expr,
190
+ `${value}[key]`,
191
+ )} : true)`;
192
+ }
193
+ function PropertiesInclude(expression: Expr.PropertiesIncludeExpression, value: string): string {
194
+ return `Object.getOwnPropertyNames(${value}).every((key) => ${SetExtern(expression.keys)}.includes(key) ? ${Visit(
195
+ expression.expr,
196
+ `${value}[key]`,
197
+ )} : true)`;
198
+ }
199
+ function PropertiesIncludePattern(expression: Expr.PropertiesIncludePatternExpression, value: string): string {
200
+ const local = SetLocal(`new RegExp(/${expression.pattern}/)`);
201
+ return `Object.getOwnPropertyNames(${value}).every((key) => ${local}.test(key) ? ${Visit(
202
+ expression.expr,
203
+ `${value}[key]`,
204
+ )} : true)`;
205
+ }
206
+ function PropertiesLength(expression: Expr.PropertiesLengthExpression, value: string): string {
207
+ return `Object.getOwnPropertyNames(${value}).length === ${expression.value}`;
208
+ }
209
+ function PropertiesMaximum(expression: Expr.PropertiesMaximumExpression, value: string): string {
210
+ return `Object.getOwnPropertyNames(${value}).length <= ${expression.value}`;
211
+ }
212
+ function PropertiesMinimum(expression: Expr.PropertiesMinimumExpression, value: string): string {
213
+ return `Object.getOwnPropertyNames(${value}).length >= ${expression.value}`;
214
+ }
215
+ function Properties(expression: Expr.PropertiesExpression, value: string): string {
216
+ return `Object.getOwnPropertyNames(${value}).every((key) => ${Visit(expression.expr, `${value}[key]`)})`;
217
+ }
218
+ function PropertyKeys(expression: Expr.PropertyKeysExpression, value: string): string {
219
+ const keys = expression.keys.map((key) => `'${key}'`).join(', ');
220
+ return `((value) => { const keys = Object.getOwnPropertyNames(value); return [${keys}].every((key) => keys.includes(key)); })(${value})`;
221
+ }
222
+ function Property(expression: Expr.PropertyExpression, value: string): string {
223
+ return MemberAccessor.IsDot(expression.key)
224
+ ? Visit(expression.expr, `${value}.${expression.key}`)
225
+ : Visit(expression.expr, `${value}['${expression.key}']`);
226
+ }
227
+ function IsPattern(expression: Expr.IsPatternExpression, value: string): string {
228
+ const local = SetLocal(`new RegExp(/${expression.pattern}/)`);
229
+ return `${local}.test(${value})`;
230
+ }
231
+ function Ref(expression: Expr.RefExpression, value: string): string {
232
+ if (!reference_map.has(expression.$ref)) throw new ReferencedExpressionNotFound(expression);
233
+ // Ref: All referenced expressions are hoisted into their own functions. We track a dictionary of
234
+ // function names and compiled expressions here which is later emitted when generating the module.
235
+ // If the function doesn't exist, build and just return the check_T() call.
236
+ if (!function_map.has(expression.$ref)) {
237
+ function_map.set(expression.$ref, ''); // terminate recursion
238
+ const reference = reference_map.get(expression.$ref)!;
239
+ function_map.set(expression.$ref, Visit(reference, 'value'));
240
+ }
241
+ return `check_${expression.$ref}(${value})`;
242
+ }
243
+ function True(expression: Expr.TrueExpression, value: string): string {
244
+ return `true`;
245
+ }
246
+ function TypeOf(expression: Expr.TypeOfExpression, value: string): string {
247
+ return `typeof ${value} === '${expression.value}'`;
248
+ }
249
+ function Visit(expression: Expr.Expression, value: string): string {
250
+ // Root and Recursive: All expressions with $ids are hoisted into their own functions. Here we check
251
+ // if we have observed the $id before, and if not, generate the function and return check_T(). This
252
+ // only occurs for discovered references which are typical of recursive expressions, but may also
253
+ // include the Root expression if it has been specified with an $id.
254
+ if (expression.$id !== undefined && !reference_map.has(expression.$id)) {
255
+ reference_map.set(expression.$id, expression);
256
+ const reference = reference_map.get(expression.$id)!;
257
+ function_map.set(expression.$id, Visit(reference, 'value'));
258
+ return `check_${expression.$id}(${value})`;
259
+ }
260
+ switch (expression.type) {
261
+ case 'And':
262
+ return And(expression, value);
263
+ case 'Call':
264
+ return Call(expression, value);
265
+ case 'ElementsDistinct':
266
+ return ElementsDistinct(expression, value);
267
+ case 'Elements':
268
+ return Elements(expression, value);
269
+ case 'Equals':
270
+ return Equals(expression, value);
271
+ case 'False':
272
+ return False(expression, value);
273
+ case 'Function':
274
+ return Function(expression, value);
275
+ case 'GreaterThanEqual':
276
+ return GreaterThanEqual(expression, value);
277
+ case 'GreaterThan':
278
+ return GreaterThan(expression, value);
279
+ case 'IfThenElse':
280
+ return IfThenElse(expression, value);
281
+ case 'InstanceOf':
282
+ return InstanceOf(expression, value);
283
+ case 'Index':
284
+ return Index(expression, value);
285
+ case 'IsArray':
286
+ return IsArray(expression, value);
287
+ case 'IsFinite':
288
+ return IsFinite(expression, value);
289
+ case 'IsInteger':
290
+ return IsInteger(expression, value);
291
+ case 'IsNaN':
292
+ return IsNaN(expression, value);
293
+ case 'IsPattern':
294
+ return IsPattern(expression, value);
295
+ case 'IsSafeInteger':
296
+ return IsSafeInteger(expression, value);
297
+ case 'IsUndefined':
298
+ return IsUndefined(expression, value);
299
+ case 'LessThan':
300
+ return LessThan(expression, value);
301
+ case 'LessThanEqual':
302
+ return LessThanEqual(expression, value);
303
+ case 'MultipleOf':
304
+ return MultipleOf(expression, value);
305
+ case 'Not':
306
+ return Not(expression, value);
307
+ case 'Or':
308
+ return Or(expression, value);
309
+ case 'PropertiesInclude':
310
+ return PropertiesInclude(expression, value);
311
+ case 'PropertiesIncludePattern':
312
+ return PropertiesIncludePattern(expression, value);
313
+ case 'PropertiesExclude':
314
+ return PropertiesExclude(expression, value);
315
+ case 'PropertiesExcludePattern':
316
+ return PropetiesExcludePattern(expression, value);
317
+ case 'PropertiesLength':
318
+ return PropertiesLength(expression, value);
319
+ case 'PropertiesMaximum':
320
+ return PropertiesMaximum(expression, value);
321
+ case 'PropertiesMinimum':
322
+ return PropertiesMinimum(expression, value);
323
+ case 'Properties':
324
+ return Properties(expression, value);
325
+ case 'PropertyKeys':
326
+ return PropertyKeys(expression, value);
327
+ case 'Property':
328
+ return Property(expression, value);
329
+ case 'Ref':
330
+ return Ref(expression, value);
331
+ case 'True':
332
+ return True(expression, value);
333
+ case 'TypeOf':
334
+ return TypeOf(expression, value);
335
+ default:
336
+ throw new UnknownExpressionError(expression);
337
+ }
338
+ }
339
+
340
+ // -------------------------------------------------------
341
+ // Compiler State
342
+ // -------------------------------------------------------
343
+ function SetLocal(code: string): string {
344
+ const key = `local_${local_map.size}`;
345
+ local_map.set(key, code);
346
+ return key;
347
+ }
348
+ function SetExtern(value: unknown): string {
349
+ const key = `extern_${extern_map.size}`;
350
+ extern_map.set(key, value);
351
+ return key;
352
+ }
353
+ const reference_map = new Map<string, Expr.Expression>();
354
+ const function_map = new Map<string, string>();
355
+ const extern_map = new Map<string, unknown>();
356
+ const local_map = new Map<string, unknown>();
357
+
358
+ /** Compiles an expression and associated references to target V8 hidden class optimizations */
359
+ export function Compile(expression: Expr.Expression, references: Expr.Expression[] = []): ExpressionCheck {
360
+ reference_map.clear();
361
+ function_map.clear();
362
+ extern_map.clear();
363
+ local_map.clear();
364
+ for (const reference of references) {
365
+ if (reference.$id === undefined) continue;
366
+ reference_map.set(reference.$id, reference);
367
+ }
368
+ const root = `return function check(value) { return ${Visit(expression, 'value')} }`;
369
+ const funcs = [...function_map.entries()].map(([key, body]) => `function check_${key}(value) { return ${body} }`);
370
+ const locals = [...local_map.entries()].map(([key, code]) => `const ${key} = ${code}`);
371
+ const check_module = [...locals, ...funcs, root].join('\n');
372
+ // generate bootstrap
373
+ const bootstrap = new globalThis.Function('__hash', ...extern_map.keys(), check_module);
374
+ const check = bootstrap((value: unknown) => Hash(value), ...extern_map.values());
375
+ return new ExpressionCheck(check, new Map(extern_map), check_module);
376
+ }
377
+ }