FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/function/string/print.js at develop · siapble/mathjs · GitHub
siapble
/
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
/
string
/
print.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
93 lines (85 loc) · 2.71 KB
Breadcrumbs
mathjs
/
src
/
function
/
string
/
print.js
Copy path
File metadata and controls
93 lines (85 loc) · 2.71 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict'
const
isString
=
require
(
'../../utils/string'
)
.
isString
const
format
=
require
(
'../../utils/string'
)
.
format
function
factory
(
type
,
config
,
load
,
typed
)
{
/**
* Interpolate values into a string template.
*
* Syntax:
*
* math.print(template, values)
* math.print(template, values, precision)
* math.print(template, values, options)
*
* Example usage:
*
* // the following outputs: 'Lucy is 5 years old'
* math.print('Lucy is $age years old', {age: 5})
*
* // the following outputs: 'The value of pi is 3.141592654'
* math.print('The value of pi is $pi', {pi: math.pi}, 10)
*
* // the following outputs: 'hello Mary! The date is 2013-03-23'
* math.print('Hello $user.name! The date is $date', {
* user: {
* name: 'Mary',
* },
* date: new Date(2013, 2, 23).toISOString().substring(0, 10)
* })
*
* // the following outputs: 'My favorite fruits are apples and bananas !'
* math.print('My favorite fruits are $0 and $1 !', [
* 'apples',
* 'bananas'
* ])
*
* See also:
*
* format
*
*
@param
{
string
} template A string containing variable placeholders.
*
@param
{
Object | Array | Matrix
} values An object or array containing variables
* which will be filled in in the template.
*
@param
{
number | Object
} [options] Formatting options,
* or the number of digits to format numbers.
* See function math.format for a description
* of all options.
*
@return
{
string
} Interpolated string
*/
const
print
=
typed
(
'print'
,
{
// note: Matrix will be converted automatically to an Array
'string, Object | Array'
:
_print
,
'string, Object | Array, number | Object'
:
_print
}
)
print
.
toTex
=
undefined
// use default template
return
print
}
/**
* Interpolate values into a string template.
*
@param
{
string
} template
*
@param
{
Object
} values
*
@param
{
number | Object
} [options]
*
@returns
{
string
} Interpolated string
*
@private
*/
function
_print
(
template
,
values
,
options
)
{
return
template
.
replace
(
/
\$
(
[
\w
.
]
+
)
/
g
,
function
(
original
,
key
)
{
const
keys
=
key
.
split
(
'.'
)
let
value
=
values
[
keys
.
shift
(
)
]
while
(
keys
.
length
&&
value
!==
undefined
)
{
const
k
=
keys
.
shift
(
)
value
=
k
?
value
[
k
]
:
value
+
'.'
}
if
(
value
!==
undefined
)
{
if
(
!
isString
(
value
)
)
{
return
format
(
value
,
options
)
}
else
{
return
value
}
}
return
original
}
)
}
exports
.
name
=
'print'
exports
.
factory
=
factory
Back
|
FazBrowse Home
|
New Git URL