-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (46 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Mask floating point values on input.
* This creates a 'backwards' text entry effect, which enforces
* whatever input format you specify.
* @author Tom McIntosh
* @licence MIT
* @param {string} inputString
* @param {string} mask
* @return {string} outputString - The formatted string
*/
export const floatMask = (inputString, mask) => {
if(!RegExp('^0+.0+$').test(mask)) {
console.warn('Invalid mask. Must be of the form: 0.00, 000.0, 0.000, etc.')
return ''
}
const [preDecimalString, postDecimalString] = mask.split('.')
const postDecimalLength = postDecimalString.length
const preDecimalLength = preDecimalString.length
const minStringLength = postDecimalLength + preDecimalLength
// Remove any decimals and create a copy of the input string
let numbersOnlyInputString = inputString.replace('.', '')
//Trim off 0's from the start
const inputStringArray = numbersOnlyInputString.split('')
let trimmedInputString = ''
let foundFirstInteger = false
inputStringArray.forEach(split => {
if (foundFirstInteger) {
trimmedInputString += split
} else if (split !== '0') {
trimmedInputString = split
foundFirstInteger = true
}
})
// If there are not at least 3 characters, we need to prepend
// 0's to form the price correctly.
// For example: 3 -> 003.
// The string must have a length of >= minStringLength to proceed
// Add 0's
while (trimmedInputString.length < minStringLength) {
trimmedInputString = `0${trimmedInputString}`
}
// Now the length of the string is >= minStringLength, we can splice in a decimal point
const decimalSuffix = trimmedInputString.slice(- postDecimalLength)
const decimalPrefix = trimmedInputString.slice(0, (trimmedInputString.length - postDecimalLength))
return `${decimalPrefix}.${decimalSuffix}`
}