js-sexpr v0.4.1
S-Expression in JavaScript
Zero-dependencies. Tree structure as plain JSON. Ideal for custom data transfer format and making DSLs.
ā Overview
š ļø Installation
npm install --save js-sexprš Quick start
TODO: simple parsing example
See API documentation for more reference.
š Author
š¤ Nikyle Nguyen
- Website: https://dephony.com/Nikyle
- Twitter: @NLKNguyen
- Github: @NLKNguyen
- LinkedIn: @NLKNguyen
š¤ Contributing
Give a āļø if this project helped you working with S-Expression easily in JavaScript!
Contributions, issues and feature requests are welcome! Feel free to check issues page.
š Your support is very much appreciated
I create open-source projects on GitHub and continue to develop/maintain as they are helping others. You can integrate and use these projects in your applications for free! You are free to modify and redistribute anyway you like, even in commercial products.
I try to respond to users' feedback and feature requests as much as possible. Obviously, this takes a lot of time and efforts (speaking of mental context-switching between different projects and daily work). Therefore, if these projects help you in your work, and you want to encourage me to continue create, here are a few ways you can support me:
- š¬ Following my blog and social profiles listed above to help me connect with your network
- āļø Starring this project and sharing with others as more users come, more great ideas arrive!
- āļø Donating any amount is a great way to help me work on the projects more regularly!
š License
Copyright Ā© 2020 Nikyle Nguyen
The project is ISC License
API
Table of Contents
- SExpr
SExpr
Class of S-Expression resolver that includes parser, serializer, tree constructors, and tree walker utilities.
Creates an instance of SExpr. Optional options input for configuring default behavior, such as how to recognize null, boolean values as it's up to the programmer to decide the syntax. Nevertheless, here is the default that you can override.
{
truthy: ['true', '#t'],
falsy: ['false', '#f'],
nully: ['null', '#nil']
}Parameters
optionsany (optional, default{})
context
Public field for programmers to store arbitrary data that might be useful for parsing expressions
parse
Parse a S-expression string into a JSON object representing an expression tree
Parameters
strstring S-expression string
Returns json an expression tree in form of list that can include nested lists similar to the structure of the input S-expression
serialize
Serialize an expression tree into an S-expression string
Parameters
Lany
Returns any
identifier
Create an identifier symbol
Parameters
idstring
Examples
const S = new SExpr()
const node = S.expression(S.identifier('a'))
// ['a']Returns string symbol
isIdentifier
Check if a node is an identifier, optionally compare to a given name
Parameters
eany a node to checkidstring optional id name to compare to (optional, defaultundefined)
Examples
const S = new SExpr()
const node = S.expression(S.identifier('a'))
console.log(S.isIdentifier(S.first(node)))
// true
console.log(S.isIdentifier(S.first(node, 'a')))
// trueReturns boolean true if it is an identifier
isEqual
Compare whether 2 nodes are identical
Parameters
aany a nodebany another node to compare to
Returns boolean true if they are the same
expression
Create an expression node
Parameters
expsrest optional initialization list of elements
Returns json a tree node
isExpression
Check if a node is an expression, and optionally compare to a given expression
Parameters
eany a node to check whether it's an expressionsjson optional expression to compare to (optional, defaultundefined)
Returns boolean true if it's an expression (and equals the compared expression if provided)
boolean
Create a boolean node with given state
Parameters
vboolean boolean value
Returns string a node with name corresponding to a boolean value
isBoolean
Check if a node is a boolean value, optionally compare to a given state
Parameters
eany a node to check whether it's a booleanbboolean optional state to compare to (optional, defaultundefined)
Returns boolean true if it's a boolean (and equals the given state if provided)
isTruthy
Check if a node is considered truthy. Anything but an explicit false value is truthy.
Parameters
eany a node to check if it's truthy
Returns boolean true if it's truthy
null
Create a null node.
Returns string a node with name representing null value
isNull
Check if a node is null.
Parameters
eany a node to check if it's null
Returns boolean true if it's null
number
Create a number node
Parameters
nnumber value of the new node
Returns number a node with number value
isNumber
Check if a node is a number
Parameters
eany a node to check if it's a number, optionally compare to a given valuennumber an optional value to compare to (optional, defaultundefined)
Returns boolean true if it's a number (and equals the given value if provided)
string
Create a string node.
Parameters
strstring string value of the node
Returns string a node with string value
isString
Check if a node is a string, optionally compare to a given string.
Parameters
eany a node to check if it's a stringsstring optional string to compare to (optional, defaultundefined)
Returns any true if it's a string (and equals the given string if provided)
valueOf
Get a value content of a symbol (not expression).
Parameters
eany a node to extract value
Returns any value
first
Get the first child of a node.
Parameters
eany a node to get its child
Returns any a child node if exists
second
Get the second child of a node.
Parameters
eany a node to get its child
Returns any a child node if exists
third
Get the third child of a node.
Parameters
eany a node to get its child
Returns any a child node if exists
nth
Get the n-th child of a node. Similar to the shorthand first, second, third, fourth, fifth ... tenth, but at any position provided.
Parameters
eany a node to get its childnnumber position of the child node, starting from 1
Returns any a child node if exists