UNPKG

@appbuckets/react-ui

Version:
36 lines (32 loc) 865 B
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function removeNumberFormatting(str, decimalSeparator) { /** Return null while parsing invalid string */ if (str === undefined || str === null || !str.length) { return null; } /** Remove all invalid char */ var rawString = str.replace( new RegExp( '[^-\\d'.concat( decimalSeparator ? '\\'.concat(decimalSeparator) : '', ']' ), 'g' ), '' ); /** If decimalSeparator exists, transform into '.' */ if (decimalSeparator) { rawString = rawString.replace( new RegExp('\\'.concat(decimalSeparator), 'g'), '.' ); } /** If is invalid return null */ if (!rawString.length || Number.isNaN(+rawString)) { return null; } return +rawString; } exports.removeNumberFormatting = removeNumberFormatting;