Skip to content

Commit

Permalink
Merge pull request #1 from wearereasonablepeople/avaq/everything
Browse files Browse the repository at this point in the history
Add everything!
  • Loading branch information
Avaq committed Jul 12, 2018
2 parents 1744378 + f52aa6d commit 530e3e2
Show file tree
Hide file tree
Showing 12 changed files with 257 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
15 changes: 15 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"root": true,
"extends": ["./node_modules/sanctuary-style/eslint-es3.json"],
"parserOptions": {
"sourceType": "module"
},
"overrides": [
{
"files": ["*.md"],
"plugins": ["markdown"],
"env": {"es6": true},
"globals": {"createReducer": false, "noopAction": false}
}
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/.nyc_output/
/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'.
97 changes: 97 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//. # Warped Reducers
//.
//. [![Build Status](https://travis-ci.com/wearereasonablepeople/warped-reducers.svg?branch=master)](https://travis-ci.com/wearereasonablepeople/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;
}

// compileActionTypes :: StrMap String
// -> StrMap (a -> { type :: String, payload :: a })
export function compileActionCreators(types) {
const creators = Object.create (null);
Object.entries (types).forEach (function(entry) {
creators[entry[0]] = function(payload) {
return {type: entry[1], payload: payload};
};
});
return creators;
}

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

//. ## API
//.
//# createReducer :: String -> StrMap (b -> a -> a) -> { types :: StrMap String, actions :: StrMap (b -> { type :: String, payload :: b }), reducer :: (a, b) -> a }
//.
//. 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) {
var types = compileActionTypes (namespace, actions);
return {
types: types,
actions: compileActionCreators (types),
reducer: compileReducer (namespace, actions)
};
};
}

//# noopAction :: a -> b -> b
//.
//. A conviently named function that does nothing to your state.
//.
//. To be used when you need to define an action type which should not affect
//. the state, but can be used as a message to your Redux side-effect handling
//. middleware.
//.
//. ```js
//. > noopAction ({do: 'nothing'}) ({myState: 'whatever'})
//. {myState: 'whatever'}
//. ```
export function noopAction(payload) {
return function(state) {
return state;
};
}

//. [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
67 changes: 67 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import assert from 'assert';
import show from 'sanctuary-show';
import Z from 'sanctuary-type-classes';

import {
createType,
compileActionTypes,
compileReducer,
createReducer,
compileActionCreators,
noopAction
} 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 ('compileActionCreators', function() {
const creators = compileActionCreators ({foo: 'Warped/Foo'});
eq (typeof creators) ('object');
eq (typeof creators.foo) ('function');
eq (creators.foo ('test')) ({type: 'Warped/Foo', payload: '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'});
});

test ('noopAction', function() {
eq (noopAction (null) ({foo: 'bar'})) ({foo: 'bar'});
});
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 530e3e2

Please sign in to comment.