@@ -8,24 +8,24 @@ export type SortDirection =
8
8
| 'desc'
9
9
| 'ascending'
10
10
| 'descending'
11
- | { $meta : string } ;
11
+ | { readonly $meta : string } ;
12
12
13
13
/** @public */
14
14
export type Sort =
15
15
| 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 ] ;
22
22
23
23
/** Below stricter types were created for sort that correspond with type that the cmd takes */
24
24
25
- /** @internal */
25
+ /** @public */
26
26
export type SortDirectionForCmd = 1 | - 1 | { $meta : string } ;
27
27
28
- /** @internal */
28
+ /** @public */
29
29
export type SortForCmd = Map < string , SortDirectionForCmd > ;
30
30
31
31
/** @internal */
@@ -55,7 +55,7 @@ function isMeta(t: SortDirection): t is { $meta: string } {
55
55
}
56
56
57
57
/** @internal */
58
- function isPair ( t : Sort ) : t is [ string , SortDirection ] {
58
+ function isPair ( t : Sort ) : t is readonly [ string , SortDirection ] {
59
59
if ( Array . isArray ( t ) && t . length === 2 ) {
60
60
try {
61
61
prepareDirection ( t [ 1 ] ) ;
@@ -67,33 +67,37 @@ function isPair(t: Sort): t is [string, SortDirection] {
67
67
return false ;
68
68
}
69
69
70
- function isDeep ( t : Sort ) : t is [ string , SortDirection ] [ ] {
70
+ function isDeep ( t : Sort ) : t is ReadonlyArray < readonly [ string , SortDirection ] > {
71
71
return Array . isArray ( t ) && Array . isArray ( t [ 0 ] ) ;
72
72
}
73
73
74
- function isMap ( t : Sort ) : t is Map < string , SortDirection > {
74
+ function isMap ( t : Sort ) : t is ReadonlyMap < string , SortDirection > {
75
75
return t instanceof Map && t . size > 0 ;
76
76
}
77
77
78
+ function isReadonlyArray < T > ( value : any ) : value is readonly T [ ] {
79
+ return Array . isArray ( value ) ;
80
+ }
81
+
78
82
/** @internal */
79
- function pairToMap ( v : [ string , SortDirection ] ) : SortForCmd {
83
+ function pairToMap ( v : readonly [ string , SortDirection ] ) : SortForCmd {
80
84
return new Map ( [ [ `${ v [ 0 ] } ` , prepareDirection ( [ v [ 1 ] ] ) ] ] ) ;
81
85
}
82
86
83
87
/** @internal */
84
- function deepToMap ( t : [ string , SortDirection ] [ ] ) : SortForCmd {
88
+ function deepToMap ( t : ReadonlyArray < readonly [ string , SortDirection ] > ) : SortForCmd {
85
89
const sortEntries : SortPairForCmd [ ] = t . map ( ( [ k , v ] ) => [ `${ k } ` , prepareDirection ( v ) ] ) ;
86
90
return new Map ( sortEntries ) ;
87
91
}
88
92
89
93
/** @internal */
90
- function stringsToMap ( t : string [ ] ) : SortForCmd {
94
+ function stringsToMap ( t : ReadonlyArray < string > ) : SortForCmd {
91
95
const sortEntries : SortPairForCmd [ ] = t . map ( key => [ `${ key } ` , 1 ] ) ;
92
96
return new Map ( sortEntries ) ;
93
97
}
94
98
95
99
/** @internal */
96
- function objectToMap ( t : { [ key : string ] : SortDirection } ) : SortForCmd {
100
+ function objectToMap ( t : { readonly [ key : string ] : SortDirection } ) : SortForCmd {
97
101
const sortEntries : SortPairForCmd [ ] = Object . entries ( t ) . map ( ( [ k , v ] ) => [
98
102
`${ k } ` ,
99
103
prepareDirection ( v )
@@ -102,7 +106,7 @@ function objectToMap(t: { [key: string]: SortDirection }): SortForCmd {
102
106
}
103
107
104
108
/** @internal */
105
- function mapToMap ( t : Map < string , SortDirection > ) : SortForCmd {
109
+ function mapToMap ( t : ReadonlyMap < string , SortDirection > ) : SortForCmd {
106
110
const sortEntries : SortPairForCmd [ ] = Array . from ( t ) . map ( ( [ k , v ] ) => [
107
111
`${ k } ` ,
108
112
prepareDirection ( v )
@@ -116,17 +120,22 @@ export function formatSort(
116
120
direction ?: SortDirection
117
121
) : SortForCmd | undefined {
118
122
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
+
120
126
if ( typeof sort !== 'object' ) {
121
127
throw new MongoInvalidArgumentError (
122
128
`Invalid sort format: ${ JSON . stringify ( sort ) } Sort must be a valid object`
123
129
) ;
124
130
}
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 ;
127
136
}
128
137
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 ]
132
141
}
0 commit comments