sexprs

1.1.4 • Public • Published

sexprs

parse and format S-expressions as JavaScript objects

npm install --save sexprs

background

Kicad files are S-expressions, i wanted an easy way to consume and produce these files.

i also think S-expressions are a cool file format, maybe i'll use them for something in the future. 😉

example

var sexprs = Sexprs({
  operators: {
    person: {
      args: ['name']
    },
    location: {
      args: ['lat', 'long']
    },
    likes: {
      hasMany: true
    }
  }
})
var inputString = `
  (person Mikey
    (version 1.0.0)
    (website http://dinosaur.is)
    (location 121 121)
    (likes (name chocolate))
    (likes (name JavaScript))
  )
`
// parse string into object
var object = sexprs.parse(inputString)
var expectedObject = {
  person: {
    name: 'Mikey',
    version: '1.0.0',
    website: 'http://dinosaur.is',
    location: { lat: 121, long: 121 },
    likes: [
      { name: 'chocolate' },
      { name: 'JavaScript' }
    ]
  }
}
t.deepEqual(object, expectedObject)

// stringify object back into string
var string = sexprs.stringify(expectedObject)
var expectedString = inputString + '\n'
t.deepEqual(string, expectedString)

usage

Sexprs = require('sexprs')

sexprs = Sexprs(options)

options is object with keys:

  • operators: an object where
    • keys are names of S-expression operators (the first symbol after an open parentheses)
    • values are objects with possible keys:
      • args: how to interpret indexed arguments as keys
      • hasMany: whether values should be grouped into an array

object = sexprs.parse(string)

string = sexprs.format(object)

license

The Apache License

Copyright © 2018 Michael Williams

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Readme

Keywords

none

Package Sidebar

Install

npm i sexprs

Weekly Downloads

2

Version

1.1.4

License

Apache-2.0

Unpacked Size

13.6 kB

Total Files

5

Last publish

Collaborators

  • ahdinosaur