GithubHelp home page GithubHelp logo

zack37 / mod.js Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 14 KB

A utility function to require all files at the same directory level as the index.js and export it as an object keyed by the filename

JavaScript 100.00%

mod.js's Introduction

mod.js

Build Status A Rust inspired utility tool for loading modules from a directory. Much like the mod.rs file that handles the repsonsibility or exporting modules inside a directory, mod.js attempts to take on a similar responsibility with minimal overhead.

Getting Started

First, install through npm

npm install --save mod.js

Then, just require:

ES5

var load = require('mod.js');

ES6

import load from 'mod.js';

Examples

load

Given the directory

├ schemas/
│   ├ index.js
│   ├ person.js
│   ├ house.json // Supports .js and .json
├ index.js

Then in your schemas/index.js file, you can simply

// ES5
var load = require('mod.js');

module.exports = load(__dirname);

---

// ES6
import load from 'mod.js';

export default load(__dirname);

And then it's as easy as

// root index.js
var schemas = require('./schemas');
// {
//    person: { ... },
//    house: { ... }
//}

loadAsync

There is another function, loadAsync, that will load the files asynchronously and return a promise.

Given the same directory as above, your schemas/index.js file should look something like:

// ES5
var loadAsync = require('mod.js').loadAsync; 

module.exports = loadAsync(__dirname);

---

// ES6

import { loadAsync } from 'mod.js';

export default loadAsync(__dirname);

And once again:

// root index.js
require('./schemas')
    .then(schemas => {
        console.log(schemas); // { person: { ... }, house: { ... } }
    });

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.