Skip to content

Commit f0da682

Browse files
committed
gulp lint task added, some code refactoring
1 parent a6e67b6 commit f0da682

File tree

9 files changed

+54
-75
lines changed

9 files changed

+54
-75
lines changed

.jshintrc

+6-19
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
{
22
"node": true,
3-
"newcap": false,
4-
"supernew": false,
3+
"freeze": true,
54
"maxlen": 100,
65
"smarttabs": true,
76
"indent": 1,
8-
"globalstrict": true,
7+
"quotmark": "single",
98
"strict": true,
9+
"globalstrict": true,
1010
// use es3 to get 'extra comma' at object literal error
1111
"es3": true,
12-
// suppress warnings about switches with just one case
13-
"onecase": true,
14-
"nonew": false,
15-
"trailing": true,
16-
"sub": false,
17-
"loopfunc": true,
18-
"boss": false,
19-
"lastsemic": false,
20-
"quotmark": "single",
2112
"undef": true,
13+
"unused": true,
2214
"immed": true,
23-
// allows ECMAScript 6 syntax (generators, etc)
24-
"esnext": true,
25-
// allow generators without a yield
26-
"noyield": true,
15+
"eqeqeq": true,
2716
"globals": {
28-
"describe": false,
29-
"it": false,
30-
"before": false
17+
"JSON": false
3118
}
3219
}

gulpfile.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
var gulp = require('gulp');
44
var mocha = require('gulp-mocha');
5+
var jshint = require('gulp-jshint');
56

67
gulp.task('test', function() {
7-
return gulp.src(['tests/*.js'], {read: false})
8+
return gulp.src(['./tests/*.js'], {read: false})
89
.pipe(
910
mocha({
1011
reporter: 'spec',
1112
bail: true
1213
})
1314
);
1415
});
16+
17+
gulp.task('lint', function() {
18+
return gulp.src('./lib/**/*.js')
19+
.pipe(jshint())
20+
.pipe(jshint.reporter('unix'));
21+
});

lib/dialects/base/blocks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = function(dialect) {
3535

3636
if (expression) {
3737
if (!_.isArray(expression)) expression = [expression];
38-
var exprSuffix = Array(expression.length + 1).join(')');
38+
var exprSuffix = (new Array(expression.length + 1)).join(')');
3939
field = expression.join('(') + '(' + field + exprSuffix;
4040
}
4141
} else {

lib/dialects/base/conditions.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,12 @@ module.exports = function(dialect) {
111111
throw new Error('`$between` array length should be 2 or greater');
112112
}
113113

114-
var placeholder1 = this._pushValue(value[0]);
115-
var placeholder2 = this._pushValue(value[1]);
116-
117-
return [this.dialect._wrapIdentifier(field), 'between', placeholder1, 'and', placeholder2].join(' ');
114+
return [
115+
this.dialect._wrapIdentifier(field),
116+
'between',
117+
this._pushValue(value[0]),
118+
'and',
119+
this._pushValue(value[1])
120+
].join(' ');
118121
});
119122
};

lib/dialects/base/index.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
var _ = require('underscore');
43
var ValuesStore = require('../../valuesStore');
54

