@welshman/domain 0.9.0-pre1

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 (443) hide show
  1. package/LICENSE +21 -0
  2. package/dist/domain/src/EventBuilder.d.ts +35 -0
  3. package/dist/domain/src/EventBuilder.d.ts.map +1 -0
  4. package/dist/domain/src/EventBuilder.js +111 -0
  5. package/dist/domain/src/EventBuilder.js.map +1 -0
  6. package/dist/domain/src/EventReader.d.ts +24 -0
  7. package/dist/domain/src/EventReader.d.ts.map +1 -0
  8. package/dist/domain/src/EventReader.js +57 -0
  9. package/dist/domain/src/EventReader.js.map +1 -0
  10. package/dist/domain/src/ListBuilder.d.ts +21 -0
  11. package/dist/domain/src/ListBuilder.d.ts.map +1 -0
  12. package/dist/domain/src/ListBuilder.js +73 -0
  13. package/dist/domain/src/ListBuilder.js.map +1 -0
  14. package/dist/domain/src/ListReader.d.ts +12 -0
  15. package/dist/domain/src/ListReader.d.ts.map +1 -0
  16. package/dist/domain/src/ListReader.js +31 -0
  17. package/dist/domain/src/ListReader.js.map +1 -0
  18. package/dist/domain/src/index.d.ts +54 -0
  19. package/dist/domain/src/index.d.ts.map +1 -0
  20. package/dist/domain/src/index.js +54 -0
  21. package/dist/domain/src/index.js.map +1 -0
  22. package/dist/domain/src/kinds/BlockedRelayList.d.ts +15 -0
  23. package/dist/domain/src/kinds/BlockedRelayList.d.ts.map +1 -0
  24. package/dist/domain/src/kinds/BlockedRelayList.js +30 -0
  25. package/dist/domain/src/kinds/BlockedRelayList.js.map +1 -0
  26. package/dist/domain/src/kinds/BlossomServerList.d.ts +15 -0
  27. package/dist/domain/src/kinds/BlossomServerList.d.ts.map +1 -0
  28. package/dist/domain/src/kinds/BlossomServerList.js +30 -0
  29. package/dist/domain/src/kinds/BlossomServerList.js.map +1 -0
  30. package/dist/domain/src/kinds/BookmarkList.d.ts +17 -0
  31. package/dist/domain/src/kinds/BookmarkList.d.ts.map +1 -0
  32. package/dist/domain/src/kinds/BookmarkList.js +36 -0
  33. package/dist/domain/src/kinds/BookmarkList.js.map +1 -0
  34. package/dist/domain/src/kinds/Classified.d.ts +27 -0
  35. package/dist/domain/src/kinds/Classified.d.ts.map +1 -0
  36. package/dist/domain/src/kinds/Classified.js +65 -0
  37. package/dist/domain/src/kinds/Classified.js.map +1 -0
  38. package/dist/domain/src/kinds/Comment.d.ts +29 -0
  39. package/dist/domain/src/kinds/Comment.d.ts.map +1 -0
  40. package/dist/domain/src/kinds/Comment.js +82 -0
  41. package/dist/domain/src/kinds/Comment.js.map +1 -0
  42. package/dist/domain/src/kinds/EmojiList.d.ts +16 -0
  43. package/dist/domain/src/kinds/EmojiList.d.ts.map +1 -0
  44. package/dist/domain/src/kinds/EmojiList.js +33 -0
  45. package/dist/domain/src/kinds/EmojiList.js.map +1 -0
  46. package/dist/domain/src/kinds/Feed.d.ts +17 -0
  47. package/dist/domain/src/kinds/Feed.d.ts.map +1 -0
  48. package/dist/domain/src/kinds/Feed.js +33 -0
  49. package/dist/domain/src/kinds/Feed.js.map +1 -0
  50. package/dist/domain/src/kinds/FeedList.d.ts +15 -0
  51. package/dist/domain/src/kinds/FeedList.d.ts.map +1 -0
  52. package/dist/domain/src/kinds/FeedList.js +30 -0
  53. package/dist/domain/src/kinds/FeedList.js.map +1 -0
  54. package/dist/domain/src/kinds/FollowList.d.ts +14 -0
  55. package/dist/domain/src/kinds/FollowList.d.ts.map +1 -0
  56. package/dist/domain/src/kinds/FollowList.js +27 -0
  57. package/dist/domain/src/kinds/FollowList.js.map +1 -0
  58. package/dist/domain/src/kinds/GroupList.d.ts +13 -0
  59. package/dist/domain/src/kinds/GroupList.d.ts.map +1 -0
  60. package/dist/domain/src/kinds/GroupList.js +24 -0
  61. package/dist/domain/src/kinds/GroupList.js.map +1 -0
  62. package/dist/domain/src/kinds/Handler.d.ts +41 -0
  63. package/dist/domain/src/kinds/Handler.d.ts.map +1 -0
  64. package/dist/domain/src/kinds/Handler.js +86 -0
  65. package/dist/domain/src/kinds/Handler.js.map +1 -0
  66. package/dist/domain/src/kinds/HandlerRecommendation.d.ts +15 -0
  67. package/dist/domain/src/kinds/HandlerRecommendation.d.ts.map +1 -0
  68. package/dist/domain/src/kinds/HandlerRecommendation.js +32 -0
  69. package/dist/domain/src/kinds/HandlerRecommendation.js.map +1 -0
  70. package/dist/domain/src/kinds/MessagingRelayList.d.ts +14 -0
  71. package/dist/domain/src/kinds/MessagingRelayList.d.ts.map +1 -0
  72. package/dist/domain/src/kinds/MessagingRelayList.js +27 -0
  73. package/dist/domain/src/kinds/MessagingRelayList.js.map +1 -0
  74. package/dist/domain/src/kinds/MuteList.d.ts +15 -0
  75. package/dist/domain/src/kinds/MuteList.d.ts.map +1 -0
  76. package/dist/domain/src/kinds/MuteList.js +30 -0
  77. package/dist/domain/src/kinds/MuteList.js.map +1 -0
  78. package/dist/domain/src/kinds/Pin.d.ts +37 -0
  79. package/dist/domain/src/kinds/Pin.d.ts.map +1 -0
  80. package/dist/domain/src/kinds/Pin.js +74 -0
  81. package/dist/domain/src/kinds/Pin.js.map +1 -0
  82. package/dist/domain/src/kinds/PinList.d.ts +15 -0
  83. package/dist/domain/src/kinds/PinList.d.ts.map +1 -0
  84. package/dist/domain/src/kinds/PinList.js +30 -0
  85. package/dist/domain/src/kinds/PinList.js.map +1 -0
  86. package/dist/domain/src/kinds/Pinboard.d.ts +21 -0
  87. package/dist/domain/src/kinds/Pinboard.d.ts.map +1 -0
  88. package/dist/domain/src/kinds/Pinboard.js +53 -0
  89. package/dist/domain/src/kinds/Pinboard.js.map +1 -0
  90. package/dist/domain/src/kinds/Poll.d.ts +37 -0
  91. package/dist/domain/src/kinds/Poll.d.ts.map +1 -0
  92. package/dist/domain/src/kinds/Poll.js +80 -0
  93. package/dist/domain/src/kinds/Poll.js.map +1 -0
  94. package/dist/domain/src/kinds/PollResponse.d.ts +15 -0
  95. package/dist/domain/src/kinds/PollResponse.d.ts.map +1 -0
  96. package/dist/domain/src/kinds/PollResponse.js +33 -0
  97. package/dist/domain/src/kinds/PollResponse.js.map +1 -0
  98. package/dist/domain/src/kinds/Profile.d.ts +36 -0
  99. package/dist/domain/src/kinds/Profile.d.ts.map +1 -0
  100. package/dist/domain/src/kinds/Profile.js +105 -0
  101. package/dist/domain/src/kinds/Profile.js.map +1 -0
  102. package/dist/domain/src/kinds/RelayAddMember.d.ts +12 -0
  103. package/dist/domain/src/kinds/RelayAddMember.d.ts.map +1 -0
  104. package/dist/domain/src/kinds/RelayAddMember.js +21 -0
  105. package/dist/domain/src/kinds/RelayAddMember.js.map +1 -0
  106. package/dist/domain/src/kinds/RelayInvite.d.ts +12 -0
  107. package/dist/domain/src/kinds/RelayInvite.d.ts.map +1 -0
  108. package/dist/domain/src/kinds/RelayInvite.js +21 -0
  109. package/dist/domain/src/kinds/RelayInvite.js.map +1 -0
  110. package/dist/domain/src/kinds/RelayJoin.d.ts +14 -0
  111. package/dist/domain/src/kinds/RelayJoin.d.ts.map +1 -0
  112. package/dist/domain/src/kinds/RelayJoin.js +27 -0
  113. package/dist/domain/src/kinds/RelayJoin.js.map +1 -0
  114. package/dist/domain/src/kinds/RelayLeave.d.ts +10 -0
  115. package/dist/domain/src/kinds/RelayLeave.d.ts.map +1 -0
  116. package/dist/domain/src/kinds/RelayLeave.js +14 -0
  117. package/dist/domain/src/kinds/RelayLeave.js.map +1 -0
  118. package/dist/domain/src/kinds/RelayList.d.ts +24 -0
  119. package/dist/domain/src/kinds/RelayList.d.ts.map +1 -0
  120. package/dist/domain/src/kinds/RelayList.js +89 -0
  121. package/dist/domain/src/kinds/RelayList.js.map +1 -0
  122. package/dist/domain/src/kinds/RelayMembers.d.ts +17 -0
  123. package/dist/domain/src/kinds/RelayMembers.d.ts.map +1 -0
  124. package/dist/domain/src/kinds/RelayMembers.js +38 -0
  125. package/dist/domain/src/kinds/RelayMembers.js.map +1 -0
  126. package/dist/domain/src/kinds/RelayRemoveMember.d.ts +12 -0
  127. package/dist/domain/src/kinds/RelayRemoveMember.d.ts.map +1 -0
  128. package/dist/domain/src/kinds/RelayRemoveMember.js +21 -0
  129. package/dist/domain/src/kinds/RelayRemoveMember.js.map +1 -0
  130. package/dist/domain/src/kinds/RelaySet.d.ts +20 -0
  131. package/dist/domain/src/kinds/RelaySet.d.ts.map +1 -0
  132. package/dist/domain/src/kinds/RelaySet.js +45 -0
  133. package/dist/domain/src/kinds/RelaySet.js.map +1 -0
  134. package/dist/domain/src/kinds/Report.d.ts +18 -0
  135. package/dist/domain/src/kinds/Report.d.ts.map +1 -0
  136. package/dist/domain/src/kinds/Report.js +59 -0
  137. package/dist/domain/src/kinds/Report.js.map +1 -0
  138. package/dist/domain/src/kinds/RoomAddMember.d.ts +12 -0
  139. package/dist/domain/src/kinds/RoomAddMember.d.ts.map +1 -0
  140. package/dist/domain/src/kinds/RoomAddMember.js +21 -0
  141. package/dist/domain/src/kinds/RoomAddMember.js.map +1 -0
  142. package/dist/domain/src/kinds/RoomAdmins.d.ts +14 -0
  143. package/dist/domain/src/kinds/RoomAdmins.d.ts.map +1 -0
  144. package/dist/domain/src/kinds/RoomAdmins.js +27 -0
  145. package/dist/domain/src/kinds/RoomAdmins.js.map +1 -0
  146. package/dist/domain/src/kinds/RoomCreate.d.ts +11 -0
  147. package/dist/domain/src/kinds/RoomCreate.d.ts.map +1 -0
  148. package/dist/domain/src/kinds/RoomCreate.js +20 -0
  149. package/dist/domain/src/kinds/RoomCreate.js.map +1 -0
  150. package/dist/domain/src/kinds/RoomCreatePermission.d.ts +15 -0
  151. package/dist/domain/src/kinds/RoomCreatePermission.d.ts.map +1 -0
  152. package/dist/domain/src/kinds/RoomCreatePermission.js +30 -0
  153. package/dist/domain/src/kinds/RoomCreatePermission.js.map +1 -0
  154. package/dist/domain/src/kinds/RoomDelete.d.ts +11 -0
  155. package/dist/domain/src/kinds/RoomDelete.d.ts.map +1 -0
  156. package/dist/domain/src/kinds/RoomDelete.js +20 -0
  157. package/dist/domain/src/kinds/RoomDelete.js.map +1 -0
  158. package/dist/domain/src/kinds/RoomEdit.d.ts +28 -0
  159. package/dist/domain/src/kinds/RoomEdit.d.ts.map +1 -0
  160. package/dist/domain/src/kinds/RoomEdit.js +80 -0
  161. package/dist/domain/src/kinds/RoomEdit.js.map +1 -0
  162. package/dist/domain/src/kinds/RoomJoin.d.ts +15 -0
  163. package/dist/domain/src/kinds/RoomJoin.d.ts.map +1 -0
  164. package/dist/domain/src/kinds/RoomJoin.js +33 -0
  165. package/dist/domain/src/kinds/RoomJoin.js.map +1 -0
  166. package/dist/domain/src/kinds/RoomLeave.d.ts +11 -0
  167. package/dist/domain/src/kinds/RoomLeave.d.ts.map +1 -0
  168. package/dist/domain/src/kinds/RoomLeave.js +20 -0
  169. package/dist/domain/src/kinds/RoomLeave.js.map +1 -0
  170. package/dist/domain/src/kinds/RoomList.d.ts +14 -0
  171. package/dist/domain/src/kinds/RoomList.d.ts.map +1 -0
  172. package/dist/domain/src/kinds/RoomList.js +27 -0
  173. package/dist/domain/src/kinds/RoomList.js.map +1 -0
  174. package/dist/domain/src/kinds/RoomMembers.d.ts +15 -0
  175. package/dist/domain/src/kinds/RoomMembers.d.ts.map +1 -0
  176. package/dist/domain/src/kinds/RoomMembers.js +30 -0
  177. package/dist/domain/src/kinds/RoomMembers.js.map +1 -0
  178. package/dist/domain/src/kinds/RoomMeta.d.ts +27 -0
  179. package/dist/domain/src/kinds/RoomMeta.d.ts.map +1 -0
  180. package/dist/domain/src/kinds/RoomMeta.js +72 -0
  181. package/dist/domain/src/kinds/RoomMeta.js.map +1 -0
  182. package/dist/domain/src/kinds/RoomRemoveMember.d.ts +14 -0
  183. package/dist/domain/src/kinds/RoomRemoveMember.d.ts.map +1 -0
  184. package/dist/domain/src/kinds/RoomRemoveMember.js +27 -0
  185. package/dist/domain/src/kinds/RoomRemoveMember.js.map +1 -0
  186. package/dist/domain/src/kinds/SearchRelayList.d.ts +15 -0
  187. package/dist/domain/src/kinds/SearchRelayList.d.ts.map +1 -0
  188. package/dist/domain/src/kinds/SearchRelayList.js +30 -0
  189. package/dist/domain/src/kinds/SearchRelayList.js.map +1 -0
  190. package/dist/domain/src/kinds/SlashCommand.d.ts +38 -0
  191. package/dist/domain/src/kinds/SlashCommand.d.ts.map +1 -0
  192. package/dist/domain/src/kinds/SlashCommand.js +91 -0
  193. package/dist/domain/src/kinds/SlashCommand.js.map +1 -0
  194. package/dist/domain/src/kinds/Thread.d.ts +12 -0
  195. package/dist/domain/src/kinds/Thread.d.ts.map +1 -0
  196. package/dist/domain/src/kinds/Thread.js +21 -0
  197. package/dist/domain/src/kinds/Thread.js.map +1 -0
  198. package/dist/domain/src/kinds/TimeEvent.d.ts +21 -0
  199. package/dist/domain/src/kinds/TimeEvent.d.ts.map +1 -0
  200. package/dist/domain/src/kinds/TimeEvent.js +58 -0
  201. package/dist/domain/src/kinds/TimeEvent.js.map +1 -0
  202. package/dist/domain/src/kinds/TopicList.d.ts +17 -0
  203. package/dist/domain/src/kinds/TopicList.d.ts.map +1 -0
  204. package/dist/domain/src/kinds/TopicList.js +36 -0
  205. package/dist/domain/src/kinds/TopicList.js.map +1 -0
  206. package/dist/domain/src/kinds/ZapGoal.d.ts +20 -0
  207. package/dist/domain/src/kinds/ZapGoal.d.ts.map +1 -0
  208. package/dist/domain/src/kinds/ZapGoal.js +52 -0
  209. package/dist/domain/src/kinds/ZapGoal.js.map +1 -0
  210. package/dist/domain/src/kinds/ZapReceipt.d.ts +28 -0
  211. package/dist/domain/src/kinds/ZapReceipt.d.ts.map +1 -0
  212. package/dist/domain/src/kinds/ZapReceipt.js +93 -0
  213. package/dist/domain/src/kinds/ZapReceipt.js.map +1 -0
  214. package/dist/domain/src/kinds/ZapRequest.d.ts +20 -0
  215. package/dist/domain/src/kinds/ZapRequest.d.ts.map +1 -0
  216. package/dist/domain/src/kinds/ZapRequest.js +47 -0
  217. package/dist/domain/src/kinds/ZapRequest.js.map +1 -0
  218. package/dist/feeds/src/compiler.d.ts +49 -0
  219. package/dist/feeds/src/compiler.d.ts.map +1 -0
  220. package/dist/feeds/src/compiler.js +253 -0
  221. package/dist/feeds/src/compiler.js.map +1 -0
  222. package/dist/feeds/src/controller.d.ts +32 -0
  223. package/dist/feeds/src/controller.d.ts.map +1 -0
  224. package/dist/feeds/src/controller.js +329 -0
  225. package/dist/feeds/src/controller.js.map +1 -0
  226. package/dist/feeds/src/core.d.ts +76 -0
  227. package/dist/feeds/src/core.d.ts.map +1 -0
  228. package/dist/feeds/src/core.js +28 -0
  229. package/dist/feeds/src/core.js.map +1 -0
  230. package/dist/feeds/src/display.d.ts +21 -0
  231. package/dist/feeds/src/display.d.ts.map +1 -0
  232. package/dist/feeds/src/display.js +144 -0
  233. package/dist/feeds/src/display.js.map +1 -0
  234. package/dist/feeds/src/index.d.ts +7 -0
  235. package/dist/feeds/src/index.d.ts.map +1 -0
  236. package/dist/feeds/src/index.js +7 -0
  237. package/dist/feeds/src/index.js.map +1 -0
  238. package/dist/feeds/src/request.d.ts +26 -0
  239. package/dist/feeds/src/request.d.ts.map +1 -0
  240. package/dist/feeds/src/request.js +83 -0
  241. package/dist/feeds/src/request.js.map +1 -0
  242. package/dist/feeds/src/utils.d.ts +58 -0
  243. package/dist/feeds/src/utils.d.ts.map +1 -0
  244. package/dist/feeds/src/utils.js +218 -0
  245. package/dist/feeds/src/utils.js.map +1 -0
  246. package/dist/feeds/src/validate.d.ts +24 -0
  247. package/dist/feeds/src/validate.d.ts.map +1 -0
  248. package/dist/feeds/src/validate.js +137 -0
  249. package/dist/feeds/src/validate.js.map +1 -0
  250. package/dist/lib/src/Deferred.d.ts +21 -0
  251. package/dist/lib/src/Deferred.d.ts.map +1 -0
  252. package/dist/lib/src/Deferred.js +21 -0
  253. package/dist/lib/src/Deferred.js.map +1 -0
  254. package/dist/lib/src/Emitter.d.ts +14 -0
  255. package/dist/lib/src/Emitter.d.ts.map +1 -0
  256. package/dist/lib/src/Emitter.js +18 -0
  257. package/dist/lib/src/Emitter.js.map +1 -0
  258. package/dist/lib/src/LRUCache.d.ts +45 -0
  259. package/dist/lib/src/LRUCache.d.ts.map +1 -0
  260. package/dist/lib/src/LRUCache.js +72 -0
  261. package/dist/lib/src/LRUCache.js.map +1 -0
  262. package/dist/lib/src/TaskQueue.d.ts +21 -0
  263. package/dist/lib/src/TaskQueue.d.ts.map +1 -0
  264. package/dist/lib/src/TaskQueue.js +63 -0
  265. package/dist/lib/src/TaskQueue.js.map +1 -0
  266. package/dist/lib/src/Tools.d.ts +828 -0
  267. package/dist/lib/src/Tools.d.ts.map +1 -0
  268. package/dist/lib/src/Tools.js +1402 -0
  269. package/dist/lib/src/Tools.js.map +1 -0
  270. package/dist/lib/src/index.d.ts +7 -0
  271. package/dist/lib/src/index.d.ts.map +1 -0
  272. package/dist/lib/src/index.js +7 -0
  273. package/dist/lib/src/index.js.map +1 -0
  274. package/dist/lib/src/normalize-url/index.d.ts +286 -0
  275. package/dist/lib/src/normalize-url/index.d.ts.map +1 -0
  276. package/dist/lib/src/normalize-url/index.js +252 -0
  277. package/dist/lib/src/normalize-url/index.js.map +1 -0
  278. package/dist/net/src/adapter.d.ts +47 -0
  279. package/dist/net/src/adapter.d.ts.map +1 -0
  280. package/dist/net/src/adapter.js +130 -0
  281. package/dist/net/src/adapter.js.map +1 -0
  282. package/dist/net/src/auth.d.ts +40 -0
  283. package/dist/net/src/auth.d.ts.map +1 -0
  284. package/dist/net/src/auth.js +124 -0
  285. package/dist/net/src/auth.js.map +1 -0
  286. package/dist/net/src/context.d.ts +10 -0
  287. package/dist/net/src/context.d.ts.map +1 -0
  288. package/dist/net/src/context.js +2 -0
  289. package/dist/net/src/context.js.map +1 -0
  290. package/dist/net/src/diff.d.ts +57 -0
  291. package/dist/net/src/diff.d.ts.map +1 -0
  292. package/dist/net/src/diff.js +162 -0
  293. package/dist/net/src/diff.js.map +1 -0
  294. package/dist/net/src/index.d.ts +15 -0
  295. package/dist/net/src/index.d.ts.map +1 -0
  296. package/dist/net/src/index.js +15 -0
  297. package/dist/net/src/index.js.map +1 -0
  298. package/dist/net/src/message.d.ts +64 -0
  299. package/dist/net/src/message.d.ts.map +1 -0
  300. package/dist/net/src/message.js +37 -0
  301. package/dist/net/src/message.js.map +1 -0
  302. package/dist/net/src/negentropy.d.ts +48 -0
  303. package/dist/net/src/negentropy.d.ts.map +1 -0
  304. package/dist/net/src/negentropy.js +535 -0
  305. package/dist/net/src/negentropy.js.map +1 -0
  306. package/dist/net/src/policy.d.ts +40 -0
  307. package/dist/net/src/policy.d.ts.map +1 -0
  308. package/dist/net/src/policy.js +210 -0
  309. package/dist/net/src/policy.js.map +1 -0
  310. package/dist/net/src/pool.d.ts +13 -0
  311. package/dist/net/src/pool.d.ts.map +1 -0
  312. package/dist/net/src/pool.js +44 -0
  313. package/dist/net/src/pool.js.map +1 -0
  314. package/dist/net/src/publish.d.ts +45 -0
  315. package/dist/net/src/publish.d.ts.map +1 -0
  316. package/dist/net/src/publish.js +98 -0
  317. package/dist/net/src/publish.js.map +1 -0
  318. package/dist/net/src/repository.d.ts +69 -0
  319. package/dist/net/src/repository.d.ts.map +1 -0
  320. package/dist/net/src/repository.js +304 -0
  321. package/dist/net/src/repository.js.map +1 -0
  322. package/dist/net/src/request.d.ts +58 -0
  323. package/dist/net/src/request.d.ts.map +1 -0
  324. package/dist/net/src/request.js +224 -0
  325. package/dist/net/src/request.js.map +1 -0
  326. package/dist/net/src/socket.d.ts +49 -0
  327. package/dist/net/src/socket.d.ts.map +1 -0
  328. package/dist/net/src/socket.js +126 -0
  329. package/dist/net/src/socket.js.map +1 -0
  330. package/dist/net/src/tracker.d.ts +16 -0
  331. package/dist/net/src/tracker.d.ts.map +1 -0
  332. package/dist/net/src/tracker.js +63 -0
  333. package/dist/net/src/tracker.js.map +1 -0
  334. package/dist/net/src/util.d.ts +2 -0
  335. package/dist/net/src/util.d.ts.map +1 -0
  336. package/dist/net/src/util.js +2 -0
  337. package/dist/net/src/util.js.map +1 -0
  338. package/dist/net/src/wrapManager.d.ts +37 -0
  339. package/dist/net/src/wrapManager.d.ts.map +1 -0
  340. package/dist/net/src/wrapManager.js +94 -0
  341. package/dist/net/src/wrapManager.js.map +1 -0
  342. package/dist/signer/src/index.d.ts +7 -0
  343. package/dist/signer/src/index.d.ts.map +1 -0
  344. package/dist/signer/src/index.js +7 -0
  345. package/dist/signer/src/index.js.map +1 -0
  346. package/dist/signer/src/nip59.d.ts +19 -0
  347. package/dist/signer/src/nip59.d.ts.map +1 -0
  348. package/dist/signer/src/nip59.js +70 -0
  349. package/dist/signer/src/nip59.js.map +1 -0
  350. package/dist/signer/src/signers/nip01.d.ts +20 -0
  351. package/dist/signer/src/signers/nip01.d.ts.map +1 -0
  352. package/dist/signer/src/signers/nip01.js +23 -0
  353. package/dist/signer/src/signers/nip01.js.map +1 -0
  354. package/dist/signer/src/signers/nip07.d.ts +23 -0
  355. package/dist/signer/src/signers/nip07.d.ts.map +1 -0
  356. package/dist/signer/src/signers/nip07.js +29 -0
  357. package/dist/signer/src/signers/nip07.js.map +1 -0
  358. package/dist/signer/src/signers/nip46.d.ts +127 -0
  359. package/dist/signer/src/signers/nip46.d.ts.map +1 -0
  360. package/dist/signer/src/signers/nip46.js +379 -0
  361. package/dist/signer/src/signers/nip46.js.map +1 -0
  362. package/dist/signer/src/signers/nip55.d.ts +20 -0
  363. package/dist/signer/src/signers/nip55.d.ts.map +1 -0
  364. package/dist/signer/src/signers/nip55.js +87 -0
  365. package/dist/signer/src/signers/nip55.js.map +1 -0
  366. package/dist/signer/src/util.d.ts +56 -0
  367. package/dist/signer/src/util.d.ts.map +1 -0
  368. package/dist/signer/src/util.js +65 -0
  369. package/dist/signer/src/util.js.map +1 -0
  370. package/dist/tsconfig.build.tsbuildinfo +1 -0
  371. package/dist/util/src/Address.d.ts +21 -0
  372. package/dist/util/src/Address.d.ts.map +1 -0
  373. package/dist/util/src/Address.js +43 -0
  374. package/dist/util/src/Address.js.map +1 -0
  375. package/dist/util/src/Blossom.d.ts +66 -0
  376. package/dist/util/src/Blossom.d.ts.map +1 -0
  377. package/dist/util/src/Blossom.js +124 -0
  378. package/dist/util/src/Blossom.js.map +1 -0
  379. package/dist/util/src/Events.d.ts +71 -0
  380. package/dist/util/src/Events.d.ts.map +1 -0
  381. package/dist/util/src/Events.js +91 -0
  382. package/dist/util/src/Events.js.map +1 -0
  383. package/dist/util/src/Filters.d.ts +48 -0
  384. package/dist/util/src/Filters.d.ts.map +1 -0
  385. package/dist/util/src/Filters.js +182 -0
  386. package/dist/util/src/Filters.js.map +1 -0
  387. package/dist/util/src/Handles.d.ts +15 -0
  388. package/dist/util/src/Handles.d.ts.map +1 -0
  389. package/dist/util/src/Handles.js +25 -0
  390. package/dist/util/src/Handles.js.map +1 -0
  391. package/dist/util/src/Keys.d.ts +37 -0
  392. package/dist/util/src/Keys.d.ts.map +1 -0
  393. package/dist/util/src/Keys.js +26 -0
  394. package/dist/util/src/Keys.js.map +1 -0
  395. package/dist/util/src/Kinds.d.ts +194 -0
  396. package/dist/util/src/Kinds.d.ts.map +1 -0
  397. package/dist/util/src/Kinds.js +203 -0
  398. package/dist/util/src/Kinds.js.map +1 -0
  399. package/dist/util/src/Links.d.ts +3 -0
  400. package/dist/util/src/Links.d.ts.map +1 -0
  401. package/dist/util/src/Links.js +3 -0
  402. package/dist/util/src/Links.js.map +1 -0
  403. package/dist/util/src/Nip42.d.ts +7 -0
  404. package/dist/util/src/Nip42.d.ts.map +1 -0
  405. package/dist/util/src/Nip42.js +9 -0
  406. package/dist/util/src/Nip42.js.map +1 -0
  407. package/dist/util/src/Nip86.d.ts +39 -0
  408. package/dist/util/src/Nip86.d.ts.map +1 -0
  409. package/dist/util/src/Nip86.js +49 -0
  410. package/dist/util/src/Nip86.js.map +1 -0
  411. package/dist/util/src/Nip98.d.ts +9 -0
  412. package/dist/util/src/Nip98.d.ts.map +1 -0
  413. package/dist/util/src/Nip98.js +16 -0
  414. package/dist/util/src/Nip98.js.map +1 -0
  415. package/dist/util/src/Pow.d.ts +11 -0
  416. package/dist/util/src/Pow.d.ts.map +1 -0
  417. package/dist/util/src/Pow.js +127 -0
  418. package/dist/util/src/Pow.js.map +1 -0
  419. package/dist/util/src/Pubkey.d.ts +10 -0
  420. package/dist/util/src/Pubkey.d.ts.map +1 -0
  421. package/dist/util/src/Pubkey.js +32 -0
  422. package/dist/util/src/Pubkey.js.map +1 -0
  423. package/dist/util/src/Relay.d.ts +38 -0
  424. package/dist/util/src/Relay.d.ts.map +1 -0
  425. package/dist/util/src/Relay.js +55 -0
  426. package/dist/util/src/Relay.js.map +1 -0
  427. package/dist/util/src/Tags.d.ts +37 -0
  428. package/dist/util/src/Tags.d.ts.map +1 -0
  429. package/dist/util/src/Tags.js +79 -0
  430. package/dist/util/src/Tags.js.map +1 -0
  431. package/dist/util/src/Wallet.d.ts +32 -0
  432. package/dist/util/src/Wallet.d.ts.map +1 -0
  433. package/dist/util/src/Wallet.js +9 -0
  434. package/dist/util/src/Wallet.js.map +1 -0
  435. package/dist/util/src/Zaps.d.ts +86 -0
  436. package/dist/util/src/Zaps.d.ts.map +1 -0
  437. package/dist/util/src/Zaps.js +184 -0
  438. package/dist/util/src/Zaps.js.map +1 -0
  439. package/dist/util/src/index.d.ts +18 -0
  440. package/dist/util/src/index.d.ts.map +1 -0
  441. package/dist/util/src/index.js +18 -0
  442. package/dist/util/src/index.js.map +1 -0
  443. package/package.json +37 -0
