( sort: Sort | undefined, direction?: SortDirection )
| 116 | |
| 117 | /** converts a Sort type into a type that is valid for the server (SortForCmd) */ |
| 118 | export 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 | } |
no test coverage detected