FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/function/utils/numeric.js at develop · CalmWeb/mathjs · GitHub
CalmWeb
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
src
/
function
/
utils
/
numeric.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
74 lines (68 loc) · 2.49 KB
Breadcrumbs
mathjs
/
src
/
function
/
utils
/
numeric.js
Copy path
File metadata and controls
74 lines (68 loc) · 2.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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 4
* math.numeric('4', 'number') // returns 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 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
=
'number'
,
check
)
{
if
(
check
!==
undefined
)
{
throw
new
SyntaxError
(
'numeric() takes one or two arguments'
)
}
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
)
}
}
}
)
Back
|
FazBrowse Home
|
New Git URL