Skip to content

Commit 4d8bf2a

Browse files
feat(is-boolean): add isBoolean helper function
this also checks for booleans created using the Boolean constructor. We implement this for completeness and to have the same logic as isString() and isNumber()
1 parent 62d694c commit 4d8bf2a

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

src/helper/is-boolean.function.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 isBoolean(value: any): boolean {
8+
return typeof value === 'boolean' || getTag(value) === Tag.BOOLEAN
9+
}

src/helper/is-boolean.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { isBoolean } from './is-boolean.function'
2+
3+
describe('is boolean', () => {
4+
it('should be a boolean', () => {
5+
expect(isBoolean(true)).toBeTruthy()
6+
expect(isBoolean(false)).toBeTruthy()
7+
// tslint:disable:no-construct
8+
expect(isBoolean(new Boolean(1))).toBeTruthy()
9+
expect(isBoolean(new Boolean(0))).toBeTruthy()
10+
})
11+
12+
it('should not be a boolean', () => {
13+
expect(isBoolean(0)).toBeFalsy()
14+
expect(isBoolean(1)).toBeFalsy()
15+
expect(isBoolean('a')).toBeFalsy()
16+
expect(isBoolean({})).toBeFalsy()
17+
expect(isBoolean([])).toBeFalsy()
18+
})
19+
})

src/helper/tag.enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export enum Tag {
33
OBJECT = '[object Object]',
44
STRING = '[object String]',
55
NUMBER = '[object Number]',
6+
BOOLEAN = '[object Boolean]',
67
}

src/mapper/util.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ describe('Util', () => {
118118
it('detects bool', () => {
119119
expect(detectType(true)).toBe('BOOL')
120120
expect(detectType(false)).toBe('BOOL')
121+
// tslint:disable-next-line:no-construct
122+
expect(detectType(new Boolean(1))).toBe('BOOL')
121123
})
122124
it('detects collection', () => {
123125
expect(detectType(new Set(['a']))).toBe('SS')

src/mapper/util.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @module mapper
33
*/
44
import { PropertyMetadata } from '../decorator/metadata/property-metadata.model'
5+
import { isBoolean } from '../helper/is-boolean.function'
56
import { isNumber } from '../helper/is-number.function'
67
import { isString } from '../helper/is-string.function'
78
import { ModelConstructor } from '../model/model-constructor'
@@ -155,7 +156,7 @@ export function detectType(value: any): AttributeType {
155156
return 'B'
156157
} else if (value === null) {
157158
return 'NULL'
158-
} else if (typeof value === 'boolean') {
159+
} else if (isBoolean(value)) {
159160
return 'BOOL'
160161
} else if (isCollection(value)) {
161162
return detectCollectionTypeFromValue(value)
@@ -181,19 +182,16 @@ export function typeOf(propertyValue: any, propertyPath?: string | null): Attrib
181182
return Map
182183
} else if (isBinary(propertyValue)) {
183184
return Binary
184-
} else {
185-
switch (typeof propertyValue) {
186-
case 'string':
187-
return String
188-
case 'number':
189-
return Number
190-
case 'boolean':
191-
return Boolean
192-
case 'undefined':
193-
return UndefinedType
194-
case 'object':
195-
return Object
196-
}
185+
} else if (isString(propertyValue)) {
186+
return String
187+
} else if (isNumber(propertyValue)) {
188+
return Number
189+
} else if (isBoolean(propertyValue)) {
190+
return Boolean
191+
} else if (typeof propertyValue === 'undefined') {
192+
return UndefinedType
193+
} else if (typeof propertyValue === 'object') {
194+
return Object
197195
}
198196

199197
throw new Error(messageWithPath(propertyPath, `typeof data ${propertyValue} could not be detected`))

0 commit comments

Comments
 (0)