Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/trevnorris/reblaze

Rewrite your JS on the fly using JS
https://github.com/trevnorris/reblaze

Last synced: 11 days ago
JSON representation

Rewrite your JS on the fly using JS

Awesome Lists containing this project

README

        

```
_ _
| | | |
_ __ ___| |__ | | __ _ _______
| '__/ _ \ '_ \| |/ _` |_ / _ \
| | | __/ |_) | | (_| |/ / __/
|_| \___|_.__/|_|\__,_/___\___|
```

A crazy way of template-izing your JS `Function`s and `Generator`s so they can
be generated on the fly.

This should be used for methods that are instantiated once then used many
times, since currently the function generation does take quite a bit of time to
do.

### API

#### `reblaze(values[, name], fn)`

* `values` {Object}
* `name` {String} Default `''`
* `fn` {Function}
* Returns new Function

### Examples

Let's show a simple example then allow you to go experiment.

```javascript
'use strict';

const reblaze = require('reblaze');

function Matrix(rows, cols) {
this._rows = rows >>> 0;
this._cols = cols >>> 0;
// Replace template entries with fields in passed object.
this.sumCols = reblaze({ COLS: this._cols, ROWS: this._rows }, sumCols);

this._data = new Uint32Array(this._rows * this._cols);
}

// This will look funky due to the template convention.
function sumCols() {
const data = this._data;
// Sum of values may exceed a uint32
const sum = new Float64Array(((COLS)));
for (var i = 0; i < ((COLS)) * ((ROWS)); i++) {
sum[i % ((COLS))] += data[i];
}
return sum;
}

// Build a new Matrix instance with custom sumCols().
const m = new Matrix(11, 7);
```

Now `sumCols` on the instance `m`:

```javascript
function sumCols() {
const data = this._data;
const sum = new Float64Array(7);
for (var i = 0; i < 7 * 11; i++) {
sum[i % 7] += this[i];
}
return sum;
}
```

It will also automatically take care of object properties as well. For example:

```javascript
function runMe(foo) {
return foo[((BAR))];
}

const fn = reblaze({ BAR: 'bar' }, runMe);
```

And the output for the new `fn` instance:

```javascript
function runMe(foo) {
return foo.bar;
}
```

The name of the function can also be replaced by passing a second optional
argument.

```javascript
function replaceMe(foo) {
return foo[((BAR))];
}

const fn = reblaze({ BAR: 'bar' }, 'wereReplaced', replaceMe);
```

Will generate the following function:

```javascript
function wereReplaced(foo) {
return foo.bar;
}
```

If you want to have the full `require`, `__filename`, etc. in your function
pass `module` as an optional third (or second if `name` isn't provided)
argument.

```javascript
function foop() {
require(__dirname + '/' + ((NAME)));
}
reblaze({ NAME: 'mod_name' }, module, foop);
```

So, give it a whirl and report any bugs. In the future I plan on implementing
even more meta-ness where `reblaze` can run on itself and return optimized
instances for a given construction case.