diff --git a/src/utils.ts b/src/utils.ts index 9c6727a..a8a9f63 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -82,8 +82,8 @@ export function* split(n: number, from: number = 2, to: number = 6) { export function toNumber(value: any): number | undefined { const normalized = String(value) - .replace('−‭', '-') - .replace(/[^0-9-]/gi, ''); + .replace('\u2212', '\u002d') // minus to hyphen-minus + .replace(/[^0-9\u002d]/g, ''); const converted = Number(normalized); return isNaN(converted) ? undefined : converted; } diff --git a/tests/UtilsTest.ts b/tests/UtilsTest.ts new file mode 100644 index 0000000..5b7df9d --- /dev/null +++ b/tests/UtilsTest.ts @@ -0,0 +1,25 @@ +import { it, describe } from 'mocha'; +import { expect } from 'chai'; +import { getNumber } from '../src/utils'; + +describe('Utils', function() { + it('Can parse positive number', function() { + const text = '123'; + expect(getNumber(text)).to.be.equals(123); + }); + + it('Can parse positive number with noise', function() { + const text = ' 123 '; + expect(getNumber(text)).to.be.equals(123); + }); + + it('Can parse negative number', function() { + const text = '-‭146'; + expect(getNumber(text)).to.be.equals(-146); + }); + + it('Can parse negative number with minus sign', function() { + const text = '\u2212132'; + expect(getNumber(text)).to.be.equals(-132); + }); +});