@@ -8,24 +8,24 @@ export type SortDirection =
88 | 'desc'
99 | 'ascending'
1010 | 'descending'
11- | { $meta : string } ;
11+ | { readonly $meta : string } ;
1212
1313/** @public */
1414export type Sort =
1515 | string
16- | Exclude < SortDirection , { $meta : string } >
17- | string [ ]
18- | { [ key : string ] : SortDirection }
19- | Map < string , SortDirection >
20- | [ string , SortDirection ] [ ]
21- | [ string , SortDirection ] ;
16+ | Exclude < SortDirection , { readonly $meta : string } >
17+ | ReadonlyArray < string >
18+ | { readonly [ key : string ] : SortDirection }
19+ | ReadonlyMap < string , SortDirection >
20+ | ReadonlyArray < readonly [ string , SortDirection ] >
21+ | readonly [ string , SortDirection ] ;
2222
2323/** Below stricter types were created for sort that correspond with type that the cmd takes */
2424
25- /** @internal */
25+ /** @public */
2626export type SortDirectionForCmd = 1 | - 1 | { $meta : string } ;
2727
28- /** @internal */
28+ /** @public */
2929export type SortForCmd = Map < string , SortDirectionForCmd > ;
3030
3131/** @internal */
@@ -55,7 +55,7 @@ function isMeta(t: SortDirection): t is { $meta: string } {
5555}
5656
5757/** @internal */
58- function isPair ( t : Sort ) : t is [ string , SortDirection ] {
58+ function isPair ( t : Sort ) : t is readonly [ string , SortDirection ] {
5959 if ( Array . isArray ( t ) && t . length === 2 ) {
6060 try {
6161 prepareDirection ( t [ 1 ] ) ;
@@ -67,33 +67,37 @@ function isPair(t: Sort): t is [string, SortDirection] {
6767 return false ;
6868}
6969
70- function isDeep ( t : Sort ) : t is [ string , SortDirection ] [ ] {
70+ function isDeep ( t : Sort ) : t is ReadonlyArray < readonly [ string , SortDirection ] > {
7171 return Array . isArray ( t ) && Array . isArray ( t [ 0 ] ) ;
7272}
7373
74- function isMap ( t : Sort ) : t is Map < string , SortDirection > {
74+ function isMap ( t : Sort ) : t is ReadonlyMap < string , SortDirection > {
7575 return t instanceof Map && t . size > 0 ;
7676}
7777
78+ function isReadonlyArray < T > ( value : any ) : value is readonly T [ ] {
79+ return Array . isArray ( value ) ;
80+ }
81+
7882/** @internal */
79- function pairToMap ( v : [ string , SortDirection ] ) : SortForCmd {
83+ function pairToMap ( v : readonly [ string , SortDirection ] ) : SortForCmd {
8084 return new Map ( [ [ `${ v [ 0 ] } ` , prepareDirection ( [ v [ 1 ] ] ) ] ] ) ;
8185}
8286
8387/** @internal */
84- function deepToMap ( t : [ string , SortDirection ] [ ] ) : SortForCmd {
88+ function deepToMap ( t : ReadonlyArray < readonly [ string , SortDirection ] > ) : SortForCmd {
8589 const sortEntries : SortPairForCmd [ ] = t . map ( ( [ k , v ] ) => [ `${ k } ` , prepareDirection ( v ) ] ) ;
8690 return new Map ( sortEntries ) ;
8791}
8892
8993/** @internal */
90- function stringsToMap ( t : string [ ] ) : SortForCmd {
94+ function stringsToMap ( t : ReadonlyArray < string > ) : SortForCmd {
9195 const sortEntries : SortPairForCmd [ ] = t . map ( key => [ `${ key } ` , 1 ] ) ;
9296 return new Map ( sortEntries ) ;
9397}
9498
9599/** @internal */
96- function objectToMap ( t : { [ key : string ] : SortDirection } ) : SortForCmd {
100+ function objectToMap ( t : { readonly [ key : string ] : SortDirection } ) : SortForCmd {
97101 const sortEntries : SortPairForCmd [ ] = Object . entries ( t ) . map ( ( [ k , v ] ) => [
98102 `${ k } ` ,
99103 prepareDirection ( v )
@@ -102,7 +106,7 @@ function objectToMap(t: { [key: string]: SortDirection }): SortForCmd {
102106}
103107
104108/** @internal */
105- function mapToMap ( t : Map < string , SortDirection > ) : SortForCmd {
109+ function mapToMap ( t : ReadonlyMap < string , SortDirection > ) : SortForCmd {
106110 const sortEntries : SortPairForCmd [ ] = Array . from ( t ) . map ( ( [ k , v ] ) => [
107111 `${ k } ` ,
108112 prepareDirection ( v )
@@ -116,17 +120,22 @@ export function formatSort(
116120 direction ?: SortDirection
117121) : SortForCmd | undefined {
118122 if ( sort == null ) return undefined ;
119- if ( typeof sort === 'string' ) return new Map ( [ [ sort , prepareDirection ( direction ) ] ] ) ;
123+
124+ if ( typeof sort === 'string' ) return new Map ( [ [ sort , prepareDirection ( direction ) ] ] ) ; // 'fieldName'
125+
120126 if ( typeof sort !== 'object' ) {
121127 throw new MongoInvalidArgumentError (
122128 `Invalid sort format: ${ JSON . stringify ( sort ) } Sort must be a valid object`
123129 ) ;
124130 }
125- if ( ! Array . isArray ( sort ) ) {
126- return isMap ( sort ) ? mapToMap ( sort ) : Object . keys ( sort ) . length ? objectToMap ( sort ) : undefined ;
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 ;
127136 }
128137 if ( ! sort . length ) return undefined ;
129- if ( isDeep ( sort ) ) return deepToMap ( sort ) ;
130- if ( isPair ( sort ) ) return pairToMap ( sort ) ;
131- return stringsToMap ( sort ) ;
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 ]
132141}
0 commit comments