MCPcopy
hub / github.com/josdejong/mathjs / _toString

Method _toString

src/expression/node/OperatorNode.js:396–463  ·  view source on GitHub ↗

* Get string representation. * @param {Object} options * @return {string} str

(options)

Source from the content-addressed store, hash-verified

394 * @return {string} str
395 */
396 _toString (options) {
397 const parenthesis =
398 (options && options.parenthesis) ? options.parenthesis : 'keep'
399 const implicit = (options && options.implicit) ? options.implicit : 'hide'
400 const args = this.args
401 const parens =
402 calculateNecessaryParentheses(this, parenthesis, implicit, args, false)
403
404 if (args.length === 1) { // unary operators
405 const assoc = getAssociativity(this, parenthesis)
406
407 let operand = args[0].toString(options)
408 if (parens[0]) {
409 operand = '(' + operand + ')'
410 }
411
412 // for example for "not", we want a space between operand and argument
413 const opIsNamed = /[a-zA-Z]+/.test(this.op)
414
415 if (assoc === 'right') { // prefix operator
416 return this.op + (opIsNamed ? ' ' : '') + operand
417 } else if (assoc === 'left') { // postfix
418 return operand + (opIsNamed ? ' ' : '') + this.op
419 }
420
421 // fall back to postfix
422 return operand + this.op
423 } else if (args.length === 2) {
424 let lhs = args[0].toString(options) // left hand side
425 let rhs = args[1].toString(options) // right hand side
426 if (parens[0]) { // left hand side in parenthesis?
427 lhs = '(' + lhs + ')'
428 }
429 if (parens[1]) { // right hand side in parenthesis?
430 rhs = '(' + rhs + ')'
431 }
432
433 if (this.implicit &&
434 (this.getIdentifier() === 'OperatorNode:multiply') &&
435 (implicit === 'hide')) {
436 return lhs + ' ' + rhs
437 }
438
439 return lhs + ' ' + this.op + ' ' + rhs
440 } else if ((args.length > 2) &&
441 ((this.getIdentifier() === 'OperatorNode:add') ||
442 (this.getIdentifier() === 'OperatorNode:multiply'))) {
443 const stringifiedArgs = args.map(function (arg, index) {
444 arg = arg.toString(options)
445 if (parens[index]) { // put in parenthesis?
446 arg = '(' + arg + ')'
447 }
448
449 return arg
450 })
451
452 if (this.implicit &&
453 (this.getIdentifier() === 'OperatorNode:multiply') &&

Callers

nothing calls this directly

Calls 5

getIdentifierMethod · 0.95
getAssociativityFunction · 0.90
toStringMethod · 0.65
mapMethod · 0.65

Tested by

no test coverage detected