Giter VIP home page Giter VIP logo

load-all's Introduction





Load All

js-standard-style CircleCI

Loads the contents of sub-directories of a specified directory into an object or an array, suitable for mass loading of small modules from a filesystem.

This library is used extensively at Beyonk for loading route mappings, validations, error objects, and anywhere where a large number of small modules needs to be loaded.

Installation

npm i -D @beyonk/load-all

Usage

The library only has two methods, both of which amalgamate all exported items from a list of files.

exportDir

exportDir which results in a hash of export name -> function (), so that a directory structured thusly:

/my-dir
|-- library1
|   `-- index.js
|-- library2and3
|   `-- index.js
| ...

can be imported with:

const exported = exportDir('/my-dir')

// {
// library1: (exported as `library` from library1.js),
// library2: (exported as `library2` from library2and3.js),
// library3: (exported as `library3` from library2and3.js)
// }

includeDir

includeDir results in a concatenated array of the contents of files (which should export an array themselves), so that a directory structured thusly:

/routes
|-- routes1
|   `-- index.js
|-- routes2and3
|   `-- index.js
| ...

can be imported with:

const routes = includeDir('/my-dir')

// [ route1, route2, route3 ]

Advanced usage

Logging

The library will let you know every file it is loading, if you specify the second parameter to any method:

includeDir('/some-dir', 'route')

will result in log messages similar to the following:

// Adding route from ./some-dir/my-route

Modifying file content

You can modify the content of the files you load before it is put into the final hash or array, if you, for instance, would like to capitalise the key names, or add metadata or similar.

const { capitalize } = require('lodash')

includeDir('/some-dir', 'some-label', exported => {
  return Object.keys(exported).reduce((acc, exportName) {
    acc[capitalize(exportName)] = exported[exportName]
    return acc
  }, {})
})

Developing

You can run the suite of unit tests with

npm run test

code is linted according to @beyonk/eslint-config

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.