UNPKG

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

251 lines (201 loc) 10.5 kB
'use strict'; exports.__esModule = true; exports.isEffect = exports.isEnhancer = exports.isSpecOrFunction = exports.hasNestedProp = exports.isAction = exports.isPrimitiveish = exports.isStringieThingie = exports.isGoodString = exports.isNotBlankString = exports.isNotEmpty = exports.isNotNil = exports.isPlainObj = exports.isPromise = exports.isValidPropName = undefined; var _all = require('ramda/src/all'); var _all2 = _interopRequireDefault(_all); var _curry = require('ramda/src/curry'); var _curry2 = _interopRequireDefault(_curry); var _pathSatisfies = require('ramda/src/pathSatisfies'); var _pathSatisfies2 = _interopRequireDefault(_pathSatisfies); var _anyPass = require('ramda/src/anyPass'); var _anyPass2 = _interopRequireDefault(_anyPass); var _either = require('ramda/src/either'); var _either2 = _interopRequireDefault(_either); var _allPass = require('ramda/src/allPass'); var _allPass2 = _interopRequireDefault(_allPass); var _is = require('ramda/src/is'); var _is2 = _interopRequireDefault(_is); var _both = require('ramda/src/both'); var _both2 = _interopRequireDefault(_both); var _not = require('ramda/src/not'); var _not2 = _interopRequireDefault(_not); var _isEmpty = require('ramda/src/isEmpty'); var _isEmpty2 = _interopRequireDefault(_isEmpty); var _isNil = require('ramda/src/isNil'); var _isNil2 = _interopRequireDefault(_isNil); var _complement = require('ramda/src/complement'); var _complement2 = _interopRequireDefault(_complement); var _defaultTo = require('ramda/src/defaultTo'); var _defaultTo2 = _interopRequireDefault(_defaultTo); var _path = require('ramda/src/path'); var _path2 = _interopRequireDefault(_path); var _equals = require('ramda/src/equals'); var _equals2 = _interopRequireDefault(_equals); var _compose = require('ramda/src/compose'); var _compose2 = _interopRequireDefault(_compose); var _test = require('ramda/src/test'); var _test2 = _interopRequireDefault(_test); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Checks to see if a prop name is an alphanumeric string (plus some symbols * allowed, like underscores, dashes and dots) * * @func * @sig a -> Boolean * @param {String} prop A prop name to check for formatting * @returns {Boolean} whether or not the prop name passed validation */ var isValidPropName = exports.isValidPropName = (0, _test2.default)(/^(?:[A-Z])([A-Z0-9_\-.]+)([A-Z0-9])$/i); /** * Checks to see if a given value is a JavaScript Promise * * @func * @sig * -> Boolean * @param {*} val A value to check to see if is a Promise * @returns {Boolean} whether or not the val is a Promise */ var isPromise = exports.isPromise = (0, _compose2.default)((0, _equals2.default)('Promise'), (0, _path2.default)(['constructor', 'name']), (0, _defaultTo2.default)('')); /** * Checks to see if a given value is an Object * * @func * @sig * -> Boolean * @param {*} val A value to check for objectishness * @returns {Boolean} whether or not the val is a plain old JS object */ var isPlainObj = exports.isPlainObj = (0, _compose2.default)((0, _equals2.default)('Object'), (0, _path2.default)(['constructor', 'name']), (0, _defaultTo2.default)('')); /** * Checks to make sure a given value isn't null or undefined * * @func * @sig * -> Boolean * @param {*} val A value which may or may not be null / undefined * @returns {Boolean} whether or not the value was non-nil */ var isNotNil = exports.isNotNil = (0, _complement2.default)(_isNil2.default); /** * Checks to make sure a given value isn't empty * * @func * @sig * -> Boolean * @param {*} val A value which may or may not be empty * @returns {Boolean} whether or not the value was non-empty */ var isNotEmpty = exports.isNotEmpty = (0, _complement2.default)(_isEmpty2.default); /** * Checks to see whether or not a given String is non-blank (one or more chars) * * @func * @sig String -> Boolean * @param {String} val A String which may or may not be blank * @returns {Boolean} whether or not a given String is non-blank */ var isNotBlankString = exports.isNotBlankString = (0, _compose2.default)(_not2.default, (0, _test2.default)(/^\s*$/)); /** * Most of the time, this is what you're looking for when checking a String value: * - Is it a String? * - Is it something other than whitespace? * * @func * @sig * -> Boolean * @param {*} val A value that may or may not be a non-blank String * @returns {Boolean} Whether or not the value is a non-blank String */ var isGoodString = exports.isGoodString = (0, _both2.default)((0, _is2.default)(String), isNotBlankString); /** * Checks to see whether or not a given value is a non-blank String (one or more chars) * * @func * @sig * -> Boolean * @param {*} val A value which may or may not be a non-blank String * @returns {Boolean} whether or not a given value is a non-blank String */ var isStringieThingie = exports.isStringieThingie = (0, _allPass2.default)([isNotBlankString, (0, _either2.default)((0, _is2.default)(Number), (0, _is2.default)(String)), isNotNil]); /** * Checks to see whether or not a given value is either: Boolean, Number, String, Data, or RegExp * * @func * @sig * -> Boolean * @param {*} val A value which may or may not be "primitive-ish" * @returns {Boolean} whether or not a given value is "primitive-ish" */ var isPrimitiveish = exports.isPrimitiveish = (0, _anyPass2.default)([(0, _is2.default)(Boolean), (0, _is2.default)(Number), (0, _is2.default)(String), (0, _is2.default)(RegExp), (0, _is2.default)(Date)]); /** * Check that action is an object with a "type" prop that is a non-blank string * * @func * @sig * -> Boolean * @param {*} val A value which may or may not be a Redux action * @returns {Boolean} */ var isAction = exports.isAction = (0, _both2.default)(isPlainObj, (0, _pathSatisfies2.default)((0, _allPass2.default)([(0, _is2.default)(String), (0, _test2.default)(/\S/)]), ['type'])); /** * Checks to see if a provided object has a given prop (path) * * @func * @sig [String] -> {k: v} -> Boolean * @param {String[]} propPath An array of string values representing the nested path to a prop * @param {Object} obj An object on which a given prop may exist * @returns {Boolean} whether or not the provided prop path exists on the provided object */ var hasNestedProp = exports.hasNestedProp = (0, _curry2.default)(function (propPath, obj) { return (0, _compose2.default)(isNotNil, (0, _path2.default)(propPath))(obj); }); /** * Checks to see if a given value a Function or an Object * (meant to be transformed into a [Shapey](https://github.com/arizonatribe/shapey) spec Function * * @func * @sig * -> Boolean * @param {*} val A value to evaluate as a possible pair of predicate (String/RegExp/Function) & transform/effect Function * @returns {Boolean} whether or not the value is an enhancer */ var isSpecOrFunction = exports.isSpecOrFunction = (0, _anyPass2.default)([(0, _both2.default)((0, _is2.default)(Array), (0, _all2.default)((0, _either2.default)((0, _is2.default)(Function), isPlainObj))), isPlainObj, (0, _is2.default)(Function)]); /** * Checks to see if a given value is an Enhancer, which means it is an Array * that has one of the following as its first index: * - A Regular Expression * - A String value (not blank; should match some Action's type value) * - A predicate function (meant to evaluate a criteria on an Action object) * * For the second index, it checks to see if it is: * - An Object (meant to be transformed into a [Shapey](https://github.com/arizonatribe/shapey) spec Function * - A Function meant to return an Action Object * * @func * @sig * -> Boolean * @param {*} val A value to evaluate as a possible pair of predicate (String/RegExp/Function) & transform/effect Function * @param {Object} obj An object on which a given prop may exist * @returns {Boolean} whether or not the value is a qualifying pre-formed Enhancer */ var isEnhancer = exports.isEnhancer = (0, _allPass2.default)([(0, _is2.default)(Array), (0, _pathSatisfies2.default)((0, _anyPass2.default)([(0, _is2.default)(RegExp), isGoodString, (0, _is2.default)(Function)]), [0]), (0, _pathSatisfies2.default)((0, _either2.default)((0, _is2.default)(Function), isPlainObj), [1])]); /** * Checks to see if a given value is an Array that is meant to be turned into an Effect handler, * which means the first index should be: * - A Regular Expression * - A String value (not blank; should match some Action's type value) * - A predicate function (meant to evaluate a criteria on an Action object) * * For the second index, it checks to see if it is: * - An Object (meant to be transformed into a [Shapey](https://github.com/arizonatribe/shapey) spec Function * - A Function meant to produce some kind of effect * * For the (optional) third index, it checks to see if it is: * - An Object (meant to be transformed into a [Shapey](https://github.com/arizonatribe/shapey) spec Function * - A Function meant to return an Action Object when the effect succeeds * - A String, meant to become the "type" prop on a new Action Object that is * returned when the effect succeeds * * For the (optional) fourth index, it checks to see if it is: * - An Object (meant to be transformed into a [Shapey](https://github.com/arizonatribe/shapey) spec Function * - A Function meant to return an Action Object when the effect fails * - A String, meant to become the "type" prop on a new Action Object that is * returned when the effect fails * * @func * @sig * -> Boolean * @param {*} val A value to evaluate as a possible pair of predicate (String/RegExp/Function) & transform/effect Function * @param {Object} obj An object on which a given prop may exist * @returns {Boolean} whether or not the value is a qualifying predicate pair */ var isEffect = exports.isEffect = (0, _allPass2.default)([(0, _is2.default)(Array), (0, _pathSatisfies2.default)((0, _anyPass2.default)([(0, _is2.default)(RegExp), isGoodString, (0, _is2.default)(Function)]), [0]), (0, _pathSatisfies2.default)((0, _either2.default)((0, _is2.default)(Function), isPlainObj), [1]), (0, _pathSatisfies2.default)((0, _anyPass2.default)([_isNil2.default, isPlainObj, isGoodString, (0, _is2.default)(Function)]), [2]), (0, _pathSatisfies2.default)((0, _anyPass2.default)([_isNil2.default, isPlainObj, isGoodString, (0, _is2.default)(Function)]), [3])]);