<commit_before>## *Note:  This project is not yet finished!*



Node.js: Module-writer
======================

`module-writer` is a simple, lightweight utility for code generation.


What can I do with this?
---

* Generate Node or UMD modules as part of a build script
* Store data which contains functions
* Write your own CLI tool


Installation
------------

    npm install --save module-writer


Basic Usage
-----------

### Using `writeModule(obj, filename, callback)` for simple module writing:

```js
const moduleWriter = require('module-writer');
const outputFilename = __dirname+'/greeter.js';

let exportsObject = {
    greeter: function(nameToGreet){
        return 'Hello, '+nameToGreet;
    },
    favoritePeople: ['Fred', 'George']
};

moduleWriter.writeModule(exportsObject, outputFilename, ()=>{
    console.log('Success!');
})
```

#### Output `greeter.js`:
```js
module.exports = {
    greeter: function(nameToGreet){
        return 'Hello, '+nameToGreet;
    },
    favoritePeople: ['Fred', 'George']
};
```

Advanced Usage
--------------

### Using `createModule()` and `Module` methods to compose more advanced modules:

```js
const moduleWriter = require('module-writer');
const outputFilename = __dirname+'/greeter.js';

let newModule = moduleWriter.createModule();

// Module.require simply adds a require statement to the generated file.
newModule.require('_', 'lodash');

// Module.addVar adds a variable to the generated module,
// by executing the supplied expression.
newModule.addVar('favoritePeople', () => ['Fred', 'George']);

newModule.addVar('greeter', (favoritePeople)=>{
    // This scope is here to help you use other variable names
    // in a variable defininition, without causing LINT errors.

    return function(nameToGreet){
        // This is the actual "greeter" function.
        if( _.includes(favoritePeople, nameToGreet) ){
            return 'Hello '+nameToGreet', my friend!';
        }else{
            return 'Hello, '+nameToGreet;
        }
    }
});

// Specifies which module-scoped variables should be exported.
newModule.exportVars(['greeter']);

newModule.writeToFile(outputFilename, ()=>{
    console.log('Success!');
});
```

#### Output `greeter.js`:
```js
const _ = require('lodash');

var favoritePeople = ['Fred', 'George'];

var greeter = function(nameToGreet){
    // This is the actual "greeter" function.
    if( _.includes(favoritePeople, nameToGreet) ){
        return 'Hello, '+nameToGreet', my friend!';
    }else{
        return 'Hello, '+nameToGreet;
    }
};

module.exports = { greeter };
```


### Specifying options:

You can pass an options object as the last argument to writeModule, createModule, or Module.writeToFile.<commit_msg>Added caveats and future plans to readme<commit_after>## *Note:  This project is not yet finished!*



Node.js: Module-writer
======================

`module-writer` is a simple, lightweight utility for code generation.


What can I do with this?
---

* Generate Node or UMD modules as part of a build script
* Store data which contains functions
* Write your own CLI tool


Installation
------------

    npm install --save module-writer


Basic Usage
-----------

### Using `writeModule(obj, filename, callback)` for simple module writing:

```js
const moduleWriter = require('module-writer');
const outputFilename = __dirname+'/greeter.js';

let exportsObject = {
    greeter: function(nameToGreet){
        return 'Hello, '+nameToGreet;
    },
    favoritePeople: ['Fred', 'George']
};

moduleWriter.writeModule(exportsObject, outputFilename, ()=>{
    console.log('Success!');
})
```

#### Output `greeter.js`:
```js
module.exports = {
    greeter: function(nameToGreet){
        return 'Hello, '+nameToGreet;
    },
    favoritePeople: ['Fred', 'George']
};
```

Advanced Usage
--------------

### Using `createModule()` and `Module` methods to compose more advanced modules:

```js
const moduleWriter = require('module-writer');
const outputFilename = __dirname+'/greeter.js';

let newModule = moduleWriter.createModule();

// Module.require simply adds a require statement to the generated file.
newModule.require('_', 'lodash');

// Module.addVar adds a variable to the generated module,
// by executing the supplied expression.
newModule.addVar('favoritePeople', () => ['Fred', 'George']);

newModule.addVar('greeter', (favoritePeople)=>{
    // This scope is here to help you use other variable names
    // in a variable defininition, without causing LINT errors.

    return function(nameToGreet){
        // This is the actual "greeter" function.
        if( _.includes(favoritePeople, nameToGreet) ){
            return 'Hello '+nameToGreet', my friend!';
        }else{
            return 'Hello, '+nameToGreet;
        }
    }
});

// Specifies which module-scoped variables should be exported.
newModule.exportVars(['greeter']);

newModule.writeToFile(outputFilename, ()=>{
    console.log('Success!');
});
```

#### Output `greeter.js`:
```js
const _ = require('lodash');

var favoritePeople = ['Fred', 'George'];

var greeter = function(nameToGreet){
    // This is the actual "greeter" function.
    if( _.includes(favoritePeople, nameToGreet) ){
        return 'Hello, '+nameToGreet', my friend!';
    }else{
        return 'Hello, '+nameToGreet;
    }
};

module.exports = { greeter };
```


### Specifying options:

You can pass an options object as the last argument to writeModule, createModule, or Module.writeToFile.


Caveats
-------

* Variables and exports must be either functions, or types which can be expressed literally.  Those types are primitives (strings, chars, numbers, symbols, booleans, regular expressions, null, undefined), boxed primitives (Number, Boolean, etc), literal objects, Arrays with numeric keys.  *However, these objects can be nested, and functions can return any type on the module's scope.*
* This package only works in Node.js, not browsers.


Future plans
------------

* Add support for non-standard keys and property descriptors through IIFE substitution.
{'fraction_non_alphanumeric': 0.14976805831676607, 'fraction_numerical': 0.0, 'mean_word_length': 3.6653786707882534, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3124983', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1689, 'n_words': 567}