Giter VIP home page Giter VIP logo

spelunk's Introduction

spelunk.js

$ npm install spelunk

spelunk.js turns a folder into an object. This folder...

data
|- config.json
|- tables
   |- population.csv
   |- growth.csv
|- slides
   |- 0.txt
   |- 1.txt
   |- 2.txt
   |- 3.txt
|- i18n
   |- en-GB.json
   |- en-US.json
   |- fr.json
   |- de.json
   |- ...

...becomes this object:

{
  config: { <contents of config.json> }, // parsed as JSON
  tables: {
    population: <contents of population.csv> // as a string
    growth:     <contents of growth.csv>
  },
  slides: [
    <contents of 0.txt>,  // because these files have
    <contents of 1.txt>,  // numeric names, `slides` is
    <contents of 2.txt>,  // an array, not an object
    <contents of 3.txt>
  ],
  i18n: {
    "en-GB": <contents of en-GB.json>,
    "en-US": <contents of en-US.json>,
    "fr":    <contents of fr.json>,
    "de":    <contents of de.json>,
    ...
  }
}

If a file contains JSON, it is parsed as JSON; if not, it is treated as text. If a folder only contains items with numeric filenames (as in the case of the slides folder above), it will become an array rather than an object.

Usage

spelunk.js uses the standard Node callback pattern...

callback = function ( error, result ) {
  if ( error ) {
    return handleError( error );
  }

  doSomething( result );
};

spelunk( 'myFolder', options, callback ); // you can omit options

...but it also returns a promise, because this is 2015 dammit and callbacks are a lousy flow control mechanism:

spelunk( 'myFolder', options ).then( doSomething, handleError );

Synchronous usage

var result = spelunk.sync( 'myFolder', options );

Options

options.exclude

Exclude files that match a certain pattern (this uses minimatch syntax):

spelunk( 'myFolder', { exclude: '**/README.md' }).then( doSomething );

The value of exclude can be an string, or an array of strings.

options.keepExtensions

If you have multiple files with the same name but different extensions, they'll conflict. This option allows you to keep their extensions, e.g. result['config.json'] instead of result.config (but really, you're better off keeping your filenames distinct).

spelunk( 'myFolder', { keepExtensions: true }).then( doSomething );

Why the name?

Because traversing a folder tree and mapping all its nooks and crannies feels a bit like spelunking. Plus it's fun to say.

Testing

$ npm run test

License

MIT, copyright 2014 @Rich_Harris

spelunk's People

Contributors

aubergene avatar ef4 avatar rich-harris avatar

Stargazers

 avatar

Watchers

 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.