Giter Site home page Giter Site logo

redstrike / golden-fleece Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rich-harris/golden-fleece

0.0 2.0 0.0 73 KB

Parse and manipulate JSON5 strings

Home Page: http://golden-fleece.surge.sh/

License: Other

JavaScript 5.21% HTML 19.65% TypeScript 75.14%

golden-fleece's Introduction

golden-fleece

Parse a JSON5 string (like JSON, but less strict).

Why?

For the Svelte REPL, where we want to allow arbitrary data in the bottom right-hand panel, but we also want to update the object without reformatting it as JSON.

Usage

Install it with npm install golden-fleece and import it into your app:

import * as fleece from 'golden-fleece';

fleece.parse(str, [options])

const ast = fleece.parse(`true`);
// { start: 0, end: 4, type: 'Literal', raw: 'true', value: true }

The returned AST is ESTree compliant.

You can optionally pass callbacks that are fired whenever a value or comment is encountered:

const ast = fleece.parse(str, {
	onComment: comment => {
		console.log('got a comment', comment);
	},
	onValue: value => {
		console.log('got a value', value);
	}
});

fleece.evaluate(str)

const { answer } = fleece.evaluate(`{ answer: 42 }`);
answer === 42; // true

fleece.patch(str, value)

This is where it gets fun:

const str = `
	number: 1,
	string: 'yes',
	object: { nested: true },
	array: ['this', 'that', 'the other']
`;

const object = fleece.evaluate(str);
object.number = 42;
object.array[2] = 'EVERYTHING';

fleece.patch(str, object) === `{
	number: 42,
	string: 'yes',
	object: { nested: true },
	array: ['this', 'that', 'EVERYTHING']
}`; // true

Notice that the formatting has been preserved.

fleece.stringify(value, [options])

const object = {
	string: 'hello',
	'quoted-property': 2,
	array: [3, 4]
};

fleece.stringify(object) === `{
	string: "hello",
	"quoted-property": 2,
	array: [
		3,
		4
	]
}`; // true

To indent with spaces instead of tabs, pass spaces: n, where n is the number of spaces at each level of indentation.

fleece.stringify(object, {
	spaces: 2
}) === `{
  string: "hello",
  "quoted-property": 2,
  array: [
    3,
    4
  ]
}`; // true

To prefer single-quotes to double-quotes, pass singleQuotes: true:

fleece.stringify(object, {
	singleQuotes: true
}) === `{
	string: 'hello',
	'quoted-property': 2,
	array: [
		3,
		4
	]
}`; // true

License

LIL

golden-fleece's People

Contributors

rich-harris avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.