src/parse.test.js
import {parse, Parser} from './parse'
import Decimal from 'decimal.js'
import LDecimal from 'decimal.js-light'
import BDecimal from 'break_infinity.js'
describe('parse', () => {
it('parses suffixes from user input', () => {
expect(parse('10')).toBe(10)
expect(parse('10k')).toBe(10000)
expect(parse('10x')).toBeNaN()
expect(parse('10x', {default: 3})).toBe(3)
expect(parse('10,000')).toBe(10000)
expect(parse('10 thousand')).toBe(10000)
expect(parse('10e3')).toBe(10000)
expect(parse('')).toBeNull()
expect(parse('', {default: 3})).toBe(3)
expect(parse(null)).toBeNull()
expect(parse(undefined)).toBeNull()
expect(parse('0', {default: 3})).toBe(0)
expect(parse('fail')).toBeNaN()
})
it('parses decimal.js', () => {
const parser = new Parser({backend: 'decimal.js'})
const parse = (text, config) => parser.parse(text, config)
expect(parse('10')).toEqual(new Decimal(10))
expect(parse('10k')).toEqual(new Decimal(10000))
expect(() => parse('10x')).toThrow()
expect(parse('10x', {default: new Decimal(3)})).toEqual(new Decimal(3))
expect(parse('10,000')).toEqual(new Decimal(10000))
expect(parse('10 thousand')).toEqual(new Decimal(10000))
expect(parse('10e3')).toEqual(new Decimal(10000))
expect(parse('')).toBeNull()
expect(parse('', {default: new Decimal(3)})).toEqual(new Decimal(3))
expect(parse(null)).toBeNull()
expect(parse(undefined)).toBeNull()
expect(parse('0', {default: new Decimal(3)})).toEqual(new Decimal(0))
expect(() => parse('fail')).toThrow()
})
it('parses decimal.js-light', () => {
const Decimal = LDecimal
const parser = new Parser({backend: 'decimal.js', Decimal})
const parse = (text, config) => parser.parse(text, config)
expect(parse('10')).toEqual(new Decimal(10))
expect(parse('10k')).toEqual(new Decimal(10000))
expect(() => parse('10x')).toThrow()
expect(parse('10x', {default: new Decimal(3)})).toEqual(new Decimal(3))
expect(parse('10,000')).toEqual(new Decimal(10000))
expect(parse('10 thousand')).toEqual(new Decimal(10000))
expect(parse('10e3')).toEqual(new Decimal(10000))
expect(parse('')).toBeNull()
expect(parse('', {default: new Decimal(3)})).toEqual(new Decimal(3))
expect(parse(null)).toBeNull()
expect(parse(undefined)).toBeNull()
expect(parse('0', {default: new Decimal(3)})).toEqual(new Decimal(0))
expect(() => parse('fail')).toThrow()
})
it('parses break_infinity.js', () => {
const Decimal = BDecimal
const parser = new Parser({backend: 'decimal.js', Decimal})
const parse = (text, config) => parser.parse(text, config)
expect(parse('10')).toEqual(new Decimal(10))
expect(parse('10k')).toEqual(new Decimal(10000))
//expect(() => parse('10x')).toThrow()
//expect(parse('10x', {default: new Decimal(3)})).toEqual(new Decimal(3))
expect(parse('10,000')).toEqual(new Decimal(10000))
expect(parse('10 thousand')).toEqual(new Decimal(10000))
expect(parse('10e3')).toEqual(new Decimal(10000))
expect(parse('')).toBeNull()
expect(parse('', {default: new Decimal(3)})).toEqual(new Decimal(3))
expect(parse(null)).toBeNull()
expect(parse(undefined)).toBeNull()
expect(parse('0', {default: new Decimal(3)})).toEqual(new Decimal(0))
//expect(() => parse('fail')).toThrow()
})
})