@markuplint/shared 4.2.0 → 4.3.1-alpha.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## [4.3.1-alpha.0](https://github.com/markuplint/markuplint/compare/@markuplint/shared@4.3.0...@markuplint/shared@4.3.1-alpha.0) (2024-05-04)
7
+
8
+ **Note:** Version bump only for package @markuplint/shared
@@ -59,3 +59,20 @@ export declare function decodeEntities(text: string): string;
59
59
  * @returns The decoded URL string or the original href if a `URIError` occurs.
60
60
  */
61
61
  export declare function decodeHref(href: string): string;
62
+ /**
63
+ * Converts an array of branches into an array of patterns.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * branchesToPatterns([1, [2, 3]]) // [[1, 2], [1, 3]]
68
+ * branchesToPatterns([1, [2, 3], 4]) // [[1, 2, 4], [1, 3, 4]]
69
+ * branchesToPatterns([1, [2, undefined], 3]) // [[1, 2, 3], [1, 3]]
70
+ * branchesToPatterns([1, [2, 3], [4, 5], 6]) // [[1, 2, 4, 6], [1, 3, 4, 6], [1, 2, 5, 6], [1, 3, 5, 6]]
71
+ * branchesToPatterns([1, [], 2]) // [[1, 2]]
72
+ * ```
73
+ *
74
+ * @template T The type of elements in the branches array.
75
+ * @param branches The array of branches to convert.
76
+ * @returns The array of patterns generated from the branches.
77
+ */
78
+ export declare function branchesToPatterns<T>(branches: ReadonlyArray<Nullable<T> | ReadonlyArray<Nullable<T>>>): T[][];
package/lib/functions.js CHANGED
@@ -82,3 +82,31 @@ export function decodeHref(href) {
82
82
  throw error;
83
83
  }
84
84
  }
85
+ /**
86
+ * Converts an array of branches into an array of patterns.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * branchesToPatterns([1, [2, 3]]) // [[1, 2], [1, 3]]
91
+ * branchesToPatterns([1, [2, 3], 4]) // [[1, 2, 4], [1, 3, 4]]
92
+ * branchesToPatterns([1, [2, undefined], 3]) // [[1, 2, 3], [1, 3]]
93
+ * branchesToPatterns([1, [2, 3], [4, 5], 6]) // [[1, 2, 4, 6], [1, 3, 4, 6], [1, 2, 5, 6], [1, 3, 5, 6]]
94
+ * branchesToPatterns([1, [], 2]) // [[1, 2]]
95
+ * ```
96
+ *
97
+ * @template T The type of elements in the branches array.
98
+ * @param branches The array of branches to convert.
99
+ * @returns The array of patterns generated from the branches.
100
+ */
101
+ export function branchesToPatterns(branches) {
102
+ // eslint-disable-next-line unicorn/no-array-reduce
103
+ return branches.reduce((accumulator, current) => {
104
+ if (Array.isArray(current)) {
105
+ if (current.length === 0) {
106
+ return accumulator;
107
+ }
108
+ return current.flatMap(item => accumulator.map(pattern => [...pattern, item].filter(nonNullableFilter)));
109
+ }
110
+ return accumulator.map(pattern => [...pattern, current].filter(nonNullableFilter));
111
+ }, [[]]);
112
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/shared",
3
- "version": "4.2.0",
3
+ "version": "4.3.1-alpha.0",
4
4
  "description": "Shared functions for Markuplint",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -26,5 +26,5 @@
26
26
  "dependencies": {
27
27
  "html-entities": "2.5.2"
28
28
  },
29
- "gitHead": "ad3db077e96bea0666c9105b250f895263907a87"
29
+ "gitHead": "d091e2199e3dc6757c2c8edbf01fd19e8b072015"
30
30
  }