UNPKG

2.91 kBTypeScriptView Raw
1import { Binding } from './binding';
2/**
3 * Compare function to sort an array of bindings.
4 * It is used by `Array.prototype.sort()`.
5 *
6 * @example
7 * ```ts
8 * const compareByKey: BindingComparator = (a, b) => a.key.localeCompare(b.key);
9 * ```
10 */
11export interface BindingComparator {
12 /**
13 * Compare two bindings
14 * @param bindingA - First binding
15 * @param bindingB - Second binding
16 * @returns A number to determine order of bindingA and bindingB
17 * - 0 leaves bindingA and bindingB unchanged
18 * - <0 bindingA comes before bindingB
19 * - >0 bindingA comes after bindingB
20 */
21 (bindingA: Readonly<Binding<unknown>>, bindingB: Readonly<Binding<unknown>>): number;
22}
23/**
24 * Creates a binding compare function to sort bindings by tagged phase name.
25 *
26 * @remarks
27 * Two bindings are compared as follows:
28 *
29 * 1. Get values for the given tag as `phase` for bindings, if the tag is not
30 * present, default `phase` to `''`.
31 * 2. If both bindings have `phase` value in `orderOfPhases`, honor the order
32 * specified by `orderOfPhases`.
33 * 3. If a binding's `phase` does not exist in `orderOfPhases`, it comes before
34 * the one with `phase` exists in `orderOfPhases`.
35 * 4. If both bindings have `phase` value outside of `orderOfPhases`, they are
36 * ordered by phase names alphabetically and symbol values come before string
37 * values.
38 *
39 * @param phaseTagName - Name of the binding tag for phase
40 * @param orderOfPhases - An array of phase names as the predefined order
41 */
42export declare function compareBindingsByTag(phaseTagName?: string, orderOfPhases?: (string | symbol)[]): BindingComparator;
43/**
44 * Compare two values by the predefined order
45 *
46 * @remarks
47 *
48 * The comparison is performed as follows:
49 *
50 * 1. If both values are included in `order`, they are sorted by their indexes in
51 * `order`.
52 * 2. The value included in `order` comes after the value not included in `order`.
53 * 3. If neither values are included in `order`, they are sorted:
54 * - symbol values come before string values
55 * - alphabetical order for two symbols or two strings
56 *
57 * @param a - First value
58 * @param b - Second value
59 * @param order - An array of values as the predefined order
60 */
61export declare function compareByOrder(a: string | symbol | undefined | null, b: string | symbol | undefined | null, order?: (string | symbol)[]): number;
62/**
63 * Sort bindings by phase names denoted by a tag and the predefined order
64 *
65 * @param bindings - An array of bindings
66 * @param phaseTagName - Tag name for phase, for example, we can use the value
67 * `'a'` of tag `order` as the phase name for `binding.tag({order: 'a'})`.
68 *
69 * @param orderOfPhases - An array of phase names as the predefined order
70 */
71export declare function sortBindingsByPhase<T = unknown>(bindings: Readonly<Binding<T>>[], phaseTagName?: string, orderOfPhases?: (string | symbol)[]): Readonly<Binding<T>>[];