Wiki source code of Front-End Utility Library

Last modified by Manuel Leduc on 2026/02/05 17:22

Hide last authors
Manuel Leduc 6.1 1 Package name: ##@xwiki/cristal-fn-utils##.
Clément Eraud 1.1 2
Manuel Leduc 6.1 3 The utility library provides several function utilities and types to simplify some common operations.
Clément Eraud 1.1 4
Manuel Leduc 6.1 5 == API ==
Clément Eraud 1.1 6
7 {{code language="typescript"}}
8 /**
9 * Ensure a statement is unreachable
10 *
11 * @param value - the error value printed when this method is reached
12 *
Manuel Leduc 7.1 13 * @since 18.0.0RC1
Clément Eraud 4.1 14 * @beta
Clément Eraud 1.1 15 */
16 function assertUnreachable(value: never): never;
17
18 /**
19 * Assert that a value is in an array, and fix its type
20 *
Manuel Leduc 7.1 21 * @since 18.0.0RC1
Clément Eraud 4.1 22 * @beta
Clément Eraud 1.1 23 *
24 * @param array - the array to check
25 * @param value - the value expected in the array
26 * @param message - a message displayed in case the value is not found in the array
27 *
28 * @returns Whether the value is in the provided array, with the correct type
29 */
Clément Eraud 4.1 30 function assertInArray<T, U extends T>(
31 value: T,
32 array: U[],
33 message: string,
34 ): U;
Clément Eraud 1.1 35
36 /**
Clément Eraud 4.1 37 * Get a function's output or `null` if it thrown an error
Clément Eraud 1.1 38 *
Manuel Leduc 7.1 39 * @since 18.0.0RC1
Clément Eraud 4.1 40 * @beta
Clément Eraud 1.1 41 *
42 * @param func - The function to try
43 *
44 * @returns The function's output, or `null` if it thrown an error
45 */
46 function tryFallible<T>(func: () => T): T | null;
47
48 /**
Clément Eraud 4.1 49 * Get a function's output or the thrown error
Clément Eraud 1.1 50 * Will construct a new Error object if the thrown value is not an instance of the Error class
51 *
Manuel Leduc 7.1 52 * @since 18.0.0RC1
Clément Eraud 4.1 53 * @beta
Clément Eraud 1.1 54 *
55 * @param func - The function to try
56 *
57 * @returns The function's output, the thrown error if it's an instance of the `Error` class, or a constructed `Error` instance
58 */
Clément Eraud 4.1 59 // eslint-disable-next-line max-statements
Clément Eraud 1.1 60 function tryFallibleOrError<T>(func: () => T): T | Error;
Pierre Jeanjean 2.1 61
62 /**
Clément Eraud 4.1 63 * Get a funcion's promise's output or the thrown error
64 * Will construct a new Error object if the thrown value is not an instance of the Error class
65 *
Manuel Leduc 7.1 66 * @since 18.0.0RC1
Clément Eraud 4.1 67 * @beta
68 *
69 * @param func - The function to try
70 *
71 * @returns The promise's output, the thrown/resolved error if it's an instance of the `Error` class, or a constructed `Error` instance
72 */
73 // eslint-disable-next-line max-statements
74 async function tryFalliblePromiseOrError<T>(
75 func: () => Promise<T>,
76 ): Promise<T | Error>;
77
78 /**
79 * Provide a type for expressions type inference
80 *
81 * This is actually an identity function - the provided value is returned as is, with no other operation.
82 *
Manuel Leduc 7.1 83 * @since 18.0.0RC1
Clément Eraud 4.1 84 * @beta
85 *
86 * @param value - The value to return
87 * @returns - The provided value
88 *
89 * @example `[1].concat("Hello")` // Type error
90 * @example `provideTypeInference<number | string>([1].concat("Hello"))` // Works
91 */
92 function provideTypeInference<T>(value: T): T;
93
94 /**
95 * Filter and map an array's values
96 *
97 * Combines both `.filter` and `.map` with the additional benefit of conditional type predicates
98 *
Manuel Leduc 7.1 99 * @since 18.0.0RC1
Clément Eraud 4.1 100 * @beta
101 *
102 * @example `filterMap([1, 2, 3], value => value >= 2 ? value.toString() : null) // ['2', '3']`
103 *
104 * @param array - The array to map
105 * @param filterMap - The function performing the mapping and filtering. Returning `null` or `undefined` corresponds to filter values out.
106 *
107 * @returns The filtered and mapped array
108 */
109 function filterMap<T, U>(
110 array: T[],
111 filterMap: (value: T, index: number) => U | null | undefined,
112 ): U[];
113
114 /**
115 * Get correctly-typed object entries, functionally equivalent to `Object.entries`
116 *
117 * Fixes builtin `Object.entries` which types the returned object values as `any`
118 *
119 * @param obj - The object to get the entries of
120 *
121 * @returns - The correctly-typed object's entries
122 *
Manuel Leduc 7.1 123 * @since 18.0.0RC1
Clément Eraud 4.1 124 * @beta
125 */
126 function objectEntries<O extends Record<string, unknown>>(
127 obj: O,
128 ): Array<[keyof O & string, O[keyof O]]>;
129
Clément Eraud 5.1 130
Clément Eraud 4.1 131 /**
Clément Eraud 5.1 132 * Produce an HTML string representing an element
133 *
134 * @param tagName - The element's tag name
135 * @param attrs - The element's attributes
136 * @param content - The element's inner HTML content
137 *
138 * @returns - The string representation of the HTML element
139 *
Manuel Leduc 7.1 140 * @since 18.0.0RC1
Clément Eraud 5.1 141 * @beta
142 */
143 function produceHtmlEl(
144 tagName: string,
145 attrs: Record<string, string | undefined>,
146 innerHTML: string | false,
147 ): string;
148
149 /**
150 * Escape HTML-specific characters in a string
151 *
152 * @param str - The string to escape
153 *
154 * @returns - The HTML-safe string
155 *
Manuel Leduc 7.1 156 * @since 18.0.0RC1
Clément Eraud 5.1 157 * @beta
158 */
159 function escapeHtml(str: string): string;
160
161 /**
Pierre Jeanjean 2.1 162 * Generic tree structure type.
Manuel Leduc 7.1 163 * @since 18.0.0RC1
Clément Eraud 4.1 164 * @beta
Pierre Jeanjean 2.1 165 */
166 type TreeNode<T> = T & {
167 children?: TreeNode<T>[];
168 };
Clément Eraud 1.1 169 {{/code}}

Get Connected