@naturalcycles/js-lib 14.276.0 → 15.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (353) hide show
  1. package/cfg/frontend/tsconfig.json +3 -3
  2. package/dist/abort.js +1 -4
  3. package/dist/array/array.util.d.ts +1 -1
  4. package/dist/array/array.util.js +47 -88
  5. package/dist/array/range.d.ts +2 -2
  6. package/dist/array/range.js +7 -12
  7. package/dist/bot.js +6 -10
  8. package/dist/browser/adminService.d.ts +1 -1
  9. package/dist/browser/adminService.js +10 -14
  10. package/dist/browser/analytics.util.js +10 -15
  11. package/dist/browser/i18n/fetchTranslationLoader.d.ts +3 -3
  12. package/dist/browser/i18n/fetchTranslationLoader.js +1 -5
  13. package/dist/browser/i18n/translation.service.d.ts +1 -1
  14. package/dist/browser/i18n/translation.service.js +5 -10
  15. package/dist/browser/imageFitter.js +1 -5
  16. package/dist/browser/script.util.js +8 -12
  17. package/dist/browser/topbar.js +7 -10
  18. package/dist/datetime/dateInterval.d.ts +2 -2
  19. package/dist/datetime/dateInterval.js +7 -11
  20. package/dist/datetime/localDate.d.ts +3 -3
  21. package/dist/datetime/localDate.js +40 -44
  22. package/dist/datetime/localTime.d.ts +3 -3
  23. package/dist/datetime/localTime.js +36 -40
  24. package/dist/datetime/timeInterval.d.ts +2 -2
  25. package/dist/datetime/timeInterval.js +6 -10
  26. package/dist/datetime/wallTime.d.ts +3 -3
  27. package/dist/datetime/wallTime.js +5 -9
  28. package/dist/decorators/asyncMemo.decorator.d.ts +3 -3
  29. package/dist/decorators/asyncMemo.decorator.js +12 -17
  30. package/dist/decorators/createPromiseDecorator.js +3 -6
  31. package/dist/decorators/debounce.d.ts +1 -1
  32. package/dist/decorators/debounce.decorator.d.ts +1 -1
  33. package/dist/decorators/debounce.decorator.js +5 -9
  34. package/dist/decorators/debounce.js +2 -6
  35. package/dist/decorators/decorator.util.d.ts +1 -1
  36. package/dist/decorators/decorator.util.js +3 -8
  37. package/dist/decorators/logMethod.decorator.d.ts +1 -1
  38. package/dist/decorators/logMethod.decorator.js +11 -14
  39. package/dist/decorators/memo.decorator.d.ts +3 -3
  40. package/dist/decorators/memo.decorator.js +11 -16
  41. package/dist/decorators/memo.util.d.ts +2 -2
  42. package/dist/decorators/memo.util.js +11 -17
  43. package/dist/decorators/memoFn.d.ts +3 -3
  44. package/dist/decorators/memoFn.js +3 -6
  45. package/dist/decorators/memoFnAsync.d.ts +3 -3
  46. package/dist/decorators/memoFnAsync.js +5 -8
  47. package/dist/decorators/memoSimple.decorator.d.ts +1 -1
  48. package/dist/decorators/memoSimple.decorator.js +6 -10
  49. package/dist/decorators/retry.decorator.d.ts +1 -1
  50. package/dist/decorators/retry.decorator.js +3 -6
  51. package/dist/decorators/swarmSafe.decorator.js +3 -7
  52. package/dist/decorators/timeout.decorator.d.ts +1 -1
  53. package/dist/decorators/timeout.decorator.js +7 -10
  54. package/dist/define.d.ts +1 -1
  55. package/dist/define.js +11 -19
  56. package/dist/deviceIdService.js +7 -11
  57. package/dist/enum.util.d.ts +1 -1
  58. package/dist/enum.util.js +20 -42
  59. package/dist/env/buildInfo.d.ts +1 -1
  60. package/dist/env/buildInfo.js +3 -6
  61. package/dist/env.js +2 -6
  62. package/dist/error/assert.d.ts +3 -3
  63. package/dist/error/assert.js +30 -44
  64. package/dist/error/error.model.d.ts +2 -2
  65. package/dist/error/error.model.js +1 -2
  66. package/dist/error/error.util.d.ts +1 -1
  67. package/dist/error/error.util.js +25 -44
  68. package/dist/error/errorMode.js +2 -5
  69. package/dist/error/try.d.ts +2 -2
  70. package/dist/error/try.js +15 -23
  71. package/dist/error/tryCatch.d.ts +2 -2
  72. package/dist/error/tryCatch.js +6 -11
  73. package/dist/form.util.d.ts +1 -1
  74. package/dist/form.util.js +2 -6
  75. package/dist/http/fetcher.d.ts +3 -3
  76. package/dist/http/fetcher.js +48 -53
  77. package/dist/http/fetcher.model.d.ts +5 -5
  78. package/dist/http/fetcher.model.js +1 -2
  79. package/dist/http/http.model.js +1 -4
  80. package/dist/index.d.ts +96 -96
  81. package/dist/index.js +96 -99
  82. package/dist/is.util.d.ts +2 -2
  83. package/dist/is.util.js +12 -27
  84. package/dist/iter/asyncIterable2.d.ts +3 -3
  85. package/dist/iter/asyncIterable2.js +8 -12
  86. package/dist/iter/iterable2.d.ts +2 -2
  87. package/dist/iter/iterable2.js +8 -12
  88. package/dist/json-schema/from-data/generateJsonSchemaFromData.d.ts +1 -1
  89. package/dist/json-schema/from-data/generateJsonSchemaFromData.js +4 -7
  90. package/dist/json-schema/jsonSchema.cnst.d.ts +1 -1
  91. package/dist/json-schema/jsonSchema.cnst.js +1 -4
  92. package/dist/json-schema/jsonSchema.model.d.ts +1 -1
  93. package/dist/json-schema/jsonSchema.model.js +1 -2
  94. package/dist/json-schema/jsonSchema.util.d.ts +2 -2
  95. package/dist/json-schema/jsonSchema.util.js +5 -8
  96. package/dist/json-schema/jsonSchemaBuilder.d.ts +2 -2
  97. package/dist/json-schema/jsonSchemaBuilder.js +16 -25
  98. package/dist/json-schema/jsonSchemas.d.ts +2 -2
  99. package/dist/json-schema/jsonSchemas.js +5 -8
  100. package/dist/log/commonLogger.js +14 -21
  101. package/dist/math/accumulatingAverage.js +1 -5
  102. package/dist/math/math.util.js +11 -19
  103. package/dist/math/sma.js +1 -5
  104. package/dist/math/stack.util.js +11 -16
  105. package/dist/nanoid.js +2 -6
  106. package/dist/number/createDeterministicRandom.js +1 -4
  107. package/dist/number/number.util.d.ts +1 -1
  108. package/dist/number/number.util.js +9 -20
  109. package/dist/object/deepEquals.js +3 -8
  110. package/dist/object/map2.js +1 -5
  111. package/dist/object/object.util.d.ts +2 -2
  112. package/dist/object/object.util.js +40 -70
  113. package/dist/object/set2.js +1 -5
  114. package/dist/object/sortObject.d.ts +1 -1
  115. package/dist/object/sortObject.js +1 -4
  116. package/dist/object/sortObjectDeep.js +1 -4
  117. package/dist/polyfill.js +1 -4
  118. package/dist/promise/abortable.d.ts +1 -1
  119. package/dist/promise/abortable.js +2 -7
  120. package/dist/promise/pDefer.js +1 -4
  121. package/dist/promise/pDelay.d.ts +2 -2
  122. package/dist/promise/pDelay.js +4 -8
  123. package/dist/promise/pFilter.d.ts +1 -1
  124. package/dist/promise/pFilter.js +1 -4
  125. package/dist/promise/pHang.js +1 -4
  126. package/dist/promise/pMap.d.ts +2 -2
  127. package/dist/promise/pMap.js +17 -20
  128. package/dist/promise/pProps.js +1 -4
  129. package/dist/promise/pQueue.d.ts +2 -2
  130. package/dist/promise/pQueue.js +7 -11
  131. package/dist/promise/pRetry.d.ts +1 -1
  132. package/dist/promise/pRetry.js +8 -12
  133. package/dist/promise/pState.js +1 -4
  134. package/dist/promise/pTimeout.d.ts +3 -3
  135. package/dist/promise/pTimeout.js +7 -11
  136. package/dist/semver.d.ts +1 -1
  137. package/dist/semver.js +13 -18
  138. package/dist/string/case.js +9 -14
  139. package/dist/string/escape.js +2 -6
  140. package/dist/string/hash.util.d.ts +1 -1
  141. package/dist/string/hash.util.js +4 -10
  142. package/dist/string/json.util.d.ts +1 -1
  143. package/dist/string/json.util.js +5 -10
  144. package/dist/string/leven.js +1 -4
  145. package/dist/string/lodash/unicodeWords.js +1 -4
  146. package/dist/string/lodash/words.js +3 -6
  147. package/dist/string/pupa.d.ts +1 -1
  148. package/dist/string/pupa.js +4 -9
  149. package/dist/string/readingTime.d.ts +1 -1
  150. package/dist/string/readingTime.js +1 -4
  151. package/dist/string/regex.js +1 -4
  152. package/dist/string/safeJsonStringify.d.ts +1 -1
  153. package/dist/string/safeJsonStringify.js +1 -4
  154. package/dist/string/slugify.js +1 -4
  155. package/dist/string/string.util.js +15 -32
  156. package/dist/string/stringify.d.ts +1 -1
  157. package/dist/string/stringify.js +10 -14
  158. package/dist/string/url.util.d.ts +1 -1
  159. package/dist/string/url.util.js +2 -6
  160. package/dist/time/time.util.d.ts +1 -1
  161. package/dist/time/time.util.js +3 -8
  162. package/dist/typeFest.js +1 -2
  163. package/dist/types.d.ts +7 -3
  164. package/dist/types.js +21 -23
  165. package/dist/unit/size.util.js +5 -12
  166. package/dist/web.d.ts +1 -1
  167. package/dist/web.js +1 -5
  168. package/dist/zod/index.d.ts +2 -2
  169. package/dist/zod/index.js +4 -10
  170. package/dist/zod/zod.shared.schemas.js +36 -39
  171. package/dist/zod/zod.util.js +7 -14
  172. package/package.json +7 -8
  173. package/src/array/array.util.ts +3 -3
  174. package/src/array/range.ts +2 -2
  175. package/src/bot.ts +1 -1
  176. package/src/browser/adminService.ts +4 -4
  177. package/src/browser/analytics.util.ts +1 -1
  178. package/src/browser/i18n/fetchTranslationLoader.ts +3 -3
  179. package/src/browser/i18n/translation.service.ts +2 -2
  180. package/src/browser/script.util.ts +2 -2
  181. package/src/datetime/dateInterval.ts +3 -3
  182. package/src/datetime/localDate.ts +5 -5
  183. package/src/datetime/localTime.ts +6 -6
  184. package/src/datetime/timeInterval.ts +3 -3
  185. package/src/datetime/wallTime.ts +4 -4
  186. package/src/decorators/asyncMemo.decorator.ts +7 -7
  187. package/src/decorators/createPromiseDecorator.ts +1 -1
  188. package/src/decorators/debounce.decorator.ts +2 -2
  189. package/src/decorators/debounce.ts +1 -1
  190. package/src/decorators/decorator.util.ts +1 -1
  191. package/src/decorators/logMethod.decorator.ts +4 -4
  192. package/src/decorators/memo.decorator.ts +7 -7
  193. package/src/decorators/memo.util.ts +4 -4
  194. package/src/decorators/memoFn.ts +4 -4
  195. package/src/decorators/memoFnAsync.ts +5 -5
  196. package/src/decorators/memoSimple.decorator.ts +4 -4
  197. package/src/decorators/retry.decorator.ts +2 -2
  198. package/src/decorators/swarmSafe.decorator.ts +2 -2
  199. package/src/decorators/timeout.decorator.ts +4 -4
  200. package/src/define.ts +3 -3
  201. package/src/deviceIdService.ts +3 -3
  202. package/src/enum.util.ts +1 -1
  203. package/src/env/buildInfo.ts +2 -2
  204. package/src/error/assert.ts +7 -7
  205. package/src/error/error.model.ts +2 -2
  206. package/src/error/error.util.ts +5 -5
  207. package/src/error/try.ts +5 -5
  208. package/src/error/tryCatch.ts +3 -3
  209. package/src/form.util.ts +1 -1
  210. package/src/http/fetcher.model.ts +5 -5
  211. package/src/http/fetcher.ts +20 -15
  212. package/src/index.ts +96 -96
  213. package/src/is.util.ts +2 -2
  214. package/src/iter/asyncIterable2.ts +3 -3
  215. package/src/iter/iterable2.ts +2 -2
  216. package/src/json-schema/from-data/generateJsonSchemaFromData.ts +2 -2
  217. package/src/json-schema/jsonSchema.cnst.ts +1 -1
  218. package/src/json-schema/jsonSchema.model.ts +1 -1
  219. package/src/json-schema/jsonSchema.util.ts +4 -4
  220. package/src/json-schema/jsonSchemaBuilder.ts +5 -5
  221. package/src/json-schema/jsonSchemas.ts +2 -2
  222. package/src/math/math.util.ts +2 -2
  223. package/src/math/stack.util.ts +1 -1
  224. package/src/number/number.util.ts +1 -1
  225. package/src/object/object.util.ts +3 -3
  226. package/src/object/sortObject.ts +1 -1
  227. package/src/promise/abortable.ts +1 -1
  228. package/src/promise/pDelay.ts +3 -3
  229. package/src/promise/pFilter.ts +1 -1
  230. package/src/promise/pMap.ts +2 -2
  231. package/src/promise/pQueue.ts +4 -4
  232. package/src/promise/pRetry.ts +2 -2
  233. package/src/promise/pTimeout.ts +4 -4
  234. package/src/semver.ts +3 -3
  235. package/src/string/case.ts +2 -2
  236. package/src/string/hash.util.ts +1 -1
  237. package/src/string/json.util.ts +2 -2
  238. package/src/string/lodash/words.ts +1 -1
  239. package/src/string/pupa.ts +2 -2
  240. package/src/string/readingTime.ts +1 -1
  241. package/src/string/safeJsonStringify.ts +1 -1
  242. package/src/string/stringify.ts +4 -4
  243. package/src/string/url.util.ts +1 -1
  244. package/src/time/time.util.ts +1 -1
  245. package/src/types.ts +15 -3
  246. package/src/web.ts +1 -1
  247. package/src/zod/index.ts +2 -2
  248. package/src/zod/zod.util.ts +1 -1
  249. package/dist-esm/abort.js +0 -12
  250. package/dist-esm/array/array.util.js +0 -458
  251. package/dist-esm/array/range.js +0 -34
  252. package/dist-esm/bot.js +0 -130
  253. package/dist-esm/browser/adminService.js +0 -94
  254. package/dist-esm/browser/analytics.util.js +0 -54
  255. package/dist-esm/browser/i18n/fetchTranslationLoader.js +0 -13
  256. package/dist-esm/browser/i18n/translation.service.js +0 -56
  257. package/dist-esm/browser/imageFitter.js +0 -65
  258. package/dist-esm/browser/script.util.js +0 -46
  259. package/dist-esm/browser/topbar.js +0 -135
  260. package/dist-esm/datetime/dateInterval.js +0 -80
  261. package/dist-esm/datetime/localDate.js +0 -719
  262. package/dist-esm/datetime/localTime.js +0 -996
  263. package/dist-esm/datetime/timeInterval.js +0 -88
  264. package/dist-esm/datetime/wallTime.js +0 -70
  265. package/dist-esm/decorators/asyncMemo.decorator.js +0 -111
  266. package/dist-esm/decorators/createPromiseDecorator.js +0 -82
  267. package/dist-esm/decorators/debounce.decorator.js +0 -17
  268. package/dist-esm/decorators/debounce.js +0 -114
  269. package/dist-esm/decorators/decorator.util.js +0 -31
  270. package/dist-esm/decorators/logMethod.decorator.js +0 -85
  271. package/dist-esm/decorators/memo.decorator.js +0 -80
  272. package/dist-esm/decorators/memo.util.js +0 -97
  273. package/dist-esm/decorators/memoFn.js +0 -29
  274. package/dist-esm/decorators/memoFnAsync.js +0 -35
  275. package/dist-esm/decorators/memoSimple.decorator.js +0 -55
  276. package/dist-esm/decorators/retry.decorator.js +0 -9
  277. package/dist-esm/decorators/swarmSafe.decorator.js +0 -38
  278. package/dist-esm/decorators/timeout.decorator.js +0 -19
  279. package/dist-esm/define.js +0 -109
  280. package/dist-esm/deviceIdService.js +0 -105
  281. package/dist-esm/enum.util.js +0 -157
  282. package/dist-esm/env/buildInfo.js +0 -19
  283. package/dist-esm/env.js +0 -19
  284. package/dist-esm/error/assert.js +0 -122
  285. package/dist-esm/error/error.model.js +0 -1
  286. package/dist-esm/error/error.util.js +0 -337
  287. package/dist-esm/error/errorMode.js +0 -20
  288. package/dist-esm/error/try.js +0 -105
  289. package/dist-esm/error/tryCatch.js +0 -41
  290. package/dist-esm/form.util.js +0 -16
  291. package/dist-esm/http/fetcher.js +0 -704
  292. package/dist-esm/http/fetcher.model.js +0 -3
  293. package/dist-esm/http/http.model.js +0 -1
  294. package/dist-esm/index.js +0 -96
  295. package/dist-esm/is.util.js +0 -70
  296. package/dist-esm/iter/asyncIterable2.js +0 -103
  297. package/dist-esm/iter/iterable2.js +0 -87
  298. package/dist-esm/json-schema/from-data/generateJsonSchemaFromData.js +0 -86
  299. package/dist-esm/json-schema/jsonSchema.cnst.js +0 -38
  300. package/dist-esm/json-schema/jsonSchema.model.js +0 -1
  301. package/dist-esm/json-schema/jsonSchema.util.js +0 -27
  302. package/dist-esm/json-schema/jsonSchemaBuilder.js +0 -352
  303. package/dist-esm/json-schema/jsonSchemas.js +0 -6
  304. package/dist-esm/log/commonLogger.js +0 -78
  305. package/dist-esm/math/accumulatingAverage.js +0 -32
  306. package/dist-esm/math/math.util.js +0 -85
  307. package/dist-esm/math/sma.js +0 -43
  308. package/dist-esm/math/stack.util.js +0 -82
  309. package/dist-esm/nanoid.js +0 -57
  310. package/dist-esm/number/createDeterministicRandom.js +0 -18
  311. package/dist-esm/number/number.util.js +0 -109
  312. package/dist-esm/object/deepEquals.js +0 -170
  313. package/dist-esm/object/map2.js +0 -21
  314. package/dist-esm/object/object.util.js +0 -435
  315. package/dist-esm/object/set2.js +0 -15
  316. package/dist-esm/object/sortObject.js +0 -21
  317. package/dist-esm/object/sortObjectDeep.js +0 -17
  318. package/dist-esm/polyfill.js +0 -6
  319. package/dist-esm/promise/abortable.js +0 -31
  320. package/dist-esm/promise/pDefer.js +0 -17
  321. package/dist-esm/promise/pDelay.js +0 -37
  322. package/dist-esm/promise/pFilter.js +0 -5
  323. package/dist-esm/promise/pHang.js +0 -6
  324. package/dist-esm/promise/pMap.js +0 -174
  325. package/dist-esm/promise/pProps.js +0 -20
  326. package/dist-esm/promise/pQueue.js +0 -101
  327. package/dist-esm/promise/pRetry.js +0 -64
  328. package/dist-esm/promise/pState.js +0 -14
  329. package/dist-esm/promise/pTimeout.js +0 -64
  330. package/dist-esm/semver.js +0 -149
  331. package/dist-esm/string/case.js +0 -24
  332. package/dist-esm/string/escape.js +0 -49
  333. package/dist-esm/string/hash.util.js +0 -67
  334. package/dist-esm/string/json.util.js +0 -46
  335. package/dist-esm/string/leven.js +0 -77
  336. package/dist-esm/string/lodash/unicodeWords.js +0 -68
  337. package/dist-esm/string/lodash/words.js +0 -31
  338. package/dist-esm/string/pupa.js +0 -54
  339. package/dist-esm/string/readingTime.js +0 -102
  340. package/dist-esm/string/regex.js +0 -6
  341. package/dist-esm/string/safeJsonStringify.js +0 -39
  342. package/dist-esm/string/slugify.js +0 -66
  343. package/dist-esm/string/string.util.js +0 -99
  344. package/dist-esm/string/stringify.js +0 -133
  345. package/dist-esm/string/url.util.js +0 -42
  346. package/dist-esm/time/time.util.js +0 -61
  347. package/dist-esm/typeFest.js +0 -2
  348. package/dist-esm/types.js +0 -59
  349. package/dist-esm/unit/size.util.js +0 -43
  350. package/dist-esm/web.js +0 -40
  351. package/dist-esm/zod/index.js +0 -4
  352. package/dist-esm/zod/zod.shared.schemas.js +0 -92
  353. package/dist-esm/zod/zod.util.js +0 -52
