namespace-constants
Advanced tools
Comparing version 0.5.0 to 1.0.0
114
lib/index.js
@@ -1,34 +0,110 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
/* eslint import/no-commonjs: 0 */ | ||
var defaultOptions = { | ||
separator: ':', | ||
transform: function transform(v) { | ||
return v; | ||
} | ||
separator: ':' | ||
}; | ||
var isPlainObject = function isPlainObject(value) { | ||
if (!value || _typeof(value) !== 'object' || {}.toString.call(value) !== '[object Object]') { | ||
return false; | ||
} | ||
var proto = Object.getPrototypeOf(value); | ||
if (proto === null) { | ||
return true; | ||
} | ||
var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor; | ||
return typeof Ctor === 'function' && Ctor instanceof Ctor && Function.prototype.toString.call(Ctor) === Function.prototype.toString.call(Object); | ||
}; | ||
var constants = function constants() { | ||
var namespace = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0]; | ||
var constants = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? defaultOptions : arguments[2]; | ||
var namespace = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; | ||
var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultOptions; | ||
if (Array.isArray(namespace)) { | ||
constants = namespace; | ||
namespace = ''; | ||
if (Array.isArray(namespace)) { | ||
values = namespace; | ||
namespace = ''; | ||
} | ||
options.separator = options.separator || defaultOptions.separator; | ||
var withNamespace = function withNamespace(value) { | ||
if (namespace) { | ||
return "".concat(namespace).concat(options.separator).concat(value); | ||
} | ||
options.separator = options.separator || defaultOptions.separator; | ||
options.transform = options.transform || defaultOptions.transform; | ||
return value; | ||
}; | ||
// Prevent new properties from being added to it | ||
return Object.freeze(constants.reduce(function (memo, constant) { | ||
var transformedConstant = options.transform(constant); | ||
return _extends({}, memo, _defineProperty({}, transformedConstant, namespace ? '' + namespace + options.separator + transformedConstant : transformedConstant)); | ||
}, {})); | ||
values = values.reduce(function (memo, v) { | ||
if (typeof v === 'function') { | ||
v = v(); | ||
} | ||
return memo.concat(v); | ||
}, []); // Prevent new properties from being added to it | ||
return Object.freeze(values.reduce(function (memo, value) { | ||
if (isPlainObject(value)) { | ||
// ```js | ||
// constants('ns', [ | ||
// 'ADD_TODO', | ||
// 'REMOVE_TODO', | ||
// 'TOGGLE_TODO', | ||
// { | ||
// fetch: ['REQUEST', 'SUCCESS', 'FAILURE'], | ||
// export: ['REQUEST', 'SUCCESS', 'FAILURE'], | ||
// } | ||
// ], { separator: ':' }); | ||
// ``` | ||
// | ||
// will produce the following output: | ||
// | ||
// ```js | ||
// { | ||
// ADD_TODO: 'ns:ADD_TODO', | ||
// REMOVE_TODO: 'ns:REMOVE_TODO', | ||
// TOGGLE_TODO: 'ns:TOGGLE_TODO', | ||
// fetch: { | ||
// REQUEST: 'ns:fetch.REQUEST', | ||
// SUCCESS: 'ns:fetch.SUCCESS', | ||
// FAILURE: 'ns:fetch.FAILURE', | ||
// }, | ||
// export: { | ||
// REQUEST: 'ns:export.REQUEST', | ||
// SUCCESS: 'ns:export.SUCCESS', | ||
// FAILURE: 'ns:export.FAILURE', | ||
// } | ||
// } | ||
// ``` | ||
Object.keys(value).forEach(function (objectKey) { | ||
var objectValue = value[objectKey]; | ||
if (Array.isArray(objectValue)) { | ||
memo[objectKey] = _objectSpread({}, memo[objectKey]); | ||
objectValue.forEach(function (v) { | ||
memo[objectKey][v] = withNamespace("".concat(objectKey, ".").concat(v)); | ||
}); | ||
} else { | ||
memo[objectKey] = withNamespace("".concat(objectKey, ".").concat(objectValue)); | ||
} | ||
}); | ||
} else { | ||
memo[value] = withNamespace(value); | ||
} | ||
return memo; | ||
}, {})); | ||
}; | ||
module.exports = constants; |
{ | ||
"name": "namespace-constants", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"description": "Add namespace to Redux action type constants without name conflicts.", | ||
@@ -14,4 +14,4 @@ "homepage": "https://github.com/cheton/namespace-constants", | ||
"eslint": "eslint src", | ||
"test": "./babel-tap --coverage test/*.js", | ||
"coveralls": "./babel-tap --coverage --coverage-report=text-lcov test/*.js | node_modules/.bin/coveralls" | ||
"test": "tap test/*.js --node-arg=--require --node-arg=@babel/register --node-arg=--require --node-arg=@babel/polyfill", | ||
"coveralls": "tap test/*.js --coverage --coverage-report=text-lcov --nyc-arg=--require --nyc-arg=@babel/register --nyc-arg=--require --nyc-arg=@babel/polyfill | coveralls" | ||
}, | ||
@@ -29,13 +29,15 @@ "author": "Cheton Wu <cheton@gmail.com>", | ||
], | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babel-cli": "^6.9.0", | ||
"babel-core": "^6.9.1", | ||
"babel-eslint": "^6.0.4", | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"coveralls": "^2.11.9", | ||
"eslint": "^2.11.1", | ||
"tap": "^5.7.2" | ||
"@babel/cli": "~7.4.4", | ||
"@babel/core": "~7.4.5", | ||
"@babel/polyfill": "~7.4.4", | ||
"@babel/preset-env": "~7.4.5", | ||
"@babel/register": "^7.4.4", | ||
"@trendmicro/babel-config": "^1.0.0-alpha", | ||
"babel-eslint": "~10.0.1", | ||
"coveralls": "~3.0.3", | ||
"eslint": "~5.16.0", | ||
"eslint-config-recommended": "~4.0.0", | ||
"tap": "~14.2.0" | ||
}, | ||
@@ -42,0 +44,0 @@ "nyc": { |
@@ -15,7 +15,12 @@ # namespace-constants [![build status](https://travis-ci.org/cheton/namespace-constants.svg?branch=master)](https://travis-ci.org/cheton/namespace-constants) [![Coverage Status](https://coveralls.io/repos/github/cheton/namespace-constants/badge.svg?branch=master)](https://coveralls.io/github/cheton/namespace-constants?branch=master) | ||
#### Global Constants | ||
### Global Constants | ||
```js | ||
import constants from 'namespace-constants'; | ||
module.exports = constants([ | ||
export const { | ||
ADD_TODO, | ||
REMOVE_TODO, | ||
TOGGLE_TODO | ||
} = constants([ | ||
'ADD_TODO', | ||
@@ -32,7 +37,12 @@ 'REMOVE_TODO', | ||
#### Namespace Constants | ||
### Namespace Constants | ||
```js | ||
import constants from 'namespace-constants'; | ||
module.exports = constants('todos', [ | ||
export const { | ||
ADD_TODO, | ||
REMOVE_TODO, | ||
TOGGLE_TODO | ||
} = constants('ns', [ | ||
'ADD_TODO', | ||
@@ -43,39 +53,49 @@ 'REMOVE_TODO', | ||
// { | ||
// 'ADD_TODO': 'todos:ADD_TODO', | ||
// 'REMOVE_TODO': 'todos:REMOVE_TODO' | ||
// 'TOGGLE_TODO': 'todos:TOGGLE_TODO' | ||
// 'ADD_TODO': 'ns:ADD_TODO', | ||
// 'REMOVE_TODO': 'ns:REMOVE_TODO' | ||
// 'TOGGLE_TODO': 'ns:TOGGLE_TODO' | ||
// } | ||
``` | ||
You can pass custom separator as below: | ||
#### Mixing with string and object types | ||
```js | ||
module.exports = constants('todos', [ | ||
const result = constants('ns', [ | ||
'ADD_TODO', | ||
'REMOVE_TODO', | ||
'TOGGLE_TODO' | ||
], { separator: '/' }); | ||
'TOGGLE_TODO', | ||
{ | ||
FETCH: ['REQUEST', 'SUCCESS', 'FAILURE'], | ||
EXPORT: 'REQUEST' | ||
} | ||
]); | ||
// { | ||
// 'ADD_TODO': 'todos/ADD_TODO', | ||
// 'REMOVE_TODO': 'todos/REMOVE_TODO' | ||
// 'TOGGLE_TODO': 'todos/TOGGLE_TODO' | ||
// 'ADD_TODO': 'ns:ADD_TODO', | ||
// 'REMOVE_TODO': 'ns:REMOVE_TODO', | ||
// 'TOGGLE_TODO': 'ns:TOGGLE_TODO', | ||
// 'FETCH': { | ||
// 'REQUEST': 'ns:FETCH.REQUEST', | ||
// 'SUCCESS': 'ns:FETCH.SUCCESS', | ||
// 'FAILURE': 'ns:FETCH.FAILURE' | ||
// }, | ||
// 'EXPORT': 'ns:EXPORT.REQUEST' | ||
// } | ||
``` | ||
#### Transform constants | ||
You can pass a custom transform function as well: | ||
#### Use a custom separator | ||
```js | ||
module.exports = constants('todos', [ | ||
'add todo', | ||
'remove todo', | ||
'toggle todo' | ||
], { | ||
separator: '/', | ||
transform: function (v) { | ||
return v.replace(/\ /g, '_').toUpperCase(); | ||
} | ||
}); | ||
export const { | ||
ADD_TODO, | ||
REMOVE_TODO, | ||
TOGGLE_TODO | ||
} = constants('ns', [ | ||
'ADD_TODO', | ||
'REMOVE_TODO', | ||
'TOGGLE_TODO' | ||
], { separator: '/' }); | ||
// { | ||
// 'ADD_TODO': 'todos/ADD_TODO', | ||
// 'REMOVE_TODO': 'todos/REMOVE_TODO' | ||
// 'TOGGLE_TODO': 'todos/TOGGLE_TODO' | ||
// 'ADD_TODO': 'ns/ADD_TODO', | ||
// 'REMOVE_TODO': 'ns/REMOVE_TODO' | ||
// 'TOGGLE_TODO': 'ns/TOGGLE_TODO' | ||
// } | ||
@@ -82,0 +102,0 @@ ``` |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8643
92
0
104
11