Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fluent interface. #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ __Generates__
}
```

#### Fluent Interface
#### esq.q(str, ..., str, value);
Operates as `esq.query()`, but returns `this` so that `.q()` can be chained fluently.
Call `.getQuery()` at the end of the chain to get the final result.

__Example__
```javascript
esq.q('query', 'bool', ['must'], { match: { foo: 'bar' } })
.q('query', 'bool', ['should'], { match: { baz: 'quux' } })
.getQuery();
```

__Generates__
```json
{
"query": {
"bool": {
"must": [
{
"match": {
"foo": "bar"
}
}
],
"should": [
{
"match": {
"baz": "quux"
}
}
]
}
}
}
```

---

## Tests
Expand Down
15 changes: 11 additions & 4 deletions esq.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
return this._query;
};

ESQ.prototype.query = function() {
var args = this._getArgsAsArray(arguments);
var value = args.pop();
ESQ.prototype._buildQuery = function(argArray) {
var value = argArray.pop();
this._createNestedObject(this._query, argArray, value);
};

this._createNestedObject(this._query, args, value);
ESQ.prototype.query = function() {
this._buildQuery(this._getArgsAsArray(arguments));
return this._query;
};

ESQ.prototype.q = function() {
this._buildQuery(this._getArgsAsArray(arguments));
return this;
};

ESQ.prototype._createNestedObject = function(base, names, value) {
var lastName = arguments.length === 3 ? names.pop() : false;

Expand Down
38 changes: 38 additions & 0 deletions test/testEsq.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,45 @@ describe('Testing ESQ', function() {
var result = esq.query('foo', false);
assert.deepEqual(result, { foo: false });
});
});

describe('q', function() {
it('with no args', function() {
var result = esq.q().getQuery();
assert.deepEqual(result, { });
});

it('with standard params', function() {
var result = esq.q('foo', 'bar', { }).getQuery();
assert.deepEqual(result, { foo: { bar: { } } });
});

it('with bad args', function() {
var result = esq.q('foo', 'bar', ['foobar'], 123).getQuery();
assert.deepEqual(result, { foo: { bar: { foobar: [ 123 ] } } });
});

it('with overwritted string', function() {
esq.q('foo', 'bar');
var result = esq.q('foo', 'bar2').getQuery();
assert.deepEqual(result, { foo: 'bar2' });
});

it('with overwritted number', function() {
esq.q('foo', 1);
var result = esq.q('foo', 42).getQuery();
assert.deepEqual(result, { foo: 42 });
});

it('with zero value', function() {
var result = esq.q('foo', 0).getQuery();
assert.deepEqual(result, { foo: 0 });
});

it('with false value', function() {
var result = esq.q('foo', false).getQuery();
assert.deepEqual(result, { foo: false });
});
});

describe('_createNestedObject', function() {
Expand Down