-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: format sort in cursor and in sort builder #2573
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2f4131d
force from master
62644ef
multi-key test
5a97524
fix lint
b832325
get rid of class / static
d068927
neals simpler deep to object
0102fc5
Merge branch 'master' into NODE-2458/cursor-sort
76ce555
removed all reducers
6b4cbc6
Merge branch 'NODE-2458/cursor-sort' of github.com:mongodb/node-mongo…
4bf2892
linter issue
f8cdd9d
Merge branch 'master' into NODE-2458/cursor-sort
d558e7b
reduce document import
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** @public */ | ||
export type SortDirection = | ||
| 1 | ||
| -1 | ||
| 'asc' | ||
| 'desc' | ||
| 'ascending' | ||
| 'descending' | ||
| { $meta: string }; | ||
|
||
/** @public */ | ||
export type Sort = | ||
| string | ||
| string[] | ||
| { [key: string]: SortDirection } | ||
| [string, SortDirection][] | ||
| [string, SortDirection]; | ||
|
||
/** Below stricter types were created for sort that correspond with type that the cmd takes */ | ||
|
||
/** @internal */ | ||
type SortDirectionForCmd = 1 | -1 | { $meta: string }; | ||
|
||
/** @internal */ | ||
type SortForCmd = { [key: string]: SortDirectionForCmd }; | ||
|
||
/** @internal */ | ||
function prepareDirection(direction: any = 1): SortDirectionForCmd { | ||
const value = ('' + direction).toLowerCase(); | ||
if (isMeta(direction)) return direction; | ||
switch (value) { | ||
case 'ascending': | ||
case 'asc': | ||
case '1': | ||
return 1; | ||
case 'descending': | ||
case 'desc': | ||
case '-1': | ||
return -1; | ||
default: | ||
throw new Error(`Invalid sort direction: ${JSON.stringify(direction)}`); | ||
} | ||
} | ||
|
||
/** @internal */ | ||
function isMeta(t: SortDirection): t is { $meta: string } { | ||
return typeof t === 'object' && t !== null && '$meta' in t && typeof t.$meta === 'string'; | ||
} | ||
|
||
/** @internal */ | ||
function isPair(t: Sort): t is [string, SortDirection] { | ||
if (Array.isArray(t) && t.length === 2) { | ||
try { | ||
prepareDirection(t[1]); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** @internal */ | ||
function pairToObject(v: [string, SortDirection]): SortForCmd { | ||
return { [v[0]]: prepareDirection(v[1]) }; | ||
} | ||
|
||
/** @internal */ | ||
function isDeep(t: Sort): t is [string, SortDirection][] { | ||
return Array.isArray(t) && Array.isArray(t[0]); | ||
} | ||
|
||
/** @internal */ | ||
function deepToObject(t: [string, SortDirection][]): SortForCmd { | ||
const sortObject: SortForCmd = {}; | ||
for (const [name, value] of t) { | ||
sortObject[name] = prepareDirection(value); | ||
} | ||
return sortObject; | ||
} | ||
|
||
/** @internal */ | ||
function stringsToObject(t: string[]): SortForCmd { | ||
return t.reduce((acq, key) => { | ||
return { ...acq, [key]: 1 }; | ||
}, {}); | ||
} | ||
|
||
/** @internal */ | ||
function validate(t: { [key: string]: SortDirection }): SortForCmd { | ||
return Object.keys(t).reduce((acq, key) => { | ||
return { ...acq, [key]: prepareDirection(t[key]) }; | ||
}, {}); | ||
} | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** converts a Sort type into a type that is valid for the server (SortForCmd) */ | ||
export function formatSort( | ||
sort: Sort | undefined, | ||
direction?: SortDirection | ||
): SortForCmd | undefined { | ||
if (sort == null) return undefined; | ||
if (Array.isArray(sort) && !sort.length) return undefined; | ||
if (typeof sort === 'object' && !Object.keys(sort).length) return undefined; | ||
if (typeof sort === 'string') return { [sort]: prepareDirection(direction) }; | ||
if (isPair(sort)) return pairToObject(sort); | ||
if (isDeep(sort)) return deepToObject(sort); | ||
if (Array.isArray(sort)) return stringsToObject(sort); | ||
if (typeof sort === 'object') return validate(sort); | ||
nbbeeken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new Error(`Invalid sort format: ${JSON.stringify(sort)}`); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dead code?