FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/expression/node/ObjectNode.js at develop · nikolas/mathjs · GitHub
nikolas
/
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
/
expression
/
node
/
ObjectNode.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
207 lines (188 loc) · 6.48 KB
Breadcrumbs
mathjs
/
src
/
expression
/
node
/
ObjectNode.js
Copy path
File metadata and controls
207 lines (188 loc) · 6.48 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
import
{
getSafeProperty
}
from
'../../utils/customs.js'
import
{
factory
}
from
'../../utils/factory.js'
import
{
isNode
}
from
'../../utils/is.js'
import
{
hasOwnProperty
}
from
'../../utils/object.js'
import
{
escape
,
stringify
}
from
'../../utils/string.js'
const
name
=
'ObjectNode'
const
dependencies
=
[
'Node'
]
export
const
createObjectNode
=
/* #__PURE__ */
factory
(
name
,
dependencies
,
(
{
Node
}
)
=>
{
class
ObjectNode
extends
Node
{
/**
*
@constructor
ObjectNode
*
@extends
{
Node
}
* Holds an object with keys/values
*
@param
{
Object.<string, Node>
} [properties] object with key/value pairs
*/
constructor
(
properties
)
{
super
(
)
this
.
properties
=
properties
||
{
}
// validate input
if
(
properties
)
{
if
(
!
(
typeof
properties
===
'object'
)
||
!
Object
.
keys
(
properties
)
.
every
(
function
(
key
)
{
return
isNode
(
properties
[
key
]
)
}
)
)
{
throw
new
TypeError
(
'Object containing Nodes expected'
)
}
}
}
static
name
=
name
get
type
(
)
{
return
name
}
get
isObjectNode
(
)
{
return
true
}
/**
* Compile a node into a JavaScript function.
* This basically pre-calculates as much as possible and only leaves open
* calculations which depend on a dynamic scope with variables.
*
@param
{
Object
} math Math.js namespace with functions and constants.
*
@param
{
Object
} argNames An object with argument names as key and `true`
* as value. Used in the SymbolNode to optimize
* for arguments from user assigned functions
* (see FunctionAssignmentNode) or special symbols
* like `end` (see IndexNode).
*
@return
{
function
} Returns a function which can be called like:
* evalNode(scope: Object, args: Object, context: *)
*/
_compile
(
math
,
argNames
)
{
const
evalEntries
=
{
}
for
(
const
key
in
this
.
properties
)
{
if
(
hasOwnProperty
(
this
.
properties
,
key
)
)
{
// we stringify/parse the key here to resolve unicode characters,
// so you cannot create a key like {"co\\u006Estructor": null}
const
stringifiedKey
=
stringify
(
key
)
const
parsedKey
=
JSON
.
parse
(
stringifiedKey
)
const
prop
=
getSafeProperty
(
this
.
properties
,
key
)
evalEntries
[
parsedKey
]
=
prop
.
_compile
(
math
,
argNames
)
}
}
return
function
evalObjectNode
(
scope
,
args
,
context
)
{
const
obj
=
{
}
for
(
const
key
in
evalEntries
)
{
if
(
hasOwnProperty
(
evalEntries
,
key
)
)
{
obj
[
key
]
=
evalEntries
[
key
]
(
scope
,
args
,
context
)
}
}
return
obj
}
}
/**
* Execute a callback for each of the child nodes of this node
*
@param
{
function(child: Node, path: string, parent: Node)
} callback
*/
forEach
(
callback
)
{
for
(
const
key
in
this
.
properties
)
{
if
(
hasOwnProperty
(
this
.
properties
,
key
)
)
{
callback
(
this
.
properties
[
key
]
,
'properties['
+
stringify
(
key
)
+
']'
,
this
)
}
}
}
/**
* Create a new ObjectNode whose children are the results of calling
* the provided callback function for each child of the original node.
*
@param
{
function(child: Node, path: string, parent: Node): Node
} callback
*
@returns
{
ObjectNode
} Returns a transformed copy of the node
*/
map
(
callback
)
{
const
properties
=
{
}
for
(
const
key
in
this
.
properties
)
{
if
(
hasOwnProperty
(
this
.
properties
,
key
)
)
{
properties
[
key
]
=
this
.
_ifNode
(
callback
(
this
.
properties
[
key
]
,
'properties['
+
stringify
(
key
)
+
']'
,
this
)
)
}
}
return
new
ObjectNode
(
properties
)
}
/**
* Create a clone of this node, a shallow copy
*
@return
{
ObjectNode
}
*/
clone
(
)
{
const
properties
=
{
}
for
(
const
key
in
this
.
properties
)
{
if
(
hasOwnProperty
(
this
.
properties
,
key
)
)
{
properties
[
key
]
=
this
.
properties
[
key
]
}
}
return
new
ObjectNode
(
properties
)
}
/**
* Get string representation
*
@param
{
Object
} options
*
@return
{
string
} str
*
@override
*/
_toString
(
options
)
{
const
entries
=
[
]
for
(
const
key
in
this
.
properties
)
{
if
(
hasOwnProperty
(
this
.
properties
,
key
)
)
{
entries
.
push
(
stringify
(
key
)
+
': '
+
this
.
properties
[
key
]
.
toString
(
options
)
)
}
}
return
'{'
+
entries
.
join
(
', '
)
+
'}'
}
/**
* Get a JSON representation of the node
*
@returns
{
Object
}
*/
toJSON
(
)
{
return
{
mathjs
:
name
,
properties
:
this
.
properties
}
}
/**
* Instantiate an OperatorNode from its JSON representation
*
@param
{
Object
} json An object structured like
* `{"mathjs": "ObjectNode", "properties": {...}}`,
* where mathjs is optional
*
@returns
{
ObjectNode
}
*/
static
fromJSON
(
json
)
{
return
new
ObjectNode
(
json
.
properties
)
}
/**
* Get HTML representation
*
@param
{
Object
} options
*
@return
{
string
} str
*
@override
*/
_toHTML
(
options
)
{
const
entries
=
[
]
for
(
const
key
in
this
.
properties
)
{
if
(
hasOwnProperty
(
this
.
properties
,
key
)
)
{
entries
.
push
(
'<span class="math-symbol math-property">'
+
escape
(
key
)
+
'</span>'
+
'<span class="math-operator math-assignment-operator '
+
'math-property-assignment-operator math-binary-operator">'
+
':</span>'
+
this
.
properties
[
key
]
.
toHTML
(
options
)
)
}
}
return
'<span class="math-parenthesis math-curly-parenthesis">{</span>'
+
entries
.
join
(
'<span class="math-separator">,</span>'
)
+
'<span class="math-parenthesis math-curly-parenthesis">}</span>'
}
/**
* Get LaTeX representation
*
@param
{
Object
} options
*
@return
{
string
} str
*/
_toTex
(
options
)
{
const
entries
=
[
]
for
(
const
key
in
this
.
properties
)
{
if
(
hasOwnProperty
(
this
.
properties
,
key
)
)
{
entries
.
push
(
'\\mathbf{'
+
key
+
':} & '
+
this
.
properties
[
key
]
.
toTex
(
options
)
+
'\\\\'
)
}
}
const
tex
=
'\\left\\{\\begin{array}{ll}'
+
entries
.
join
(
'\n'
)
+
'\\end{array}\\right\\}'
return
tex
}
}
return
ObjectNode
}
,
{
isClass
:
true
,
isNode
:
true
}
)
Back
|
FazBrowse Home
|
New Git URL