GithubHelp home page GithubHelp logo

busterc / fs-ndjson Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 1.0 47 KB

:information_source: read and write NDJSON files (Newline Delimited JSON)

License: ISC License

JavaScript 100.00%
ndjson ldjson jsonld jsonlines json

fs-ndjson's Introduction

fs-ndjson NPM version Build Status Dependency Status Coverage percentage

read and write NDJSON files (Newline Delimited JSON)

FYI

While NDJSON is often used for efficient streaming of JSON, it is also a good format for persisting and retreiving object data collections to and from disk. For example, NeDB uses the format.

See Also:

API

The following fs-ndjson methods mirror the fs core module method APIs, however, the methods have been universalified so that you can use promises or callbacks for async.

  • readFile
  • readFileSync
  • writeFile
  • writeFileSync

BONUS: writeFile and writeFileSync will create the parent directory, if it does not already exist.

Installation

$ npm install --save fs-ndjson

Usage

Given /Users/tyler/project-mayhem/enemies.db contains

  {"name":"Equifax","location":"Atlanta, GA"}
  {"name":"Visa","location":"Foster City, CA"}
const fsNdjson = require('fs-ndjson');

// can return a promise
fsNdjson.readFile('/Users/tyler/project-mayhem/enemies.db').then(targets => {
  console.log(targets);
  // [
  //   {
  //     name: "Equifax",
  //     location: "Atlanta, GA"
  //   },
  //   {
  //     name: "Visa",
  //     location: "Foster City, CA"
  //   }
  // ]
});

// can use a callback
fsNdjson.readFile('/Users/tyler/project-mayhem/enemies.db', (err, enemies) => {
  if (err) throw err;
  console.log(enemies.length);
  // => 2
});

// can be used synchronously
let enemies = fsNdjson.readFileSync('/Users/tyler/project-mayhem/enemies.db');

// ******** BUT WAIT, THERE'S MORE! ******** //
// Not only does it read, but it writes too! //

// fsNdjson.writeFile     // use callback or promise
// fsNdjson.writeFileSync // does what you think it does

License

ISC © Buster Collings

fs-ndjson's People

Contributors

busterc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

henriquelbsouza

fs-ndjson's Issues

Append to file support

Would be useful to implement fs.append, something extremely simple to use this lib for logging purposes.
fs.appendFileSync(filePath, JSON.stringify(newObject)+'\n');

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.