MCPcopy
hub / github.com/mongodb/node-mongodb-native / formatSort

Function formatSort

src/sort.ts:118–141  ·  view source on GitHub ↗
(
  sort: Sort | undefined,
  direction?: SortDirection
)

Source from the content-addressed store, hash-verified

116
117/** converts a Sort type into a type that is valid for the server (SortForCmd) */
118export function formatSort(
119 sort: Sort | undefined,
120 direction?: SortDirection
121): SortForCmd | undefined {
122 if (sort == null) return undefined;
123
124 if (typeof sort === 'string') return new Map([[sort, prepareDirection(direction)]]); // 'fieldName'
125
126 if (typeof sort !== 'object') {
127 throw new MongoInvalidArgumentError(
128 `Invalid sort format: ${JSON.stringify(sort)} Sort must be a valid object`
129 );
130 }
131
132 if (!isReadonlyArray(sort)) {
133 if (isMap(sort)) return mapToMap(sort); // Map<fieldName, SortDirection>
134 if (Object.keys(sort).length) return objectToMap(sort); // { [fieldName: string]: SortDirection }
135 return undefined;
136 }
137 if (!sort.length) return undefined;
138 if (isDeep(sort)) return deepToMap(sort); // [ [fieldName, sortDir], [fieldName, sortDir] ... ]
139 if (isPair(sort)) return pairToMap(sort); // [ fieldName, sortDir ]
140 return stringsToMap(sort); // [ fieldName, fieldName ]
141}

Callers 9

constructorMethod · 0.90
addQueryModifierMethod · 0.90
sortMethod · 0.90
makeUpdateStatementFunction · 0.90
buildCommandDocumentMethod · 0.90
makeFindCommandFunction · 0.90
createUpdateOperationFunction · 0.90
buildReplaceOneOperationFunction · 0.90

Calls 10

prepareDirectionFunction · 0.85
isReadonlyArrayFunction · 0.85
isMapFunction · 0.85
mapToMapFunction · 0.85
objectToMapFunction · 0.85
isDeepFunction · 0.85
deepToMapFunction · 0.85
isPairFunction · 0.85
pairToMapFunction · 0.85
stringsToMapFunction · 0.85

Tested by

no test coverage detected