Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/allouis/indexjs
handy index.js helper for multiple files
https://github.com/allouis/indexjs
Last synced: 24 days ago
JSON representation
handy index.js helper for multiple files
- Host: GitHub
- URL: https://github.com/allouis/indexjs
- Owner: allouis
- Created: 2014-04-18T16:35:46.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-06-16T08:29:27.000Z (over 8 years ago)
- Last Synced: 2024-10-03T00:41:44.507Z (about 1 month ago)
- Language: JavaScript
- Size: 15.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# indexjs [![Build Status](https://travis-ci.org/allouis/indexjs.svg?branch=master)](https://travis-ci.org/allouis/indexjs)
You can use indexjs to simplify the requiring of similar modules within a directory, for examples models;
## API
### `indexjs(directory, output, [transform])`
directory will usually be __dirname, when run from index.js of a directory.output can be either an array or an object, it doesn't have to be empty.
transform is an optional function that gets called on all the required modules, the return value is used instead of the module
## Usage
### File structure
```
-- app.js
-- models/
----- index.js
----- user.js
----- comment.js
----- post.js
----- .hiddenfile
-- routers/
----- index.js
----- user.js
----- comment.js
----- post.js```
### models/index.js
```javascript/**
* /project/models/index.js
*/var indexjs = require('indexjs');
module.exports = indexjs(__dirname, {}, function (model) {
return model.init(); // example of transform
});
```
### routers/index.js
```javascript/**
* /project/routers/index.js
*/var indexjs = require('indexjs');
module.exports = indexjs(__dirname, []);
```
### app.js
```javascript/**
* /project/app.js
*/
var models = require('./models');
var routers = require('./routers');
routers.forEach(function(router) {
router(app);
});models.user; // the equivalent of require('./models/user');
models.comment; // the quivalent of require('./models/comment');
// etc..
models['.hiddenfile']; // undefined;
```