11'use strict' ;
22
33var _ = require ( 'underscore' ) ;
4+ var objectUtils = require ( '../../utils/object' ) ;
45
56var removeTopBrackets = function ( condition ) {
67 if ( condition . length && condition [ 0 ] === '(' &&
@@ -11,29 +12,9 @@ var removeTopBrackets = function(condition) {
1112 return condition ;
1213} ;
1314
14- // check if object contains any of expected keys
15- var hasSome = function ( obj , keys ) {
16- var objKeys = _ ( obj ) . keys ( ) ;
17- return _ ( keys ) . some ( function ( key ) {
18- return _ ( objKeys ) . contains ( key ) ;
19- } ) ;
20- } ;
21-
2215var termKeys = [ 'select' , 'query' , 'field' , 'value' , 'func' , 'expression' ] ;
2316var isTerm = function ( obj ) {
24- return _ . isObject ( obj ) && ! _ . isArray ( obj ) && hasSome ( obj , termKeys ) ;
25- } ;
26-
27- var isSimpleValue = function ( value ) {
28- return (
29- _ . isString ( value ) ||
30- _ . isNumber ( value ) ||
31- _ . isBoolean ( value ) ||
32- _ . isNull ( value ) ||
33- _ . isUndefined ( value ) ||
34- _ . isRegExp ( value ) ||
35- _ . isDate ( value )
36- ) ;
17+ return objectUtils . isObjectObject ( obj ) && objectUtils . hasSome ( obj , termKeys ) ;
3718} ;
3819
3920module . exports = function ( dialect ) {
@@ -53,7 +34,7 @@ module.exports = function(dialect) {
5334 // If fields is array: ['a', {b: 'c'}, {name: '', table: 't', alias: 'r'}]
5435 if ( _ . isArray ( fields ) ) {
5536 fields = _ ( fields ) . map ( function ( field ) {
56- if ( isSimpleValue ( field ) || isTerm ( field ) || _ . has ( field , 'name' ) ) {
37+ if ( objectUtils . isSimpleValue ( field ) || isTerm ( field ) || _ . has ( field , 'name' ) ) {
5738 // if field has simple type or is field object: {name: '', table: 't', alias: 'r'}
5839 return dialect . buildBlock ( 'term' , { term : field , type : 'field' } ) ;
5940 } else {
@@ -83,11 +64,12 @@ module.exports = function(dialect) {
8364 var term = params . term ;
8465 var type = params . type || 'field' ;
8566
86- var notObject = isSimpleValue ( term ) || _ . isArray ( term ) ;
67+ var isSimpleValue = objectUtils . isSimpleValue ( term ) ;
68+ var isArray = _ . isArray ( term ) ;
8769
88- if ( notObject && ! _ . isString ( term ) ) type = 'value' ;
70+ if ( isSimpleValue && ! _ . isString ( term ) || isArray ) type = 'value' ;
8971
90- if ( notObject || ! isTerm ( term ) ) {
72+ if ( isSimpleValue || ! isTerm ( term ) || isArray ) {
9173 term = _ ( term ) . chain ( ) . pick ( 'cast' , 'alias' ) . extend ( _ . object ( [ type ] , [ term ] ) ) . value ( ) ;
9274 }
9375
@@ -253,7 +235,7 @@ module.exports = function(dialect) {
253235 // if join is object -> set table name from key and make each joinItem
254236 } else if ( _ . isObject ( join ) ) {
255237 result = _ ( join ) . map ( function ( joinItem , table ) {
256- if ( ! hasSome ( joinItem , [ 'table' , 'query' , 'select' , 'expression' ] ) ) {
238+ if ( ! objectUtils . hasSome ( joinItem , [ 'table' , 'query' , 'select' , 'expression' ] ) ) {
257239 joinItem = _ . defaults ( { table : table } , joinItem ) ;
258240 }
259241
0 commit comments