-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcc2b3d
commit 1d563e8
Showing
8 changed files
with
115 additions
and
0 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,6 @@ | ||
--- | ||
'lit': minor | ||
'lit-html': minor | ||
--- | ||
|
||
Add choose() directive |
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,45 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
/** | ||
* Chooses and evaluates a template function from a list based on matching | ||
* the given `value` to a case. | ||
* | ||
* Cases are structured as `[caseValue, func]`. `value` is matched to | ||
* `caseValue` by strict equality. The first match is selected. Case values | ||
* can be of any type including primitives, objects, and symbols. | ||
* | ||
* This is similar to a switch statement, but as an expression and without | ||
* fallthrough. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* render() { | ||
* return html` | ||
* ${choose(this.section, [ | ||
* ['home', () => <h1>Home</h1>] | ||
* ['about', () => <h1>About</h1>] | ||
* ], | ||
* () => html`<h1>Error</h1>)} | ||
* `; | ||
* } | ||
* ``` | ||
*/ | ||
export const choose = <T, V>( | ||
value: T, | ||
cases: Array<[T, () => V]>, | ||
defaultCase?: () => V | ||
) => { | ||
for (const c of cases) { | ||
const caseValue = c[0]; | ||
if (caseValue === value) { | ||
const fn = c[1]; | ||
return fn(); | ||
} | ||
} | ||
return defaultCase?.(); | ||
}; |
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,48 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
import {choose} from '../../directives/choose.js'; | ||
import {assert} from '@esm-bundle/chai'; | ||
|
||
suite('choose', () => { | ||
test('no cases', () => { | ||
assert.strictEqual(choose(1, []), undefined); | ||
}); | ||
|
||
test('matching cases', () => { | ||
assert.strictEqual(choose(1, [[1, () => 'A']]), 'A'); | ||
assert.strictEqual( | ||
choose(2, [ | ||
[1, () => 'A'], | ||
[2, () => 'B'], | ||
]), | ||
'B' | ||
); | ||
|
||
const a = {}; | ||
const b = {}; | ||
assert.strictEqual( | ||
choose(b, [ | ||
[a, () => 'A'], | ||
[b, () => 'B'], | ||
]), | ||
'B' | ||
); | ||
}); | ||
|
||
test('default case', () => { | ||
assert.strictEqual( | ||
choose( | ||
3, | ||
[ | ||
[1, () => 'A'], | ||
[2, () => 'B'], | ||
], | ||
() => 'C' | ||
), | ||
'C' | ||
); | ||
}); | ||
}); |
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,7 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
export * from 'lit-html/directives/choose.js'; |