Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very similar utilities between paying for college and retirement app #7332

Closed
anselmbradford opened this issue Nov 2, 2022 · 1 comment
Closed

Comments

@anselmbradford
Copy link
Member

Task

This function

function handleStringInput( numberString ) {
if ( typeof numberString === 'number' ) {
return numberString;
}
let signMaker = 1;
const minusPosition = numberString.indexOf( numberString.match( '-' ) );
const digitPosition = numberString.indexOf( numberString.match( /\d/ ) );
const dashPosition = numberString.indexOf( numberString.match( '-' ) );
// If a '-' appears before the first digit, we assume numberString is negative
if ( dashPosition !== -1 && minusPosition < digitPosition ) {
signMaker = -1;
}
// Strip non-numeric values, maintaining periods
numberString = numberString.replace( /[^0-9.]+/g, '' );
/**
* This helper function places commas in the string. It's set up to
* be passed as a parameter to String.replace()
* @param {string} match - The matched substring.
* @param {number} offset - The numeric offset of the matched substring.
* @param {string} full - The full string to be matched against.
* @returns {string} new string to replace.
*/
function replaceCommas( match, offset, full ) {
if ( offset === full.indexOf( '.' ) ) {
return '.';
}
return '';
}
numberString = numberString.replace( /\./g, replaceCommas );
// Get number value of string, then multiply by signMaker and return
return Number( numberString ) * signMaker;
}

and

function stringToNum( numberString ) {
if ( typeof numberString === 'number' ) {
return numberString;
} else if ( typeof numberString !== 'string' ) {
return 0;
}
let signMaker = 1;
const minusPosition = numberString.indexOf( numberString.match( '-' ) );
const digitPosition = numberString.indexOf( numberString.match( /\d/ ) );
// If a '-' appears before the first digit, we assume numberString is negative
if (
numberString.indexOf( numberString.match( '-' ) ) !== -1 &&
minusPosition < digitPosition
) {
signMaker = -1;
}
// Strip non-numeric values, maintaining periods
numberString = numberString.replace( /[^0-9.]+/g, '' );
// Strip any periods after the first
function replaceCommas( match, offset, full ) {
if ( offset === full.indexOf( '.' ) ) {
return '.';
}
return '';
}
numberString = numberString.replace( /\./g, replaceCommas );
// Get number value of string, then multiply by signMaker and return
return Number( numberString ) * signMaker;
}

Are very similar. Perhaps they could be combined into a general utility?

@anselmbradford
Copy link
Member Author

Fixed in #7400

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant