Code Monkey home page Code Monkey logo

split-json's Introduction

split-json

Build Status

split JSON was sent through some kind of readable stream in JSON objects

API

stream.pipe(split([match], [encoding], [replace]))
.on('data', function (data) {
    console.log(split.JSONValid(data))
})

// js objects
stream.pipe(split.obj())
.on('data', function (obj) {
    console.log(obj)
})
  • [match] => RegExp, default to os.EOL
  • [encoding] => String, utf8/ascii, default to utf8
  • [replace] => RegExp, enable you to remove some delimiters at begin and end of a big JSON file (ex: /(^[\n)|(\n]\n$)/)

emitters

on('data', callback(data)) on('end', [callback])


split#JSONValid

validates JSON, instead to use JSON.parse inside of a try/catch

split.JSONValid(obj) -> return object {value, error}

  • value:* should be the object corresponding to the given JSON text or undefined in case of error
  • error: should be an Error object in case of error or undefined in case of success
split#isObject

check is a valid Object split.isObject(obj) -> return boolean

Examples

var split = require('split-json')

// from file

var rs = fs.createReadStream('./test/file1.json')

rs.pipe(split([match])).on('data', callback)
        

// for mongoDB with mongojs use split#obj

var mongojs = require('mongojs')

var db = mongojs('str_conn')
var collection = db.collection('my_coll')

collection.find({})
.pipe(split.obj())
.on('data', function (doc) {
    console.log(doc)
})
.on('end', function () {
    db.close()
})
      
  
  
// TCP socket

on('data', function (data) {
    var rs = new Readable()
    rs.push(data)
    rs.push(null)

    rs.pipe(split(/\n/)).on('data', function cb (data) {
      cb(null, data);
    })
})

Development

this projet has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit

to run test

npm test

to run jshint

npm run jshint

to run code style

npm run code-style

to run check code coverage

npm run check-coverage

to open the code coverage report

npm run open-coverage

split-json's People

Contributors

joaquimserafim 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.