mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
* Add `.js` extension to source file imports * Specify package `exports` in `package.json` Specify package type as `commonjs` (It's good to be specific) * Move all compiled scripts into `lib` directory Remove ./number.js (You can use the compiled ones in `./lib/*`) Tell node that the `esm` directory is type `module` and enable tree shaking. Remove unused files from packages `files` property * Allow importing of package.json * Make library ESM first * - Fix merge conflicts - Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs` - Refactor unit tests to be able to run with plain nodejs (no transpiling) - Fix browser examples * Fix browser and browserstack tests * Fix running unit tests on Node 10 (which has no support for modules) * Fix node.js examples (those are still commonjs) * Remove the need for `browserBundle.cjs` * Generate minified bundle only * [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963) Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.** - [Release notes](https://github.com/bitinn/node-fetch/releases) - [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md) - [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * Cleanup console.log * Add integration tests to test the entry points (commonjs/esm, full/number only) * Create backward compatibility error messages in the files moved/removed since v8 * Describe breaking changes in HISTORY.md * Bump karma from 5.2.1 to 5.2.2 (#1965) Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2. - [Release notes](https://github.com/karma-runner/karma/releases) - [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md) - [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
72 lines
2.4 KiB
JavaScript
72 lines
2.4 KiB
JavaScript
import { typeOf } from '../../utils/is.js'
|
|
import { factory } from '../../utils/factory.js'
|
|
import { noBignumber, noFraction } from '../../utils/noop.js'
|
|
|
|
const name = 'numeric'
|
|
const dependencies = ['number', '?bignumber', '?fraction']
|
|
|
|
export const createNumeric = /* #__PURE__ */ factory(name, dependencies, ({ number, bignumber, fraction }) => {
|
|
const validInputTypes = {
|
|
string: true,
|
|
number: true,
|
|
BigNumber: true,
|
|
Fraction: true
|
|
}
|
|
|
|
// Load the conversion functions for each output type
|
|
const validOutputTypes = {
|
|
number: (x) => number(x),
|
|
BigNumber: bignumber
|
|
? (x) => bignumber(x)
|
|
: noBignumber,
|
|
Fraction: fraction
|
|
? (x) => fraction(x)
|
|
: noFraction
|
|
}
|
|
|
|
/**
|
|
* Convert a numeric input to a specific numeric type: number, BigNumber, or Fraction.
|
|
*
|
|
* Syntax:
|
|
*
|
|
* math.numeric(x)
|
|
*
|
|
* Examples:
|
|
*
|
|
* math.numeric('4') // returns number 4
|
|
* math.numeric('4', 'number') // returns number 4
|
|
* math.numeric('4', 'BigNumber') // returns BigNumber 4
|
|
* math.numeric('4', 'Fraction') // returns Fraction 4
|
|
* math.numeric(4, 'Fraction') // returns Fraction 4
|
|
* math.numeric(math.fraction(2, 5), 'number') // returns number 0.4
|
|
*
|
|
* See also:
|
|
*
|
|
* number, fraction, bignumber, string, format
|
|
*
|
|
* @param {string | number | BigNumber | Fraction } value
|
|
* A numeric value or a string containing a numeric value
|
|
* @param {string} outputType
|
|
* Desired numeric output type.
|
|
* Available values: 'number', 'BigNumber', or 'Fraction'
|
|
* @return {number | BigNumber | Fraction}
|
|
* Returns an instance of the numeric in the requested type
|
|
*/
|
|
return function numeric (value, outputType) {
|
|
const inputType = typeOf(value)
|
|
|
|
if (!(inputType in validInputTypes)) {
|
|
throw new TypeError('Cannot convert ' + value + ' of type "' + inputType + '"; valid input types are ' + Object.keys(validInputTypes).join(', '))
|
|
}
|
|
if (!(outputType in validOutputTypes)) {
|
|
throw new TypeError('Cannot convert ' + value + ' to type "' + outputType + '"; valid output types are ' + Object.keys(validOutputTypes).join(', '))
|
|
}
|
|
|
|
if (outputType === inputType) {
|
|
return value
|
|
} else {
|
|
return validOutputTypes[outputType](value)
|
|
}
|
|
}
|
|
})
|