From 2975377813be2a0fee5c0e8238823b072d8e9e62 Mon Sep 17 00:00:00 2001 From: Alexander Shabunevich Date: Sat, 1 Feb 2025 19:01:19 +0300 Subject: [PATCH] feat: allow float input --- src/mask.ts | 14 +++++++------- test/number.test.ts | 7 +++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/mask.ts b/src/mask.ts index e3c141d..43381b3 100644 --- a/src/mask.ts +++ b/src/mask.ts @@ -53,12 +53,12 @@ export class Mask { this.opts = opts } - masked (value: string): string { - return this.process(value, this.findMask(value)) + masked (value: string | number): string { + return this.process(String(value), this.findMask(String(value))) } - unmasked (value: string): string { - return this.process(value, this.findMask(value), false) + unmasked (value: string | number): string { + return this.process(String(value), this.findMask(String(value)), false) } isEager (): boolean { @@ -69,12 +69,12 @@ export class Mask { return this.opts.reversed === true } - completed (value: string): boolean { - const mask = this.findMask(value) + completed (value: string | number): boolean { + const mask = this.findMask(String(value)) 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') { return length >= this.opts.mask.length diff --git a/test/number.test.ts b/test/number.test.ts index d15c5c8..e897d87 100644 --- a/test/number.test.ts +++ b/test/number.test.ts @@ -182,3 +182,10 @@ test('NaN check', () => { 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') +})