2
0
mirror of https://github.com/tenrok/maska.git synced 2026-06-23 20:40:35 +03:00

feat: allow float input

This commit is contained in:
Alexander Shabunevich
2025-02-01 19:01:19 +03:00
parent 2c37c32fa5
commit 2975377813
2 changed files with 14 additions and 7 deletions
+7 -7
View File
@@ -53,12 +53,12 @@ export class Mask {
this.opts = opts this.opts = opts
} }
masked (value: string): string { masked (value: string | number): string {
return this.process(value, this.findMask(value)) return this.process(String(value), this.findMask(String(value)))
} }
unmasked (value: string): string { unmasked (value: string | number): string {
return this.process(value, this.findMask(value), false) return this.process(String(value), this.findMask(String(value)), false)
} }
isEager (): boolean { isEager (): boolean {
@@ -69,12 +69,12 @@ export class Mask {
return this.opts.reversed === true return this.opts.reversed === true
} }
completed (value: string): boolean { completed (value: string | number): boolean {
const mask = this.findMask(value) const mask = this.findMask(String(value))
if (this.opts.mask == null || mask == null) return false if (this.opts.mask == null || mask == null) return false
const length = this.process(value, mask).length const length = this.process(String(value), mask).length
if (typeof this.opts.mask === 'string') { if (typeof this.opts.mask === 'string') {
return length >= this.opts.mask.length return length >= this.opts.mask.length
+7
View File
@@ -182,3 +182,10 @@ test('NaN check', () => {
expect(mask.masked('.')).toBe('') expect(mask.masked('.')).toBe('')
expect(mask.masked(',')).toBe('') expect(mask.masked(',')).toBe('')
}) })
test('float input', () => {
const mask = new Mask({ number: { locale: 'uk', fraction: 2 } })
expect(mask.masked(1234.56)).toBe('1 234,56')
expect(mask.unmasked(1234.56)).toBe('1234.56')
})