https://github.com/dy/parse-fraction
Parse numerator and denominator from a string
https://github.com/dy/parse-fraction
Last synced: 9 months ago
JSON representation
Parse numerator and denominator from a string
- Host: GitHub
- URL: https://github.com/dy/parse-fraction
- Owner: dy
- License: mit
- Created: 2017-11-13T04:23:45.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-04-10T08:52:27.000Z (about 4 years ago)
- Last Synced: 2025-08-18T05:29:09.862Z (10 months ago)
- Language: JavaScript
- Size: 41 KB
- Stars: 8
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# parse-fraction [](http://github.com/badges/stability-badges) [](https://travis-ci.org/dy/parse-fraction)
Parse numerator and denominator from a string.
[](https://npmjs.org/package/parse-fraction/)
```js
const parseFract = require('parse-fraction')
parseFract('half') // [1, 2]
parseFract('one third') // [1, 3]
parseFract('seven eighths') // [7, 8]
parseFract('¼') // [1, 4]
parseFract('13 / 15') // [13, 15]
parseFract('19 ÷ 21') // [19, 21]
parseFract('full') // [1, 1]
parseFract('one millionth') // [1 , 1000000]
parseFract('five over six') // [5, 6]
parseFract('zero-point-one-two') // [12, 100]
parseFract('.12') // [12, 100]
parseFract('12%') // [12, 100]
parseFract('tau over phi') // [Math.PI * 2, (1 + Math.sqrt(5)) / 2]
parseFract('one hundred twenty-three thousand four hundred fifty-six one hundred twenty-three thousand four hundred fifty-sixths') // [123456, 123456]
parseFract('fifty-fifty') // [50, 50]
```
See [/test](./test.js) for all cases.
## parseFraction(str, locale?)
Return `[numerator, denominator]` couple from a string. Optionally pass locale object for other than `en` locales, see [en locale](https://github.com/dy/parse-fraction/blob/master/en.js) example.
## License
(c) 2017 Dima Yv. MIT License