-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parser): move sort by required to common parsing
- Loading branch information
1 parent
c32eb39
commit 34b3ddb
Showing
4 changed files
with
53 additions
and
19 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { toSortedByRequired } from '../sort'; | ||
|
||
describe('sort', () => { | ||
it.each([ | ||
{ | ||
input: [ | ||
{ id: 'test', isRequired: false }, | ||
{ id: 'test2', isRequired: true }, | ||
{ id: 'test3', isRequired: true }, | ||
], | ||
expected: [ | ||
{ id: 'test2', isRequired: true }, | ||
{ id: 'test3', isRequired: true }, | ||
{ id: 'test', isRequired: false }, | ||
], | ||
}, | ||
{ | ||
input: [ | ||
{ id: 'test', isRequired: false }, | ||
{ id: 'test2', isRequired: false }, | ||
{ id: 'test3', isRequired: true, default: 'something' }, | ||
], | ||
expected: [ | ||
{ id: 'test', isRequired: false }, | ||
{ id: 'test2', isRequired: false }, | ||
{ id: 'test3', isRequired: true, default: 'something' }, | ||
], | ||
}, | ||
])('should sort $input by required to produce $expected', ({ input, expected }) => { | ||
expect(toSortedByRequired(input)).toEqual(expected); | ||
}); | ||
}); |
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,13 @@ | ||
/** | ||
* Sort list of values and ensure that required parameters are first so that we do not generate | ||
* invalid types. Optional parameters cannot be positioned after required ones. | ||
*/ | ||
export function toSortedByRequired<T extends { isRequired: boolean; default?: string }>(values: T[]): T[] { | ||
return values.sort((a, b) => { | ||
const aNeedsValue = a.isRequired && a.default === undefined; | ||
const bNeedsValue = b.isRequired && b.default === undefined; | ||
if (aNeedsValue && !bNeedsValue) return -1; | ||
if (bNeedsValue && !aNeedsValue) return 1; | ||
return 0; | ||
}); | ||
} |
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