redux-autoform-utils
Version:
Common javascript files to all the redux-autoform related projects
44 lines (31 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (numbro) {
if (!numbro) throw Error('\'numbro\' should be truthy');
if (typeof numbro !== 'function') throw new TypeError('You must provide a valid numbro object');
var localizer = {
cultureData: numbro.cultureData(),
parse: function parse(value, format, culture) {
if (format) throw Error('numbro localizer\'s parse method does not support passing the format');
if (culture) throw Error('numbro localizer\'s parse method does not support passing the culture');
if (value === null || value === undefined || value === '') return undefined; // localizers should return undefined for empty inputs and null for invalid inputs
// numbro's unformat is very permissive, so we need to check for a valid number
if (/^[+-]?[0-9,\.]+$/.test(value)) {
// ToDo: improve this regular expression
var result = numbro().unformat(value);
return isNaN(result) ? null : result; // numbro returns NaN sometimes. We need to make sure that the localizers always return null for invalid inputs
}
return null; // localizers should return null for invalid inputs
},
format: function format(value, _format, culture) {
if (culture) throw Error('numbro localizer\'s format method does not support passing the culture');
return numbro(value).format(_format);
}
};
(0, _numberLocalizer.setNumberLocalizer)(localizer);
return localizer;
};
var _numberLocalizer = require('./numberLocalizer');
module.exports = exports['default'];