all files / src/ helpers.js

100% Statements 58/58
90.32% Branches 56/62
100% Functions 13/13
100% Lines 41/41
1 statement, 1 function, 7 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69         58× 58× 58×     374× 374× 374× 25×   349×   311×         25×     915×     245× 63×     14× 14×   14× 450× 450×           572× 572× 572× 796× 796× 796×     572×     738×  
import React from 'react'
import { FormattedMessage } from 'react-intl'
import format from './format'
 
 
export var DEFAULT_ALLOW_BLANK = false;
 
 
export function regFormat (options, reg, messageId) {
  options.msg = options.msg || options.message || messageId
  options.with = reg
  return format(options)
}
 
export function prepare (ifCond, unlessCond, allowBlank, func) {
  return function (value='', allValues={}) {
    value = '' + value
    if (allowBlank && !value.trim()) {
      return
    }
    if (('function' !== typeof ifCond || ifCond(allValues, value)) &&
        ('function' !== typeof unlessCond || !unlessCond(allValues, value))) {
      return func(value, allValues)
    }
  }
}
 
export function trunc (num) {
  return Math.trunc ? Math.trunc(num) : num < 0 ? Math.ceil(num) : Math.floor(num)
}
 
export function isNumber (num) {
  return !isNaN(num) && '' !== ('' + num).trim()
}
 
export function formatMessage (msg) {
  if (null == msg) return null
  return 'string' === typeof msg ? <FormattedMessage id={msg} /> : <FormattedMessage {...(msg.props || msg)} />
}
 
export function memoize (func) {
  Eif (!func.cache) {
    func.cache = {}
  }
  return function(options) {
    let key = stringify(options)
    return HAS_PROP.call(func.cache, key) ? func.cache[key] : (func.cache[key] = func(options))
  }
}
 
// private
const HAS_PROP = ({}).hasOwnProperty
const TO_STRING = ({}).toString
 
function stringify (options) {
  let arr = []
  let value
  for (var k in options) {
    Eif (HAS_PROP.call(options, k)) {
      value = options[k]
      arr.push(k, React.isValidElement(value) ? stringify(value.props) : isObject(value) ? stringify(value) :  value)
    }
  }
  return arr.toString()
}
 
function isObject(obj) {
  return 'object' === typeof obj && '[object Object]' === TO_STRING.call(obj) && null !== obj;
}