Skip to content

Commit 0f92b24

Browse files
author
Kye Hohenberger
committed
Initial commit 🎉
0 parents  commit 0f92b24

File tree

9 files changed

+217
-0
lines changed

9 files changed

+217
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/coverage
2+
/demo/dist
3+
/dist
4+
/lib
5+
/node_modules
6+
/umd
7+
npm-debug.log
8+
.idea

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
sudo: false
2+
3+
language: node_js
4+
node_js:
5+
- 4
6+
7+
cache:
8+
directories:
9+
- node_modules
10+
11+
before_install:
12+
- npm install codecov.io coveralls
13+
14+
after_success:
15+
- cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js
16+
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
17+
18+
branches:
19+
only:
20+
- master

CONTRIBUTING.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Prerequisites
2+
3+
[Node.js](http://nodejs.org/) must be installed.
4+
5+
## Installation
6+
7+
* Running `npm install` in the module's root directory will install everything you need for development.
8+
9+
## Running Tests
10+
11+
* `npm test` will run the tests once.
12+
13+
## Building
14+
15+
* `npm run build` will build the module for publishing to npm.
16+
* `npm run clean` will delete built resources.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Kye Hohenberger
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# smitty
2+
3+
[![npm version](https://badge.fury.io/js/smitty.svg)](https://badge.fury.io/js/smitty)
4+
5+
Tiny flux implementation built on [mitt](https://git.io/mitt)

package.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "smitty",
3+
"amdName": "smitty",
4+
"version": "0.0.1",
5+
"description": "200b flux implementation built on mitt",
6+
"main": "dist/smitty.js",
7+
"umd:main": "dist/smitty.min.js",
8+
"jsnext:main": "src/index.js",
9+
"files": [
10+
"src",
11+
"test",
12+
"dist"
13+
],
14+
"scripts": {
15+
"test": "standard src test && mocha --compilers js:babel-register test/**/*.js",
16+
"build": "npm-run-all clean -p rollup:* -p minify:* -s size",
17+
"clean": "rimraf dist && mkdirp dist",
18+
"rollup:cjs": "rollup -c rollup.config.js -m -f cjs -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_main",
19+
"rollup:umd": "rollup -c rollup.config.js -m -f umd -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_umd_main",
20+
"minify:cjs": "uglifyjs $npm_package_main -cm toplevel -o $npm_package_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_main}.map",
21+
"minify:umd": "uglifyjs $npm_package_umd_main -cm -o $npm_package_umd_main -p relative --in-source-map ${npm_package_umd_main}.map --source-map ${npm_package_umd_main}.map",
22+
"size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size | pretty-bytes)\"",
23+
"release": "npm run build -s && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
24+
},
25+
"peerDependencies": {
26+
"mitt": "^1.0.0"
27+
},
28+
"devDependencies": {
29+
"babel-core": "^6.9.1",
30+
"babel-eslint": "^7.1.1",
31+
"babel-preset-es2015": "^6.9.0",
32+
"babel-preset-stage-0": "^6.5.0",
33+
"babel-register": "^6.9.0",
34+
"expect": "^1.20.2",
35+
"gzip-size-cli": "^1.0.0",
36+
"jsdom": "^9.8.3",
37+
"mocha": "^3.1.2",
38+
"npm-run-all": "^4.0.1",
39+
"preact": "*",
40+
"pretty-bytes-cli": "^2.0.0",
41+
"rollup": "^0.41.4",
42+
"rollup-plugin-buble": "^0.15.0",
43+
"standard": "^8.5.0",
44+
"strip-json-comments-cli": "^1.0.1",
45+
"uglify-js": "^2.7.4",
46+
"undom": "latest"
47+
},
48+
"author": "Kye Hohenberger",
49+
"homepage": "",
50+
"license": "MIT",
51+
"repository": {
52+
"type": "git",
53+
"url": ""
54+
},
55+
"keywords": [],
56+
"babel": {
57+
"presets": [
58+
"es2015",
59+
"stage-0"
60+
]
61+
},
62+
"standard": {
63+
"ignore": [
64+
"/dist/"
65+
]
66+
},
67+
"directories": {
68+
"test": "test"
69+
},
70+
"dependencies": {
71+
"mitt": "^1.0.0",
72+
"npm-run-all": "^4.0.1"
73+
}
74+
}

rollup.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import buble from 'rollup-plugin-buble'
2+
3+
export default { useStrict: false, plugins: [buble()] }

src/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import mitt from 'mitt'
2+
3+
export default function createStore (initialState) {
4+
let state = initialState
5+
let events = mitt()
6+
7+
return {
8+
emit: events.emit,
9+
addReducer (reducer) {
10+
Object.keys(reducer).forEach((type) => {
11+
events.on(type, (e) => {
12+
this.state = reducer[type](this.state, e)
13+
})
14+
})
15+
},
16+
get state () {
17+
return state
18+
},
19+
set state (nextState) {
20+
state = nextState
21+
}
22+
}
23+
}

test/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-env mocha */
2+
import expect from 'expect'
3+
import createStore from '../src/index'
4+
5+
describe('smitty', () => {
6+
it('store is created', () => {
7+
expect(createStore({})).toExist()
8+
})
9+
10+
it('store has a state', () => {
11+
expect(createStore({ foo: 5 }).state).toEqual({ foo: 5 })
12+
})
13+
14+
it('reducer is called with prev state and event data', (done) => {
15+
const store = createStore({ foo: 5 })
16+
store.addReducer({
17+
'foo/ADD': (state, e) => {
18+
expect(state.foo).toBe(5)
19+
expect(e).toEqual({ foo: 'bar' })
20+
done()
21+
}
22+
})
23+
24+
store.emit('foo/ADD', { foo: 'bar' })
25+
})
26+
27+
it('reducers are called in order with updated state', (done) => {
28+
const store = createStore({ foo: 5 })
29+
store.addReducer({
30+
'foo/ADD': (state, e) => {
31+
expect(state.foo).toBe(5)
32+
expect(e).toEqual({ foo: 'bar' })
33+
return { foo: state.foo + 1 }
34+
}
35+
})
36+
37+
store.addReducer({
38+
'foo/ADD': (state, e) => {
39+
expect(state.foo).toBe(6)
40+
expect(e).toEqual({ foo: 'bar' })
41+
done()
42+
}
43+
})
44+
45+
store.emit('foo/ADD', { foo: 'bar' })
46+
})
47+
})

0 commit comments

Comments
 (0)