@@ -1,352 +0,0 @@
1
- import { _uniq } from '../array/array.util';
2
- import { _deepCopy, _sortObject, mergeJsonSchemaObjects } from '../index';
3
- import { JSON_SCHEMA_ORDER } from './jsonSchema.cnst';
4
- /**
5
- * Fluent (chainable) API to manually create Json Schemas.
6
- * Inspired by Joi
7
- */
8
- export const jsonSchema = {
9
- any() {
10
- return new JsonSchemaAnyBuilder({});
11
- },
12
- const(value) {
13
- return new JsonSchemaAnyBuilder({
14
- const: value,
15
- });
16
- },
17
- null() {
18
- return new JsonSchemaAnyBuilder({
19
- type: 'null',
20
- });
21
- },
22
- ref($ref) {
23
- return new JsonSchemaAnyBuilder({
24
- $ref,
25
- });
26
- },
27
- enum(enumValues) {
28
- return new JsonSchemaAnyBuilder({ enum: enumValues });
29
- },
30
- boolean() {
31
- return new JsonSchemaAnyBuilder({
32
- type: 'boolean',
33
- });
34
- },
35
- buffer() {
36
- return new JsonSchemaAnyBuilder({
37
- instanceof: 'Buffer',
38
- });
39
- },
40
- // number types
41
- number() {
42
- return new JsonSchemaNumberBuilder();
43
- },
44
- integer() {
45
- return new JsonSchemaNumberBuilder().integer();
46
- },
47
- unixTimestamp() {
48
- return new JsonSchemaNumberBuilder().unixTimestamp();
49
- },
50
- unixTimestamp2000() {
51
- return new JsonSchemaNumberBuilder().unixTimestamp2000();
52
- },
53
- // string types
54
- string() {
55
- return new JsonSchemaStringBuilder();
56
- },
57
- dateString() {
58
- return new JsonSchemaStringBuilder().date();
59
- },
60
- // email: () => new JsonSchemaStringBuilder().email(),
61
- // complex types
62
- object(props) {
63
- return new JsonSchemaObjectBuilder().addProperties(props);
64
- },
65
- rootObject(props) {
66
- return new JsonSchemaObjectBuilder().addProperties(props).$schemaDraft7();
67
- },
68
- array(itemSchema) {
69
- return new JsonSchemaArrayBuilder(itemSchema);
70
- },
71
- tuple(items) {
72
- return new JsonSchemaTupleBuilder(items);
73
- },
74
- oneOf(items) {
75
- return new JsonSchemaAnyBuilder({
76
- oneOf: items.map(b => b.build()),
77
- });
78
- },
79
- allOf(items) {
80
- return new JsonSchemaAnyBuilder({
81
- allOf: items.map(b => b.build()),
82
- });
83
- },
84
- };
85
- export class JsonSchemaAnyBuilder {
86
- constructor(schema) {
87
- this.schema = schema;
88
- }
89
- /**
90
- * Used in ObjectBuilder to access schema.optionalProperty
91
- */
92
- getSchema() {
93
- return this.schema;
94
- }
95
- $schema($schema) {
96
- Object.assign(this.schema, { $schema });
97
- return this;
98
- }
99
- $schemaDraft7() {
100
- this.$schema('http://json-schema.org/draft-07/schema#');
101
- return this;
102
- }
103
- $id($id) {
104
- Object.assign(this.schema, { $id });
105
- return this;
106
- }
107
- title(title) {
108
- Object.assign(this.schema, { title });
109
- return this;
110
- }
111
- description(description) {
112
- Object.assign(this.schema, { description });
113
- return this;
114
- }
115
- deprecated(deprecated = true) {
116
- Object.assign(this.schema, { deprecated });
117
- return this;
118
- }
119
- type(type) {
120
- Object.assign(this.schema, { type });
121
- return this;
122
- }
123
- default(v) {
124
- Object.assign(this.schema, { default: v });
125
- return this;
126
- }
127
- oneOf(schemas) {
128
- Object.assign(this.schema, { oneOf: schemas });
129
- return this;
130
- }
131
- allOf(schemas) {
132
- Object.assign(this.schema, { allOf: schemas });
133
- return this;
134
- }
135
- instanceof(of) {
136
- this.schema.instanceof = of;
137
- return this;
138
- }
139
- optional(optional = true) {
140
- if (optional) {
141
- this.schema.optionalField = true;
142
- }
143
- else {
144
- this.schema.optionalField = undefined;
145
- }
146
- return this;
147
- }
148
- /**
149
- * Produces a "clean schema object" without methods.
150
- * Same as if it would be JSON.stringified.
151
- */
152
- build() {
153
- return _sortObject(JSON.parse(JSON.stringify(this.schema)), JSON_SCHEMA_ORDER);
154
- }
155
- clone() {
156
- return new JsonSchemaAnyBuilder(_deepCopy(this.schema));
157
- }
158
- }
159
- export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder {
160
- constructor() {
161
- super({
162
- type: 'number',
163
- });
164
- this.int32 = () => this.format('int32');
165
- this.int64 = () => this.format('int64');
166
- this.float = () => this.format('float');
167
- this.double = () => this.format('double');
168
- this.unixTimestamp = () => this.format('unixTimestamp');
169
- this.unixTimestamp2000 = () => this.format('unixTimestamp2000');
170
- this.unixTimestampMillis = () => this.format('unixTimestampMillis');
171
- this.unixTimestampMillis2000 = () => this.format('unixTimestampMillis2000');
172
- this.utcOffset = () => this.format('utcOffset');
173
- this.utcOffsetHours = () => this.format('utcOffsetHours');
174
- }
175
- integer() {
176
- Object.assign(this.schema, { type: 'integer' });
177
- return this;
178
- }
179
- multipleOf(multipleOf) {
180
- Object.assign(this.schema, { multipleOf });
181
- return this;
182
- }
183
- min(minimum) {
184
- Object.assign(this.schema, { minimum });
185
- return this;
186
- }
187
- exclusiveMin(exclusiveMinimum) {
188
- Object.assign(this.schema, { exclusiveMinimum });
189
- return this;
190
- }
191
- max(maximum) {
192
- Object.assign(this.schema, { maximum });
193
- return this;
194
- }
195
- exclusiveMax(exclusiveMaximum) {
196
- Object.assign(this.schema, { exclusiveMaximum });
197
- return this;
198
- }
199
- /**
200
- * Both ranges are inclusive.
201
- */
202
- range(minimum, maximum) {
203
- Object.assign(this.schema, { minimum, maximum });
204
- return this;
205
- }
206
- format(format) {
207
- Object.assign(this.schema, { format });
208
- return this;
209
- }
210
- }
211
- export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
212
- constructor() {
213
- super({
214
- type: 'string',
215
- });
216
- this.email = () => this.format('email');
217
- this.date = () => this.format('date');
218
- this.url = () => this.format('url');
219
- this.ipv4 = () => this.format('ipv4');
220
- this.ipv6 = () => this.format('ipv6');
221
- this.password = () => this.format('password');
222
- this.id = () => this.format('id');
223
- this.slug = () => this.format('slug');
224
- this.semVer = () => this.format('semVer');
225
- this.languageTag = () => this.format('languageTag');
226
- this.countryCode = () => this.format('countryCode');
227
- this.currency = () => this.format('currency');
228
- this.trim = (trim = true) => this.transformModify('trim', trim);
229
- this.toLowerCase = (toLowerCase = true) => this.transformModify('toLowerCase', toLowerCase);
230
- this.toUpperCase = (toUpperCase = true) => this.transformModify('toUpperCase', toUpperCase);
231
- }
232
- pattern(pattern) {
233
- Object.assign(this.schema, { pattern });
234
- return this;
235
- }
236
- min(minLength) {
237
- Object.assign(this.schema, { minLength });
238
- return this;
239
- }
240
- max(maxLength) {
241
- Object.assign(this.schema, { maxLength });
242
- return this;
243
- }
244
- length(minLength, maxLength) {
245
- Object.assign(this.schema, { minLength, maxLength });
246
- return this;
247
- }
248
- format(format) {
249
- Object.assign(this.schema, { format });
250
- return this;
251
- }
252
- transformModify(t, add) {
253
- if (add) {
254
- this.schema.transform = _uniq([...(this.schema.transform || []), t]);
255
- }
256
- else {
257
- this.schema.transform = this.schema.transform?.filter(s => s !== t);
258
- }
259
- return this;
260
- }
261
- }
262
- export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
263
- constructor() {
264
- super({
265
- type: 'object',
266
- properties: {},
267
- required: [],
268
- additionalProperties: false,
269
- });
270
- }
271
- addProperties(props) {
272
- Object.entries(props).forEach(([k, builder]) => {
273
- const schema = builder.build();
274
- if (!schema.optionalField) {
275
- this.schema.required.push(k);
276
- }
277
- else {
278
- schema.optionalField = undefined;
279
- }
280
- this.schema.properties[k] = schema;
281
- });
282
- this.required(this.schema.required); // ensure it's sorted and _uniq
283
- return this;
284
- }
285
- /**
286
- * Ensures `required` is always sorted and _uniq
287
- */
288
- required(required) {
289
- Object.assign(this.schema, { required });
290
- this.schema.required = _uniq(required).sort();
291
- return this;
292
- }
293
- addRequired(required) {
294
- return this.required([...this.schema.required, ...required]);
295
- }
296
- minProps(minProperties) {
297
- Object.assign(this.schema, { minProperties });
298
- return this;
299
- }
300
- maxProps(maxProperties) {
301
- Object.assign(this.schema, { maxProperties });
302
- return this;
303
- }
304
- additionalProps(additionalProperties) {
305
- Object.assign(this.schema, { additionalProperties });
306
- return this;
307
- }
308
- baseDBEntity() {
309
- Object.assign(this.schema.properties, {
310
- id: { type: 'string' },
311
- created: { type: 'number', format: 'unixTimestamp2000' },
312
- updated: { type: 'number', format: 'unixTimestamp2000' },
313
- });
314
- return this.addRequired(['id', 'created', 'updated']);
315
- }
316
- extend(s2) {
317
- const builder = new JsonSchemaObjectBuilder();
318
- Object.assign(builder.schema, _deepCopy(this.schema));
319
- mergeJsonSchemaObjects(builder.schema, s2.schema);
320
- return builder;
321
- }
322
- }
323
- export class JsonSchemaArrayBuilder extends JsonSchemaAnyBuilder {
324
- constructor(itemsSchema) {
325
- super({
326
- type: 'array',
327
- items: itemsSchema.build(),
328
- });
329
- }
330
- min(minItems) {
331
- Object.assign(this.schema, { minItems });
332
- return this;
333
- }
334
- max(maxItems) {
335
- Object.assign(this.schema, { maxItems });
336
- return this;
337
- }
338
- unique(uniqueItems) {
339
- Object.assign(this.schema, { uniqueItems });
340
- return this;
341
- }
342
- }
343
- export class JsonSchemaTupleBuilder extends JsonSchemaAnyBuilder {
344
- constructor(items) {
345
- super({
346
- type: 'array',
347
- items: items.map(b => b.build()),
348
- minItems: items.length,
349
- maxItems: items.length,
350
- });
351
- }
352
- }
@@ -1,6 +0,0 @@
1
- import { jsonSchema } from './jsonSchemaBuilder';
2
- export const baseDBEntityJsonSchema = jsonSchema.object({
3
- id: jsonSchema.string(),
4
- created: jsonSchema.unixTimestamp2000(),
5
- updated: jsonSchema.unixTimestamp2000(),
6
- });
@@ -1,78 +0,0 @@
1
- // copy-pasted to avoid weird circular dependency
2
- const _noop = (..._args) => undefined;
3
- export const commonLogLevelNumber = {
4
- log: 10,
5
- warn: 20,
6
- error: 30,
7
- };
8
- /**
9
- * SimpleLogger that does nothing (noop).
10
- *
11
- * @experimental
12
- */
13
- export const commonLoggerNoop = {
14
- log: _noop,
15
- warn: _noop,
16
- error: _noop,
17
- };
18
- /**
19
- * Creates a "child" logger that is "limited" to the specified CommonLogLevel.
20
- */
21
- export function commonLoggerMinLevel(logger, minLevel, mutate = false) {
22
- const level = commonLogLevelNumber[minLevel];
23
- if (mutate) {
24
- if (level > commonLogLevelNumber['log']) {
25
- logger.log = _noop;
26
- if (level > commonLogLevelNumber['warn']) {
27
- logger.warn = _noop;
28
- if (level > commonLogLevelNumber['error']) {
29
- logger.error = _noop;
30
- }
31
- }
32
- }
33
- return logger;
34
- }
35
- if (level <= commonLogLevelNumber['log']) {
36
- // All levels are kept
37
- return logger;
38
- }
39
- if (level > commonLogLevelNumber['error']) {
40
- // "Log nothing" logger
41
- return commonLoggerNoop;
42
- }
43
- return {
44
- log: _noop, // otherwise it is "log everything" logger (same logger as input)
45
- warn: level <= commonLogLevelNumber['warn'] ? logger.warn.bind(logger) : _noop,
46
- error: logger.error.bind(logger), // otherwise it's "log nothing" logger (same as noopLogger)
47
- };
48
- }
49
- /**
50
- * Creates a "proxy" CommonLogger that pipes log messages to all provided sub-loggers.
51
- */
52
- export function commonLoggerPipe(loggers) {
53
- return {
54
- log: (...args) => loggers.forEach(logger => logger.log(...args)),
55
- warn: (...args) => loggers.forEach(logger => logger.warn(...args)),
56
- error: (...args) => loggers.forEach(logger => logger.error(...args)),
57
- };
58
- }
59
- /**
60
- * Creates a "child" CommonLogger with prefix (one or multiple).
61
- */
62
- export function commonLoggerPrefix(logger, ...prefixes) {
63
- return {
64
- log: (...args) => logger.log(...prefixes, ...args),
65
- warn: (...args) => logger.warn(...prefixes, ...args),
66
- error: (...args) => logger.error(...prefixes, ...args),
67
- };
68
- }
69
- /**
70
- * Creates a CommonLogger from a single function that takes `level` and `args`.
71
- */
72
- export function commonLoggerCreate(fn) {
73
- return {
74
- log: (...args) => fn('log', args),
75
- warn: (...args) => fn('warn', args),
76
- error: (...args) => fn('error', args),
77
- };
78
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * Container that allows to accumulate average of a set of numbers,
3
- * without the need to store all of them in memory.
4
- *
5
- * @experimental
6
- */
7
- export class AccumulatingAverage {
8
- constructor() {
9
- this.total = 0;
10
- this.count = 0;
11
- }
12
- /**
13
- * Returns the current average.
14
- * Returns 0 if no values have been added.
15
- */
16
- get average() {
17
- if (this.count === 0)
18
- return 0;
19
- return this.total / this.count;
20
- }
21
- /**
22
- * Adds a new value.
23
- */
24
- add(value) {
25
- this.total += value;
26
- this.count++;
27
- }
28
- reset() {
29
- this.total = 0;
30
- this.count = 0;
31
- }
32
- }
@@ -1,85 +0,0 @@
1
- import { _assert } from '../error/assert';
2
- import { _sortNumbers } from '../number/number.util';
3
- /**
4
- * @returns Average of the array of numbers
5
- *
6
- * @example
7
- *
8
- * _average([1, 2, 3, 4])
9
- * // 2.5
10
- */
11
- export function _average(values) {
12
- _assert(values.length, '_average is called on empty array');
13
- let total = 0;
14
- for (const n of values)
15
- total += n;
16
- return total / values.length;
17
- }
18
- /**
19
- * Same as _average, but safely returns null if input array is empty or nullish.
20
- */
21
- export function _averageOrNull(values) {
22
- return values?.length ? _average(values) : null;
23
- }
24
- /**
25
- * valuesArray and weightsArray length is expected to be the same.
26
- */
27
- export function _averageWeighted(values, weights) {
28
- let numerator = 0;
29
- let denominator = 0;
30
- // eslint-disable-next-line unicorn/no-for-loop
31
- for (let i = 0; i < values.length; i++) {
32
- numerator += values[i] * weights[i];
33
- denominator += weights[i];
34
- }
35
- return numerator / denominator;
36
- }
37
- /**
38
- * @example
39
- *
40
- * _percentile([1, 2, 3, 4], 50)
41
- * // 2.5
42
- *
43
- * _percentile([1, 2, 3], 50)
44
- * // 2
45
- *
46
- * _percentile([1, 2, 3], 100)
47
- * // 3
48
- */
49
- export function _percentile(values, pc) {
50
- const sorted = _sortNumbers(values);
51
- // Floating pos in the range of [0; length - 1]
52
- const pos = ((values.length - 1) * pc) / 100;
53
- const dec = pos % 1;
54
- const floorPos = Math.floor(pos);
55
- const ceilPos = Math.ceil(pos);
56
- return _averageWeighted([sorted[floorPos], sorted[ceilPos]], [1 - dec, dec]);
57
- }
58
- /**
59
- * A tiny bit more efficient function than calling _percentile individually.
60
- */
61
- export function _percentiles(values, pcs) {
62
- const r = {};
63
- const sorted = _sortNumbers(values);
64
- for (const pc of pcs) {
65
- // Floating pos in the range of [0; length - 1]
66
- const pos = ((values.length - 1) * pc) / 100;
67
- const dec = pos % 1;
68
- const floorPos = Math.floor(pos);
69
- const ceilPos = Math.ceil(pos);
70
- r[pc] = _averageWeighted([sorted[floorPos], sorted[ceilPos]], [1 - dec, dec]);
71
- }
72
- return r;
73
- }
74
- /**
75
- * @example
76
- *
77
- * _median([1, 2, 3])
78
- * // 2
79
- *
80
- * _median([1, 2, 3, 4])
81
- * // 2.5
82
- */
83
- export function _median(values) {
84
- return _percentile(values, 50);
85
- }
@@ -1,43 +0,0 @@
1
- /**
2
- * Implements a Simple Moving Average algorithm.
3
- */
4
- export class SimpleMovingAverage {
5
- constructor(size, data = []) {
6
- this.size = size;
7
- this.data = data;
8
- /**
9
- * Next index of array to push to
10
- */
11
- this.nextIndex = 0;
12
- }
13
- /**
14
- * Current average (calculated on the fly).
15
- * Returns 0 (not undefined) for empty data.
16
- */
17
- get avg() {
18
- if (this.data.length === 0)
19
- return 0;
20
- let total = 0;
21
- for (const n of this.data)
22
- total += n;
23
- return total / this.data.length;
24
- }
25
- /**
26
- * Push new value.
27
- * Returns newly calculated average (using newly pushed value).
28
- */
29
- pushGetAvg(n) {
30
- this.push(n);
31
- return this.avg;
32
- }
33
- /**
34
- * Push new value.
35
- */
36
- push(n) {
37
- this.data[this.nextIndex] = n;
38
- this.nextIndex =
39
- this.nextIndex === this.size - 1
40
- ? 0 // reset
41
- : this.nextIndex + 1;
42
- }
43
- }
@@ -1,82 +0,0 @@
1
- import { _average, _percentile, _percentiles, _range } from '../index';
2
- /**
3
- * Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
4
- * Like an array of a fixed size. When it runs out of space - it starts writing on top of itself
5
- * from index 0.
6
- *
7
- *
8
- */
9
- export class Stack {
10
- constructor(size) {
11
- this.size = size;
12
- /**
13
- * Index of a slot to get written TO next.
14
- * Currently this slot contains OLDEST item (if any).
15
- */
16
- this.nextIndex = 0;
17
- this.items = [];
18
- }
19
- push(item) {
20
- this.items[this.nextIndex] = item;
21
- this.nextIndex = this.nextIndex === this.size - 1 ? 0 : this.nextIndex + 1;
22
- return this;
23
- }
24
- /**
25
- * Fill (overwrite) the whole Stack (all its items) with the passed `item`.
26
- */
27
- fill(item) {
28
- _range(this.size).forEach(i => (this.items[i] = item));
29
- return this;
30
- }
31
- /**
32
- * Returns last items in the right order.
33
- * Unlike raw `items` property that returns "items buffer" as-is (not ordered properly).
34
- */
35
- get itemsOrdered() {
36
- if (this.items.length < this.size) {
37
- // Buffer is not filled yet, just return it as-is
38
- return this.items;
39
- }
40
- // Buffer was filled and started to "overwrite itself", will need to return 2 slices
41
- return [...this.items.slice(this.nextIndex), ...this.items.slice(0, this.nextIndex)];
42
- }
43
- }
44
- /**
45
- * Fixed-size FILO stack of Numbers.
46
- * Has convenience stat methods, e.g percentile, avg, etc.
47
- */
48
- export class NumberStack extends Stack {
49
- avg() {
50
- // _assert(this.items.length, 'NumberStack.avg cannot be called on empty stack')
51
- return _average(this.items);
52
- }
53
- /**
54
- * Returns null if Stack is empty.
55
- */
56
- avgOrNull() {
57
- return this.items.length === 0 ? null : _average(this.items);
58
- }
59
- median() {
60
- return _percentile(this.items, 50);
61
- }
62
- medianOrNull() {
63
- return this.items.length === 0 ? null : _percentile(this.items, 50);
64
- }
65
- /**
66
- * `pc` is a number from 0 to 100 inclusive.
67
- */
68
- percentile(pc) {
69
- // _assert(this.items.length, 'NumberStack.percentile cannot be called on empty stack')
70
- return _percentile(this.items, pc);
71
- }
72
- /**
73
- * `pc` is a number from 0 to 100 inclusive.
74
- * Returns null if Stack is empty.
75
- */
76
- percentileOrNull(pc) {
77
- return this.items.length === 0 ? null : _percentile(this.items, pc);
78
- }
79
- percentiles(pcs) {
80
- return _percentiles(this.items, pcs);
81
- }
82
- }