-
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.
- Loading branch information
0 parents
commit cd6c3f9
Showing
11 changed files
with
411 additions
and
0 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,67 @@ | ||
# https://github.com/github/gitignore/blob/master/Node.gitignore | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# next.js build output | ||
.next | ||
|
||
yarn.lock | ||
|
||
lib |
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 @@ | ||
package-lock=false |
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,20 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "8" | ||
- "10" | ||
|
||
after_success: npm run test && npm run coverage | ||
|
||
jobs: | ||
include: | ||
- stage: npm release | ||
node_js: 10 | ||
script: echo "deploying to npm" | ||
deploy: | ||
provider: npm | ||
email: uniquecolesmith@gmail.com | ||
api_key: $NPM_AUTH_TOKEN | ||
skip_cleanup: true | ||
on: | ||
tags: true |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Eason | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,48 @@ | ||
# rematch | ||
|
||
[![NPM version](https://img.shields.io/npm/v/@zcorky/rematch.svg?style=flat)](https://www.npmjs.com/package/@zcorky/rematch) | ||
[![Coverage Status](https://img.shields.io/coveralls/zcorky/rematch.svg?style=flat)](https://coveralls.io/r/zcorky/rematch) | ||
[![Dependencies](https://david-dm.org/@zcorky/rematch/status.svg)](https://david-dm.org/@zcorky/rematch) | ||
[![Build Status](https://travis-ci.com/zcorky/rematch.svg?branch=master)](https://travis-ci.com/zcorky/rematch) | ||
![license](https://img.shields.io/github/license/zcorky/rematch.svg) | ||
[![issues](https://img.shields.io/github/issues/zcorky/rematch.svg)](https://github.com/zcorky/rematch/issues) | ||
|
||
> Stop `switch`, love `rematch` with Redux. | ||
### Install | ||
|
||
``` | ||
$ npm install @zcorky/rematch | ||
``` | ||
|
||
### Usage | ||
|
||
```javascript | ||
import rematch from '@zcorky/rematch'; | ||
|
||
// before | ||
export function app(state, action) { | ||
switch(action.type) { | ||
case '+': | ||
return { ...state, value: state.value + action.payload }; | ||
case '-': | ||
return { ...state, value: state.value - action.payload }; | ||
case 'x': | ||
return { ...state, value: state.value * action.payload }; | ||
case '/': | ||
return { ...state, value: state.value / action.payload }; | ||
// .... | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
// after | ||
export const app = rematch({ | ||
'+': (state, action) => ({ ...state, value: state.value + action.payload }), | ||
'-': (state, action) => ({ ...state, value: state.value - action.payload }), | ||
'x': (state, action) => ({ ...state, value: state.value * action.payload }), | ||
'/': (state, action) => ({ ...state, value: state.value / action.payload }), | ||
// ... | ||
}); | ||
``` |
Empty file.
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,57 @@ | ||
{ | ||
"name": "@zcorky/rematch", | ||
"version": "1.0.0", | ||
"description": "Use rematch instead of siwtch in Redux", | ||
"main": "lib/index.js", | ||
"repository": "https://github.com/zcorky/rematch", | ||
"author": "Zero", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "npm run clean && tsc", | ||
"clean": "rimraf -rf lib", | ||
"test": "NODE_ENV=test nyc mocha test/*.spec.ts --exit --timeout 10000", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"prepublish": "npm run build" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.9.4", | ||
"@types/sinon": "^5.0.2", | ||
"chai": "^4.1.2", | ||
"coveralls": "^3.0.2", | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.0.1", | ||
"rimraf": "^2.6.2", | ||
"sinon": "^6.3.4", | ||
"source-map-support": "^0.5.9", | ||
"ts-node": "^7.0.1", | ||
"tslint": "^5.10.0", | ||
"tslint-eslint-rules": "^5.3.1", | ||
"tslint-jsdoc-rules": "^0.1.2", | ||
"typescript": "^3.1.1" | ||
}, | ||
"files": [ | ||
"lib/" | ||
], | ||
"nyc": { | ||
"check-coverage": true, | ||
"include": [ | ||
"src/*.ts" | ||
], | ||
"extension": [ | ||
".ts" | ||
], | ||
"require": [ | ||
"ts-node/register", | ||
"source-map-support/register" | ||
], | ||
"sourceMap": true, | ||
"instrument": true | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@zcorky/is": "^1.0.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export interface Action { | ||
type: string | ||
} | ||
|
||
export function rematch<S, A extends Action>(mappings: Record<string, (state: S, action: A) => S>) { | ||
|
||
return function(state: S, action: A): S { | ||
const mapping = mappings[action.type]; | ||
if (!mapping) return state; | ||
|
||
return mapping(state, action); | ||
} | ||
} | ||
|
||
export default rematch; |
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,48 @@ | ||
import { expect } from 'chai'; | ||
|
||
import rematch from '../src'; | ||
|
||
describe('classnames', () => { | ||
it('rematch reducer primitive', () => { | ||
type State = number; | ||
interface Action { type: string }; | ||
|
||
const reducer = rematch<State, Action>({ | ||
'+': (state) => state + 1, | ||
'-': (state) => state - 1, | ||
}); | ||
|
||
expect(reducer(1, { type: '+' })).to.equal(2); | ||
expect(reducer(1, { type: '-' })).to.equal(0); | ||
expect(reducer(1, { type: 'x' })).to.equal(1); | ||
expect(reducer(1, { type: '/' })).to.equal(1); | ||
}); | ||
|
||
it('rematch reducer immutable', () => { | ||
type State = Readonly<{ value: number }>; | ||
type Action = Readonly<{ type: string }>; | ||
|
||
const reducer = rematch<State, Action>({ | ||
'+': (state) => ({ ...state, value: state.value + 1 }), | ||
'-': (state) => ({ ...state, value: state.value - 1 }), | ||
// 'x': (state) => (state.value *= 2, state), // @readonly keep immutable | ||
}); | ||
|
||
expect(reducer({ value: 1 }, { type: '+' })).to.deep.equal({ value: 2 }); | ||
expect(reducer({ value: 1 }, { type: '-' })).to.deep.equal({ value: 0 }); | ||
expect(reducer({ value: 1 }, { type: 'x' })).to.deep.equal({ value: 1 }); | ||
expect(reducer({ value: 1 }, { type: '/' })).to.deep.equal({ value: 1 }); | ||
}); | ||
|
||
it('rematch reducer with action.payload', () => { | ||
type State = Readonly<{ step: 'FIRST' | 'SECOND' | 'END' }>; | ||
type Action = Readonly<{ type: string, payload: 'FIRST' | 'SECOND' | 'END' }>; | ||
|
||
const reducer = rematch<State, Action>({ | ||
'app/step': (state, { payload: step }) => ({ ...state, step }), | ||
}); | ||
|
||
expect(reducer({ step: 'FIRST' }, { type: 'app/step', payload: 'SECOND' })).to.deep.equal({ step: 'SECOND' }); | ||
expect(reducer({ step: 'FIRST' }, { type: 'app/step', payload: 'END' })).to.deep.equal({ step: '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,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"moduleResolution": "node", | ||
"module": "commonjs", | ||
"target": "es5", | ||
"strictNullChecks": true, | ||
"noImplicitThis": false, | ||
"noUnusedLocals": true, | ||
"noImplicitAny": false, | ||
"outDir": "lib", | ||
"lib": ["es2015", "es7"], | ||
"declaration": true, | ||
"sourceMap": false, | ||
"removeComments": true | ||
}, | ||
"include": ["src/**/*.ts"] | ||
} |
Oops, something went wrong.