UNPKG

namespace-constants

Version:

Add namespace to Redux action type constants without name conflicts.

142 lines (116 loc) 4.76 kB
"use strict"; 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: ':' }; // Checks if `value` is object-like. A value is object-like if it's not `null` and has a `typeof` result of "object". var isObjectLike = function isObjectLike(value) { return _typeof(value) === 'object' && value !== null; }; // Checks if `value` is a plain object, that is, an object created by the `Object` constructor or one with a `[[Prototype]]` of `null`. var isPlainObject = function isPlainObject(value) { if (!isObjectLike(value) || {}.toString.call(value) !== '[object Object]') { return false; } if (Object.getPrototypeOf(value) === null) { return true; } var proto = value; while (Object.getPrototypeOf(proto) !== null) { proto = Object.getPrototypeOf(proto); } return Object.getPrototypeOf(value) === proto; }; var constants = function constants() { 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 (isObjectLike(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); } return value; }; // If the `values` is not object-like, pass an empty array to the `values`. if (!isObjectLike(values)) { values = []; } // If the `values` is object-like but not an array, it will be wrapped with an array. // // ```js // { // FETCH: ['REQUEST', 'SUCCESS', 'FAILURE'] // } // ``` // // will be converted to: // // ```js // [{ // FETCH: ['REQUEST', 'SUCCESS', 'FAILURE'] // }] // ``` if (!Array.isArray(values)) { values = [values]; } values = values.reduce(function (memo, value) { if (typeof value === 'function') { value = value(); } return memo.concat(value); }, []); // 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(objectKey); } }); } else { memo[value] = withNamespace(value); } return memo; }, {})); }; module.exports = constants;