ruddy
Version:
Modularized state-management tools for modern front-end applications. Manage dispatched messages in a clean and predictable way for either small or large scale projects
91 lines (84 loc) • 3.87 kB
JavaScript
import _default19 from 'ramda/src/curry';
import _default18 from 'ramda/src/filter';
import _default17 from 'ramda/src/always';
import _default16 from 'ramda/src/zipObj';
import _default15 from 'ramda/src/map';
import _default14 from 'ramda/src/isEmpty';
import _default13 from 'ramda/src/isNil';
import _default12 from 'ramda/src/either';
import _default11 from 'ramda/src/any';
import _default10 from 'ramda/src/reject';
import _default9 from 'ramda/src/reduce';
import _default8 from 'ramda/src/toPairs';
import _default7 from 'ramda/src/not';
import _default6 from 'ramda/src/allPass';
import _default5 from 'ramda/src/ifElse';
import _default4 from 'ramda/src/is';
import _default3 from 'ramda/src/prop';
import _default2 from 'ramda/src/values';
import _default from 'ramda/src/compose';
import { isPlainObj, isPrimitiveish, listOfPairsToOneObject, coerceToArray } from '../util';
/**
* Retrieves all a duck's namespaced types
*
* @func
* @sig {k: v} -> [String]
* @param {Object} duck An object containing an object of action types, whose values are namespaced
* @returns {String[]} A list of all the namespaced types for a given duck
*/
export var getTypes = _default(_default2, _default3('types'));
/**
* Merges together either two lists or appends a non-list type into another list
*
* @func
* @sig ([a], b) -> [a, b]
* @param {Array} left A list of any type of values
* @param {Array|*} right Either a list of values or any kind of non-list value
* to append to the 'left' list
* @returns {Array|*} a merged list (if 'left' was an array, otherwise 'right')
*/
export var concatOrReplace = function concatOrReplace(left, right) {
return _default4(Array, left) ? [].concat(left, coerceToArray(right)) : right;
};
/**
* A function which validates and then deep freezes an object of constants
*
* @func
* @sig {k: v} -> {k: v}
* @param {Object} constants An Object which should contain only primitive values or arrays of primitives values
* @returns {Object} A frozen Object with only the values which have been validated
*/
export var createConstants = _default5(
/* no nested functions or objects, just primitives or conversion of arrays
* of primitives into simple objects whose keys and vals are the same */
_default6([_default(_default7, isPrimitiveish), _default(_default7, _default4(Array)), _default4(Object)]), _default(Object.freeze, _default9(listOfPairsToOneObject, {}), _default10(_default11(_default12(_default13, _default14))), _default15(function (_ref) {
var name = _ref[0],
value = _ref[1];
if (_default4(Array, value)) {
/* Creates an object whose keys and values are identical */
return [name, Object.freeze(_default16(value.filter(isPrimitiveish), value.filter(isPrimitiveish)))];
} else if (isPlainObj(value)) {
return [name, _default(_default5(_default14, _default17(null), Object.freeze), _default18(isPrimitiveish))(value)];
}
return isPrimitiveish(value) ? [name, value] : null;
}), _default8, _default10(_default14)), _default17({}));
/**
* A function which takes an Object containing namespace and store values and returns
* a function that will formats an array of strings to have the namespace and
* store as prefixes.
*
* @func
* @sig {k: v} -> ([String] -> {k: v})
* @param {Object} p An Object with both a 'namespace' and 'store' string value,
* from which to format each action type
* @returns {Function} A function which takes an array of Strings (action types)
* and creates an Object formatted such that the each key and value matches the string,
* but formatted as 'namespace/store/type'
*/
export var createTypes = _default19(function (_ref2, types) {
var namespace = _ref2.namespace,
store = _ref2.store;
return _default16(types, types.map(function (type) {
return (namespace || '') + '/' + (store || '') + '/' + type;
}));
});