-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sort-objects): handle context-based configurations
- Loading branch information
Showing
6 changed files
with
247 additions
and
29 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
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,44 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { getMatchingContextOptions } from '../utils/get-matching-context-options' | ||
|
||
describe('get-matching-context-options', () => { | ||
describe('`allNamesMatchPattern`', () => { | ||
it('matches the appropriate context options with `allNamesMatchPattern`', () => { | ||
let barContextOptions = buildContextOptions('bar') | ||
let contextOptions = [buildContextOptions('foo'), barContextOptions] | ||
let nodeNames = ['bar1', 'bar2'] | ||
|
||
expect(getMatchingContextOptions({ contextOptions, nodeNames })).toEqual( | ||
barContextOptions, | ||
) | ||
}) | ||
|
||
it('returns `undefined` if no configuration matches', () => { | ||
let contextOptions = [buildContextOptions('foo')] | ||
let nodeNames = ['bar1', 'bar2'] | ||
|
||
expect( | ||
getMatchingContextOptions({ contextOptions, nodeNames }), | ||
).toBeUndefined() | ||
}) | ||
|
||
it('returns the first context options if no filters are entered', () => { | ||
let emptyContextOptions = buildContextOptions() | ||
let contextOptions = [emptyContextOptions, buildContextOptions()] | ||
let nodeNames = ['bar1', 'bar2'] | ||
|
||
expect(getMatchingContextOptions({ contextOptions, nodeNames })).toEqual( | ||
emptyContextOptions, | ||
) | ||
}) | ||
}) | ||
|
||
let buildContextOptions = ( | ||
allNamesMatchPattern?: string, | ||
): { useConfigurationIf: { allNamesMatchPattern?: string } } => ({ | ||
useConfigurationIf: { | ||
...(allNamesMatchPattern ? { allNamesMatchPattern } : {}), | ||
}, | ||
}) | ||
}) |
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,22 @@ | ||
import { matches } from './matches' | ||
|
||
interface Options { | ||
useConfigurationIf?: { | ||
allNamesMatchPattern?: string | ||
} | ||
} | ||
|
||
export let getMatchingContextOptions = ({ | ||
contextOptions, | ||
nodeNames, | ||
}: { | ||
contextOptions: Options[] | ||
nodeNames: string[] | ||
}): undefined | Options => | ||
contextOptions.find(options => { | ||
let allNamesMatchPattern = options.useConfigurationIf?.allNamesMatchPattern | ||
return ( | ||
!allNamesMatchPattern || | ||
nodeNames.every(nodeName => matches(nodeName, allNamesMatchPattern)) | ||
) | ||
}) |