@likec4/core 1.40.0 → 1.42.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 (257) hide show
  1. package/builder/package.json +4 -0
  2. package/compute-view/package.json +4 -0
  3. package/compute-view/relationships/package.json +4 -0
  4. package/dist/builder/index.d.mts +83 -20
  5. package/dist/builder/index.mjs +210 -126
  6. package/dist/compute-view/index.d.mts +8 -7
  7. package/dist/compute-view/index.mjs +14 -11
  8. package/dist/compute-view/relationships-view/index.d.mts +9 -8
  9. package/dist/compute-view/relationships-view/index.mjs +6 -6
  10. package/dist/index.d.mts +7 -7
  11. package/dist/index.mjs +12 -12
  12. package/dist/model/connection/deployment/index.d.mts +11 -10
  13. package/dist/model/connection/deployment/index.mjs +2 -2
  14. package/dist/model/connection/index.d.mts +12 -11
  15. package/dist/model/connection/index.mjs +3 -3
  16. package/dist/model/connection/model/index.d.mts +11 -10
  17. package/dist/model/connection/model/index.mjs +2 -2
  18. package/dist/model/index.d.mts +14 -14
  19. package/dist/model/index.mjs +4 -5
  20. package/dist/shared/{core.DHQ32OHa.mjs → core.01VTr9qu.mjs} +10 -29
  21. package/dist/shared/core.1vX5a9AY.mjs +86 -0
  22. package/dist/shared/{core.BVNaR17u.mjs → core.B7KhhkRY.mjs} +1 -1
  23. package/dist/shared/core.B85MJLTf.mjs +5 -0
  24. package/dist/shared/{core.BIAlzwKc.mjs → core.BIqHjQiH.mjs} +85 -45
  25. package/dist/shared/core.BKlholso.mjs +3 -0
  26. package/dist/shared/core.BR4zXDH-.mjs +13 -0
  27. package/dist/shared/{core.yHe79zEl.mjs → core.BYNkXQ8A.mjs} +21 -12
  28. package/dist/shared/core.BazD6cot.mjs +14 -0
  29. package/dist/shared/{core.BuoXfSMj.mjs → core.BbE4y-yl.mjs} +1 -1
  30. package/dist/shared/core.BoYfZOBm.mjs +3 -0
  31. package/dist/shared/core.BsSXS-_R.mjs +7 -0
  32. package/dist/shared/{core.T-eLZTW_.mjs → core.BzTakU3I.mjs} +15 -7
  33. package/dist/shared/{core.4u1dVmfX.mjs → core.C2TiyjI6.mjs} +2 -2
  34. package/dist/shared/{core.DLpidoWc.mjs → core.C3JowhaZ.mjs} +3 -3
  35. package/dist/shared/{core.CNGyqkJh.mjs → core.C4x8aaWa.mjs} +3 -1
  36. package/dist/shared/{core.BB6zA-UU.mjs → core.C8YY6Uub.mjs} +38 -9
  37. package/dist/shared/{core.FPITT8Vx.mjs → core.C8sl5Y8Z.mjs} +144 -61
  38. package/dist/shared/{core.DuR3R-xJ.mjs → core.CNgZvKb8.mjs} +3 -5176
  39. package/dist/shared/{core.BbThmf6M.d.mts → core.CQngdcuj.d.mts} +85 -4
  40. package/dist/shared/{core.Be1n9a0i.mjs → core.C_Ni6yHl.mjs} +5 -4
  41. package/dist/shared/core.CbT8JVYL.mjs +5 -0
  42. package/dist/shared/core.CqAJJpK_.mjs +3 -0
  43. package/dist/shared/{core.CySf-2gP.d.mts → core.D0PjZzIS.d.mts} +52 -15
  44. package/dist/shared/{core.DrvfVXkh.d.mts → core.D5fzJWbF.d.mts} +11 -2
  45. package/dist/shared/{core.DrVOJuxX.d.mts → core.DACpKp35.d.mts} +5 -5
  46. package/dist/shared/core.DFi2dOTl.mjs +39548 -0
  47. package/dist/shared/{core.D704WZES.d.mts → core.DIol3syK.d.mts} +2 -2
  48. package/dist/shared/{core.DlG1xWt2.mjs → core.DMMOWQqc.mjs} +3 -2
  49. package/dist/shared/core.DNbUPnve.mjs +3990 -0
  50. package/dist/shared/{core.CCD1ID81.mjs → core.DRJma0Ol.mjs} +4 -3
  51. package/dist/shared/{core.DgttqsJc.mjs → core.DRhg-gVU.mjs} +9 -9
  52. package/dist/shared/core.DSO_rTC2.mjs +5322 -0
  53. package/dist/shared/{core.vWJTxpLV.d.mts → core.D_vkffV4.d.mts} +4 -2
  54. package/dist/shared/core.DdMJk7nx.d.mts +476 -0
  55. package/dist/shared/{core.D_fI5Lf6.d.mts → core.Dgs6-nuz.d.mts} +24 -4
  56. package/dist/shared/{core.Be4Jy9Qx.mjs → core.FZ305VYd.mjs} +2 -2
  57. package/dist/shared/core.OHD4KXm9.mjs +5 -0
  58. package/dist/shared/{core.GTgsmXS_.mjs → core.QJD_fcrc.mjs} +2 -2
  59. package/dist/shared/core.RfnaEmgA.mjs +7 -0
  60. package/dist/shared/core.fHR_8Tnq.d.mts +450 -0
  61. package/dist/shared/{core.D0EgVMQK.mjs → core.lRvxPoao.mjs} +14 -12
  62. package/dist/shared/{core.CaT0SUUK.mjs → core.rCeaAqgW.mjs} +1 -84
  63. package/dist/shared/{core.kxeJe0OY.d.mts → core.s9uwOt60.d.mts} +4 -4
  64. package/dist/shared/core.yna0OdV5.d.mts +98 -0
  65. package/dist/styles/index.d.mts +106 -0
  66. package/dist/styles/index.mjs +1 -0
  67. package/dist/types/_aux.d.mts +2 -2
  68. package/dist/types/_aux.mjs +1 -5
  69. package/dist/types/expression-model.d.mts +3 -4
  70. package/dist/types/expression.d.mts +3 -4
  71. package/dist/types/fqnRef.d.mts +2 -2
  72. package/dist/types/fqnRef.mjs +3 -3
  73. package/dist/types/index.d.mts +8 -8
  74. package/dist/types/index.mjs +151 -6
  75. package/dist/types/scalar.mjs +3 -2
  76. package/dist/utils/graphology/index.d.mts +1864 -0
  77. package/dist/utils/graphology/index.mjs +1 -0
  78. package/dist/utils/index.d.mts +11 -6
  79. package/dist/utils/index.mjs +21 -37
  80. package/dist/utils/iterable/index.d.mts +1 -1
  81. package/dist/utils/iterable/index.mjs +45 -7
  82. package/model/package.json +4 -0
  83. package/package.json +42 -24
  84. package/styles/package.json +4 -0
  85. package/types/package.json +4 -0
  86. package/utils/graphology/package.json +4 -0
  87. package/utils/package.json +4 -0
  88. package/dist/shared/core.6_Vp2iiN.mjs +0 -172
  89. package/dist/shared/core.Bpou6c2f.d.mts +0 -850
  90. package/dist/shared/core.CUw_2J9j.mjs +0 -47
  91. package/dist/shared/core.CzdwsPmY.d.mts +0 -69
  92. package/dist/shared/core.DoM6aM9N.mjs +0 -7
  93. package/dist/shared/core.ILwny7BV.mjs +0 -38
  94. package/dist/theme/index.d.mts +0 -11
  95. package/dist/theme/index.mjs +0 -232
  96. package/src/builder/Builder.deployment.ts +0 -224
  97. package/src/builder/Builder.deploymentModel.ts +0 -449
  98. package/src/builder/Builder.element.ts +0 -241
  99. package/src/builder/Builder.model.ts +0 -494
  100. package/src/builder/Builder.ts +0 -870
  101. package/src/builder/Builder.view-common.ts +0 -273
  102. package/src/builder/Builder.view-deployment.ts +0 -135
  103. package/src/builder/Builder.view-element.ts +0 -202
  104. package/src/builder/Builder.views.ts +0 -399
  105. package/src/builder/Builder.with.ts +0 -156
  106. package/src/builder/_types.ts +0 -320
  107. package/src/builder/index.ts +0 -11
  108. package/src/compute-view/compute-view.ts +0 -80
  109. package/src/compute-view/deployment-view/_types.ts +0 -38
  110. package/src/compute-view/deployment-view/clean-connections.ts +0 -236
  111. package/src/compute-view/deployment-view/compute.ts +0 -94
  112. package/src/compute-view/deployment-view/memory/index.ts +0 -9
  113. package/src/compute-view/deployment-view/memory/memory.ts +0 -96
  114. package/src/compute-view/deployment-view/predicates/deploymentRefs-where.ts +0 -18
  115. package/src/compute-view/deployment-view/predicates/deploymentRefs.ts +0 -154
  116. package/src/compute-view/deployment-view/predicates/index.ts +0 -8
  117. package/src/compute-view/deployment-view/predicates/relation-direct.ts +0 -243
  118. package/src/compute-view/deployment-view/predicates/relation-in-out.ts +0 -70
  119. package/src/compute-view/deployment-view/predicates/relation-incoming.ts +0 -101
  120. package/src/compute-view/deployment-view/predicates/relation-outgoing.ts +0 -95
  121. package/src/compute-view/deployment-view/predicates/relation-where.ts +0 -18
  122. package/src/compute-view/deployment-view/predicates/utils.ts +0 -282
  123. package/src/compute-view/deployment-view/predicates/wildcard.ts +0 -41
  124. package/src/compute-view/deployment-view/stages/stage-exclude.ts +0 -5
  125. package/src/compute-view/deployment-view/stages/stage-final.ts +0 -245
  126. package/src/compute-view/deployment-view/stages/stage-include.ts +0 -95
  127. package/src/compute-view/deployment-view/utils.ts +0 -286
  128. package/src/compute-view/dynamic-view/compute.ts +0 -312
  129. package/src/compute-view/element-view/_types.ts +0 -47
  130. package/src/compute-view/element-view/clean-connections.ts +0 -117
  131. package/src/compute-view/element-view/compute.ts +0 -324
  132. package/src/compute-view/element-view/memory/NodeGroup.ts +0 -31
  133. package/src/compute-view/element-view/memory/index.ts +0 -10
  134. package/src/compute-view/element-view/memory/memory.ts +0 -178
  135. package/src/compute-view/element-view/memory/stage-exclude.ts +0 -59
  136. package/src/compute-view/element-view/memory/stage-final.ts +0 -129
  137. package/src/compute-view/element-view/memory/stage-include.ts +0 -130
  138. package/src/compute-view/element-view/predicates/_utils.ts +0 -91
  139. package/src/compute-view/element-view/predicates/element-expand.ts +0 -29
  140. package/src/compute-view/element-view/predicates/element-kind-tag.ts +0 -27
  141. package/src/compute-view/element-view/predicates/element-ref.ts +0 -24
  142. package/src/compute-view/element-view/predicates/relation-direct.ts +0 -321
  143. package/src/compute-view/element-view/predicates/relation-in-out.ts +0 -64
  144. package/src/compute-view/element-view/predicates/relation-in.ts +0 -115
  145. package/src/compute-view/element-view/predicates/relation-out.ts +0 -119
  146. package/src/compute-view/element-view/predicates/wildcard.ts +0 -88
  147. package/src/compute-view/element-view/utils.ts +0 -65
  148. package/src/compute-view/index.ts +0 -4
  149. package/src/compute-view/memory/AbstractMemory.ts +0 -45
  150. package/src/compute-view/memory/AbstractStageExclude.ts +0 -215
  151. package/src/compute-view/memory/AbstractStageInclude.ts +0 -200
  152. package/src/compute-view/memory/_types.ts +0 -148
  153. package/src/compute-view/memory/index.ts +0 -5
  154. package/src/compute-view/memory/ops.ts +0 -43
  155. package/src/compute-view/relationships-view/_types.ts +0 -16
  156. package/src/compute-view/relationships-view/compute.ts +0 -159
  157. package/src/compute-view/relationships-view/index.ts +0 -3
  158. package/src/compute-view/relationships-view/layout.ts +0 -357
  159. package/src/compute-view/relationships-view/utils.ts +0 -73
  160. package/src/compute-view/utils/ancestorsOfNode.ts +0 -19
  161. package/src/compute-view/utils/applyCustomElementProperties.ts +0 -107
  162. package/src/compute-view/utils/applyCustomRelationProperties.ts +0 -58
  163. package/src/compute-view/utils/applyViewRuleStyles.ts +0 -73
  164. package/src/compute-view/utils/buildComputedNodes.ts +0 -153
  165. package/src/compute-view/utils/buildElementNotations.ts +0 -63
  166. package/src/compute-view/utils/elementExpressionToPredicate.ts +0 -50
  167. package/src/compute-view/utils/link-nodes-with-edges.ts +0 -56
  168. package/src/compute-view/utils/merge-props-from-relationships.ts +0 -161
  169. package/src/compute-view/utils/relationExpressionToPredicates.ts +0 -55
  170. package/src/compute-view/utils/resolve-extended-views.ts +0 -85
  171. package/src/compute-view/utils/resolve-global-rules.ts +0 -88
  172. package/src/compute-view/utils/topological-sort.ts +0 -256
  173. package/src/compute-view/utils/view-hash.ts +0 -45
  174. package/src/compute-view/utils/with-readable-edges.ts +0 -40
  175. package/src/index.ts +0 -32
  176. package/src/model/DeploymentElementModel.ts +0 -682
  177. package/src/model/DeploymentModel.ts +0 -393
  178. package/src/model/ElementModel.ts +0 -293
  179. package/src/model/LikeC4Model.ts +0 -882
  180. package/src/model/RelationModel.ts +0 -152
  181. package/src/model/connection/Connection.ts +0 -72
  182. package/src/model/connection/deployment/DeploymentConnectionModel.ts +0 -185
  183. package/src/model/connection/deployment/find.ts +0 -120
  184. package/src/model/connection/deployment/index.ts +0 -8
  185. package/src/model/connection/index.ts +0 -22
  186. package/src/model/connection/model/ConnectionModel.ts +0 -158
  187. package/src/model/connection/model/find.ts +0 -117
  188. package/src/model/connection/model/index.ts +0 -8
  189. package/src/model/connection/ops.ts +0 -276
  190. package/src/model/guards.ts +0 -77
  191. package/src/model/index.ts +0 -72
  192. package/src/model/types.ts +0 -67
  193. package/src/model/utils.ts +0 -66
  194. package/src/model/view/EdgeModel.ts +0 -120
  195. package/src/model/view/LikeC4ViewModel.ts +0 -361
  196. package/src/model/view/LikeC4ViewsFolder.ts +0 -151
  197. package/src/model/view/NodeModel.ts +0 -247
  198. package/src/theme/element.ts +0 -61
  199. package/src/theme/index.ts +0 -127
  200. package/src/theme/relationships.ts +0 -54
  201. package/src/types/RichText.ts +0 -206
  202. package/src/types/_aux.ts +0 -291
  203. package/src/types/_common.ts +0 -80
  204. package/src/types/const.ts +0 -34
  205. package/src/types/expression-model.ts +0 -294
  206. package/src/types/expression.ts +0 -313
  207. package/src/types/fqnRef.ts +0 -91
  208. package/src/types/geometry.ts +0 -159
  209. package/src/types/global.ts +0 -21
  210. package/src/types/index.ts +0 -42
  211. package/src/types/model-data.ts +0 -77
  212. package/src/types/model-deployment.ts +0 -88
  213. package/src/types/model-dump.ts +0 -79
  214. package/src/types/model-logical.ts +0 -135
  215. package/src/types/model-spec.ts +0 -85
  216. package/src/types/operators.ts +0 -156
  217. package/src/types/project.ts +0 -14
  218. package/src/types/scalar.ts +0 -162
  219. package/src/types/styles.ts +0 -122
  220. package/src/types/view-changes.ts +0 -32
  221. package/src/types/view-common.ts +0 -157
  222. package/src/types/view-computed.ts +0 -118
  223. package/src/types/view-layouted.ts +0 -98
  224. package/src/types/view-parsed.deployment.ts +0 -39
  225. package/src/types/view-parsed.dynamic.ts +0 -89
  226. package/src/types/view-parsed.element.ts +0 -65
  227. package/src/types/view.ts +0 -113
  228. package/src/utils/common-head.ts +0 -22
  229. package/src/utils/compare-natural.ts +0 -60
  230. package/src/utils/const.ts +0 -1
  231. package/src/utils/fqn.ts +0 -302
  232. package/src/utils/getOrCreate.ts +0 -8
  233. package/src/utils/guards.ts +0 -9
  234. package/src/utils/index.ts +0 -68
  235. package/src/utils/invariant.ts +0 -29
  236. package/src/utils/iterable/_types.ts +0 -5
  237. package/src/utils/iterable/filter.ts +0 -55
  238. package/src/utils/iterable/find.ts +0 -38
  239. package/src/utils/iterable/first.ts +0 -48
  240. package/src/utils/iterable/flat.ts +0 -17
  241. package/src/utils/iterable/head.ts +0 -22
  242. package/src/utils/iterable/index.ts +0 -15
  243. package/src/utils/iterable/map.ts +0 -40
  244. package/src/utils/iterable/reduce.ts +0 -24
  245. package/src/utils/iterable/some.ts +0 -36
  246. package/src/utils/iterable/to.ts +0 -19
  247. package/src/utils/iterable/unique.ts +0 -23
  248. package/src/utils/markdown/index.ts +0 -2
  249. package/src/utils/markdown/to-html.ts +0 -32
  250. package/src/utils/markdown/to-text.ts +0 -14
  251. package/src/utils/memoize-prop.ts +0 -30
  252. package/src/utils/mnemonist.ts +0 -9
  253. package/src/utils/object-hash.ts +0 -14
  254. package/src/utils/promises.ts +0 -24
  255. package/src/utils/relations.ts +0 -36
  256. package/src/utils/set.ts +0 -83
  257. package/src/utils/string-hash.ts +0 -16
