UNPKG

1.01 kBJavaScriptView Raw
1import Fraction from 'fraction.js'
2import { factory } from '../../utils/factory'
3
4const name = 'Fraction'
5const dependencies = []
6
7export const createFractionClass = /* #__PURE__ */ factory(name, dependencies, () => {
8 /**
9 * Attach type information
10 */
11 Fraction.prototype.type = 'Fraction'
12 Fraction.prototype.isFraction = true
13
14 /**
15 * Get a JSON representation of a Fraction containing type information
16 * @returns {Object} Returns a JSON object structured as:
17 * `{"mathjs": "Fraction", "n": 3, "d": 8}`
18 */
19 Fraction.prototype.toJSON = function () {
20 return {
21 mathjs: 'Fraction',
22 n: this.s * this.n,
23 d: this.d
24 }
25 }
26
27 /**
28 * Instantiate a Fraction from a JSON object
29 * @param {Object} json a JSON object structured as:
30 * `{"mathjs": "Fraction", "n": 3, "d": 8}`
31 * @return {BigNumber}
32 */
33 Fraction.fromJSON = function (json) {
34 return new Fraction(json)
35 }
36
37 return Fraction
38}, { isClass: true })