https://github.com/allouis/indexjs
  
  
    handy index.js helper for multiple files 
    https://github.com/allouis/indexjs
  
        Last synced: 3 months 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 11 years ago)
- Default Branch: master
- Last Pushed: 2016-06-16T08:29:27.000Z (over 9 years ago)
- Last Synced: 2025-06-25T12:07:19.243Z (4 months ago)
- Language: JavaScript
- Size: 15.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
- 
            Metadata Files:
            - Readme: README.md
 
Awesome Lists containing this project
README
          # indexjs [](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;
```