Repository URL to install this package:
Version:
1.2.22 ▾
|
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// @todo import from get
const get_1 = __importDefault(require("lodash/get"));
const forOwn_1 = __importDefault(require("lodash/forOwn"));
const exotic_1 = require("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) => parseInt(x, 10);
const getFloat = (data, path) => {
return toFloat(get_1.default(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 = exotic_1.toEmpty(response);
forOwn_1.default(response, (value, key) => {
// @todo fallback
coerced[key] = autofixTypes(value, get_1.default(response, key, ''));
});
if (exotic_1.isArray(response)) {
return exotic_1.toArray(coerced);
}
else {
return exotic_1.toObject(coerced);
}
}
exports.autofixComplex = autofixComplex;
function autofixTypes(response, fallbackValue = undefined) {
if (exotic_1.isNil(response)) {
return '';
}
if (exotic_1.isBuiltIn(response)) {
return exotic_1.toEmpty(response);
}
if (exotic_1.isFunction(response)) {
return response;
}
if (exotic_1.hasDecimals(response)) {
const asFloat = exotic_1.isSafeInteger(response) ? toFloat(response) : 0;
if (exotic_1.isNaN(asFloat) && asFloat !== 0) {
return asFloat;
}
else if (response > 0) {
return response;
}
else {
return `${response}`;
}
// return isSafeInteger(response) ? toFloat(response) : response || 0.00
}
if (exotic_1.isNumber(response)) {
return exotic_1.isSafeInteger(response) ? exotic_1.toNumber(response) : `${response}`;
// return toNumber(response)
}
if (exotic_1.isBoolean(response)) {
return exotic_1.toBoolean(response);
}
if (exotic_1.isString(response)) {
return `${response}`;
// return toString(response)
}
if (exotic_1.isObj(response)) {
return autofixComplex(response, fallbackValue);
}
throw new TypeError('missing some data! ' + JSON.stringify(response));
}
exports.autofixTypes = autofixTypes;
exports.autofix = autofixTypes;
// @todo could also clone...
function autofixSafe(response, fallbackValue = undefined) {
if (exotic_1.isNaN(response)) {
return -0;
}
// main functionality here
if (exotic_1.isNull(response) || exotic_1.isUndefined(response)) {
return '';
}
if (exotic_1.isBuiltIn(response)) {
return exotic_1.toEmpty(response);
}
if (exotic_1.isFunction(response)) {
return response;
}
// and if the number is low... beyond MAX_SAFE_INTEGER
// if (isNumber(response)) {
// return toNumber(response)
// }
if (exotic_1.isBoolean(response)) {
return exotic_1.toBoolean(response);
}
if (exotic_1.isString(response)) {
return `${response}`;
// return toString(response)
}
if (exotic_1.isObj(response)) {
return autofixComplex(response, fallbackValue);
}
throw new TypeError('missing some data! ' + JSON.stringify(response));
}
exports.autofixSafe = autofixSafe;
exports.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)
// }
//# sourceMappingURL=autofix.js.map