Skip to content

Commit

Permalink
fix(kit): maskitoParseNumber should interpret japanese prolonged so…
Browse files Browse the repository at this point in the history
…und mark as pseudo minus
  • Loading branch information
nsbarsukov committed Mar 4, 2024
1 parent fa4f900 commit c6f9f42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 10 additions & 2 deletions projects/kit/src/lib/masks/number/utils/parse-number.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import {CHAR_EM_DASH, CHAR_EN_DASH, CHAR_HYPHEN, CHAR_MINUS} from '../../../constants';
import {
CHAR_EM_DASH,
CHAR_EN_DASH,
CHAR_HYPHEN,
CHAR_JP_HYPHEN,
CHAR_MINUS,
} from '../../../constants';
import {escapeRegExp} from '../../../utils';

export function maskitoParseNumber(maskedNumber: string, decimalSeparator = '.'): number {
const hasNegativeSign = !!maskedNumber.match(
new RegExp(`^\\D*[${CHAR_MINUS}\\${CHAR_HYPHEN}${CHAR_EN_DASH}${CHAR_EM_DASH}]`),
new RegExp(
`^\\D*[${CHAR_MINUS}\\${CHAR_HYPHEN}${CHAR_EN_DASH}${CHAR_EM_DASH}${CHAR_JP_HYPHEN}]`,
),
);
const escapedDecimalSeparator = escapeRegExp(decimalSeparator);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import {CHAR_EM_DASH, CHAR_EN_DASH, CHAR_HYPHEN, CHAR_MINUS} from '../../../../constants';
import {
CHAR_EM_DASH,
CHAR_EN_DASH,
CHAR_HYPHEN,
CHAR_JP_HYPHEN,
CHAR_MINUS,
} from '../../../../constants';
import {maskitoParseNumber} from '../parse-number';

describe('maskitoParseNumber', () => {
Expand Down Expand Up @@ -63,6 +69,10 @@ describe('maskitoParseNumber', () => {
it('can be em-dash', () => {
expect(maskitoParseNumber(`${CHAR_EM_DASH}42`)).toBe(-42);
});

it('can be katakana-hiragana prolonged sound mark', () => {
expect(maskitoParseNumber(`${CHAR_JP_HYPHEN}42`)).toBe(-42);
});
});

it('parses negative integer number when thousand separator is hyphen & minus sign is hyphen', () => {
Expand Down

0 comments on commit c6f9f42

Please sign in to comment.