Skip to content

Commit

Permalink
Add everything!
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq committed Jul 11, 2018
1 parent 1744378 commit 867ce83
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
author-name = WEAREREASONABLEPEOPLE
repo-owner = wearereasonablepeople
repo-name = warped-reducers
source-files = index.mjs
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,
"extends": ["./node_modules/sanctuary-style/eslint-es3.json"],
"parserOptions": {
"sourceType": "module"
},
"overrides": [
{
"files": ["*.md"],
"plugins": ["markdown"],
"env": {"es6": true},
"globals": {"createReducer": false},
"rules": {
"strict": ["off"]
}
}
]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/.nyc_output/
/coverage.lcov
/coverage/
/node_modules/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "10"
after_success: npm run coverage
18 changes: 18 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Contribution Guideline

## Making a contribution

1. Fork the repo if you do not have write access
1. Clone the remote (fork) from GitHub
1. Create a branch named `<yourgithubusername>/<yourfeature>`
1. Make one or more atomic commits
1. Make sure the tests pass locally
1. Create a pull-request on GitHub

## Publishing a new version

1. Make sure you have write access to the module on npm
1. Make sure you have write access to the master branch on GitHub
1. Checkout `master` and make sure it's up to date with the remote
1. Run `npm run release <level>`, where `<level>` can be any of: 'major',
'minor', 'patch', 'premajor', 'preminor', 'prepatch', or 'prerelease'.
63 changes: 63 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//. # Warped Reducers
//.
//. Compile a standard Redux reducer from a brief definition.
//.
//. Works nicely with [Warped Components][1].
//.
//. Usage in Node depends on `--experimental-modules`.
//. With older Node versions, use [std/esm][2].

// createType :: (String, String) -> String
export function createType(namespace, actionName) {
return 'Warped/' + namespace + '/' + actionName;
}

// compileActionTypes :: (String, StrMap Any) -> StrMap String
export function compileActionTypes(namespace, actions) {
var actionTypes = Object.create (null);
Object.keys (actions).forEach (key => {
actionTypes[key] = createType (namespace, key);
});
return actionTypes;
}

// compileReducer :: (String, StrMap b -> a -> a) -> (a, b) -> a
export function compileReducer(namespace, actions) {
const handlers = Object.create (null);
Object.entries (actions).forEach (([key, handler]) => {
handlers[createType (namespace, key)] = handler;
});
return function(state, action) {
return handlers[action.type] ?
handlers[action.type] (action.payload) (state) :
state;
};
}

//. ## API
//.
//# createReducer :: String -> StrMap b -> a -> a -> { reducer :: (a, b) -> a, types :: StrMap String }
//.
//. Given a String representing the namespace, and a StrMap of curried and
//. flipped reducers, returns a Record containing a single reducer, and a new
//. StrMap mapping the original keys to canonical identifiers of the types that
//. the reducer can handle.
//.
//. This is the default export from this module.
//.
//. ```js
//. const setMyProp = myProp => state => Object.assign ({}, {state, myProp});
//. createReducer ('MyNamespace') ({setMyProp});
//. ```
export default createReducer;
export function createReducer(namespace) {
return function(actions) {
return {
reducer: compileReducer (namespace, actions),
types: compileActionTypes (namespace, actions)
};
};
}

//. [1]: https://github.com/wearereasonablepeople/warped-components
//. [2]: https://github.com/standard-things/esm
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "warped-reducers",
"version": "0.0.1",
"description": "Compile a standard Redux reducer from a brief definition",
"main": "index.mjs",
"module": "index.mjs",
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"doctest": "sanctuary-doctest",
"lint": "sanctuary-lint",
"release": "sanctuary-release",
"test": "npm run lint && sanctuary-test && npm run doctest"
},
"repository": {
"type": "git",
"url": "git://github.com/wearereasonablepeople/warped-reducers.git"
},
"files": [
"/index.mjs",
"/LICENSE",
"/package.json",
"/README.md"
],
"author": "Aldwin Vlasblom <aldwin.vlasblom@gmail.com> (https://github.com/Avaq)",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"coveralls": "^3.0.2",
"esm": "^3.0.66",
"nyc": "^12.0.2",
"sanctuary-scripts": "^2.0.0",
"sanctuary-show": "^1.0.0",
"sanctuary-type-classes": "^9.0.0"
}
}
2 changes: 2 additions & 0 deletions scripts/doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
echo 'Doctests cannot run on mjs files. Skipping :('
9 changes: 9 additions & 0 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -euf -o pipefail

source "${BASH_SOURCE%/*}/../node_modules/sanctuary-scripts/functions"

branches="$(get min-branch-coverage)"

node_modules/.bin/nyc --extension .mjs node_modules/.bin/mocha
node_modules/.bin/nyc check-coverage --branches $branches
54 changes: 54 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import assert from 'assert';
import show from 'sanctuary-show';
import Z from 'sanctuary-type-classes';

import {
createType,
compileActionTypes,
compileReducer,
createReducer
} from '..';


function eq(actual) {
return function eqq(expected) {
assert.strictEqual (show (actual), show (expected));
assert.strictEqual (Z.equals (actual, expected), true);
};
}

function mockHandler(payload) {
return function(state) {
return Object.assign ({}, state, {actionPayload: payload});
};
}

test ('createType', function() {
eq (createType ('Test', 'foo'))
('Warped/Test/foo');
});

test ('compileActionTypes', function() {
eq (compileActionTypes ('Test', {})) ({});
eq (compileActionTypes ('Test', {foo: mockHandler, bar: mockHandler}))
({foo: 'Warped/Test/foo', bar: 'Warped/Test/bar'});
});

test ('compileReducer', function() {
var reducer = compileReducer ('Test', {foo: mockHandler});
eq (typeof reducer) ('function');
eq (reducer.length) (2);
eq (reducer ({}, {type: 'NotHandled'})) ({});
eq (reducer ({}, {type: 'Warped/Test/foo', payload: 'test'}))
({actionPayload: 'test'});
});

test ('createReducer', function() {
var result = createReducer ('Test') ({foo: mockHandler});
eq (typeof result) ('object');
eq (typeof result.reducer) ('function');
eq (typeof result.types) ('object');
eq (result.reducer ({}, {type: 'Warped/Test/foo', payload: 'test'}))
({actionPayload: 'test'});
eq (result.types) ({foo: 'Warped/Test/foo'});
});
3 changes: 3 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--bail
--ui tdd
--require esm

0 comments on commit 867ce83

Please sign in to comment.