Repository URL to install this package:
Version:
1.2.13 ▾
|
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* 1. @todo import from get or at least `get`
* 2. @todo this export should be a preset of exotic, used elsewhere
*/
const get_1 = __importDefault(require("lodash/get"));
const forOwn_1 = __importDefault(require("lodash/forOwn"));
const exotic_1 = require("exotic");
const toFloat = (x) => parseInt(x, 10);
/* eslint-disable max-statements */
function autofixComplex(response, fallbackValue = undefined) {
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;
//# sourceMappingURL=autofix.js.map