FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/utils/string.js at develop · rpeg/mathjs · GitHub
rpeg
/
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
/
utils
/
string.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
211 lines (191 loc) · 5.76 KB
Breadcrumbs
mathjs
/
src
/
utils
/
string.js
Copy path
File metadata and controls
211 lines (191 loc) · 5.76 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import
{
isBigNumber
,
isString
,
typeOf
}
from
'./is'
import
{
format
as
formatNumber
}
from
'./number'
import
{
format
as
formatBigNumber
}
from
'./bignumber/formatter'
/**
* Check if a text ends with a certain string.
*
@param
{
string
} text
*
@param
{
string
} search
*/
export
function
endsWith
(
text
,
search
)
{
const
start
=
text
.
length
-
search
.
length
const
end
=
text
.
length
return
(
text
.
substring
(
start
,
end
)
===
search
)
}
/**
* Format a value of any type into a string.
*
* Usage:
* math.format(value)
* math.format(value, precision)
*
* When value is a function:
*
* - When the function has a property `syntax`, it returns this
* syntax description.
* - In other cases, a string `'function'` is returned.
*
* When `value` is an Object:
*
* - When the object contains a property `format` being a function, this
* function is invoked as `value.format(options)` and the result is returned.
* - When the object has its own `toString` method, this method is invoked
* and the result is returned.
* - In other cases the function will loop over all object properties and
* return JSON object notation like '{"a": 2, "b": 3}'.
*
* Example usage:
* math.format(2/7) // '0.2857142857142857'
* math.format(math.pi, 3) // '3.14'
* math.format(new Complex(2, 3)) // '2 + 3i'
* math.format('hello') // '"hello"'
*
*
@param
{
*
} value Value to be stringified
*
@param
{
Object | number | Function
} [options] Formatting options. See
* lib/utils/number:format for a
* description of the available
* options.
*
@return
{
string
} str
*/
export
function
format
(
value
,
options
)
{
if
(
typeof
value
===
'number'
)
{
return
formatNumber
(
value
,
options
)
}
if
(
isBigNumber
(
value
)
)
{
return
formatBigNumber
(
value
,
options
)
}
// note: we use unsafe duck-typing here to check for Fractions, this is
// ok here since we're only invoking toString or concatenating its values
if
(
looksLikeFraction
(
value
)
)
{
if
(
!
options
||
options
.
fraction
!==
'decimal'
)
{
// output as ratio, like '1/3'
return
(
value
.
s
*
value
.
n
)
+
'/'
+
value
.
d
}
else
{
// output as decimal, like '0.(3)'
return
value
.
toString
(
)
}
}
if
(
Array
.
isArray
(
value
)
)
{
return
formatArray
(
value
,
options
)
}
if
(
isString
(
value
)
)
{
return
'"'
+
value
+
'"'
}
if
(
typeof
value
===
'function'
)
{
return
value
.
syntax
?
String
(
value
.
syntax
)
:
'function'
}
if
(
value
&&
typeof
value
===
'object'
)
{
if
(
typeof
value
.
format
===
'function'
)
{
return
value
.
format
(
options
)
}
else
if
(
value
&&
value
.
toString
(
options
)
!==
{
}
.
toString
(
)
)
{
// this object has a non-native toString method, use that one
return
value
.
toString
(
options
)
}
else
{
const
entries
=
Object
.
keys
(
value
)
.
map
(
key
=>
{
return
'"'
+
key
+
'": '
+
format
(
value
[
key
]
,
options
)
}
)
return
'{'
+
entries
.
join
(
', '
)
+
'}'
}
}
return
String
(
value
)
}
/**
* Stringify a value into a string enclosed in double quotes.
* Unescaped double quotes and backslashes inside the value are escaped.
*
@param
{
*
} value
*
@return
{
string
}
*/
export
function
stringify
(
value
)
{
const
text
=
String
(
value
)
let
escaped
=
''
let
i
=
0
while
(
i
<
text
.
length
)
{
let
c
=
text
.
charAt
(
i
)
if
(
c
===
'\\'
)
{
escaped
+=
c
i
++
c
=
text
.
charAt
(
i
)
if
(
c
===
''
||
'"\\/bfnrtu'
.
indexOf
(
c
)
===
-
1
)
{
escaped
+=
'\\'
// no valid escape character -> escape it
}
escaped
+=
c
}
else
if
(
c
===
'"'
)
{
escaped
+=
'\\"'
}
else
{
escaped
+=
c
}
i
++
}
return
'"'
+
escaped
+
'"'
}
/**
* Escape special HTML characters
*
@param
{
*
} value
*
@return
{
string
}
*/
export
function
escape
(
value
)
{
let
text
=
String
(
value
)
text
=
text
.
replace
(
/
&
/
g
,
'&'
)
.
replace
(
/
"
/
g
,
'"'
)
.
replace
(
/
'
/
g
,
'''
)
.
replace
(
/
<
/
g
,
'<'
)
.
replace
(
/
>
/
g
,
'>'
)
return
text
}
/**
* Recursively format an n-dimensional matrix
* Example output: "[[1, 2], [3, 4]]"
*
@param
{
Array
} array
*
@param
{
Object | number | Function
} [options] Formatting options. See
* lib/utils/number:format for a
* description of the available
* options.
*
@returns
{
string
} str
*/
function
formatArray
(
array
,
options
)
{
if
(
Array
.
isArray
(
array
)
)
{
let
str
=
'['
const
len
=
array
.
length
for
(
let
i
=
0
;
i
<
len
;
i
++
)
{
if
(
i
!==
0
)
{
str
+=
', '
}
str
+=
formatArray
(
array
[
i
]
,
options
)
}
str
+=
']'
return
str
}
else
{
return
format
(
array
,
options
)
}
}
/**
* Check whether a value looks like a Fraction (unsafe duck-type check)
*
@param
{
*
} value
*
@return
{
boolean
}
*/
function
looksLikeFraction
(
value
)
{
return
(
value
&&
typeof
value
===
'object'
&&
typeof
value
.
s
===
'number'
&&
typeof
value
.
n
===
'number'
&&
typeof
value
.
d
===
'number'
)
||
false
}
/**
* Compare two strings
*
@param
{
string
} x
*
@param
{
string
} y
*
@returns
{
number
}
*/
export
function
compareText
(
x
,
y
)
{
// we don't want to convert numbers to string, only accept string input
if
(
!
isString
(
x
)
)
{
throw
new
TypeError
(
'Unexpected type of argument in function compareText '
+
'(expected: string or Array or Matrix, actual: '
+
typeOf
(
x
)
+
', index: 0)'
)
}
if
(
!
isString
(
y
)
)
{
throw
new
TypeError
(
'Unexpected type of argument in function compareText '
+
'(expected: string or Array or Matrix, actual: '
+
typeOf
(
y
)
+
', index: 1)'
)
}
return
(
x
===
y
)
?
0
:
(
x
>
y
?
1
:
-
1
)
}
Back
|
FazBrowse Home
|
New Git URL