preidman-ccxt
Version:
A JavaScript / Python / PHP cryptocurrency trading library with support for 100+ exchanges
58 lines (40 loc) • 2.45 kB
JavaScript
;
/* ------------------------------------------------------------------------ */
const isNumber = Number.isFinite
, isArray = Array.isArray
, isString = s => (typeof s === 'string')
, isObject = o => (o !== null) && (typeof o === 'object')
, isDictionary = o => (isObject (o) && !isArray (o))
, isStringCoercible = x => (hasProps (x) && x.toString) || isNumber (x)
/* ............................................. */
const hasProps = o => (o !== undefined) &&
(o !== null)
, prop = (o, k) => (isObject (o) ? o[k] : undefined)
, prop2 = (o, k1, k2) => (!isObject (o) ? undefined : ((k1 in o) ? o[k1] : o[k2]))
/* ............................................. */
const asFloat = x => ((isNumber (x) || isString (x)) ? parseFloat (x) : NaN)
, asInteger = x => ((isNumber (x) || isString (x)) ? parseInt (x, 10) : NaN)
/* ............................................. */
module.exports =
{ isNumber
, isArray
, isObject
, isString
, isStringCoercible
, isDictionary
, hasProps
, prop
, asFloat
, asInteger
, safeFloat: (o, k, $default, n = asFloat (prop (o, k))) => isNumber (n) ? n : $default
, safeInteger: (o, k, $default, n = asInteger (prop (o, k))) => isNumber (n) ? n : $default
, safeValue: (o, k, $default, x = prop (o, k) ) => hasProps (x) ? x : $default
, safeString: (o, k, $default, x = prop (o, k) ) => isStringCoercible (x) ? String (x) : $default
// not using safeFloats with an array argument as we're trying to save some cycles here
// we're not using safeFloat3 either because those cases are too rare to deserve their own optimization
, safeFloat2: (o, k1, k2, $default, n = asFloat (prop2 (o, k1, k2))) => isNumber (n) ? n : $default
, safeInteger2: (o, k1, k2, $default, n = asInteger (prop2 (o, k1, k2))) => isNumber (n) ? n : $default
, safeValue2: (o, k1, k2, $default, x = prop2 (o, k1, k2) ) => hasProps (x) ? x : $default
, safeString2: (o, k1, k2, $default, x = prop2 (o, k1, k2) ) => isStringCoercible (x) ? String (x) : $default
}
/* ------------------------------------------------------------------------ */