Giter VIP home page Giter VIP logo

data-directory's Introduction

data-directory

This is a Node module for loading structured data from a directory in the style of Jekyll's _data. Install it with:

npm install data-directory

API

The data-directory module exports two top-level functions:

  1. load(dirname, callback) asynchronously loads all of the data in a directory and calls the callback function with the entire data structure when finished.
var datadir = require('data-directory');
datadir.load('path/to/data', function(error, data) {
  // your data here
});
  1. proxy(dirname [, data]) creates a proxy object that loads data synchronously, and only as needed, from the same directory structure.
var datadir = require('data-directory');
var data = datadir.proxy('path/to/data');
// reads path/to/data/foo.{csv,json,tsv,ya?ml}
// or, if path/to/data/foo is a directory, returns a new proxy
var foo = data.foo;

⚠️ In order to use the proxy feature, you'll need to run Node with the --harmony-proxies flag.

Directory Structure

Your directory should contain one or more files with the following extensions:

  • .csv for comma-separated values
  • .json for JSON
  • .tsv for tab-separated values
  • .yaml or .yml for YAML

You can read them all into a single data structure like this:

var datadir = require('data-directory');
datadir.load('_data', function(error, data) {
  if (error) return console.error('error:', error);
  console.log('data:', JSON.stringify(data, null, '  '));
});

Nested Directories

Nested directories will introduce new levels in the data structure. For instance, if you data directory looks like this:

├─ bar.json
└─ baz
   └─ qux.csv

Then it should parse into a JSON structure that looks like:

{
  "bar": {
    // contents of bar.json
  },
  "baz": {
    "qux": [
      // rows in baz/qux.csv
    ]
  }
}

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.