package/src/utils/fqn.ts DELETED
@@ -1,302 +0,0 @@
1
- import type { Fqn, IterableContainer, ReorderedArray } from '../types'
2
- import { compareNatural } from './compare-natural'
3
- import { isString } from './guards'
4
-
5
- export type Predicate<T> = (x: T) => boolean
6
-
7
- export function parentFqn<E extends string>(fqn: E): E | null {
8
- const lastDot = fqn.lastIndexOf('.')
9
- if (lastDot > 0) {
10
- return fqn.slice(0, lastDot) as E
11
- }
12
- return null
13
- }
14
-
15
- export function parentFqnPredicate<T extends { parent: Fqn | null }>(parent: Fqn): Predicate<T> {
16
- const prefix = parent + '.'
17
- return (e: T) => !!e.parent && (e.parent === parent || e.parent.startsWith(prefix))
18
- }
19
-
20
- export function nameFromFqn<E extends string>(fqn: E): string {
21
- const lastDot = fqn.lastIndexOf('.')
22
- if (lastDot > 0) {
23
- return fqn.slice(lastDot + 1)
24
- } else {
25
- return fqn
26
- }
27
- }
28
-
29
- const asString: <E extends string | { id: string }>(e: E) => string = e => (isString(e) ? e : e.id)
30
-
31
- /**
32
- * Check if one element is an ancestor of another
33
- * Composable version
34
- * @signature
35
- * isAncestor(another)(ancestor)
36
- */
37
- export function isAncestor<A extends string | { id: string }>(another: NoInfer<A>): (ancestor: A) => boolean
38
- /**
39
- * Check if one element is an ancestor of another
40
- * @signature
41
- * isAncestor(ancestor, another)
42
- */
43
- export function isAncestor<A extends string | { id: string }>(ancestor: A, another: A): boolean
44
-
45
- export function isAncestor<T extends string>(
46
- arg1: T | WithId<T>,
47
- arg2?: NoInfer<T> | WithId<NoInfer<T>>,
48
- ) {
49
- const arg1Id = asString(arg1)
50
- if (arg2) {
51
- const arg2Id = asString(arg2)
52
- return arg2Id.startsWith(arg1Id + '.')
53
- }
54
- return (ancestor: T | WithId<T>) => {
55
- const ancestorId = asString(ancestor)
56
- return arg1Id.startsWith(ancestorId + '.')
57
- }
58
- }
59
-
60
- export function isSameHierarchy<T extends string>(
61
- one: NoInfer<T> | WithId<NoInfer<T>>,
62
- ): (another: T | WithId<T>) => boolean
63
- export function isSameHierarchy<T extends string>(one: T | WithId<T>, another: T | WithId<T>): boolean
64
- export function isSameHierarchy<T extends string>(one: T | WithId<T>, another?: T | WithId<T>) {
65
- if (!another) {
66
- return (b: T | WithId<T>) => isSameHierarchy(one, b)
67
- }
68
- const first = asString(one)
69
- const second = asString(another)
70
- return first === second || second.startsWith(first + '.') || first.startsWith(second + '.')
71
- }
72
-
73
- type WithId<T = string> = { id: T }
74
-
75
- export function isDescendantOf<T extends string>(ancestor: WithId<T>): (descedant: WithId<T>) => boolean
76
- export function isDescendantOf<T extends string>(descedant: WithId<T>, ancestor: WithId<T>): boolean
77
- export function isDescendantOf<T extends string>(descedant: WithId<T>, ancestor?: WithId<T>) {
78
- if (!ancestor) {
79
- return (d: WithId<T>) => isAncestor(descedant, d)
80
- }
81
- return isAncestor(ancestor, descedant)
82
- }
83
-
84
- /**
85
- * How deep in the hierarchy the element is.
86
- * Root element has depth 1
87
- */
88
- export function hierarchyLevel<E extends string | { id: Fqn }>(elementOfFqn: E): number {
89
- const first = isString(elementOfFqn) ? elementOfFqn : elementOfFqn.id
90
- return first.split('.').length
91
- }
92
-
93
- /**
94
- * Calculate the distance as number of steps from one element to another, i.e.
95
- * going up to the common ancestor, then going down to the other element.
96
- * Sibling distance is always 1
97
- *
98
- * Can be used for hierarchical clustering
99
- */
100
- export function hierarchyDistance<E extends string | { id: Fqn }>(one: E, another: E): number {
101
- const first = isString(one) ? one as Fqn : one.id
102
- const second = isString(another) ? another as Fqn : another.id
103
-
104
- if (first === second) {
105
- return 0
106
- }
107
-
108
- const firstDepth = hierarchyLevel(first)
109
- const secondDepth = hierarchyLevel(second)
110
-
111
- if (isSameHierarchy(first, second)) {
112
- return Math.abs(firstDepth - secondDepth)
113
- }
114
-
115
- const ancestor = commonAncestor(first as Fqn, second as Fqn)
116
- const ancestorDepth = ancestor ? hierarchyLevel(ancestor) : 0
117
-
118
- return firstDepth + secondDepth - (2 * ancestorDepth + 1)
119
- }
120
-
121
- export function commonAncestor<E extends string>(first: E, second: E): E | null {
122
- const a = first.split('.')
123
- if (a.length < 2) {
124
- return null
125
- }
126
- const b = second.split('.')
127
- if (b.length < 2) {
128
- return null
129
- }
130
- let ancestor = [] as string[]
131
- for (let i = 0; i < Math.min(a.length, b.length) - 1 && a[i] === b[i]; i++) {
132
- ancestor.push(a[i]!)
133
- }
134
- if (ancestor.length === 0) {
135
- return null
136
- }
137
- return ancestor.join('.') as E
138
- }
139
-
140
- /**
141
- * Get all ancestor elements (i.e. parent, parent’s parent, etc.)
142
- * going up from parent to the root
143
- */
144
- export function ancestorsFqn<Id extends string>(fqn: Id): Id[] {
145
- const path = fqn.split('.') as Id[]
146
- path.pop()
147
- if (path.length === 0) {
148
- return []
149
- }
150
- return path.reduce((acc, part, idx) => {
151
- if (idx === 0) {
152
- acc.push(part)
153
- return acc
154
- }
155
- acc.unshift(`${acc[0]}.${part}` as Id)
156
- return acc
157
- }, [] as Id[])
158
- }
159
-
160
- /**
161
- * Compares two fully qualified names (fqns) hierarchically based on their depth.
162
- * From parent nodes to leaves
163
- *
164
- * @param {string} a - The first fqn to compare.
165
- * @param {string} b - The second fqn to compare.
166
- * @returns {number} - 0 if the fqns have the same depth.
167
- * - Positive number if a is deeper than b.
168
- * - Negative number if b is deeper than a.
169
- */
170
- export function compareFqnHierarchically<T extends string = string>(a: T, b: T): -1 | 0 | 1 {
171
- const depthA = a.split('.').length
172
- const depthB = b.split('.').length
173
- switch (true) {
174
- case depthA > depthB: {
175
- return 1
176
- }
177
- case depthA < depthB: {
178
- return -1
179
- }
180
- default: {
181
- return 0
182
- }
183
- }
184
- }
185
-
186
- export function compareByFqnHierarchically<T extends { id: string }>(a: T, b: T): -1 | 0 | 1 {
187
- return compareFqnHierarchically(a.id, b.id)
188
- }
189
-
190
- /**
191
- * Sorts an array of objects hierarchically based on their fully qualified names (FQN).
192
- * Objects are sorted by the number of segments in their FQN (defined by dot-separated ID).
193
- *
194
- * @typeParam T - Object type that contains an 'id' string property
195
- * @typeParam A - Type extending IterableContainer of T
196
- *
197
- * @param array - Array of objects to be sorted
198
- * @returns A new array with items sorted by their FQN hierarchy depth (number of segments)
199
- *
200
- * @example
201
- * ```ts
202
- * const items = [
203
- * { id: "a.b.c" },
204
- * { id: "a" },
205
- * { id: "a.b" }
206
- * ];
207
- * sortByFqnHierarchically(items);
208
- * // Result: [
209
- * // { id: "a" },
210
- * // { id: "a.b" },
211
- * // { id: "a.b.c" }
212
- * // ]
213
- * ```
214
- */
215
- export function sortByFqnHierarchically<T extends { id: string }, A extends IterableContainer<T>>(
216
- array: A,
217
- ): ReorderedArray<A> {
218
- return array
219
- .map(item => ({ item, fqn: item.id.split('.') }))
220
- .sort((a, b) => {
221
- return a.fqn.length - b.fqn.length
222
- })
223
- .map(({ item }) => item) as ReorderedArray<A>
224
- }
225
-
226
- function findTopAncestor<T extends { id: string }>(items: T[], item: T): T | null {
227
- let parent = item
228
- for (const e of items) {
229
- if (isAncestor(e, parent)) {
230
- parent = e
231
- }
232
- }
233
- return parent !== item ? parent : null
234
- }
235
-
236
- /**
237
- * Keeps initial order of the elements, but ensures that parents are before children
238
- */
239
- export function sortParentsFirst<T extends { id: string }, A extends IterableContainer<T>>(
240
- array: A,
241
- ): ReorderedArray<A> {
242
- const result = [] as T[]
243
- const items = [...array]
244
- let item
245
- while ((item = items.shift())) {
246
- let parent
247
- while ((parent = findTopAncestor(items, item))) {
248
- result.push(items.splice(items.indexOf(parent), 1)[0]!)
249
- }
250
- result.push(item)
251
- }
252
- return result as ReorderedArray<A>
253
- }
254
-
255
- /**
256
- * Sorts an array of objects naturally by their fully qualified name (FQN) identifier.
257
- *
258
- * @template T - Type of objects containing an 'id' string property
259
- * @template A - Type extending IterableContainer of T
260
- *
261
- * @param first - Optional. Either the array to sort or the sort direction ('asc'|'desc')
262
- * @param sort - Optional. The sort direction ('asc'|'desc'). Defaults to 'asc' if not specified
263
- *
264
- * @example
265
- * // As a function that returns a sorting function
266
- * const sorted = sortNaturalByFqn('desc')(myArray);
267
- *
268
- * // Direct array sorting
269
- * const sorted = sortNaturalByFqn(myArray, 'desc');
270
- */
271
- export function sortNaturalByFqn(
272
- sort?: 'asc' | 'desc',
273
- ): <I extends WithId<string>, A extends IterableContainer<I>>(array: A) => ReorderedArray<A>
274
- export function sortNaturalByFqn<A extends IterableContainer<WithId>>(
275
- array: A,
276
- sort?: 'asc' | 'desc',
277
- ): ReorderedArray<A>
278
- export function sortNaturalByFqn<A extends IterableContainer<WithId>>(
279
- array?: A | 'asc' | 'desc',
280
- sort?: 'asc' | 'desc',
281
- ) {
282
- if (!array || isString(array)) {
283
- const dir = array ?? 'asc'
284
- return (arr: A) => sortNaturalByFqn(arr, dir)
285
- }
286
- const dir = sort === 'desc' ? -1 : 1
287
- return array
288
- .map(item => ({ item, fqn: item.id.split('.') }))
289
- .sort((a, b) => {
290
- if (a.fqn.length !== b.fqn.length) {
291
- return (a.fqn.length - b.fqn.length) * dir
292
- }
293
- for (let i = 0; i < a.fqn.length; i++) {
294
- const compare = compareNatural(a.fqn[i], b.fqn[i])
295
- if (compare !== 0) {
296
- return compare
297
- }
298
- }
299
- return 0
300
- })
301
- .map(({ item }) => item) as ReorderedArray<A>
302
- }
@@ -1,8 +0,0 @@
1
- export function getOrCreate<K, V>(map: Map<K, V>, key: K, create: (key: K) => V): V {
2
- let entry = map.get(key)
3
- if (!entry) {
4
- entry = create(key)
5
- map.set(key, entry)
6
- }
7
- return entry
8
- }
@@ -1,9 +0,0 @@
1
- import type { NonEmptyArray } from '../types'
2
-
3
- export function isString(value: unknown): value is string {
4
- return value != null && typeof value === 'string'
5
- }
6
-
7
- export function isNonEmptyArray<A>(arr: ArrayLike<A> | undefined): arr is NonEmptyArray<A> {
8
- return !!arr && Array.isArray(arr) && arr.length > 0
9
- }
@@ -1,68 +0,0 @@
1
- export {
2
- BiMap,
3
- DefaultMap,
4
- DefaultWeakMap,
5
- LinkedList,
6
- MultiMap,
7
- Queue,
8
- Stack,
9
- } from './mnemonist'
10
-
11
- export { invariant, nonexhaustive, nonNullable } from './invariant'
12
-
13
- export { commonHead } from './common-head'
14
- export { compareNatural, compareNaturalHierarchically } from './compare-natural'
15
- export {
16
- ancestorsFqn,
17
- commonAncestor,
18
- compareByFqnHierarchically,
19
- compareFqnHierarchically,
20
- hierarchyDistance,
21
- hierarchyLevel,
22
- isAncestor,
23
- isDescendantOf,
24
- isSameHierarchy,
25
- nameFromFqn,
26
- parentFqn,
27
- sortByFqnHierarchically,
28
- sortNaturalByFqn,
29
- sortParentsFirst,
30
- } from './fqn'
31
-
32
- export { getOrCreate } from './getOrCreate'
33
-
34
- export {
35
- isNonEmptyArray,
36
- isString,
37
- } from './guards'
38
- export {
39
- ifilter,
40
- ifind,
41
- ifirst,
42
- iflat,
43
- ihead,
44
- imap,
45
- ireduce,
46
- isIterable,
47
- isome,
48
- iunique,
49
- toArray,
50
- toSet,
51
- } from './iterable'
52
- export { memoizeProp } from './memoize-prop'
53
- export { delay, promiseNextTick } from './promises'
54
-
55
- export { compareRelations } from './relations'
56
-
57
- export {
58
- difference,
59
- equals as equalsSet,
60
- intersection,
61
- symmetricDifference,
62
- union,
63
- } from './set'
64
-
65
- export { objectHash } from './object-hash'
66
- export { stringHash } from './string-hash'
67
-
68
- export { markdownToHtml, markdownToText } from './markdown'
@@ -1,29 +0,0 @@
1
- // Ensure that the value is NonNullable
2
- // Mostly as safer `value!`
3
- // eslint-disable-next-line @typescript-eslint/ban-types
4
- export function nonNullable<T>(value: T, message?: string | (() => string)): NonNullable<T> {
5
- if (typeof value === 'undefined' || value == null) {
6
- const msg = typeof message === 'function' ? message() : message
7
- throw new Error(msg ?? `Expected defined value, but received ${value}`)
8
- }
9
- return value
10
- }
11
-
12
- // Throw an error if the condition fails
13
- // > Not providing an inline default argument for message as the result is smaller
14
- export function invariant(
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- condition: any,
17
- // Can provide a string, or a function that returns a string for cases where
18
- // the message takes a fair amount of effort to compute
19
- message?: string,
20
- ): asserts condition {
21
- if (condition) {
22
- return
23
- }
24
- throw new Error(message ?? 'Invariant failed')
25
- }
26
-
27
- export function nonexhaustive(value: never): never {
28
- throw new Error(`NonExhaustive value: ${value}`)
29
- }
@@ -1,5 +0,0 @@
1
- import { isNonNullish } from 'remeda'
2
-
3
- export function isIterable<T, I extends Iterable<T>>(something: I | unknown): something is Iterable<T> {
4
- return isNonNullish(something) && typeof something === 'object' && Symbol.iterator in something
5
- }
@@ -1,55 +0,0 @@
1
- import { isFunction } from 'remeda'
2
- import type { IteratorLike } from '../../types'
3
- import { invariant } from '../../utils/invariant'
4
-
5
- /**
6
- * Filters an iterable based on a predicate.
7
- * Composabel first version of `ifilter`.
8
- * @signature
9
- * ifilter(predicate)(data)
10
- */
11
- export function ifilter<T, S extends T>(
12
- predicate: (v: T) => v is S,
13
- ): (iterable: Iterable<T>) => IteratorLike<S>
14
- export function ifilter<T>(
15
- predicate: (v: T) => boolean,
16
- ): (iterable: Iterable<T>) => IteratorLike<T>
17
-
18
- /**
19
- * Filters an iterable based on a predicate.
20
- * Data first version of `ifilter`.
21
- * @signature
22
- * ifilter(data, predicate)
23
- * @example
24
- * ifilter(new Set([1, 2, 3]), x => x % 2 === 1) // => Iterable<[1, 3]>
25
- */
26
- export function ifilter<T, S extends T>(
27
- iterable: Iterable<T>,
28
- predicate: (v: T) => v is S,
29
- ): IteratorLike<S>
30
- export function ifilter<T>(
31
- iterable: Iterable<T>,
32
- predicate: (v: T) => boolean,
33
- ): IteratorLike<T>
34
-
35
- export function ifilter(
36
- arg1: unknown,
37
- arg2?: unknown,
38
- ): IteratorLike<unknown> | ((it: Iterable<unknown>) => IteratorLike<unknown>) {
39
- const pred = (arg2 ?? arg1) as (v: any) => boolean
40
- invariant(isFunction(pred))
41
-
42
- function* _filter(iter: Iterable<unknown>): IteratorLike<unknown> {
43
- for (const value of iter) {
44
- if (pred(value)) {
45
- yield value
46
- }
47
- }
48
- return
49
- }
50
-
51
- if (!arg2) {
52
- return _filter
53
- }
54
- return _filter(arg1 as Iterable<unknown>)
55
- }
@@ -1,38 +0,0 @@
1
- import { isFunction } from 'remeda'
2
- import { invariant } from '../../utils/invariant'
3
-
4
- /**
5
- * Finds the first element in the iterable that satisfies the predicate.
6
- * Composable first version of `find`.
7
- * @signature
8
- * ifind(predicate)(data)
9
- */
10
- export function ifind<T, S extends T>(predicate: (item: T) => item is S): (iterable: Iterable<T>) => S | undefined
11
- export function ifind<T>(predicate: (item: T) => boolean): (iterable: Iterable<T>) => T | undefined
12
- /**
13
- * Finds the first element in the iterable that satisfies the predicate.
14
- * Data first version of `find`.
15
- * @signature
16
- * ifind(data, predicate)
17
- */
18
- export function ifind<T, S extends T>(iterable: Iterable<T>, predicate: (item: T) => item is S): S | undefined
19
- export function ifind<T>(iterable: Iterable<T>, predicate: (item: T) => boolean): T | undefined
20
-
21
- export function ifind(arg1: unknown, arg2?: unknown) {
22
- const pred = (arg2 ?? arg1) as (v: any) => boolean
23
- invariant(isFunction(pred))
24
-
25
- function _find(iter: Iterable<unknown>) {
26
- for (const value of iter) {
27
- if (pred(value)) {
28
- return value
29
- }
30
- }
31
- return
32
- }
33
-
34
- if (!arg2) {
35
- return _find
36
- }
37
- return _find(arg1 as Iterable<unknown>)
38
- }
@@ -1,48 +0,0 @@
1
- import type { IteratorLike } from '../../types'
2
- import { invariant } from '../../utils/invariant'
3
-
4
- /**
5
- * Takes the first N elements from an iterable.
6
- * Composable first version of `ifirst`.
7
- * @signature
8
- * ifirst(count)(data)
9
- */
10
- export function ifirst<T>(count: number): (iterable: Iterable<T>) => IteratorLike<T>
11
-
12
- /**
13
- * Takes the first N elements from an iterable.
14
- * Data first version of `ifirst`.
15
- * @signature
16
- * ifirst(data, count)
17
- * @example
18
- * ifirst([1, 2, 3, 4, 5], 3) // => Iterable<[1, 2, 3]>
19
- */
20
- export function ifirst<T>(iterable: Iterable<T>, count: number): IteratorLike<T>
21
-
22
- export function ifirst(
23
- arg1: unknown,
24
- arg2?: unknown,
25
- ): IteratorLike<unknown> | ((it: Iterable<unknown>) => IteratorLike<unknown>) {
26
- const count = (arg2 ?? arg1) as number
27
- invariant(typeof count === 'number' && count >= 0, 'Count must be a non-negative number')
28
-
29
- function* _first(iter: Iterable<unknown>): IteratorLike<unknown> {
30
- let taken = 0
31
- for (const value of iter) {
32
- if (taken >= count) {
33
- break
34
- }
35
- yield value
36
- taken++
37
- }
38
- return
39
- }
40
-
41
- if (arg2 === undefined) {
42
- // Composable version: ifirst(count)(iterable)
43
- return (iterable: Iterable<unknown>) => _first(iterable)
44
- }
45
-
46
- // Data-first version: ifirst(iterable, count)
47
- return _first(arg1 as Iterable<unknown>)
48
- }
@@ -1,17 +0,0 @@
1
- import type { IteratorLike } from '../../types'
2
-
3
- export function iflat(): <T>(iterable: Iterable<IteratorLike<T>>) => IteratorLike<T>
4
- export function iflat<T>(iterable: Iterable<IteratorLike<T>>): IteratorLike<T>
5
-
6
- export function iflat<T>(
7
- iterable?: Iterable<IteratorLike<T>>,
8
- ): IteratorLike<T> | ((iterable: Iterable<IteratorLike<T>>) => IteratorLike<T>) {
9
- return iterable ? _iflat(iterable) : _iflat
10
- }
11
-
12
- function* _iflat<T>(iterable: Iterable<IteratorLike<T>>): IteratorLike<T> {
13
- for (const inner of iterable) {
14
- yield* inner
15
- }
16
- return
17
- }
@@ -1,22 +0,0 @@
1
- export function ihead<T>(): (iterable: Iterable<T>) => T | undefined
2
- /**
3
- * Finds the first element in the iterable that satisfies the predicate.
4
- * Data first version of `find`.
5
- * @signature
6
- * ifind(data, predicate)
7
- */
8
- export function ihead<T>(iterable: Iterable<T>): T | undefined
9
-
10
- export function ihead<T>(iterable?: Iterable<T>) {
11
- if (!iterable) {
12
- return _head
13
- }
14
- return _head(iterable)
15
- }
16
-
17
- function _head<T>(iter: Iterable<T>) {
18
- for (const value of iter) {
19
- return value
20
- }
21
- return
22
- }
@@ -1,15 +0,0 @@
1
- export { isIterable } from './_types'
2
-
3
- /**
4
- * operators
5
- */
6
- export { ifilter } from './filter'
7
- export { ifind } from './find'
8
- export { ifirst } from './first'
9
- export { iflat } from './flat'
10
- export { ihead } from './head'
11
- export { imap } from './map'
12
- export { ireduce } from './reduce'
13
- export { isome } from './some'
14
- export { toArray, toSet } from './to'
15
- export { iunique } from './unique'
@@ -1,40 +0,0 @@
1
- import { isFunction } from 'remeda'
2
- import type { IteratorLike } from '../../types'
3
- import { invariant } from '../../utils/invariant'
4
-
5
- type MapFunction<T, S> = (item: T) => S
6
-
7
- /**
8
- * Maps an iterable using a mapper function.
9
- * Composable first version of `imap`.
10
- * @signature
11
- * imap(mapper)(data)
12
- */
13
- export function imap<T, S>(mapper: MapFunction<T, S>): (iterable: Iterable<T>) => IteratorLike<S>
14
- /**
15
- * Maps an iterable using a mapper function.
16
- * Data first version of `imap`.
17
- * @signature
18
- * imap(data, mapper)
19
- */
20
- export function imap<T, S>(iterable: Iterable<T>, mapper: MapFunction<T, S>): IteratorLike<S>
21
-
22
- export function imap(
23
- arg1: unknown,
24
- arg2?: unknown,
25
- ): IteratorLike<unknown> | ((it: Iterable<unknown>) => IteratorLike<unknown>) {
26
- const mapper = (arg2 ?? arg1) as MapFunction<any, any>
27
- invariant(isFunction(mapper))
28
-
29
- function* _map(iter: Iterable<unknown>): IteratorLike<unknown> {
30
- for (const value of iter) {
31
- yield mapper(value)
32
- }
33
- return
34
- }
35
-
36
- if (!arg2) {
37
- return _map
38
- }
39
- return _map(arg1 as Iterable<unknown>)
40
- }
@@ -1,24 +0,0 @@
1
- import { isDefined, isFunction } from 'remeda'
2
- import { invariant } from '../../utils/invariant'
3
-
4
- export function ireduce<T, R>(reducer: (acc: R, item: T) => R, initialValue: R): (iterable: Iterable<T>) => R
5
- export function ireduce<T, R>(iterable: Iterable<T>, reducer: (acc: R, item: T) => R, initialValue: R): R
6
- export function ireduce<T, R>(
7
- arg1: unknown,
8
- arg2: unknown,
9
- arg3?: unknown,
10
- ): R | ((iterable: Iterable<T>) => R) {
11
- const reducer = (isDefined(arg3) ? arg2 : arg1) as (acc: R, item: T) => R
12
- const initialValue = arg3 ?? arg2
13
- invariant(isFunction(reducer))
14
-
15
- function _reduce(iter: Iterable<T>): R {
16
- let acc = initialValue as R
17
- for (const value of iter) {
18
- acc = reducer(acc, value)
19
- }
20
- return acc
21
- }
22
-
23
- return isDefined(arg3) ? _reduce(arg1 as Iterable<T>) : _reduce
24
- }