0.4.1 ā€¢ Published 2 years ago

js-sexpr v0.4.1

Weekly downloads
2
License
Unlicense
Repository
github
Last release
2 years ago

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

šŸ¤ 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

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

  • options any (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

  • str string 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

  • L any

Returns any

identifier

Create an identifier symbol

Parameters

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

  • e any a node to check
  • id string optional id name to compare to (optional, default undefined)

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')))
// true

Returns boolean true if it is an identifier

isEqual

Compare whether 2 nodes are identical

Parameters

  • a any a node
  • b any another node to compare to

Returns boolean true if they are the same

expression

Create an expression node

Parameters

  • exps rest 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

  • e any a node to check whether it's an expression
  • s json optional expression to compare to (optional, default undefined)

Returns boolean true if it's an expression (and equals the compared expression if provided)

boolean

Create a boolean node with given state

Parameters

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

  • e any a node to check whether it's a boolean
  • b boolean optional state to compare to (optional, default undefined)

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

  • e any 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

  • e any a node to check if it's null

Returns boolean true if it's null

number

Create a number node

Parameters

  • n number value of the new node

Returns number a node with number value

isNumber

Check if a node is a number

Parameters

  • e any a node to check if it's a number, optionally compare to a given value
  • n number an optional value to compare to (optional, default undefined)

Returns boolean true if it's a number (and equals the given value if provided)

string

Create a string node.

Parameters

  • str string 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

  • e any a node to check if it's a string
  • s string optional string to compare to (optional, default undefined)

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

  • e any a node to extract value

Returns any value

first

Get the first child of a node.

Parameters

  • e any a node to get its child

Returns any a child node if exists

second

Get the second child of a node.

Parameters

  • e any a node to get its child

Returns any a child node if exists

third

Get the third child of a node.

Parameters

  • e any 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

  • e any a node to get its child
  • n number position of the child node, starting from 1

Returns any a child node if exists

0.4.1

2 years ago

0.4.0

2 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago