gulp lint task added, some code refactoring

This commit is contained in:
Artem Zhukov 2015-04-17 00:49:19 +03:00
Родитель a6e67b64c3
Коммит f0da682c7f
9 изменённых файлов: 54 добавлений и 75 удалений

Просмотреть файл

@ -1,32 +1,19 @@
{
"node": true,
"newcap": false,
"supernew": false,
"freeze": true,
"maxlen": 100,
"smarttabs": true,
"indent": 1,
"globalstrict": true,
"quotmark": "single",
"strict": true,
"globalstrict": true,
// use es3 to get 'extra comma' at object literal error
"es3": true,
// suppress warnings about switches with just one case
"onecase": true,
"nonew": false,
"trailing": true,
"sub": false,
"loopfunc": true,
"boss": false,
"lastsemic": false,
"quotmark": "single",
"undef": true,
"unused": true,
"immed": true,
// allows ECMAScript 6 syntax (generators, etc)
"esnext": true,
// allow generators without a yield
"noyield": true,
"eqeqeq": true,
"globals": {
"describe": false,
"it": false,
"before": false
"JSON": false
}
}

Просмотреть файл

@ -2,9 +2,10 @@
var gulp = require('gulp');
var mocha = require('gulp-mocha');
var jshint = require('gulp-jshint');
gulp.task('test', function() {
return gulp.src(['tests/*.js'], {read: false})
return gulp.src(['./tests/*.js'], {read: false})
.pipe(
mocha({
reporter: 'spec',
@ -12,3 +13,9 @@ gulp.task('test', function() {
})
);
});
gulp.task('lint', function() {
return gulp.src('./lib/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('unix'));
});

Просмотреть файл

@ -35,7 +35,7 @@ module.exports = function(dialect) {
if (expression) {
if (!_.isArray(expression)) expression = [expression];
var exprSuffix = Array(expression.length + 1).join(')');
var exprSuffix = (new Array(expression.length + 1)).join(')');
field = expression.join('(') + '(' + field + exprSuffix;
}
} else {

Просмотреть файл

@ -111,9 +111,12 @@ module.exports = function(dialect) {
throw new Error('`$between` array length should be 2 or greater');
}
var placeholder1 = this._pushValue(value[0]);
var placeholder2 = this._pushValue(value[1]);
return [this.dialect._wrapIdentifier(field), 'between', placeholder1, 'and', placeholder2].join(' ');
return [
this.dialect._wrapIdentifier(field),
'between',
this._pushValue(value[0]),
'and',
this._pushValue(value[1])
].join(' ');
});
};

Просмотреть файл

@ -1,6 +1,5 @@
'use strict';
var _ = require('underscore');
var ValuesStore = require('../../valuesStore');
var templatesInit = require('./templates');
@ -24,9 +23,8 @@ var Dialect = module.exports = function(builder) {
modifiersInit(this);
logicalOperatorsInit(this);
this.config.wrapIdentifierRegexp = new RegExp(
'^\\' + this.config.identifierPrefix + '.*\\' +
this.config.identifierSuffix + '$'
this.config.wrappedIdentifierRegexp = new RegExp(
'^\\' + this.config.identifierPrefix + '.*\\' + this.config.identifierSuffix + '$'
);
};
@ -39,18 +37,16 @@ Dialect.prototype.config = {
Dialect.prototype._wrapIdentifier = function(name) {
if (name !== '*' && this.builder.options.wrappedIdentifiers &&
!this.config.wrapIdentifierRegexp.test(name)) {
!this.config.wrappedIdentifierRegexp.test(name)) {
// try to split name by dot
var nameParts = name.split('.');
if (nameParts.length > 2) {
throw new Error('Can\'t wrap identifier with name name "' + name + '"');
throw new Error('Identifier "' + name + '" contains more than one dot');
} else if (nameParts.length === 2) {
name = this._wrapIdentifier(nameParts[0]) + '.' +
this._wrapIdentifier(nameParts[1]);
name = this._wrapIdentifier(nameParts[0]) + '.' + this._wrapIdentifier(nameParts[1]);
} else {
name = this.config.identifierPrefix + name +
this.config.identifierSuffix;
name = this.config.identifierPrefix + name + this.config.identifierSuffix;
}
}

Просмотреть файл

@ -2,7 +2,7 @@
var _ = require('underscore');
var buildInCondition = function(builder, field, operator, value) {
var buildJsonInCondition = function(builder, field, operator, value) {
field = builder.dialect._wrapIdentifier(field);
var placeholder;
@ -15,7 +15,7 @@ var buildInCondition = function(builder, field, operator, value) {
return [field, operator, placeholder].join(' ');
};
var buildHasCondition = function(builder, field, operator, value) {
var buildJsonHasCondition = function(builder, field, operator, value) {
field = builder.dialect._wrapIdentifier(field);
var placeholder;
@ -32,11 +32,11 @@ var buildHasCondition = function(builder, field, operator, value) {
module.exports = function(dialect) {
dialect.conditions.add('$jsonContains', function(field, operator, value) {
return buildInCondition(this, field, '@>', value);
return buildJsonInCondition(this, field, '@>', value);
});
dialect.conditions.add('$jsonIn', function(field, operator, value) {
return buildInCondition(this, field, '<@', value);
return buildJsonInCondition(this, field, '<@', value);
});
dialect.conditions.add('$jsonHas', function(field, operator, value) {
@ -53,10 +53,10 @@ module.exports = function(dialect) {
});
dialect.conditions.add('$jsonHasAny', function(field, operator, value) {
return buildHasCondition(this, field, '?|', value);
return buildJsonHasCondition(this, field, '?|', value);
});
dialect.conditions.add('$jsonHasAll', function(field, operator, value) {
return buildHasCondition(this, field, '?&', value);
return buildJsonHasCondition(this, field, '?&', value);
});
};

Просмотреть файл

@ -14,41 +14,27 @@ util.inherits(Dialect, BaseDialect);
Dialect.prototype.config = _({
jsonSeparatorRegexp: /->>?/g,
jsonPathWrap: '\''
jsonIdentifierWrap: '\''
}).extend(BaseDialect.prototype.config);
Dialect.prototype._wrapJsonPathPart = function(pathPart) {
// check maybe it already wrapped
var wrappedPart;
if (pathPart.indexOf(this.config.jsonPathWrap) === -1) {
wrappedPart = this.config.jsonPathWrap + pathPart +
this.config.jsonPathWrap;
} else {
wrappedPart = pathPart;
}
return wrappedPart;
Dialect.prototype._wrapJsonIdentifier = function(name) {
return this.config.jsonIdentifierWrap + name + this.config.jsonIdentifierWrap;
};
Dialect.prototype._wrapIdentifier = function(name) {
// check if this is json field
var identifier;
if (this.config.jsonSeparatorRegexp.test(name)) {
var self =this,
regExp = this.config.jsonSeparatorRegexp;
// split and wrap each json path part
var jsonPathArr = _(name.split(regExp)).map(function(pathPart, ind) {
return ind ? (self.config.jsonPathWrap + pathPart + self.config.jsonPathWrap) :
self._wrapIdentifier(pathPart);
});
// and concat it back
var identifier = _(jsonPathArr).reduce(function(memo, pathPart) {
var pathSeparator = regExp.exec(name);
return memo + pathPart + (pathSeparator || '');
}, '');
// set regexp lastIndex to 0
regExp.lastIndex = 0;
} else {
identifier = BaseDialect.prototype._wrapIdentifier.call(this, name);
}
var self = this;
// split by json separator
var nameParts = name.split(this.config.jsonSeparatorRegexp);
var separators = name.match(this.config.jsonSeparatorRegexp);
// wrap base identifier
var identifier = BaseDialect.prototype._wrapIdentifier.call(this, nameParts[0]);
// wrap all json identifier and join them with separators
identifier += _(separators).reduce(function(memo, separator, index) {
return memo + separator + self._wrapJsonIdentifier(nameParts[index + 1]);
}, '');
return identifier;
};

Просмотреть файл

@ -22,8 +22,8 @@
"devDependencies": {
"chai": "2.2.0",
"gulp": "^3.8.11",
"gulp-mocha": "^2.0.1",
"mocha": "2.2.4"
"gulp-jshint": "^1.10.0",
"gulp-mocha": "^2.0.1"
},
"main": "./lib/index"
}

Просмотреть файл

@ -251,7 +251,7 @@ describe('Builder', function() {
'users.a.b': 1
}
});
}).to.throw('Can\'t wrap identifier with name name "users.a.b"');
}).to.throw('Identifier "users.a.b" contains more than one dot');
});
it('shouldn\'t wrap identifiers twice', function() {