Repository URL to install this package:
Version:
1.2.21 ▾
|
// @todo import from get
import get from 'lodash/get'
import forOwn from 'lodash/forOwn'
import {
isNaN,
isNaN as isNotEhNumber,
toEmpty,
isEmpty,
isUndefined,
isNull,
EMPTY_STRING,
isNil,
isObj,
isArray,
isNumber,
isString,
toBoolean,
toString,
isSafeInteger,
toNumber,
toArray,
toObject,
isBoolean,
isFunction,
hasDecimals,
isBuiltIn,
} from 'exotic'
// } from 'exotic'
// import { hasOwnProp } from 'exotic/types/attributes/properties'
// import fromArgumentsToArray from 'exotic/types/array/cast/fromArgumentsToArray'
// import { isNil } from 'exotic/types/primitive/nil'
// import { isObj } from 'exotic/types/obj'
// import { fromStringToUint31 } from 'exotic/types/primitive/number'
// import { toTypeTag } from 'exotic/types/kind'
// import { toTypeCode, toStringCode } from './toTypeCode'
const toFloat = (x: any): number => parseInt(x, 10)
const getFloat = (data, path) => {
return toFloat(get(data, path, 0))
}
// autofix('1') => 1
// autofix(10) => 1
// autofix(10) => 10 *
// autofix('10.1') => 10.1
// autofix({eh: '100'}) => {eh: 100}
// autofix(['100']) => [100]
// autofix('false') => false
// autofix('true') => true
// autofix(new Error('eh')) => {}
// autofix(undefined) => ''
// autofix(null) => ''
// autofix(Symbol.for('@@empty')) => ''
// autofix([]) => []
/* eslint-disable max-statements */
function autofixComplex(response) {
const coerced = toEmpty(response)
forOwn(response, (value, key) => {
// @todo fallback
coerced[key] = autofixTypes(value, get(response, key, ''))
})
if (isArray(response)) {
return toArray(coerced)
} else {
return toObject(coerced)
}
}
function autofixTypes(response, fallbackValue = undefined) {
if (isNil(response)) {
return ''
}
if (isBuiltIn(response)) {
return toEmpty(response)
}
if (isFunction(response)) {
return response
}
if (hasDecimals(response)) {
const asFloat = isSafeInteger(response) ? toFloat(response) : 0
if (isNaN(asFloat) && asFloat !== 0) {
return asFloat
} else if (response > 0) {
return response
} else {
return `${response}`
}
// return isSafeInteger(response) ? toFloat(response) : response || 0.00
}
if (isNumber(response)) {
return isSafeInteger(response) ? toNumber(response) : `${response}`
// return toNumber(response)
}
if (isBoolean(response)) {
return toBoolean(response)
}
if (isString(response)) {
return `${response}`
// return toString(response)
}
if (isObj(response)) {
return autofixComplex(response, fallbackValue)
}
throw new TypeError('missing some data! ' + JSON.stringify(response))
}
// @todo could also clone...
function autofixSafe(response, fallbackValue = undefined) {
if (isNaN(response)) {
return -0
}
// main functionality here
if (isNull(response) || isUndefined(response)) {
return ''
}
if (isBuiltIn(response)) {
return toEmpty(response)
}
if (isFunction(response)) {
return response
}
// and if the number is low... beyond MAX_SAFE_INTEGER
// if (isNumber(response)) {
// return toNumber(response)
// }
if (isBoolean(response)) {
return toBoolean(response)
}
if (isString(response)) {
return `${response}`
// return toString(response)
}
if (isObj(response)) {
return autofixComplex(response, fallbackValue)
}
throw new TypeError('missing some data! ' + JSON.stringify(response))
}
export { autofixSafe }
export { autofixComplex, autofixTypes }
export { autofixTypes as autofix }
export default autofixTypes
// if (isSymbol(x)) {
// return asValidator(isSymbol)
// }
// if (isElement(x)) {
// return asValidator(isElement)
// }
// if (isFunction(x)) {
// return asValidator(isSymbol)
// }
// if (isMap(x)) {
// return asValidator(isMap)
// }
// if (isFrozen(x)) {
// return asValidator(isFrozen)
// }