Skip to content

Commit

Permalink
feat: boolean expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Jan 5, 2017
1 parent 521e502 commit d0c758e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 15 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,44 @@ Output

* <a name="module_config-expander.expand"></a>

## config-expander.expand(config, options) ⇒ <code>Promise</code>
## config-expander.expand(config, [options]) ⇒ <code>Promise</code>
Expands expressions in a configuration object

**Kind**: static method of <code>[config-expander](#module_config-expander)</code>
**Returns**: <code>Promise</code> - expanded configuration

| Param | Type |
| --- | --- |
| config | <code>Object</code> |
| options | <code>Object</code> |
| config | <code>object</code> |
| [options] | <code>object</code> |


* <a name="module_config-expander..functions.encrypt.apply"></a>

## module:config-expander~functions.encrypt.apply(key, plaintext) ⇒ <code>String</code>
## module:config-expander~functions.encrypt.apply(key, plaintext) ⇒ <code>string</code>
Encrypt a plaintext value

**Kind**: static method of <code>module:config-expander~functions.encrypt</code>
**Returns**: <code>String</code> - encrypted value
**Returns**: <code>string</code> - encrypted value

| Param | Type |
| --- | --- |
| key | <code>String</code> |
| plaintext | <code>String</code> |
| key | <code>string</code> |
| plaintext | <code>string</code> |


* <a name="module_config-expander..functions.decrypt.apply"></a>

## module:config-expander~functions.decrypt.apply(key, encrypted) ⇒ <code>String</code>
Decrypt a string
## module:config-expander~functions.decrypt.apply(key, encrypted) ⇒ <code>string</code>
Decrypt a former encrypted string

**Kind**: static method of <code>module:config-expander~functions.decrypt</code>
**Returns**: <code>String</code> - plaintext
**Returns**: <code>string</code> - plaintext

| Param | Type |
| --- | --- |
| key | <code>String</code> |
| encrypted | <code>String</code> |
| key | <code>string</code> |
| encrypted | <code>string</code> |


* * *
Expand Down
4 changes: 2 additions & 2 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const functions = {
},
string: {
arguments: ['string|buffer'],
returns: 'number',
returns: 'string',
apply: (context, args) => {
const v = args[0].value;
return createValue(v instanceof Buffer ? v.toString() : v);
Expand Down Expand Up @@ -107,7 +107,7 @@ export const functions = {
arguments: ['string', 'string'],
returns: 'string',
/**
* Decrypt a former encrypted string
* Decrypt a former encrypted string
* @param {string} key
* @param {string} encrypted
* @return {string} plaintext
Expand Down
34 changes: 34 additions & 0 deletions src/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,40 @@ export const grammar = create({
}
}
},
infixr: {
'&&': {
precedence: 30,
combine: (left, right) => new BinOP(left, right, (l, r) => l.value && r.value)
},
'||': {
precedence: 30,
combine: (left, right) => new BinOP(left, right, (l, r) => l.value || r.value)
},
'==': {
precedence: 40,
combine: (left, right) => new BinOP(left, right, (l, r) => l.value === r.value)
},
'!=': {
precedence: 40,
combine: (left, right) => new BinOP(left, right, (l, r) => l.value !== r.value)
},
'>=': {
precedence: 40,
combine: (left, right) => new BinOP(left, right, (l, r) => l.value >= r.value)
},
'<=': {
precedence: 40,
combine: (left, right) => new BinOP(left, right, (l, r) => l.value <= r.value)
},
'>': {
precedence: 40,
combine: (left, right) => new BinOP(left, right, (l, r) => l.value > r.value)
},
'<': {
precedence: 40,
combine: (left, right) => new BinOP(left, right, (l, r) => l.value < r.value)
}
},
infix: {
'.': {
precedence: 1,
Expand Down
40 changes: 39 additions & 1 deletion tests/simple_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,42 @@ describe('expander', () => {
});

describe('expression', () => {
it('str concat', () => expand("${'x' + 'y'}").then(r => assert.equal(r, 'xy')));
it('string concat', () => expand("${'x' + 'y'}").then(r => assert.equal(r, 'xy')));
it('addition', () => expand("${1 + 2}").then(r => assert.equal(r, 3)));
it('substraction', () => expand("${3 - 2}").then(r => assert.equal(r, 1)));
it('multiplication', () => expand("${3*2)}").then(r => assert.equal(r, 6)));
it('division', () => expand("${8/2)}").then(r => assert.equal(r, 4)));
it('number', () => expand("${number('77')}").then(r => assert.equal(r, 77)));
});

describe('boolean expression', () => {
it('greater than false', () => expand("${1 > 2}").then(r => assert.equal(r, false)));
it('greater than true', () => expand("${2 > 1}").then(r => assert.equal(r, true)));
it('greater equal than false', () => expand("${1 >= 2}").then(r => assert.equal(r, false)));
it('greater equal than true', () => expand("${2 >= 1}").then(r => assert.equal(r, true)));
it('less than false', () => expand("${2 < 1}").then(r => assert.equal(r, false)));
it('less than true', () => expand("${1 < 2}").then(r => assert.equal(r, true)));
it('less equal than false', () => expand("${2 <= 1}").then(r => assert.equal(r, false)));
it('less equal than true', () => expand("${1 <= 2}").then(r => assert.equal(r, true)));
it('equal true', () => expand("${1 == 1}").then(r => assert.equal(r, true)));
it('equal false', () => expand("${1 == 2}").then(r => assert.equal(r, false)));
it('not equal true', () => expand("${2 != 1}").then(r => assert.equal(r, true)));
it('not equal false', () => expand("${2 != 2}").then(r => assert.equal(r, false)));
it('or false', () => expand("${0 || 0}").then(r => assert.equal(r, false)));
it('or true', () => expand("${1 || 0}").then(r => assert.equal(r, true)));

it('and false', () => expand("${1 && 0}").then(r => assert.equal(r, false)));
it('and true', () => expand("${1 && 1}").then(r => assert.equal(r, true)));

describe('combined', () => {
it('or true', () => expand("${1 > 2 || 1 > 0}").then(r => assert.equal(r, true)));
it('or false', () => expand("${1 > 2 || 1 < 0}").then(r => assert.equal(r, false)));

it('and false', () => expand("${1>0 && 0>1}").then(r => assert.equal(r, false)));
it('and true', () => expand("${1>0 && 2>0}").then(r => assert.equal(r, true)));
});
});

describe('functions', () => {
it('unknown function', () => expand("${thisFunctionIsUnknown()}")
.then(e => assert.equal(e, {}))
Expand Down Expand Up @@ -192,6 +220,16 @@ describe('expander', () => {
});

describe('combined paths', () => {
it('access objects first than array', () => expand("${myObject.level1.level2[1]}", {
constants: {
myObject: {
level1: {
level2: [1, 'val2']
}
},
}
}).then(r => assert.equal(r, 'val2')));

xit('access several levels', () => expand("${myObject.level1[1].level2}", {
constants: {
myObject: {
Expand Down

0 comments on commit d0c758e

Please sign in to comment.