MCPcopy Create free account
hub / github.com/loopbackio/loopback-next / compareByOrder

Function compareByOrder

packages/context/src/binding-sorter.ts:83–107  ·  view source on GitHub ↗
(
  a: string | symbol | undefined | null,
  b: string | symbol | undefined | null,
  order: (string | symbol)[] = [],
)

Source from the content-addressed store, hash-verified

81 * @param order - An array of values as the predefined order
82 */
83export 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

Callers 3

compareBindingsByTagFunction · 0.85
sortParsersFunction · 0.85

Calls 1

toStringMethod · 0.45

Tested by

no test coverage detected