Skip to content

Commit

Permalink
Tests, cogent package, updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
barneycarroll committed Nov 3, 2015
1 parent 22254b6 commit 3f2c47a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015","stage-0" ]
}
20 changes: 9 additions & 11 deletions README.md
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' )
```
19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "xet",
"version": "0.0.3",
"version": "1.0.0",
"description": "map::xet( key, factory ) // => If map doesn't have key, set it to the return value of factory. Return value.",
"main": "xet.js",
"main": "xet.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"prepublish": "npm run build && npm run test",
"build": "babel xet.es7.js | uglifyjs -o xet.min.js",
"dev": "chokidar '**/*.js' -c 'npm test'",
"test": "babel-node tests.js | faucet"
},
"repository": {
"type": "git",
Expand All @@ -24,5 +27,13 @@
"bugs": {
"url": "https://github.com/barneycarroll/xet/issues"
},
"homepage": "https://github.com/barneycarroll/xet#readme"
"homepage": "https://github.com/barneycarroll/xet#readme",
"devDependencies": {
"babel-cli": "^6.1.1",
"babel-preset-es2015": "^6.0.15",
"babel-preset-stage-0": "^6.0.15",
"faucet": "0.0.1",
"tape": "^4.2.2",
"uglify-js": "^2.5.0"
}
}
74 changes: 74 additions & 0 deletions tests.js
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()
}
)
}
)


6 changes: 6 additions & 0 deletions xet.es7.js
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 )
}
1 change: 1 addition & 0 deletions xet.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3f2c47a

Please sign in to comment.