Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A node module free creating tree data structure from text input

License

NotificationsYou must be signed in to change notification settings

liushuping/freetree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

varfreetree=require('freetree');vartree=freetree.parse(str,settings);

settings has below listed properties

  • leadingChar: optional, defines leading character for the tree, defaulted to '#'
  • compact: optional, output the object in compact mode.

Code examples

Prepare aninput.txt file as above demonstrated.

varfs=require('fs');varfreetree=require('freetree');varstr=fs.readFileSync('input.txt','utf8');vartree=freetree.parse(str);

then, thetree object is an in-memory JavaScript 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"}]}

If thecompact option is set totrue, the object will be compressed in below structure:

{"root":[{"node1":[{"node11":null}]},{"node2":null}]}

Test

Make suremocha is installed globally

npm install mocha -g

Runnpm test to run unit test

License

MIT

About

A node module free creating tree data structure from text input

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp