Skip to content

Commit 6f2ae57

Browse files
committed
fix codestyle, move options check to .configure(), fix tests
1 parent 6e5cb91 commit 6f2ae57

3 files changed

Lines changed: 33 additions & 32 deletions

File tree

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ gulp.task('lint', function() {
3636
});
3737

3838
gulp.task('lintTests', function() {
39-
return gulp.src('./tests/*.js')
39+
return gulp.src('./tests/**/*.js')
4040
.pipe(jshint())
4141
.pipe(jshint.reporter('unix'));
4242
});

lib/builder.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@ Builder.prototype._reset = function() {
2727
};
2828

2929
Builder.prototype._getPlaceholder = function () {
30-
if (this.options.namedValues && !this.options.indexedValues) {
31-
throw new Error('Use of options "indexedValues: false" is ' +
32-
'not allowed together with "namedValues: true"');
33-
}
34-
var placeholder = this.options.namedValues ? 'p' : '';
35-
var index = this.options.indexedValues ? (this._placeholderId++) : '';
36-
return placeholder + index;
30+
var placeholder = '';
31+
if (this.options.namedValues) placeholder += 'p';
32+
if (this.options.indexedValues) placeholder += this._placeholderId++;
33+
return placeholder;
3734
};
3835

3936
Builder.prototype._wrapPlaceholder = function(name) {
@@ -63,9 +60,7 @@ Builder.prototype._pushValue = function(value) {
6360
};
6461

6562
Builder.prototype.configure = function(options) {
66-
options = options || {};
67-
68-
this.options = _.defaults({}, options, {
63+
options = _.defaults({}, options, {
6964
separatedValues: true,
7065
namedValues: true,
7166
valuesPrefix: '$',
@@ -74,6 +69,15 @@ Builder.prototype.configure = function(options) {
7469
indexedValues: true
7570
});
7671

72+
if (options.namedValues && !options.indexedValues) {
73+
throw new Error(
74+
'Option `indexedValues`: false is ' +
75+
'not allowed together with option `namedValues`: true'
76+
);
77+
}
78+
79+
this.options = options;
80+
7781
this.setDialect(this.options.dialect);
7882

7983
this._reset();

tests/0_base.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe('Builder', function() {
205205

206206
expect(result.query).to.be.equal('select * from "users" where "name" = $1;');
207207
expect(result.values).to.be.eql(['John']);
208-
});
208+
});
209209

210210
it('should use prefix `@` for values with option `valuesPrefix` = @', function() {
211211
jsonSql.configure({
@@ -219,7 +219,7 @@ describe('Builder', function() {
219219

220220
expect(result.query).to.be.equal('select * from "users" where "name" = @p1;');
221221
expect(result.values).to.be.eql({p1: 'John'});
222-
});
222+
});
223223

224224
it('should return prefixed values with method `prefixValues`', function() {
225225
var result = jsonSql.build({
@@ -230,7 +230,7 @@ describe('Builder', function() {
230230
expect(result.query).to.be.equal('select * from "users" where "name" = @p1;');
231231
expect(result.values).to.be.eql({p1: 'John'});
232232
expect(result.prefixValues()).to.be.eql({'@p1': 'John'});
233-
});
233+
});
234234

235235
it('should return array values with method `getValuesArray`', function() {
236236
var result = jsonSql.build({
@@ -260,26 +260,23 @@ describe('Builder', function() {
260260
expect(result.values).to.be.eql(['John']);
261261
expect(result.prefixValues()).to.be.eql({'$1': 'John'});
262262
expect(result.getValuesObject()).to.be.eql({1: 'John'});
263-
});
264-
265-
it('should throw if `indexedValues = false` and `namedValues = true`', function() {
266-
jsonSql.configure({
267-
namedValues: true,
268-
indexedValues: false
269-
});
263+
});
270264

271-
expect(function() {
272-
jsonSql.build({
273-
table: 'users',
274-
condition: {name: 'John'}
275-
});
276-
}).to.throw('Use of options "indexedValues: false" is ' +
277-
'not allowed together with "namedValues: true"');
265+
it('should throw if `indexedValues = false` and `namedValues = true`', function() {
266+
expect(function() {
267+
jsonSql.configure({
268+
namedValues: true,
269+
indexedValues: false
270+
});
271+
}).to.throw(
272+
'Option `indexedValues`: false is not allowed ' +
273+
'together with option `namedValues`: true'
274+
);
278275
});
279-
280-
it('should not use index for values with option `indexedValues` = false', function() {
281-
jsonSql.configure({
282-
namedValues: false,
276+
277+
it('should not use index for values with option `indexedValues` = false', function() {
278+
jsonSql.configure({
279+
namedValues: false,
283280
indexedValues: false
284281
});
285282

0 commit comments

Comments
 (0)