jbj-parse
JBJ parse module
Last updated 4 years ago by touv .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install jbj-parse 
SYNC missed versions from official npm registry.

JBJ parse module

Parse module of JBJ: file format conversion, through parsing (csv, parseCSV, parseCSVFile, json, parseJSON, xml, parseXML)

Contributors

Installation

$ npm install jbj-parse

Usage

This JBJ module cannot be used alone. JBJ has to be installed.

var JBJ = require('jbj');
JBJ.use(require('jbj-parse'));

Tests

Use mocha to run the tests.

$ npm install
$ npm test

Actions

Once the module is declared as used for JBJ, you can use the following actions:

csv: separator

Pack input to CSV, return string

    var stylesheet = {
        "set" : ["x","y","z"],
        "csv" : ","
    };
    // output : "x,y,z\r\n"

parseCSV: separator

aliases : parseCSVField, fromCSV, uncsv

Parse input as CSV string, return array

    var stylesheet = {
        "set" : "x,y,z",
        "parseCSV": ",",
    };
    // output : ["x","y","z"]

parseCSVFile: separator

alias : fromCSVFile

Parse input as CSV string, return an array of arrays of columns content.

    var stylesheet = {
        "set" : "\"Afghanistan\";\"AFG\"\n\"Aland Islands\";\"ALA\"",
        "parseCSVFile": ";",
    };
    // output : [ [ 'Afghanistan', 'AFG' ], [ 'Aland Islands', 'ALA' ] ]

json: none

alias : toJSON

Pack input to JSON, return string

    var stylesheet = {
        "set" : ["x","y","z"],
        "json": true
    };
    // output : "[\"x\",\"y\",\"z\"]"

Warning: when parsing a whole CSV file, be aware that file line columns can't have space in their names, neither special characters (they must fit in variable names)

parseJSON:

aliases : fromJSON, unjson

Parse input as JSON string, return object

    var stylesheet = {
        "set" : "[\"x\",\"y\",\"z\"]",
        "parseJSON": true
    };
    // output : ["x","y","z"]

xml: options

Pack input to XML, return string

options are detailed in the xml-mapping documentation

    var stylesheet = {
        "set": {
            "root" : {
                "item" : [
                    { "index" : "1", "$t" : "A"},
                    { "index" : "2", "$t" : "B"},
                    { "index" : "3", "$t" : "C"}
                ]
            }
        },
        "xml" : {
            "indent": false
        }
    };
    // output : <root><item index="1">A</item><item index="2">B</item><item index="3">C</item></root>

parseXML: options

aliases : fromXML, unxml

Parse input as XML string, return object

options are detailed in the xml-mapping documentation

    var stylesheet = {
        "set": "<root><item xml:id=\"1\">A</item><item xml:id=\"2\">B</item><item xml:id=\"3\">C</item></root>",
        "parseXML" : {
            "specialChar": "#",
            "longTag" : true
        }
    };
    // output : { root : { item : [ { xml#id: 1, #text: A }, { xml#id: 2, #text: B }, { xml#id: 3, #text: C } ] } }

Examples

See unit tests : https://github.com/Inist-CNRS/node-jbj-parse/tree/master/test

Try it

http://Inist-CNRS.github.io/node-jbj/

License

MIT

Current Tags

  • 2.0.0                                ...           latest (4 years ago)

2 Versions

  • 2.0.0                                ...           4 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |