constant
Functions for working with constant values.
When a PENMAN string is parsed to a tree or a graph, constant values
are left as strings or, if the value is missing, as null
. Penman
nevertheless recognizes four datatypes commonly used in PENMAN data:
integers, floats, strings, and symbols. A fifth type, called a "null"
value, is used when an attribute is missing its target, but aside from
robustness measures it is not a supported datatype.
Function: quoteConstant
▸ quoteConstant(constant
): string
Return constant
as a quoted string.
If constant
is null
, this function returns an empty string
constant ('""'
). All other types are cast to a string and
quoted.
Example
import { quoteConstant } from 'penman-js';
console.log(quoteConstant(null)); // Outputs: '""'
console.log(quoteConstant('')); // Outputs: '""'
console.log(quoteConstant('foo')); // Outputs: '"foo"'
console.log(quoteConstant('"foo"')); // Outputs: '"\\"foo\\""'
console.log(quoteConstant(1)); // Outputs: '"1"'
console.log(quoteConstant(1.5)); // Outputs: '"1.5"'
Function: evaluateConstant
▸ evaluateConstant(constantString
): Constant
Evaluate and return the value of constantString
.
If constantString
is null
or an empty symbol (''
), this
function returns null
. An empty string constant ('""'
) returns an empty string (''
).
Symbols are returned unchanged, while strings get quotes removed and escape sequences unescaped.
Note that this means it is impossible to recover the original type of
strings and symbols once they have been evaluated. For integer and
float constants, this function returns the equivalent JavaScript
Number
object.
Example
import { evaluateConstant } from 'penman-js';
console.log(evaluateConstant('-')); // Outputs: '-'
console.log(evaluateConstant('"foo"')); // Outputs: 'foo'
console.log(evaluateConstant('1')); // Outputs: 1
console.log(evaluateConstant('1.2')); // Outputs: 1.2
console.log(evaluateConstant('') === null); // Outputs: true
Function: constantType
▸ constantType(constant_string
): ConstantType
Return the type of constant encoded by constantString
.
Example
import { constantType } from 'penman-js';
console.log(constantType('-')); // Outputs: 'Symbol'
console.log(constantType('"foo"')); // Outputs: 'String'
console.log(constantType('1')); // Outputs: 'Integer'
console.log(constantType('1.2')); // Outputs: 'Float'
console.log(constantType('')); // Outputs: 'Null'