( a: string | symbol | undefined | null, b: string | symbol | undefined | null, order: (string | symbol)[] = [], )
| 81 | * @param order - An array of values as the predefined order |
| 82 | */ |
| 83 | export function compareByOrder( |
| 84 | a: string | symbol | undefined | null, |
| 85 | b: string | symbol | undefined | null, |
| 86 | order: (string | symbol)[] = [], |
| 87 | ) { |
| 88 | a = a ?? ''; |
| 89 | b = b ?? ''; |
| 90 | const i1 = order.indexOf(a); |
| 91 | const i2 = order.indexOf(b); |
| 92 | if (i1 !== -1 || i2 !== -1) { |
| 93 | // Honor the order |
| 94 | return i1 - i2; |
| 95 | } else { |
| 96 | // Neither value is in the pre-defined order |
| 97 | |
| 98 | // symbol comes before string |
| 99 | if (typeof a === 'symbol' && typeof b === 'string') return -1; |
| 100 | if (typeof a === 'string' && typeof b === 'symbol') return 1; |
| 101 | |
| 102 | // both a and b are symbols or both a and b are strings |
| 103 | if (typeof a === 'symbol') a = a.toString(); |
| 104 | if (typeof b === 'symbol') b = b.toString(); |
| 105 | return a < b ? -1 : a > b ? 1 : 0; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Sort bindings by phase names denoted by a tag and the predefined order |
no test coverage detected