Skip to content

Commit

Permalink
$mol_number dynamic mask
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 24, 2024
1 parent 29e7d0e commit dc71771
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions number/number.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,29 @@ namespace $.$$ {

}

@ $mol_mem
static formatter() {
return new Intl.NumberFormat('en-EN')
}
format(num_str: string) {
let result = ''

formatter() { return this.$.$mol_number.formatter() }
for (let i = num_str.length - 1; i >= 0; i--) {
result += '_'
if ((i % 3) === 0) result += ' '
}
return result.trim()

}

override mask(val: string) {
if (! val) return ''
const current = Number(val.replace(/,/g, '.').replace(/[^\d\.\-]/g, ''))
if (Number.isNaN(current)) return ''
return this.formatter().format(current).replace(',', ' ').replace(/[^\s]/g, '_')
// const str = val.replace(/,/g, '.').replace(/[^\d\.\-]/g, '')
// const [ main = '', frac = ''] = str.split('.') ?? []

const [_, main = '', frac = ''] = val.match(/(\-?\d+)(?:\.?(\d+))?/) ?? []

const prefix = this.format(main)
if (! frac) return prefix

const suffix = this.format(frac).split('').reverse().join('')

return prefix + '_' + suffix
}

@ $mol_mem
Expand Down

0 comments on commit dc71771

Please sign in to comment.