Giter Club home page Giter Club logo

neon-js's Introduction

NEON-JS - NEON decoder and encoder for JavaScript

Build Status NPM version

NEON is very similar to YAML.The main difference is that the NEON supports "entities" (so can be used e.g. to parse phpDoc annotations) and tab characters for indentation. NEON syntax is a little simpler and the parsing is faster.

Example of Neon code:

# my web application config

php:
	date.timezone: Europe/Prague
	zlib.output_compression: yes  # use gzip

database:
	driver: mysql
	username: root
	password: beruska92

users:
	- Dave
	- Kryten
	- Rimmer

Installation:

NEON module for node.js

npm install neon-js

CLI executable is not available yet.

NEON for browser

You can create browser compatible library using browserify.

npm install browserify
browserify -r neon-js -s neon

You can found precompiled browserifed version in repository matej21/neon-js-dist.

<script src="dist/neon.js"></script>
<script type="text/javascript">
var data = neon.decode("hello: world")
</script>

API

var neon = require('neon-js');

try {
	var result = neon.decode("foo: {bar: 123}")
} catch (e) {
	if (e instanceof neon.Error) {
		console.log(e.message);
	}
	throw e;
}

decode(input)

Decodes input NEON string.

var result = neon.decode(input);

encode(var[, options])

Encodes javascript to NEON.

var data = {foo: "bar"}
var result = neon.encode(data);
//or you can use block mode
var result = neon.encode(data, neon.BLOCK);

Entity

Class representing NEON entity.

var entity = neon.decode("Foo(bar)");
entity.value; "Foo";
entity.attributes// neon.Map instance, see bellow

Map

NEON was originally written for PHP where all NEON array-like structures (lists, objects or mixed) were converted to the PHP array. And arrays in PHP are very powerful - they can be treated as a list, hash table and more. neon.Map is trying to keep the very basic of this behaviour - items are ordered (list) and can have string key (hash table). Therefore all NEON array-like structures are mapped to the neon.Map.

#example neon
foo: bar
- lorem
ipsum: dolor
- sit
var map = neon.decode(input);
//map instanceof neon.Map

Map.get(key)

Returns value by the key.

map.get("foo"); // bar
map.get(0); // lorem
map.get("xxx"); //throws an Error

Map.has(key)

Checks if the given key exists in the map.

map.has("foo"); // true
map.has("xxx"); // false

Map.forEach(callable)

Calls provided function for every item in the map. Key is passed as a first argument, value as a second argument.

map.forEach(function (key, value) {

});

Map.isList()

Checks if Map is a list. That means the map doesn't contain any string keys and numeric keys are in range 0..(length-1).

map.isList(); //false

Map.values()

Returns values as an array.

var values = map.values();
// ["bar", "lorem", "dolor", "sit"]

Map.keys()

Returns keys as an array.

var keys = map.keys();
// ["foo", 0, "ipsum", 1]

Map.items()

Returns items structure. This structure is an array where every item is object with properties key and value.

var items = map.items();

// [{key: "foo", value: "bar"}, ...]

Map.toObject([deep])

Converts Map to javascript object. Items order may be lost.

var obj = map.toObject();
// {foo: "bar", 0: "lorem", ipsum: "dolor", 1: sit}

Dumper.toText(data)

Dumps decoded structure into simple text output as you can see in the demo.

var text = neon.Dumper.toText(data)`;

Links:

neon-js's People

Contributors

jkuchar avatar matej21 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.