65
var templatesInit = require('./templates');
@@ -24,9 +23,8 @@ var Dialect = module.exports = function(builder) {
2423
modifiersInit(this);
2524
logicalOperatorsInit(this);
2625

27-
this.config.wrapIdentifierRegexp = new RegExp(
28-
'^\\' + this.config.identifierPrefix + '.*\\' +
29-
this.config.identifierSuffix + '$'
26+
this.config.wrappedIdentifierRegexp = new RegExp(
27+
'^\\' + this.config.identifierPrefix + '.*\\' + this.config.identifierSuffix + '$'
3028
);
3129
};
3230

@@ -39,18 +37,16 @@ Dialect.prototype.config = {
3937

4038
Dialect.prototype._wrapIdentifier = function(name) {
4139
if (name !== '*' && this.builder.options.wrappedIdentifiers &&
42-
!this.config.wrapIdentifierRegexp.test(name)) {
40+
!this.config.wrappedIdentifierRegexp.test(name)) {
4341
// try to split name by dot
4442
var nameParts = name.split('.');
4543

4644
if (nameParts.length > 2) {
47-
throw new Error('Can\'t wrap identifier with name name "' + name + '"');
45+
throw new Error('Identifier "' + name + '" contains more than one dot');
4846
} else if (nameParts.length === 2) {
49-
name = this._wrapIdentifier(nameParts[0]) + '.' +
50-
this._wrapIdentifier(nameParts[1]);
47+
name = this._wrapIdentifier(nameParts[0]) + '.' + this._wrapIdentifier(nameParts[1]);
5148
} else {
52-
name = this.config.identifierPrefix + name +
53-
this.config.identifierSuffix;
49+
name = this.config.identifierPrefix + name + this.config.identifierSuffix;
5450
}
5551
}
5652

lib/dialects/postgresql/conditions.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var _ = require('underscore');
44

5-
var buildInCondition = function(builder, field, operator, value) {
5+
var buildJsonInCondition = function(builder, field, operator, value) {
66
field = builder.dialect._wrapIdentifier(field);
77

88
var placeholder;
@@ -15,7 +15,7 @@ var buildInCondition = function(builder, field, operator, value) {
1515
return [field, operator, placeholder].join(' ');
1616
};
1717

18-
var buildHasCondition = function(builder, field, operator, value) {
18+
var buildJsonHasCondition = function(builder, field, operator, value) {
1919
field = builder.dialect._wrapIdentifier(field);
2020

2121
var placeholder;
@@ -32,11 +32,11 @@ var buildHasCondition = function(builder, field, operator, value) {
3232

3333
module.exports = function(dialect) {
3434
dialect.conditions.add('$jsonContains', function(field, operator, value) {
35-
return buildInCondition(this, field, '@>', value);
35+
return buildJsonInCondition(this, field, '@>', value);
3636
});
3737

3838
dialect.conditions.add('$jsonIn', function(field, operator, value) {
39-
return buildInCondition(this, field, '<@', value);
39+
return buildJsonInCondition(this, field, '<@', value);
4040
});
4141

4242
dialect.conditions.add('$jsonHas', function(field, operator, value) {
@@ -53,10 +53,10 @@ module.exports = function(dialect) {
5353
});
5454

5555
dialect.conditions.add('$jsonHasAny', function(field, operator, value) {
56-
return buildHasCondition(this, field, '?|', value);
56+
return buildJsonHasCondition(this, field, '?|', value);
5757
});
5858

5959
dialect.conditions.add('$jsonHasAll', function(field, operator, value) {
60-
return buildHasCondition(this, field, '?&', value);
60+
return buildJsonHasCondition(this, field, '?&', value);
6161
});
6262
};

lib/dialects/postgresql/index.js

+17-31
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,27 @@ util.inherits(Dialect, BaseDialect);
1414

1515
Dialect.prototype.config = _({
1616
jsonSeparatorRegexp: /->>?/g,
17-
jsonPathWrap: '\''
17+
jsonIdentifierWrap: '\''
1818
}).extend(BaseDialect.prototype.config);
1919

20-
Dialect.prototype._wrapJsonPathPart = function(pathPart) {
21-
// check maybe it already wrapped
22-
var wrappedPart;
23-
if (pathPart.indexOf(this.config.jsonPathWrap) === -1) {
24-
wrappedPart = this.config.jsonPathWrap + pathPart +
25-
this.config.jsonPathWrap;
26-
} else {
27-
wrappedPart = pathPart;
28-
}
29-
return wrappedPart;
20+
Dialect.prototype._wrapJsonIdentifier = function(name) {
21+
return this.config.jsonIdentifierWrap + name + this.config.jsonIdentifierWrap;
3022
};
3123

3224
Dialect.prototype._wrapIdentifier = function(name) {
33-
// check if this is json field
34-
var identifier;
35-
if (this.config.jsonSeparatorRegexp.test(name)) {
36-
var self =this,
37-
regExp = this.config.jsonSeparatorRegexp;
38-
// split and wrap each json path part
39-
var jsonPathArr = _(name.split(regExp)).map(function(pathPart, ind) {
40-
return ind ? (self.config.jsonPathWrap + pathPart + self.config.jsonPathWrap) :
41-
self._wrapIdentifier(pathPart);
42-
});
43-
// and concat it back
44-
var identifier = _(jsonPathArr).reduce(function(memo, pathPart) {
45-
var pathSeparator = regExp.exec(name);
46-
return memo + pathPart + (pathSeparator || '');
47-
}, '');
48-
// set regexp lastIndex to 0
49-
regExp.lastIndex = 0;
50-
} else {
51-
identifier = BaseDialect.prototype._wrapIdentifier.call(this, name);
52-
}
25+
var self = this;
26+
27+
// split by json separator
28+
var nameParts = name.split(this.config.jsonSeparatorRegexp);
29+
var separators = name.match(this.config.jsonSeparatorRegexp);
30+
31+
// wrap base identifier
32+
var identifier = BaseDialect.prototype._wrapIdentifier.call(this, nameParts[0]);
33+
34+
// wrap all json identifier and join them with separators
35+
identifier += _(separators).reduce(function(memo, separator, index) {
36+
return memo + separator + self._wrapJsonIdentifier(nameParts[index + 1]);
37+
}, '');
38+
5339
return identifier;
5440
};

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"devDependencies": {
2323
"chai": "2.2.0",
2424
"gulp": "^3.8.11",
25-
"gulp-mocha": "^2.0.1",
26-
"mocha": "2.2.4"
25+
"gulp-jshint": "^1.10.0",
26+
"gulp-mocha": "^2.0.1"
2727
},
2828
"main": "./lib/index"
2929
}

tests/0_base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe('Builder', function() {
251251
'users.a.b': 1
252252
}
253253
});
254-
}).to.throw('Can\'t wrap identifier with name name "users.a.b"');
254+
}).to.throw('Identifier "users.a.b" contains more than one dot');
255255
});
256256

257257
it('shouldn\'t wrap identifiers twice', function() {

0 commit comments

Comments
 (0)