UNPKG

english-words-to-numbers

Version:

Convert all words in a string into numbers. Unlike other packages, it can optionally return the entire revised string with Arabic numbers instead of words.

204 lines (194 loc) 5.4 kB
const wtn = require('./index'); const TESTS = [ [ 'simple integer', '2345', '2345' ], [ 'simple decimal', '55.55', '55.55' ], [ 'simple integer in words', 'twenty-three thousand five hundred seventy-six', '23576' ], [ 'simple integer in words, spaces instead of dashes', 'twenty three thousand five hundred seventy six', '23576' ], [ 'simple integer in words, spaces instead of dashes, the word "and"', 'twenty three thousand five hundred and seventy six', '23576' ], [ 'uppercase', 'FIVE HUNDRED AND SEVENTEEN DECIMAL FIFTY-THREE', '517.53' ], [ 'two integers, mixed case', 'Twenty-one million... Three hundred.', '21000000... 300.' ], [ 'simple decimal', 'two million five decimal five', '2000005.5' ], [ 'simple mixed arabic and words', '23 million 3 hundred', '23000300' ], [ 'simple mixed arabic and words + decimal', '22 thousand three hundred point fifty-five', '22300.55' ], [ 'non-numeric words', 'It was somewhere about 3 million, maybe 4 million at most.', 'It was somewhere about 3000000, maybe 4000000 at most.' ], [ 'decimal before thousand, all in words', ', and three point five thousand people gathered there.', ', and 3500 people gathered there.' ], [ 'decimal before million', ', and 3.55 million people gathered there.', ', and 3550000 people gathered there.' ], [ '"seventeen" is not matched by "seven"', ', and seventeen point seventy-five billion dollars, wow.', ', and 17750000000 dollars, wow.' ], [ 'integer before million', '...and more than a hundred fifty million records.', '...and more than 150000000 records.' ], [ 'article "a" is not being replaced incorrectly', 'hi i want a place for 3 million, around eight hundred to a thousand feet', 'hi i want a place for 3000000, around 800 to 1000 feet' ], [ 'decimal before "thousand", "million" plus cents', 'six point six million and five cents', '6600005 cents' ], [ 'decimal before "thousand", "million" plus cents (2)', 'six point six million dollars and five cents', '6600000 dollars and 5 cents' ], [ 'hunreds + million-like word', 'there was as much as A HUNDRED FIFTY MILLION items there', 'there was as much as 150000000 items there' ], [ 'XX hundred', 'the repairs were over 12 hundred dollars', 'the repairs were over 1200 dollars' ], [ 'XX hundred (complex XX)', 'the repairs were over twenty five hundred dollars', 'the repairs were over 2500 dollars' ], [ 'XX hundred (complex XX) plus cents', 'the repairs came up at fourty five hundred point five dollars', 'the repairs came up at 4500.5 dollars' ], [ 'mixed numbers and words in decimals', '3 point five thousand HKD', '3500 HKD' ], [ 'extras - k (1)', 'the total is 2.7k usd', 'the total is 2700 usd' ], [ 'extras - k (2)', 'the total is one hundred and five k usd', 'the total is 105000 usd' ], [ 'extras - grand (1)', 'the total is 2.7 grand', 'the total is 2700' ], [ 'extras - grand (2)', 'the total is three hundred and fifty six grand', 'the total is 356000' ], [ 'extras - mil (1)', 'the total is 2.7 mil', 'the total is 2700000' ], [ 'extras - mil (2)', 'the total is three hundred and fifty six mil', 'the total is 356000000' ], [ 'extras - bn (1)', 'the total is 7 bn people', 'the total is 7000000000 people' ], [ 'extras - bn (2)', 'the total is seven point three bn people', 'the total is 7300000000 people' ], [ 'iphone not converted to iph1', 'iPhone', 'iPhone', ], [ '0 is not removed from string and "k" is not mistakenly recognized as thousand', 'sample0123@example.tk', 'sample0123@example.tk', ] ]; for (const [name, from, to] of TESTS) { test(name, () => { expect(wtn(from, { enableExtras: true })).toBe(to); }); } /* test('long decimal before "thousand", "million"', () => { expect(wtn('three point fourteen thousand one hundred and fifty-nine million', { enableExtras: true })).toBe('3141590'); }); test('long decimal before "thousand", "million" with complex first part of decimal', () => { expect(wtn('three point forty-five thousand one hundred and fifty-nine million', { enableExtras: true })).toBe('3451590'); }); */ // three and a half thousand // five and a half dollars // somewhere between three and five dollars - fail // somewhere between 12 hundred and five grand - fail // i am looking for 2-3 flats - fail // three point fourteen thousand one hundred and fifty-nine million // three point forty-five thousand one hundred and fifty-nine million // more than three grand, and five hundred cars - will nto be supported // ten to the forty-fifth => 10^45 // dozen, half a dozen, two dozen, twelve dozen // one two two point three => 1 2 2.3 - not supported, ok // three point one four one five nine // cannot do big numbers, e.g. tens of trillions due to js limitations