File tree Expand file tree Collapse file tree 10 files changed +78
-5
lines changed Expand file tree Collapse file tree 10 files changed +78
-5
lines changed Original file line number Diff line number Diff line change 3
3
*/
4
4
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
5
5
import { isEmpty } from '../../helper/is-empty.function'
6
+ import { isString } from '../../helper/is-string.function'
6
7
import { ConditionalParams } from '../operation-params.type'
7
8
import { resolveAttributeValueNameConflicts } from './functions/resolve-attribute-value-name-conflicts.function'
8
9
import { Expression } from './type/expression.type'
@@ -44,7 +45,7 @@ export function addExpression(
44
45
}
45
46
46
47
const statement = params [ expressionType ]
47
- if ( typeof statement === 'string' && statement !== '' ) {
48
+ if ( isString ( statement ) && statement !== '' ) {
48
49
switch ( expressionType ) {
49
50
case 'UpdateExpression' :
50
51
; ( < any > params ) [ expressionType ] = mergeUpdateExpressions ( statement , nameSafeCondition . statement )
Original file line number Diff line number Diff line change
1
+ import { Tag } from './tag.enum'
2
+
3
+ /**
4
+ * @return Returns the value (we call it tag) returned by function call `value.toString`,
5
+ */
6
+ export function getTag ( value : any ) : Tag | string {
7
+ return Object . prototype . toString . call ( value )
8
+ }
Original file line number Diff line number Diff line change
1
+ import { getTag } from './get-tag.function'
2
+ import { Tag } from './tag.enum'
3
+
4
+ /**
5
+ * @return Returns true for any value where typeof equals 'number' or an object created with Number constructor
6
+ */
7
+ export function isNumber ( value : any ) : boolean {
8
+ return typeof value === 'number' || getTag ( value ) === Tag . NUMBER
9
+ }
Original file line number Diff line number Diff line change
1
+ import { isNumber } from './is-number.function'
2
+
3
+ describe ( 'is number' , ( ) => {
4
+ it ( 'should be a number' , ( ) => {
5
+ expect ( isNumber ( 3 ) ) . toBeTruthy ( )
6
+ expect ( isNumber ( NaN ) ) . toBeTruthy ( )
7
+ expect ( isNumber ( Infinity ) ) . toBeTruthy ( )
8
+ // tslint:disable:no-construct
9
+ expect ( isNumber ( new Number ( '2' ) ) ) . toBeTruthy ( )
10
+ expect ( isNumber ( new Number ( 'myNumber' ) ) ) . toBeTruthy ( )
11
+ } )
12
+
13
+ it ( 'should not be a number' , ( ) => {
14
+ expect ( isNumber ( 'a' ) ) . toBeFalsy ( )
15
+ expect ( isNumber ( { } ) ) . toBeFalsy ( )
16
+ expect ( isNumber ( [ ] ) ) . toBeFalsy ( )
17
+ } )
18
+ } )
Original file line number Diff line number Diff line change 1
1
// https://github.com/jonschlinkert/is-plain-object
2
+ import { getTag } from './get-tag.function'
3
+ import { Tag } from './tag.enum'
2
4
3
5
function isObject ( val : any ) {
4
6
return val != null && typeof val === 'object' && Array . isArray ( val ) === false
5
7
}
6
8
7
9
function isObjectObject ( o : any ) : boolean {
8
- return isObject ( o ) === true && Object . prototype . toString . call ( o ) === '[object Object]'
10
+ return isObject ( o ) === true && getTag ( o ) === Tag . OBJECT
9
11
}
10
12
11
13
export function isPlainObject ( o : any ) : boolean {
Original file line number Diff line number Diff line change
1
+ import { getTag } from './get-tag.function'
2
+ import { Tag } from './tag.enum'
3
+
4
+ /**
5
+ * @return Returns true for any value where typeof equals 'string' or an object created with String constructor
6
+ */
7
+ export function isString ( value : any ) : boolean {
8
+ return typeof value === 'string' || getTag ( value ) === Tag . STRING
9
+ }
Original file line number Diff line number Diff line change
1
+ import { isString } from './is-string.function'
2
+
3
+ describe ( 'is string' , ( ) => {
4
+ it ( 'should be a string' , ( ) => {
5
+ expect ( isString ( 'myValue' ) ) . toBeTruthy ( )
6
+ // tslint:disable:no-construct
7
+ expect ( isString ( new String ( '2' ) ) ) . toBeTruthy ( )
8
+ expect ( isString ( new String ( 'someValue' ) ) ) . toBeTruthy ( )
9
+ } )
10
+
11
+ it ( 'should not be a string' , ( ) => {
12
+ expect ( isString ( 3 ) ) . toBeFalsy ( )
13
+ expect ( isString ( true ) ) . toBeFalsy ( )
14
+ expect ( isString ( { } ) ) . toBeFalsy ( )
15
+ expect ( isString ( [ ] ) ) . toBeFalsy ( )
16
+ } )
17
+ } )
Original file line number Diff line number Diff line change
1
+ /** used to compare with value returned by @link getTag(value), this is not a complete list only the one values used are defined */
2
+ export enum Tag {
3
+ OBJECT = '[object Object]' ,
4
+ STRING = '[object String]' ,
5
+ NUMBER = '[object Number]' ,
6
+ }
Original file line number Diff line number Diff line change 1
1
/**
2
2
* @module mapper
3
3
*/
4
+ import { isNumber } from '../../helper/is-number.function'
4
5
import { NumberAttribute } from '../type/attribute.type'
5
6
import { MapperForType } from './base.mapper'
6
7
@@ -17,7 +18,7 @@ function numberFromDb(attributeValue: NumberAttribute): number {
17
18
}
18
19
19
20
function numberToDb ( modelValue : number ) : NumberAttribute | null {
20
- if ( typeof modelValue !== 'number' ) {
21
+ if ( ! isNumber ( modelValue ) ) {
21
22
throw new Error ( `this mapper only support values of type number, value given: ${ JSON . stringify ( modelValue ) } ` )
22
23
}
23
24
Original file line number Diff line number Diff line change 2
2
* @module mapper
3
3
*/
4
4
import { PropertyMetadata } from '../decorator/metadata/property-metadata.model'
5
+ import { isNumber } from '../helper/is-number.function'
6
+ import { isString } from '../helper/is-string.function'
5
7
import { ModelConstructor } from '../model/model-constructor'
6
8
import { AttributeCollectionType , AttributeType } from './type/attribute-type.type'
7
9
import { AttributeValueType } from './type/attribute-value-type.type'
@@ -145,9 +147,9 @@ export function isSet(value: any): value is Set<any> {
145
147
* @hidden
146
148
*/
147
149
export function detectType ( value : any ) : AttributeType {
148
- if ( typeof value === 'string' ) {
150
+ if ( isString ( value ) ) {
149
151
return 'S'
150
- } else if ( typeof value === 'number' ) {
152
+ } else if ( isNumber ( value ) ) {
151
153
return 'N'
152
154
} else if ( isBinary ( value ) ) {
153
155
return 'B'
You can’t perform that action at this time.
0 commit comments