11'use strict' ;
22
3- const _ = require ( 'lodash' ) ;
4- const utils = require ( './utils' ) ;
5- const serializer = require ( './serializer' ) ;
3+ const Utils = require ( './utils' ) ;
4+ const Serializer = require ( './serializer' ) ;
5+ const Includes = require ( 'lodash.includes' ) ;
6+ const IsDate = require ( 'lodash.isdate' ) ;
7+ const IsPlainObject = require ( 'lodash.isplainobject' ) ;
8+ const IsString = require ( 'lodash.isstring' ) ;
9+ const IsEmpty = require ( 'lodash.isempty' ) ;
10+ const Reduce = require ( 'lodash.reduce' ) ;
11+ const Keys = require ( 'lodash.keys' ) ;
612
713const internals = { } ;
814
915internals . actionWords = [ 'SET' , 'ADD' , 'REMOVE' , 'DELETE' ] ;
1016
11- internals . regexMap = _ . reduce ( internals . actionWords , ( result , key ) => {
17+ internals . regexMap = Reduce ( internals . actionWords , ( result , key ) => {
1218 result [ key ] = new RegExp ( `${ key } \\s*(.+?)\\s*(SET|ADD|REMOVE|DELETE|$)` ) ;
1319 return result ;
1420} , { } ) ;
@@ -26,46 +32,46 @@ internals.match = (actionWord, str) => {
2632 }
2733} ;
2834
29- exports . parse = str => _ . reduce ( internals . actionWords , ( result , actionWord ) => {
35+ exports . parse = str => Reduce ( internals . actionWords , ( result , actionWord ) => {
3036 result [ actionWord ] = internals . match ( actionWord , str ) ;
3137 return result ;
3238} , { } ) ;
3339
3440exports . serializeUpdateExpression = ( schema , item ) => {
3541 const datatypes = schema . _modelDatatypes ;
3642
37- const data = utils . omitPrimaryKeys ( schema , item ) ;
43+ const data = Utils . omitPrimaryKeys ( schema , item ) ;
3844
3945 const memo = {
4046 expressions : { } ,
4147 attributeNames : { } ,
4248 values : { } ,
4349 } ;
4450
45- memo . expressions = _ . reduce ( internals . actionWords , ( result , key ) => {
51+ memo . expressions = Reduce ( internals . actionWords , ( result , key ) => {
4652 result [ key ] = [ ] ;
4753
4854 return result ;
4955 } , { } ) ;
5056
51- const result = _ . reduce ( data , ( result , value , key ) => {
57+ const result = Reduce ( data , ( result , value , key ) => {
5258 const valueKey = `:${ key } ` ;
5359 const nameKey = `#${ key } ` ;
5460
55- if ( _ . isNull ( value ) || ( _ . isString ( value ) && _ . isEmpty ( value ) ) ) {
61+ if ( value === null || ( IsString ( value ) && IsEmpty ( value ) ) ) {
5662 result . expressions . REMOVE . push ( nameKey ) ;
5763 result . attributeNames [ nameKey ] = key ;
58- } else if ( _ . isPlainObject ( value ) && value . $add ) {
64+ } else if ( IsPlainObject ( value ) && value . $add ) {
5965 result . expressions . ADD . push ( `${ nameKey } ${ valueKey } ` ) ;
60- result . values [ valueKey ] = serializer . serializeAttribute ( value . $add , datatypes [ key ] ) ;
66+ result . values [ valueKey ] = Serializer . serializeAttribute ( value . $add , datatypes [ key ] ) ;
6167 result . attributeNames [ nameKey ] = key ;
62- } else if ( _ . isPlainObject ( value ) && value . $del ) {
68+ } else if ( IsPlainObject ( value ) && value . $del ) {
6369 result . expressions . DELETE . push ( `${ nameKey } ${ valueKey } ` ) ;
64- result . values [ valueKey ] = serializer . serializeAttribute ( value . $del , datatypes [ key ] ) ;
70+ result . values [ valueKey ] = Serializer . serializeAttribute ( value . $del , datatypes [ key ] ) ;
6571 result . attributeNames [ nameKey ] = key ;
6672 } else {
6773 result . expressions . SET . push ( `${ nameKey } = ${ valueKey } ` ) ;
68- result . values [ valueKey ] = serializer . serializeAttribute ( value , datatypes [ key ] ) ;
74+ result . values [ valueKey ] = Serializer . serializeAttribute ( value , datatypes [ key ] ) ;
6975 result . attributeNames [ nameKey ] = key ;
7076 }
7177
@@ -75,9 +81,9 @@ exports.serializeUpdateExpression = (schema, item) => {
7581 return result ;
7682} ;
7783
78- exports . stringify = expressions => _ . reduce ( expressions , ( result , value , key ) => {
79- if ( ! _ . isEmpty ( value ) ) {
80- if ( _ . isArray ( value ) ) {
84+ exports . stringify = expressions => Reduce ( expressions , ( result , value , key ) => {
85+ if ( ! IsEmpty ( value ) ) {
86+ if ( Array . isArray ( value ) ) {
8187 result . push ( `${ key } ${ value . join ( ', ' ) } ` ) ;
8288 } else {
8389 result . push ( `${ key } ${ value } ` ) ;
@@ -88,27 +94,27 @@ exports.stringify = expressions => _.reduce(expressions, (result, value, key) =>
8894} , [ ] ) . join ( ' ' ) ;
8995
9096internals . formatAttributeValue = val => {
91- if ( _ . isDate ( val ) ) {
97+ if ( IsDate ( val ) ) {
9298 return val . toISOString ( ) ;
9399 }
94100
95101 return val ;
96102} ;
97103
98- internals . isFunctionOperator = operator => _ . includes ( [ 'attribute_exists' ,
99- 'attribute_not_exists' ,
100- 'attribute_type' ,
101- 'begins_with' ,
102- 'contains' ,
103- 'NOT contains' ,
104- 'size' ] , operator ) ;
104+ internals . isFunctionOperator = operator => Includes ( [ 'attribute_exists' ,
105+ 'attribute_not_exists' ,
106+ 'attribute_type' ,
107+ 'begins_with' ,
108+ 'contains' ,
109+ 'NOT contains' ,
110+ 'size' ] , operator ) ;
105111
106112internals . uniqAttributeValueName = ( key , existingValueNames ) => {
107113 const cleanedKey = key . replace ( / \. / g, '_' ) . replace ( / \W / g, '' ) ;
108114 let potentialName = `:${ cleanedKey } ` ;
109115 let idx = 1 ;
110116
111- while ( _ . includes ( existingValueNames , potentialName ) ) {
117+ while ( Includes ( existingValueNames , potentialName ) ) {
112118 idx ++ ;
113119 potentialName = `:${ cleanedKey } _${ idx } ` ;
114120 }
@@ -140,7 +146,7 @@ exports.buildFilterExpression = (key, operator, existingValueNames, val1, val2)
140146 let statement = '' ;
141147
142148 if ( internals . isFunctionOperator ( operator ) ) {
143- if ( ! _ . isNull ( v1 ) && ! _ . isUndefined ( v1 ) ) {
149+ if ( v1 !== null && v1 !== undefined ) {
144150 statement = `${ operator } (${ path } , ${ v1ValueName } )` ;
145151 } else {
146152 statement = `${ operator } (${ path } )` ;
@@ -153,11 +159,11 @@ exports.buildFilterExpression = (key, operator, existingValueNames, val1, val2)
153159
154160 const attributeValues = { } ;
155161
156- if ( ! _ . isNull ( v1 ) && ! _ . isUndefined ( v1 ) ) {
162+ if ( v1 !== null && v1 !== undefined ) {
157163 attributeValues [ v1ValueName ] = v1 ;
158164 }
159165
160- if ( ! _ . isNull ( v2 ) && ! _ . isUndefined ( v2 ) ) {
166+ if ( v2 !== null && v2 !== undefined ) {
161167 attributeValues [ v2ValueName ] = v2 ;
162168 }
163169
@@ -179,16 +185,16 @@ internals.buildInFilterExpression = (key, existingValueNames, values) => {
179185 const attributeNames = { } ;
180186 attributeNames [ path . split ( '.' ) [ 0 ] ] = key . split ( '.' ) [ 0 ] ;
181187
182- const attributeValues = _ . reduce ( values , ( result , val ) => {
183- const existing = _ . keys ( result ) . concat ( existingValueNames ) ;
188+ const attributeValues = Reduce ( values , ( result , val ) => {
189+ const existing = Keys ( result ) . concat ( existingValueNames ) ;
184190 const p = internals . uniqAttributeValueName ( key , existing ) ;
185191 result [ p ] = internals . formatAttributeValue ( val ) ;
186192 return result ;
187193 } , { } ) ;
188194
189195 return {
190196 attributeNames : attributeNames ,
191- statement : `${ path } IN (${ _ . keys ( attributeValues ) } )` ,
197+ statement : `${ path } IN (${ Keys ( attributeValues ) } )` ,
192198 attributeValues : attributeValues
193199 } ;
194200} ;
0 commit comments