Coder Social home page Coder Social logo

freetree's Introduction

freetree

A node module for creating tree data structure from text input

build status

This module takes a simplified tree structured input string and build a JavaScript tree object in memory.

A simple tree structured input string (input.txt) example:

#root node
##node1
###node11
##node2

It illustrates a tree as below:

root node
 |-- node1
 |    |--node11
 |-- node2

Conventions

Each line represents a node in the tree, it should begins with the leading character (default is #). The count of leading chracters minus 1 is the level of the node. There should be only 1 root node which has only 1 leading character, and it should be at the first line of the input string.

Usage

var freetree = require('freetree');
var tree = freetree.parse(str, settings);

settings has below listed properties

  • leadingChar: optional, defines leading character for the tree, defaulted to '#'

Code examples

Prepare an input.txt file as above demonstrated.

var fs = require('fs');
var freetree = require('freetree');
var str = fs.readFileSync('input.txt', 'utf8');
var tree = freetree.parse(str);

then, the tree object is an in-memory JavaScript tree object. In this example, the object is in structure:

{
 "level":0,
 "value":"root",
 "nodes":[{
	    "level":1,
	    "value":"node1",
	    "nodes":[{
		       "level":2,
	   	       "value":"node11"
		    }]
	  },
	  {
	    "level":1,
	    "value":"node2"
	  }]
}

Test

Make sure mocha is installed globally

npm install mocha -g

Run npm test to run unit test

License

MIT

freetree's People

Contributors

liushuping avatar

Watchers

James Cloos avatar Rahman Usta 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.