Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Darmikon committed Jan 15, 2017
0 parents commit 0c4e078
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": [
"latest",
"stage-0"
],
"compact": true
}
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/*

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Node template
# Logs
logs
*.log
npm-debug.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

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# 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
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ducks-helpers
Utils for ducks in redux

Api is not stable. You can use some ideas in your projects.
`constants()` - generates constants, however if `~` sign presents at the beginning then extra sufixes will be generated

```
import {constants} from 'ducks-helpers'
```

```
export const TYPE = constants('module-name/namespace', [
'~GET_NOTE', //it will create GET_NOTE, GET_NOTE_LOADING, ...
'NORMAL_ACTION' //it will create only itself
])
```
89 changes: 89 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { createAction } from 'redux-actions';

/* --------------------------example--------------------------------
export const TYPE = constants('module-name/namespace', [
'~GET_NOTE', //it will create GET_NOTE, GET_NOTE_LOADING, ...
'NORMAL_ACTION' //it will create only itself
])
------------------------------------------------------------------*/

export function constants(namespace = '', types){
const _SUFFIXES = [
'LOADING',
'PENDING',
'SUCCESS',
'ERROR',
'FAILED',
'CANCELED'
]

if(arguments.length === 1 || Array.isArray(namespace)){
types = namespace;
namespace = '';
}

if(!Array.isArray(types)){
throw 'types should be an array';
}

return types.reduce((obj,name)=>{
if(name.indexOf('~') === 0){
let type = name.slice(1);

obj[type] = (namespace ? `${namespace}/` : '') + `${type}`;

_SUFFIXES.forEach(suf=>{
obj[type + '_' + suf] = (namespace ? `${namespace}/` : '') + `${type}_${suf}`
})
}else{
obj[name] = name;
}

return obj;
},{})
}

//HELPERS
export function loading(state, action){
return { ...state, error: null, loading: true }
}

export function success(state, action){
return { ...state, payload: action.payload, error: null, loading: false }
}

export function error(state, action){
let error = action.payload && action.payload
return { ...state, payload: [], error, loading: false }
}


function camelize(str) {
return str.toLowerCase()
.split('_')
.reduce((output,word)=>{
if(!output){
output = word;
}else{
output += word.charAt(0).toUpperCase() + word.slice(1);
}

return output;
});
}

function isObject(obj) {
return obj === Object(obj);
}

export function actions(TYPES){
if(!isObject(TYPES)){
throw 'TYPES should be an object'
}

return Object.keys(TYPES).reduce((actions,TYPE)=>{
actions[camelize(TYPE)] = createAction(TYPE);

return actions;
},{})
}
7 changes: 7 additions & 0 deletions lib/index.js

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

36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "ducks-helpers",
"version": "1.0.0",
"description": "Utils for redux",
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel index.js --out-dir lib"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Darmikon/ducks-helpers.git"
},
"keywords": [
"redux",
"react"
],
"author": "Roman Yudin",
"license": "ISC",
"bugs": {
"url": "https://github.com/Darmikon/ducks-helpers/issues"
},
"homepage": "https://github.com/Darmikon/ducks-helpers#readme",
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-latest": "^6.16.0",
"babel-preset-stage-0": "^6.16.0"
},
"dependencies": {
"redux-actions": "^1.2.0"
}
}

0 comments on commit 0c4e078

Please sign in to comment.