@@ -0,0 +1,1402 @@
1
+ import { bech32, utf8 } from "@scure/base";
2
+ export const maybe = (x) => x;
3
+ export const isDefined = (x, ...args) => x !== undefined;
4
+ export const isUndefined = (x, ...args) => x === undefined;
5
+ export const assertDefined = (x, ...args) => x;
6
+ // ----------------------------------------------------------------------------
7
+ // Basic functional programming utilities
8
+ // ----------------------------------------------------------------------------
9
+ /** Function that does nothing and returns undefined */
10
+ export const noop = (...args) => undefined;
11
+ /**
12
+ * Returns the input value unchanged
13
+ * @param x - Any value
14
+ * @returns The same value
15
+ */
16
+ export const identity = (x, ...args) => x;
17
+ /**
18
+ * Creates a function that always returns the same value
19
+ * @param x - Value to return
20
+ * @returns Function that returns x
21
+ */
22
+ export const always = (x, ...args) => () => x;
23
+ /**
24
+ * Returns the logical NOT of a value
25
+ * @param x - Value to negate
26
+ * @returns !x
27
+ */
28
+ export const not = (x, ...args) => !x;
29
+ /**
30
+ * Bind function
31
+ * @param f - Function to bind
32
+ * @returns bound function
33
+ */
34
+ export const bind = (f, ...args) => f.bind(null, ...args);
35
+ /**
36
+ * Runs the provided function on the given value and returns the value
37
+ * @param f - Function to call
38
+ * @returns tapped function
39
+ */
40
+ export const tap = (f) => (x) => {
41
+ f(x);
42
+ return x;
43
+ };
44
+ /**
45
+ * Deep equality comparison
46
+ * @param a - First value
47
+ * @param b - Second value
48
+ * @returns True if values are deeply equal
49
+ */
50
+ export const equals = (a, b) => {
51
+ if (a === b)
52
+ return true;
53
+ if (a instanceof Set) {
54
+ if (!(b instanceof Set) || a.size !== b.size) {
55
+ return false;
56
+ }
57
+ return Array.from(a).every(x => b.has(x));
58
+ }
59
+ if (Array.isArray(a)) {
60
+ if (!Array.isArray(b) || a.length !== b.length) {
61
+ return false;
62
+ }
63
+ for (let i = 0; i < a.length; i++) {
64
+ if (!equals(a[i], b[i])) {
65
+ return false;
66
+ }
67
+ }
68
+ return true;
69
+ }
70
+ if (isPojo(a)) {
71
+ if (!isPojo(b)) {
72
+ return false;
73
+ }
74
+ const aKeys = Object.keys(a);
75
+ const bKeys = Object.keys(b);
76
+ if (aKeys.length !== bKeys.length) {
77
+ return false;
78
+ }
79
+ for (const k of aKeys) {
80
+ if (!equals(a[k], b[k])) {
81
+ return false;
82
+ }
83
+ }
84
+ return true;
85
+ }
86
+ return false;
87
+ };
88
+ // ----------------------------------------------------------------------------
89
+ // Numbers
90
+ // ----------------------------------------------------------------------------
91
+ /** Converts string or number to number */
92
+ export const ensureNumber = (x) => parseFloat(x);
93
+ /** Converts a `number | undefined` to a number, defaulting to 0 */
94
+ export const num = (x) => x || 0;
95
+ /** Adds two numbers, handling undefined values */
96
+ export const add = (x, y) => num(x) + num(y);
97
+ /** Subtracts two numbers, handling undefined values */
98
+ export const sub = (x, y) => num(x) - num(y);
99
+ /** Multiplies two numbers, handling undefined values */
100
+ export const mul = (x, y) => num(x) * num(y);
101
+ /** Divides two numbers, handling undefined values */
102
+ export const div = (x, y) => num(x) / y;
103
+ /** Increments a number by 1, handling undefined values */
104
+ export const inc = (x) => add(x, 1);
105
+ /** Decrements a number by 1, handling undefined values */
106
+ export const dec = (x) => sub(x, 1);
107
+ /** Less than comparison, handling undefined values */
108
+ export const lt = (x, y) => num(x) < num(y);
109
+ /** Less than or equal comparison, handling undefined values */
110
+ export const lte = (x, y) => num(x) <= num(y);
111
+ /** Greater than comparison, handling undefined values */
112
+ export const gt = (x, y) => num(x) > num(y);
113
+ /** Greater than or equal comparison, handling undefined values */
114
+ export const gte = (x, y) => num(x) >= num(y);
115
+ /** Returns maximum value in array, handling undefined values */
116
+ export const max = (xs) => xs.reduce((a, b) => Math.max(num(a), num(b)), 0);
117
+ /** Returns minimum value in array, handling undefined values */
118
+ export const min = (xs) => {
119
+ const [head, ...tail] = xs.filter(x => x !== undefined);
120
+ if (tail.length === 0)
121
+ return head || 0;
122
+ return tail.reduce((a, b) => Math.min(a, b), head);
123
+ };
124
+ /** Returns sum of array values, handling undefined values */
125
+ export const sum = (xs) => xs.reduce((a, b) => add(a, b), 0);
126
+ /** Returns average of array values, handling undefined values */
127
+ export const avg = (xs) => sum(xs) / xs.length;
128
+ /**
129
+ * Checks if a number is between two values (exclusive)
130
+ * @param bounds - Lower and upper bounds
131
+ * @param n - Number to check
132
+ * @returns True if n is between low and high
133
+ */
134
+ export const between = ([low, high], n) => n > low && n < high;
135
+ /**
136
+ * Checks if a number is between two values (inclusive)
137
+ * @param bounds - Lower and upper bounds
138
+ * @param n - Number to check
139
+ * @returns True if n is between low and high
140
+ */
141
+ export const within = ([low, high], n) => n >= low && n <= high;
142
+ /**
143
+ * Constrains number between min and max values
144
+ * @param bounds - Minimum and maximum allowed values
145
+ * @param n - Number to clamp
146
+ * @returns Clamped value
147
+ */
148
+ export const clamp = ([min, max], n) => Math.min(max, Math.max(min, n));
149
+ /**
150
+ * Round a number to the nearest float precision
151
+ * @param precision - Number of decimal places
152
+ * @param x - Number to round
153
+ * @returns Formatted number
154
+ */
155
+ export const round = (precision, x) => Math.round(x * Math.pow(10, precision)) / Math.pow(10, precision);
156
+ // ----------------------------------------------------------------------------
157
+ // Timestamps
158
+ // ----------------------------------------------------------------------------
159
+ /** One minute in seconds */
160
+ export const MINUTE = 60;
161
+ /** One hour in seconds */
162
+ export const HOUR = 60 * MINUTE;
163
+ /** One day in seconds */
164
+ export const DAY = 24 * HOUR;
165
+ /** One week in seconds */
166
+ export const WEEK = 7 * DAY;
167
+ /** One month in seconds (approximate) */
168
+ export const MONTH = 30 * DAY;
169
+ /** One quarter in seconds (approximate) */
170
+ export const QUARTER = 90 * DAY;
171
+ /** One year in seconds (approximate) */
172
+ export const YEAR = 365 * DAY;
173
+ /** User's default locale */
174
+ export const LOCALE = new Intl.DateTimeFormat().resolvedOptions().locale;
175
+ /** User's default timezone */
176
+ export const TIMEZONE = (() => {
177
+ const [_, sign = "+", offset] = new Date().toString().match(/GMT([-\+]?)(\d+)/);
178
+ return sign + offset.slice(0, 2) + ":" + offset.slice(2);
179
+ })();
180
+ /**
181
+ * Multiplies time unit by count
182
+ * @param unit - Time unit in seconds
183
+ * @param count - Number of units
184
+ * @returns Total seconds
185
+ */
186
+ export const int = (unit, count = 1) => unit * count;
187
+ /** Returns current Unix timestamp in seconds */
188
+ export const now = () => Math.round(Date.now() / 1000);
189
+ /**
190
+ * Returns Unix timestamp from specified time ago
191
+ * @param unit - Time unit in seconds
192
+ * @param count - Number of units
193
+ * @returns Timestamp in seconds
194
+ */
195
+ export const ago = (unit, count = 1) => now() - int(unit, count);
196
+ /**
197
+ * Converts seconds to milliseconds
198
+ * @param seconds - Time in seconds
199
+ * @returns Time in milliseconds
200
+ */
201
+ export const ms = (seconds) => seconds * 1000;
202
+ /**
203
+ * Converts seconds to date
204
+ * @param seconds - Time in seconds
205
+ * @returns Date object
206
+ */
207
+ export const secondsToDate = (seconds) => new Date(seconds * 1000);
208
+ /**
209
+ * Converts date object to seconds
210
+ * @param date - Date object
211
+ * @returns timestamp in seconds
212
+ */
213
+ export const dateToSeconds = (date) => Math.round(date.valueOf() / 1000);
214
+ /**
215
+ * Creates a local date from a date string
216
+ * @param dateString - date string
217
+ * @param timezone - timezone string
218
+ * @returns timezone-aware Date object
219
+ */
220
+ export const createLocalDate = (dateString, timezone = TIMEZONE) => new Date(`${dateString} GMT${timezone}`);
221
+ /** Formatter for date+time */
222
+ export const dateTimeFormatter = new Intl.DateTimeFormat(LOCALE, {
223
+ dateStyle: "short",
224
+ timeStyle: "short",
225
+ });
226
+ /**
227
+ * Formats seconds as a datetime
228
+ * @param seconds - timestamp in seconds
229
+ * @returns datetime string
230
+ */
231
+ export const formatTimestamp = (seconds) => dateTimeFormatter.format(secondsToDate(seconds));
232
+ /** Formatter for date */
233
+ export const dateFormatter = new Intl.DateTimeFormat(LOCALE, {
234
+ year: "numeric",
235
+ month: "long",
236
+ day: "numeric",
237
+ });
238
+ /**
239
+ * Formats seconds as a date
240
+ * @param seconds - timestamp in seconds
241
+ * @returns date string
242
+ */
243
+ export const formatTimestampAsDate = (ts) => dateFormatter.format(secondsToDate(ts));
244
+ /** Formatter for time */
245
+ export const timeFormatter = new Intl.DateTimeFormat(LOCALE, {
246
+ timeStyle: "short",
247
+ });
248
+ /**
249
+ * Formats seconds as a time
250
+ * @param seconds - timestamp in seconds
251
+ * @returns time string
252
+ */
253
+ export const formatTimestampAsTime = (ts) => timeFormatter.format(secondsToDate(ts));
254
+ /**
255
+ * Formats seconds as a relative date (x minutes ago)
256
+ * @param seconds - timestamp in seconds
257
+ * @returns relative date string
258
+ */
259
+ export const formatTimestampRelative = (ts) => {
260
+ let unit;
261
+ let delta = now() - ts;
262
+ const absDelta = Math.abs(delta);
263
+ if (absDelta < int(MINUTE)) {
264
+ unit = "second";
265
+ }
266
+ else if (absDelta < int(HOUR)) {
267
+ unit = "minute";
268
+ delta = Math.round(delta / int(MINUTE));
269
+ }
270
+ else if (absDelta < int(DAY, 2)) {
271
+ unit = "hour";
272
+ delta = Math.round(delta / int(HOUR));
273
+ }
274
+ else {
275
+ unit = "day";
276
+ delta = Math.round(delta / int(DAY));
277
+ }
278
+ const locale = new Intl.RelativeTimeFormat().resolvedOptions().locale;
279
+ const formatter = new Intl.RelativeTimeFormat(locale, {
280
+ numeric: "auto",
281
+ });
282
+ return formatter.format(-delta, unit);
283
+ };
284
+ // ----------------------------------------------------------------------------
285
+ // Sequences
286
+ // ----------------------------------------------------------------------------
287
+ /**
288
+ * Returns the first element of an array
289
+ * @param xs - The array
290
+ * @returns First element or undefined
291
+ */
292
+ export const first = (xs, ...args) => {
293
+ for (const x of xs) {
294
+ return x;
295
+ }
296
+ };
297
+ /**
298
+ * Returns the first element of the first array in a nested array
299
+ * @param xs - Array of arrays
300
+ * @returns First element of first array or undefined
301
+ */
302
+ export const ffirst = (xs, ...args) => {
303
+ for (const chunk of xs) {
304
+ for (const x of chunk) {
305
+ return x;
306
+ }
307
+ }
308
+ };
309
+ /**
310
+ * Returns the last element of an array
311
+ * @param xs - The array
312
+ * @returns Last element or undefined
313
+ */
314
+ export const last = (xs, ...args) => {
315
+ const a = Array.from(xs);
316
+ return a[a.length - 1];
317
+ };
318
+ /**
319
+ * Returns array with first n elements removed
320
+ * @param n - Number of elements to drop
321
+ * @param xs - Input array
322
+ * @returns Array with first n elements removed
323
+ */
324
+ export const drop = (n, xs) => Array.from(xs).slice(n);
325
+ /**
326
+ * Returns first n elements of array
327
+ * @param n - Number of elements to take
328
+ * @param xs - Input array
329
+ * @returns Array of first n elements
330
+ */
331
+ export const take = (n, xs) => Array.from(xs).slice(0, n);
332
+ /**
333
+ * Concatenates multiple arrays, filtering out null/undefined
334
+ * @param xs - Arrays to concatenate
335
+ * @returns Combined array
336
+ */
337
+ export const concat = (...xs) => xs.flatMap(x => (x === undefined ? [] : x));
338
+ /**
339
+ * Appends element to array
340
+ * @param x - Element to append
341
+ * @param xs - Array to append to
342
+ * @returns New array with element appended
343
+ */
344
+ export const append = (x, xs) => concat(Array.from(xs), [x]);
345
+ /**
346
+ * Prepends element to array
347
+ * @param x - Element to prepend
348
+ * @param xs - Array to prepend to
349
+ * @returns New array with element prepended
350
+ */
351
+ export const prepend = (x, xs) => concat([x], Array.from(xs));
352
+ /**
353
+ * Creates union of two arrays
354
+ * @param a - First array
355
+ * @param b - Second array
356
+ * @returns Array containing unique elements from both arrays
357
+ */
358
+ export const union = (a, b) => uniq([...a, ...b]);
359
+ /**
360
+ * Returns elements common to both arrays
361
+ * @param a - First array
362
+ * @param b - Second array
363
+ * @returns Array of elements present in both inputs
364
+ */
365
+ export const intersection = (a, b) => {
366
+ const s = new Set(b);
367
+ return Array.from(a).filter(x => s.has(x));
368
+ };
369
+ /**
370
+ * Returns elements in first array not present in second
371
+ * @param a - Source array
372
+ * @param b - Array of elements to exclude
373
+ * @returns Array containing elements unique to first array
374
+ */
375
+ export const difference = (a, b) => {
376
+ const s = new Set(b);
377
+ return Array.from(a).filter(x => !s.has(x));
378
+ };
379
+ /**
380
+ * Removes all instances of an element from array
381
+ * @param a - Element to remove
382
+ * @param xs - Source array
383
+ * @returns New array with element removed
384
+ */
385
+ export const remove = (a, xs) => xs.filter(x => x !== a);
386
+ /**
387
+ * Removes element at index
388
+ * @param i - Index to remove
389
+ * @param xs - Source array
390
+ * @returns New array with element removed
391
+ */
392
+ export const removeAt = (i, xs) => [...xs.slice(0, i), ...xs.slice(i + 1)];
393
+ /**
394
+ * Returns elements from second array not present in first
395
+ * @param a - Array of elements to exclude
396
+ * @param b - Source array
397
+ * @returns Filtered array
398
+ */
399
+ export const without = (a, b) => b.filter(x => !a.includes(x));
400
+ /**
401
+ * Toggles presence of element in array
402
+ * @param x - Element to toggle
403
+ * @param xs - Source array
404
+ * @returns New array with element added or removed
405
+ */
406
+ export const toggle = (x, xs) => (xs.includes(x) ? remove(x, xs) : append(x, xs));
407
+ /**
408
+ * Generates sequence of numbers from a to b
409
+ * @param a - Start number (inclusive)
410
+ * @param b - End number (exclusive)
411
+ * @param step - Increment between numbers
412
+ * @yields Numbers in sequence
413
+ */
414
+ export function* range(a, b, step = 1) {
415
+ for (let i = a; i < b; i += step) {
416
+ yield i;
417
+ }
418
+ }
419
+ /**
420
+ * Yields indexed items
421
+ * @param items - A collection of items
422
+ * @yields tuples of [index, item]
423
+ */
424
+ export function* enumerate(items) {
425
+ for (let i = 0; i < items.length; i += 1) {
426
+ yield [i, items[i]];
427
+ }
428
+ }
429
+ /** Returns a function that gets property value from object */
430
+ export const pluck = (k, xs) => xs.map(x => x[k]);
431
+ /**
432
+ * Creates object from array of key-value pairs
433
+ * @param pairs - Array of [key, value] tuples
434
+ * @returns Object with keys and values from pairs
435
+ */
436
+ export const fromPairs = (pairs) => {
437
+ const r = {};
438
+ for (const [k, v] of pairs) {
439
+ if (k && v) {
440
+ r[k] = v;
441
+ }
442
+ }
443
+ return r;
444
+ };
445
+ /**
446
+ * Flattens array of arrays into single array
447
+ * @param xs - Array of arrays to flatten
448
+ * @returns Flattened array
449
+ */
450
+ export const flatten = (xs, ...args) => xs.flatMap(identity);
451
+ /**
452
+ * Splits array into two arrays based on predicate
453
+ * @param f - Function to test elements
454
+ * @param xs - Array to partition
455
+ * @returns Tuple of [matching, non-matching] arrays
456
+ */
457
+ export const partition = (f, xs) => {
458
+ const a = [];
459
+ const b = [];
460
+ for (const x of xs) {
461
+ if (f(x)) {
462
+ a.push(x);
463
+ }
464
+ else {
465
+ b.push(x);
466
+ }
467
+ }
468
+ return [a, b];
469
+ };
470
+ /** Maps any iterable */
471
+ export const map = (f, xs) => Array.from(xs).map(f);
472
+ /**
473
+ * Keeps items based on predicate
474
+ * @param f - Whether to keep an item
475
+ * @param xs - Items to filter
476
+ * @returns Filtered array
477
+ */
478
+ export const filter = (f, xs) => Array.from(xs).filter(f);
479
+ /**
480
+ * Removes items based on predicate
481
+ * @param f - Whether to remove an item from the array
482
+ * @param xs - Items to filter
483
+ * @returns Filtered array
484
+ */
485
+ export const reject = (f, xs) => Array.from(xs).filter(complement(f));
486
+ /**
487
+ * Finds a single item based on predicate
488
+ * @param f - Whether an item matches
489
+ * @param xs - Items to filter
490
+ * @returns first matching item
491
+ */
492
+ export const find = (f, xs) => {
493
+ for (const x of xs) {
494
+ if (f(x))
495
+ return x;
496
+ }
497
+ };
498
+ /**
499
+ * Finds a single item based on predicate
500
+ * @param f - Whether an item matches
501
+ * @param xs - Items to filter
502
+ * @returns whether an item matches
503
+ */
504
+ export const some = (f, xs) => {
505
+ for (const x of xs) {
506
+ if (f(x))
507
+ return true;
508
+ }
509
+ return false;
510
+ };
511
+ /**
512
+ * Returns array with duplicate elements removed
513
+ * @param xs - Array with possible duplicates
514
+ * @returns Array with unique elements
515
+ */
516
+ export const uniq = (xs) => Array.from(new Set(xs));
517
+ /**
518
+ * Returns array with elements unique by key function
519
+ * @param f - Function to generate key for each element
520
+ * @param xs - Input array
521
+ * @returns Array with elements unique by key
522
+ */
523
+ export const uniqBy = (f, xs) => {
524
+ const s = new Set();
525
+ const r = [];
526
+ for (const x of xs) {
527
+ const k = f(x);
528
+ if (s.has(k)) {
529
+ continue;
530
+ }
531
+ s.add(k);
532
+ r.push(x);
533
+ }
534
+ return r;
535
+ };
536
+ /**
537
+ * Returns sorted copy of array
538
+ * @param xs - Array to sort
539
+ * @returns New sorted array
540
+ */
541
+ export const sort = (xs) => [...xs].sort();
542
+ /**
543
+ * Returns array sorted by key function
544
+ * @param f - Function to generate sort key
545
+ * @param xs - Array to sort
546
+ * @returns Sorted array
547
+ */
548
+ export const sortBy = (f, xs) => [...xs].sort((a, b) => {
549
+ const x = f(a);
550
+ const y = f(b);
551
+ return x < y ? -1 : x > y ? 1 : 0;
552
+ });
553
+ /**
554
+ * Groups array elements by key function
555
+ * @param f - Function to generate group key
556
+ * @param xs - Array to group
557
+ * @returns Map of groups
558
+ */
559
+ export const groupBy = (f, xs) => {
560
+ const r = new Map();
561
+ for (const x of xs) {
562
+ const k = f(x);
563
+ let v = r.get(k);
564
+ if (!v) {
565
+ v = [];
566
+ r.set(k, v);
567
+ }
568
+ v.push(x);
569
+ }
570
+ return r;
571
+ };
572
+ /**
573
+ * Counts array elements by key function
574
+ * @param f - Function to generate group key
575
+ * @param xs - Array to count entries
576
+ * @returns Map of counts
577
+ */
578
+ export const countBy = (f, xs) => {
579
+ const r = new Map();
580
+ for (const [k, items] of groupBy(f, xs)) {
581
+ r.set(k, items.length);
582
+ }
583
+ return r;
584
+ };
585
+ /**
586
+ * Creates map from array using key function
587
+ * @param f - Function to generate key
588
+ * @param xs - Array to index
589
+ * @returns Map of values by key
590
+ */
591
+ export const indexBy = (f, xs) => {
592
+ const r = new Map();
593
+ for (const x of xs) {
594
+ r.set(f(x), x);
595
+ }
596
+ return r;
597
+ };
598
+ /**
599
+ * Creates array of specified length using generator function
600
+ * @param n - Length of array
601
+ * @param f - Function to generate each element
602
+ * @returns Generated array
603
+ */
604
+ export const initArray = (n, f) => {
605
+ const result = [];
606
+ for (let i = 0; i < n; i++) {
607
+ result.push(f());
608
+ }
609
+ return result;
610
+ };
611
+ /**
612
+ * Splits array into chunks of specified length
613
+ * @param chunkLength - Maximum length of each chunk
614
+ * @param xs - Array to split
615
+ * @returns Array of chunks
616
+ */
617
+ export const chunk = (chunkLength, xs) => {
618
+ const result = [];
619
+ const current = [];
620
+ for (const item of xs) {
621
+ if (current.length < chunkLength) {
622
+ current.push(item);
623
+ }
624
+ else {
625
+ result.push(current.splice(0));
626
+ current.push(item);
627
+ }
628
+ }
629
+ if (current.length > 0) {
630
+ result.push(current);
631
+ }
632
+ return result;
633
+ };
634
+ /**
635
+ * Splits array into specified number of chunks
636
+ * @param n - Number of chunks
637
+ * @param xs - Array to split
638
+ * @returns Array of n chunks
639
+ */
640
+ export const chunks = (n, xs) => {
641
+ const result = initArray(n, () => []);
642
+ for (let i = 0; i < xs.length; i++) {
643
+ result[i % n].push(xs[i]);
644
+ }
645
+ return result;
646
+ };
647
+ /** Splits array into two parts at index */
648
+ export const splitAt = (n, xs) => [xs.slice(0, n), xs.slice(n)];
649
+ /** Inserts element into array at index */
650
+ export const insertAt = (n, x, xs) => [...xs.slice(0, n), x, ...xs.slice(n)];
651
+ /** Replaces array element at index */
652
+ export const replaceAt = (n, x, xs) => [...xs.slice(0, n), x, ...xs.slice(n + 1)];
653
+ /** Returns random element from array */
654
+ export const choice = (xs) => xs[Math.floor(xs.length * Math.random())];
655
+ /** Returns shuffled copy of iterable */
656
+ export const shuffle = (xs) => Array.from(xs).sort(() => (Math.random() > 0.5 ? 1 : -1));
657
+ /** Returns n random elements from array */
658
+ export const sample = (n, xs) => shuffle(xs).slice(0, n);
659
+ /** Checks if value is iterable */
660
+ export const isIterable = (x) => Symbol.iterator in Object(x);
661
+ /** Ensures value is iterable by wrapping in array if needed */
662
+ export const toIterable = (x) => (isIterable(x) ? x : [x]);
663
+ /** Ensures value is array by wrapping if needed */
664
+ export const ensurePlural = (x) => (x instanceof Array ? x : [x]);
665
+ /** Ensures values are not undefined */
666
+ export const removeUndefined = (xs) => xs.filter(isDefined).map(assertDefined);
667
+ /** Returns a list of overlapping pairs of elements in xs */
668
+ export const overlappingPairs = (xs) => {
669
+ const result = [];
670
+ for (let i = 0; i < xs.length - 1; i++) {
671
+ result.push([xs[i], xs[i + 1]]);
672
+ }
673
+ return result;
674
+ };
675
+ // ----------------------------------------------------------------------------
676
+ // Objects
677
+ // ----------------------------------------------------------------------------
678
+ /**
679
+ * Checks if value is a plain object
680
+ * @param obj - Value to check
681
+ * @returns True if value is a plain object
682
+ */
683
+ export const isPojo = (obj) => {
684
+ if (obj === null || typeof obj !== "object") {
685
+ return false;
686
+ }
687
+ return Object.getPrototypeOf(obj) === Object.prototype;
688
+ };
689
+ /**
690
+ * Creates new object with only specified keys
691
+ * @param ks - Keys to keep
692
+ * @param x - Source object
693
+ * @returns New object with only specified keys
694
+ */
695
+ export const pick = (ks, x) => {
696
+ const r = { ...x };
697
+ for (const k of Object.keys(x)) {
698
+ if (!ks.includes(k)) {
699
+ delete r[k];
700
+ }
701
+ }
702
+ return r;
703
+ };
704
+ /**
705
+ * Creates new object with specified keys removed
706
+ * @param ks - Keys to remove
707
+ * @param x - Source object
708
+ * @returns New object without specified keys
709
+ */
710
+ export const omit = (ks, x) => {
711
+ const r = { ...x };
712
+ for (const k of ks) {
713
+ delete r[k];
714
+ }
715
+ return r;
716
+ };
717
+ /**
718
+ * Creates new object excluding entries with specified values
719
+ * @param xs - Values to exclude
720
+ * @param x - Source object
721
+ * @returns New object without entries containing specified values
722
+ */
723
+ export const omitVals = (xs, x) => {
724
+ const r = {};
725
+ for (const [k, v] of Object.entries(x)) {
726
+ if (!xs.includes(v)) {
727
+ r[k] = v;
728
+ }
729
+ }
730
+ return r;
731
+ };
732
+ /**
733
+ * Filters object values based on predicate
734
+ * @param f - Function to test values
735
+ * @param x - Object to filter
736
+ * @returns Object with only values that pass predicate
737
+ */
738
+ export const filterVals = (f, x) => {
739
+ const r = {};
740
+ for (const k in x) {
741
+ if (f(x[k])) {
742
+ r[k] = x[k];
743
+ }
744
+ }
745
+ return r;
746
+ };
747
+ /**
748
+ * Creates new object with transformed keys
749
+ * @param f - Function to transform keys
750
+ * @param x - Source object
751
+ * @returns Object with transformed keys
752
+ */
753
+ export const mapKeys = (f, x) => {
754
+ const r = {};
755
+ for (const [k, v] of Object.entries(x)) {
756
+ r[f(k)] = v;
757
+ }
758
+ return r;
759
+ };
760
+ /**
761
+ * Creates new object with transformed values
762
+ * @param f - Function to transform values
763
+ * @param x - Source object
764
+ * @returns Object with transformed values
765
+ */
766
+ export const mapVals = (f, x) => {
767
+ const r = {};
768
+ for (const [k, v] of Object.entries(x)) {
769
+ r[k] = f(v);
770
+ }
771
+ return r;
772
+ };
773
+ /**
774
+ * Merges two objects, with left object taking precedence
775
+ * @param a - Left object
776
+ * @param b - Right object
777
+ * @returns Merged object with a"s properties overriding b"s
778
+ */
779
+ export const mergeLeft = (a, b) => ({
780
+ ...b,
781
+ ...a,
782
+ });
783
+ /**
784
+ * Merges two objects, with right object taking precedence
785
+ * @param a - Left object
786
+ * @param b - Right object
787
+ * @returns Merged object with b"s properties overriding a"s
788
+ */
789
+ export const mergeRight = (a, b) => ({
790
+ ...a,
791
+ ...b,
792
+ });
793
+ /** Deep merge two objects, prioritizing the first argument. */
794
+ export const deepMergeLeft = (a, b) => deepMergeRight(b, a);
795
+ /** Deep merge two objects, prioritizing the second argument. */
796
+ export const deepMergeRight = (a, b) => {
797
+ a = { ...a };
798
+ for (const [k, v] of Object.entries(b)) {
799
+ if (isPojo(v) && isPojo(a[k])) {
800
+ a[k] = deepMergeRight(a[k], v);
801
+ }
802
+ else {
803
+ a[k] = v;
804
+ }
805
+ }
806
+ return a;
807
+ };
808
+ /**
809
+ * Switches on key in object, with default fallback
810
+ * @param k - Key to look up
811
+ * @param m - Object with values and optional default
812
+ * @returns Value at key or default value
813
+ */
814
+ export const switcher = (k, m) => m[k] === undefined ? m.default : m[k];
815
+ // ----------------------------------------------------------------------------
816
+ // Maps
817
+ // ----------------------------------------------------------------------------
818
+ export const mapPop = (k, m) => {
819
+ const v = m.get(k);
820
+ m.delete(k);
821
+ return v;
822
+ };
823
+ // ----------------------------------------------------------------------------
824
+ // Combinators
825
+ // ----------------------------------------------------------------------------
826
+ /** Returns a function that returns the boolean negation of the given function */
827
+ export const complement = (f) => (...args) => !f(...args);
828
+ /**
829
+ * Safely executes function and handles errors
830
+ * @param f - Function to execute
831
+ * @param onError - Optional error handler
832
+ * @returns Function result or undefined if error
833
+ */
834
+ export const tryCatch = (f, onError) => {
835
+ try {
836
+ const r = f();
837
+ if (r instanceof Promise) {
838
+ r.catch(e => onError?.(e));
839
+ }
840
+ return r;
841
+ }
842
+ catch (e) {
843
+ onError?.(e);
844
+ }
845
+ return undefined;
846
+ };
847
+ /**
848
+ * Throws an error with the given message
849
+ */
850
+ export const thrower = (message) => (...args) => {
851
+ throw new Error(message);
852
+ };
853
+ /**
854
+ * Creates function that only executes once
855
+ * @param f - Function to wrap
856
+ * @returns Function that executes f only on first call
857
+ */
858
+ export const once = (f) => {
859
+ let called = false;
860
+ return (...args) => {
861
+ if (!called) {
862
+ called = true;
863
+ f(...args);
864
+ }
865
+ };
866
+ };
867
+ /**
868
+ * Calls a function
869
+ * @param f - Function to call
870
+ * @returns Whatever f returns
871
+ */
872
+ export const call = (f, ...args) => f();
873
+ /**
874
+ * Memoizes function results based on arguments
875
+ * @param f - Function to memoize
876
+ * @returns Memoized function
877
+ */
878
+ export const memoize = (f) => {
879
+ let prevArgs;
880
+ let result;
881
+ return (...args) => {
882
+ if (!equals(prevArgs, args)) {
883
+ prevArgs = args;
884
+ result = f(...args);
885
+ }
886
+ return result;
887
+ };
888
+ };
889
+ /**
890
+ * Executes a function on the given value
891
+ * @param x - The value
892
+ * @param f - Function to execute
893
+ * @returns result of f(x)
894
+ */
895
+ export const doLet = (x, f) => f(x);
896
+ /**
897
+ * Executes a function if the value is defined
898
+ * @param x - The value to check
899
+ * @param f - Function to execute if x is defined
900
+ * @returns result of f(x) if x is defined, undefined otherwise
901
+ */
902
+ export const ifLet = (x, f) => x === undefined ? undefined : f(x);
903
+ // ----------------------------------------------------------------------------
904
+ // Randomness
905
+ // ----------------------------------------------------------------------------
906
+ /**
907
+ * Generates random integer between min and max (inclusive)
908
+ * @param min - Minimum value
909
+ * @param max - Maximum value
910
+ * @returns Random integer
911
+ */
912
+ export const randomInt = (min = 0, max = 9) => min + Math.round(Math.random() * (max - min));
913
+ /**
914
+ * Generates random string ID
915
+ * @returns Random string suitable for use as an ID
916
+ */
917
+ export const randomId = () => Math.random().toString().slice(2);
918
+ // ----------------------------------------------------------------------------
919
+ // Async
920
+ // ----------------------------------------------------------------------------
921
+ /**
922
+ * Creates a promise that resolves after specified time
923
+ * @param t - Time in milliseconds
924
+ * @returns Promise that resolves after t milliseconds
925
+ */
926
+ export const sleep = (t) => new Promise(resolve => setTimeout(resolve, t));
927
+ /**
928
+ * Creates a promise that resolves after the condition completes or timeout
929
+ * @param options - PollOptions
930
+ * @returns void Promise
931
+ */
932
+ export const poll = ({ interval = 300, condition, signal }) => new Promise(resolve => {
933
+ const int = setInterval(() => {
934
+ if (condition()) {
935
+ resolve();
936
+ clearInterval(int);
937
+ }
938
+ }, interval);
939
+ if (condition()) {
940
+ resolve();
941
+ clearInterval(int);
942
+ }
943
+ signal.addEventListener("abort", () => {
944
+ resolve();
945
+ clearInterval(int);
946
+ });
947
+ });
948
+ /**
949
+ * Creates a microtask that yields to other tasks in the event loop
950
+ * @returns Promise that resolves after yielding
951
+ */
952
+ export const yieldThread = () => {
953
+ if (typeof window !== "undefined" &&
954
+ "scheduler" in window &&
955
+ "yield" in window.scheduler) {
956
+ return window.scheduler.yield();
957
+ }
958
+ return new Promise(resolve => {
959
+ setTimeout(resolve, 0);
960
+ });
961
+ };
962
+ /**
963
+ * Creates throttled version of function
964
+ * @param ms - Minimum time between calls
965
+ * @param f - Function to throttle
966
+ * @returns Throttled function
967
+ */
968
+ export const throttle = (ms, f) => {
969
+ if (ms === 0) {
970
+ return f;
971
+ }
972
+ let timeoutId;
973
+ let lastArgs;
974
+ const later = () => {
975
+ if (lastArgs !== undefined) {
976
+ const args = lastArgs;
977
+ lastArgs = undefined;
978
+ f(...args);
979
+ timeoutId = setTimeout(later, ms);
980
+ }
981
+ else {
982
+ timeoutId = undefined;
983
+ }
984
+ };
985
+ return (...args) => {
986
+ lastArgs = args;
987
+ if (timeoutId === undefined) {
988
+ f(...args);
989
+ lastArgs = undefined;
990
+ timeoutId = setTimeout(later, ms);
991
+ }
992
+ };
993
+ };
994
+ /**
995
+ * Creates throttled function that returns cached value
996
+ * @param ms - Minimum time between updates
997
+ * @param f - Function to throttle
998
+ * @returns Function returning latest value
999
+ */
1000
+ export const throttleWithValue = (ms, f) => {
1001
+ let value;
1002
+ const update = throttle(ms, () => {
1003
+ value = f();
1004
+ });
1005
+ return () => {
1006
+ update();
1007
+ return value;
1008
+ };
1009
+ };
1010
+ /**
1011
+ * Creates batching function that collects items
1012
+ * this function does not delay execution, if a series of items is passed in sequence
1013
+ * the first item will be processed immediately, and the rest will be batched
1014
+ * @param t - Time window for batching
1015
+ * @param f - Function to process batch
1016
+ * @returns Function that adds items to batch
1017
+ */
1018
+ export const batch = (t, f) => {
1019
+ const xs = [];
1020
+ let timeoutId;
1021
+ const later = () => {
1022
+ if (xs.length > 0) {
1023
+ f(xs.splice(0));
1024
+ timeoutId = setTimeout(later, t);
1025
+ }
1026
+ else {
1027
+ timeoutId = undefined;
1028
+ }
1029
+ };
1030
+ return (x) => {
1031
+ xs.push(x);
1032
+ if (!timeoutId) {
1033
+ f(xs.splice(0));
1034
+ timeoutId = setTimeout(later, t);
1035
+ }
1036
+ };
1037
+ };
1038
+ /**
1039
+ * Creates batching function that returns results
1040
+ * @param t - Time window for batching
1041
+ * @param execute - Function to process batch
1042
+ * @returns Function that returns promise of result
1043
+ */
1044
+ export const batcher = (t, execute) => {
1045
+ const queue = [];
1046
+ let timeoutId;
1047
+ const _execute = async () => {
1048
+ timeoutId = undefined;
1049
+ const items = queue.splice(0);
1050
+ const results = await execute(items.map(item => item.request));
1051
+ if (results.length === items.length) {
1052
+ results.forEach((r, i) => items[i].resolve(r));
1053
+ }
1054
+ else {
1055
+ items.forEach(item => item.reject("Execute must return a result for each request"));
1056
+ }
1057
+ };
1058
+ return (request) => new Promise((resolve, reject) => {
1059
+ queue.push({ request, resolve, reject });
1060
+ if (timeoutId === undefined) {
1061
+ timeoutId = setTimeout(_execute, t);
1062
+ }
1063
+ });
1064
+ };
1065
+ /**
1066
+ * Returns a promise that resolves after some proportion of promises complete
1067
+ * @param threshold - number between 0 and 1 for how many promises to wait for
1068
+ * @param promises - array of promises
1069
+ * @returns promise
1070
+ */
1071
+ export const race = (threshold, promises) => {
1072
+ let count = 0;
1073
+ if (threshold === 0) {
1074
+ return Promise.resolve();
1075
+ }
1076
+ return new Promise((resolve, reject) => {
1077
+ promises.forEach(p => {
1078
+ p.then(() => {
1079
+ count++;
1080
+ if (count >= threshold * promises.length) {
1081
+ resolve();
1082
+ }
1083
+ }).catch(reject);
1084
+ });
1085
+ });
1086
+ };
1087
+ // ----------------------------------------------------------------------------
1088
+ // URLs
1089
+ // ----------------------------------------------------------------------------
1090
+ /**
1091
+ * Removes protocol (http://, https://, etc) from URL
1092
+ * @param url - URL to process
1093
+ * @returns URL without protocol
1094
+ */
1095
+ export const stripProtocol = (url) => url.replace(/.*:\/\//, "");
1096
+ /**
1097
+ * Formats URL for display by removing protocol, www, and trailing slash
1098
+ * @param url - URL to format
1099
+ * @returns Formatted URL
1100
+ */
1101
+ export const displayUrl = (url) => stripProtocol(url)
1102
+ .replace(/^(www\.)?/i, "")
1103
+ .replace(/\/$/, "");
1104
+ /**
1105
+ * Extracts and formats domain from URL
1106
+ * @param url - URL to process
1107
+ * @returns Formatted domain name
1108
+ */
1109
+ export const displayDomain = (url) => displayUrl(first(url.split(/[\/\?]/)) || "");
1110
+ // ----------------------------------------------------------------------------
1111
+ // JSON, localStorage, fetch, event emitters, etc
1112
+ // ----------------------------------------------------------------------------
1113
+ /**
1114
+ * Safely parses JSON string
1115
+ * @param json - JSON string to parse
1116
+ * @returns Parsed object or null if invalid
1117
+ */
1118
+ export function parseJson(json) {
1119
+ if (!json)
1120
+ return undefined;
1121
+ try {
1122
+ return JSON.parse(json);
1123
+ }
1124
+ catch (e) {
1125
+ return undefined;
1126
+ }
1127
+ }
1128
+ /**
1129
+ * Gets and parses JSON from localStorage
1130
+ * @param k - Storage key
1131
+ * @returns Parsed value or undefined if invalid/missing
1132
+ */
1133
+ export const getJson = (k) => parseJson(localStorage.getItem(k) || "");
1134
+ /**
1135
+ * Stringifies and stores value in localStorage
1136
+ * @param k - Storage key
1137
+ * @param v - Value to store
1138
+ */
1139
+ export const setJson = (k, v) => localStorage.setItem(k, JSON.stringify(v));
1140
+ /**
1141
+ * Fetches JSON from URL with options
1142
+ * @param url - URL to fetch from
1143
+ * @param opts - Fetch options
1144
+ * @returns Promise of parsed JSON response
1145
+ */
1146
+ export const fetchJson = async (url, opts = {}) => {
1147
+ // Make sure the url is valid, this will throw if not
1148
+ url = String(new URL(url));
1149
+ if (!opts.headers) {
1150
+ opts.headers = {};
1151
+ }
1152
+ if (!opts.headers["Accept"]) {
1153
+ opts.headers["Accept"] = "application/json";
1154
+ }
1155
+ const res = await fetch(url, opts);
1156
+ const json = await res.json();
1157
+ return json;
1158
+ };
1159
+ /**
1160
+ * Posts JSON data to URL
1161
+ * @param url - URL to post to
1162
+ * @param data - Data to send
1163
+ * @param opts - Additional fetch options
1164
+ * @returns Promise of parsed JSON response
1165
+ */
1166
+ export const postJson = async (url, data, opts = {}) => {
1167
+ if (!opts.method) {
1168
+ opts.method = "POST";
1169
+ }
1170
+ if (!opts.headers) {
1171
+ opts.headers = {};
1172
+ }
1173
+ if (!opts.headers["Content-Type"]) {
1174
+ opts.headers["Content-Type"] = "application/json";
1175
+ }
1176
+ opts.body = JSON.stringify(data);
1177
+ return fetchJson(url, opts);
1178
+ };
1179
+ /**
1180
+ * Uploads file to URL
1181
+ * @param url - Upload URL
1182
+ * @param file - File to upload
1183
+ * @returns Promise of parsed JSON response
1184
+ */
1185
+ export const uploadFile = (url, file) => {
1186
+ const body = new FormData();
1187
+ body.append("file", file);
1188
+ return fetchJson(url, { method: "POST", body });
1189
+ };
1190
+ /**
1191
+ * A generic type-safe event listener function that works with event emitters.
1192
+ *
1193
+ * @param target - The event target object with add/remove listener methods
1194
+ * @param eventName - The name of the event to listen for
1195
+ * @param callback - The callback function to execute when the event occurs
1196
+ * @returns A function that removes the event listener when called
1197
+ */
1198
+ export const on = (target, eventName, callback) => {
1199
+ target.on(eventName, callback);
1200
+ return () => {
1201
+ target.off(eventName, callback);
1202
+ };
1203
+ };
1204
+ // ----------------------------------------------------------------------------
1205
+ // Strings
1206
+ // ----------------------------------------------------------------------------
1207
+ /**
1208
+ * Truncates string to length, breaking at word boundaries
1209
+ * @param s - String to truncate
1210
+ * @param l - Maximum length
1211
+ * @param suffix - String to append if truncated
1212
+ * @returns Truncated string
1213
+ */
1214
+ export const ellipsize = (s, l, suffix = "...") => {
1215
+ if (s.length < l * 1.1) {
1216
+ return s;
1217
+ }
1218
+ while (s.length > l && s.includes(" ")) {
1219
+ s = s.split(" ").slice(0, -1).join(" ");
1220
+ }
1221
+ return s + suffix;
1222
+ };
1223
+ /** Displays a list of items with oxford commas and a chosen conjunction */
1224
+ export const displayList = (xs, conj = "and", n = 6) => {
1225
+ if (xs.length > n + 2) {
1226
+ return `${xs.slice(0, n).join(", ")}, ${conj} ${xs.length - n} others`;
1227
+ }
1228
+ if (xs.length < 3) {
1229
+ return xs.join(` ${conj} `);
1230
+ }
1231
+ return `${xs.slice(0, -1).join(", ")}, ${conj} ${xs.slice(-1).join("")}`;
1232
+ };
1233
+ /** Generates a hash from input string */
1234
+ export const hash = (s) => Math.abs(s.split("").reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0));
1235
+ // ----------------------------------------------------------------------------
1236
+ // Curried utilities for working with collections
1237
+ // ----------------------------------------------------------------------------
1238
+ /** Returns a function that gets the nth element of an array */
1239
+ export const nth = (i) => (xs, ...args) => xs[i];
1240
+ /** Returns a function that checks if nth element equals value */
1241
+ export const nthEq = (i, v) => (xs, ...args) => xs[i] === v;
1242
+ /** Returns a function that checks if nth element is in value */
1243
+ export const nthIn = (i, v) => (xs, ...args) => v.includes(xs[i]);
1244
+ /** Returns a function that checks if nth element does not equal value */
1245
+ export const nthNe = (i, v) => (xs, ...args) => xs[i] !== v;
1246
+ /** Returns a function that checks if nth element is not in value */
1247
+ export const nthNotIn = (i, v) => (xs, ...args) => !v.includes(xs[i]);
1248
+ /** Returns a function that checks if key/value pairs of x match all pairs in spec */
1249
+ export const spec = (values) => (x, ...args) => {
1250
+ if (Array.isArray(values)) {
1251
+ for (let i = 0; i < values.length; i++) {
1252
+ if (x[i] !== values[i]) {
1253
+ return false;
1254
+ }
1255
+ }
1256
+ }
1257
+ else {
1258
+ for (const [k, v] of Object.entries(values)) {
1259
+ if (x[k] !== v)
1260
+ return false;
1261
+ }
1262
+ }
1263
+ return true;
1264
+ };
1265
+ /** Returns a function that checks equality with value */
1266
+ export const eq = (v) => (x, ...args) => x === v;
1267
+ /** Returns a function that checks inequality with value */
1268
+ export const ne = (v) => (x, ...args) => x !== v;
1269
+ /** Returns a function that gets property value from object */
1270
+ export const prop = (k) => (x) => x[k];
1271
+ /** Returns a function that checks whether the object's property value is in a list */
1272
+ export const propIn = (k, xs) => (x) => xs.includes(x[k]);
1273
+ /** Returns a function that adds/updates a property on object */
1274
+ export const assoc = (k, v) => (o) => ({ ...o, [k]: v });
1275
+ /** Returns a function that removes a property on object */
1276
+ export const dissoc = (k) => (o) => omit([k], o);
1277
+ /** Returns a function that checks whether a value is in the given sequence */
1278
+ export const member = (xs) => (x) => Array.from(xs).includes(x);
1279
+ /** Returns a function that checks whether all predicates pass */
1280
+ export const allPass = (...predicates) => (x) => predicates.every(predicate => predicate(x));
1281
+ /** Returns a function that checks whether some predicate passes */
1282
+ export const somePass = (...predicates) => (x) => predicates.some(predicate => predicate(x));
1283
+ // ----------------------------------------------------------------------------
1284
+ // Sets
1285
+ // ----------------------------------------------------------------------------
1286
+ /**
1287
+ * Adds value to Set at key in object
1288
+ * @param m - Object mapping keys to Sets
1289
+ * @param k - Key to add to
1290
+ * @param v - Value to add
1291
+ */
1292
+ export const addToKey = (m, k, v) => {
1293
+ const s = m[k];
1294
+ if (s) {
1295
+ s.add(v);
1296
+ }
1297
+ else {
1298
+ m[k] = new Set([v]);
1299
+ }
1300
+ };
1301
+ /**
1302
+ * Pushes value to array at key in object
1303
+ * @param m - Object mapping keys to arrays
1304
+ * @param k - Key to push to
1305
+ * @param v - Value to push
1306
+ */
1307
+ export const pushToKey = (m, k, v) => {
1308
+ const a = m[k];
1309
+ if (a) {
1310
+ a.push(v);
1311
+ }
1312
+ else {
1313
+ m[k] = [v];
1314
+ }
1315
+ };
1316
+ // ----------------------------------------------------------------------------
1317
+ // Maps
1318
+ // ----------------------------------------------------------------------------
1319
+ /**
1320
+ * Adds value to Set at key in Map
1321
+ * @param m - Map of Sets
1322
+ * @param k - Key to add to
1323
+ * @param v - Value to add
1324
+ */
1325
+ export const addToMapKey = (m, k, v) => {
1326
+ const s = m.get(k);
1327
+ if (s) {
1328
+ s.add(v);
1329
+ }
1330
+ else {
1331
+ m.set(k, new Set([v]));
1332
+ }
1333
+ };
1334
+ /**
1335
+ * Pushes value to array at key in Map
1336
+ * @param m - Map of arrays
1337
+ * @param k - Key to push to
1338
+ * @param v - Value to push
1339
+ */
1340
+ export const pushToMapKey = (m, k, v) => {
1341
+ const a = m.get(k);
1342
+ if (a) {
1343
+ a.push(v);
1344
+ }
1345
+ else {
1346
+ m.set(k, [v]);
1347
+ }
1348
+ };
1349
+ // ----------------------------------------------------------------------------
1350
+ // Bech32 <-> hex encoding
1351
+ // ----------------------------------------------------------------------------
1352
+ /**
1353
+ * Converts hex string to bech32 format
1354
+ * @param prefix - Bech32 prefix
1355
+ * @param hex - Hex string to convert
1356
+ * @returns Bech32 encoded string
1357
+ */
1358
+ export const hexToBech32 = (prefix, hex) => bech32.encode(prefix, bech32.toWords(utf8.decode(hex)), false);
1359
+ /**
1360
+ * Converts bech32 string to hex format
1361
+ * @param b32 - Bech32 string to convert
1362
+ * @returns Hex encoded string
1363
+ */
1364
+ export const bech32ToHex = (b32) => utf8.encode(bech32.fromWords(bech32.decode(b32, false).words));
1365
+ // ----------------------------------------------------------------------------
1366
+ // Bytes <-> hex encoding
1367
+ // ----------------------------------------------------------------------------
1368
+ /**
1369
+ * Converts an array buffer or Uint8Array to hex format
1370
+ * @param buffer - ArrayBuffer or Uint8Array to convert
1371
+ * @returns Hex encoded string
1372
+ */
1373
+ export const bytesToHex = (buffer) => {
1374
+ if (buffer instanceof ArrayBuffer) {
1375
+ buffer = new Uint8Array(buffer);
1376
+ }
1377
+ return Array.from(buffer)
1378
+ .map(b => b.toString(16).padStart(2, "0"))
1379
+ .join("");
1380
+ };
1381
+ /**
1382
+ * Converts a hex string to an array buffer
1383
+ * @param hex - hex string
1384
+ * @returns ArrayBuffer
1385
+ */
1386
+ export const hexToBytes = (hex) => new Uint8Array(hex.match(/.{2}/g).map(hex => parseInt(hex, 16)));
1387
+ // ----------------------------------------------------------------------------
1388
+ // Binary data
1389
+ // ----------------------------------------------------------------------------
1390
+ export const textEncoder = new TextEncoder();
1391
+ export const textDecoder = new TextDecoder();
1392
+ /**
1393
+ * Computes SHA-256 hash of binary data
1394
+ * @param data - Binary data to hash
1395
+ * @returns Promise resolving to hex-encoded hash string
1396
+ */
1397
+ export const sha256 = async (data) => {
1398
+ const hashBuffer = await crypto.subtle.digest("SHA-256", data);
1399
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
1400
+ return hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
1401
+ };
1402
+ //# sourceMappingURL=Tools.js.map