-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests, cogent package, updated README
- Loading branch information
1 parent
22254b6
commit 3f2c47a
Showing
6 changed files
with
108 additions
and
15 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,3 @@ | ||
{ | ||
"presets": [ "es2015","stage-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
# xet | ||
`xet` (pronounced **ʃɛt**) retrieves a map's value for a given key, setting it with a provided factory if it isn't present. | ||
|
||
> Designed for use with ES6 [Maps](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map), and accordingly written in ES6. Recommend importing via [Babel](https://babeljs.io). | ||
This is especially useful when you're writing in a declarative dialect - vritual DOM view templates for example - and want to tersely express setup logic without breaking stride. | ||
|
||
xet is (sometimes set, and then) get. | ||
`xet` is written as a method for maps (weak or otherwise), and benefits from the ES7 bind operator: | ||
|
||
```javascript | ||
map::xet( key, factory ) // => value | ||
``` | ||
|
||
* If `map` doesn't have `key`, execute `factory` and set its return as `key`'s `value`. | ||
* Return `value`. | ||
|
||
Like function memoization based on input, except the cache is part of your application logic. | ||
```es7 | ||
// ES6 | ||
xet.call( map, 'foo', () => 'bar' ) | ||
// ES7 | ||
map::xet( 'foo', () => 'bar' ) | ||
``` |
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,74 @@ | ||
import test from 'tape' | ||
import give from './xet.min' | ||
|
||
test( | ||
"When called on a Map without an entry for the supplied key", | ||
|
||
assert => { | ||
assert.test( | ||
"assigns the output of the supplied factory to that key", | ||
|
||
assert => { | ||
const map = new Map() | ||
|
||
map::give( 'foo', () => 'bar' ) | ||
|
||
assert.equals( map.get( 'foo' ), 'bar' ) | ||
|
||
assert.end() | ||
} | ||
) | ||
|
||
assert.test( | ||
"returns the value", | ||
|
||
assert => { | ||
assert.equals( | ||
new Map()::give( 'foo', () => 'bar' ), | ||
|
||
'bar' | ||
) | ||
|
||
assert.end() | ||
} | ||
) | ||
} | ||
) | ||
|
||
test( | ||
"When called on a Map with an entry at the supplied key", | ||
|
||
assert => { | ||
const map = new Map().set( 'foo', 'bar' ) | ||
|
||
assert.test( | ||
"doesn't execute the supplied factory", | ||
|
||
assert => { | ||
let executed = false | ||
|
||
map::give( 'foo', () => executed = true ) | ||
|
||
assert.equals( executed, false ) | ||
|
||
assert.end() | ||
} | ||
) | ||
|
||
assert.test( | ||
"returns the value assigned to that key", | ||
|
||
assert => { | ||
assert.equals( | ||
map::give( 'foo', () => 'baz' ), | ||
|
||
'bar' | ||
) | ||
|
||
assert.end() | ||
} | ||
) | ||
} | ||
) | ||
|
||
|
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 @@ | ||
export default function xet( key, factory ){ | ||
if( !this.has( key ) ) | ||
this.set( key, factory() ) | ||
|
||
return this.get( key ) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.