Skip to content

Commit

Permalink
feat: add support for user defined functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Feb 22, 2017
1 parent cc9ed09 commit 2aa770e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Expands expressions in a configuration object
| Param | Type | Description |
| --- | --- | --- |
| config | <code>object</code> | |
| [options] | <code>object</code> | constants object holding additional constants |
| [options] | <code>object</code> | constants object holding additional constants functions object holding additional function |


* <a name="module_config-expander..functions.split.apply"></a>
Expand Down
18 changes: 16 additions & 2 deletions src/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ import {
}
from './grammar';

import {
functions
}
from './functions';

import {
createValue
}
from './util';

/**
* Expands expressions in a configuration object
* @param {object} config
* @param {object} [options]
* constants object holding additional constants
* functions object holding additional function
* @returns {Promise} expanded configuration
*/
export function expand(config, options = {}) {
Expand All @@ -30,13 +41,14 @@ export function expand(config, options = {}) {
constants: Object.assign({
basedir: '/',
os: os
}, options.constants)
}, options.constants),
functions: Object.assign( {}, functions, options.functions)
};

const parser = new ConfigParser();

const ee = createContext({
evaluate: (expression, _context, path) => {
evaluate: (expression, _unusedContext, path) => {
context.path = path;
const ast = parser.parse(expression, context);
return ast.value;
Expand All @@ -48,3 +60,5 @@ export function expand(config, options = {}) {
return Promise.reject(e);
}
}

export { createValue };
7 changes: 1 addition & 6 deletions src/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import {
}
from './util';

import {
functions
}
from './functions';

class AST {
get value() {
return undefined;
Expand Down Expand Up @@ -185,7 +180,7 @@ const grammar = {

grammar.advance(')');

const f = functions[left.value];
const f = grammar.context.functions[left.value];
if (f) {
//console.log(`${f.arguments} <> ${args.map(a => a.type)}`);
return new FCall(f, grammar.context, args);
Expand Down
11 changes: 10 additions & 1 deletion tests/simple_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const chai = require('chai'),
expect = chai.expect,
should = chai.should();

const expand = require('../dist/expander').expand;
const {expand, createValue} = require('../dist/expander');

describe('expander', () => {

Expand Down Expand Up @@ -145,6 +145,15 @@ describe('expander', () => {
r, 'secret')));
});

describe('user defined functions', () => {
it('can call', () => expand(
"${myFunction()}", {
functions: {
myFunction: { apply: (context, args) => { return createValue(77); }}
}
}).then(r => assert.equal(r, 77)));
});

describe('promise function args', () => {
it('one promise arg', () => expand(
"${substring(string(document('fixtures/short.txt')),0,4)}", {
Expand Down

0 comments on commit 2aa770e

Please sign in to comment.