[Functional Programming] propSatisfies with implies

// implies :: ((a -> Boolean), (a -> Boolean)) -> a -> Boolean
const implies = (p, q) =>
  ifElse(
    p,
    compose(
      Boolean,
      q
    ),
    constant(true)
  );

// hasLEngth :: a -> Boolean
const hasLength = compose(
  Boolean,
  length
);
// isLarge :: a -> Boolean
const isLarge = propSatisfies(flip(gt, 3), "length");
const arrayWithLength = implies(isArray, hasLength);
const isLargeString = implies(isString, isLarge);

/**
 * isValidStringOrArray is week can check array has length
 * or string is large, only for those two types
 * other types, such as number, objet, it return false
 */
const isValidStringOrArray = allPass([
  or(isString, isArray),
  arrayWithLength,
  isLargeString
]);

log(isLargeString(undefined)); // true
log(arrayWithLength(undefined)); // true
log(isValidStringOrArray(undefined)); // false
log(isValidStringOrArray({})); // false
log(isValidStringOrArray([1, 2])); // true
log(isValidStringOrArray("fwe")); // false
log(isValidStringOrArray("fwef")); // true

  

Crocks.js has the implementation, no need to do it yourself.

https://evilsoft.github.io/crocks/docs/functions/logic-functions.html#implies

posted @ 2019-05-17 23:00  Zhentiw  阅读(172)  评论(0编辑  收藏  举报