Skip to content

Commit

Permalink
feat(utils): isFloat, isInteger
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 16, 2023
1 parent 4a6f2bc commit 2612779
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 7 deletions.
2 changes: 1 addition & 1 deletion __fixtures__/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* @module fixtures/FLOAT
*/

import INT from './int'
import INT from './integer'

export default INT / 100
6 changes: 0 additions & 6 deletions __fixtures__/int.ts

This file was deleted.

6 changes: 6 additions & 0 deletions __fixtures__/integer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Fixtures - INTEGER
* @module fixtures/INTEGER
*/

export default 13
13 changes: 13 additions & 0 deletions src/utils/__tests__/is-float.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Tests - isFloat
* @module tutils/guards/tests/unit-d/isFloat
*/

import type { Float } from '#src/types'
import type testSubject from '../is-float'

describe('unit-d:guards/isFloat', () => {
it('should guard Float', () => {
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf<Float>()
})
})
18 changes: 18 additions & 0 deletions src/utils/__tests__/is-float.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file Unit Tests - isFloat
* @module tutils/utils/tests/unit/isFloat
*/

import FLOAT from '#fixtures/float'
import INT from '#fixtures/integer'
import testSubject from '../is-float'

describe('unit:utils/isFloat', () => {
it('should return false if value is not a float', () => {
expect(testSubject(INT)).to.be.false
})

it('should return true if value is a float', () => {
expect(testSubject(FLOAT)).to.be.true
})
})
13 changes: 13 additions & 0 deletions src/utils/__tests__/is-integer.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Tests - isInteger
* @module tutils/guards/tests/unit-d/isInteger
*/

import type { Integer } from '#src/types'
import type testSubject from '../is-integer'

describe('unit-d:guards/isInteger', () => {
it('should guard Integer', () => {
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf<Integer>()
})
})
20 changes: 20 additions & 0 deletions src/utils/__tests__/is-integer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file Unit Tests - isInteger
* @module tutils/utils/tests/unit/isInteger
*/

import FLOAT from '#fixtures/float'
import INTEGER from '#fixtures/integer'
import testSubject from '../is-integer'

describe('unit:utils/isInteger', () => {
it('should return false if value is not an integer', () => {
expect(testSubject(FLOAT)).to.be.false
})

it('should return true if value is an integer', () => {
expect(testSubject(INTEGER)).to.be.true
expect(testSubject(INTEGER * 0)).to.be.true
expect(testSubject(INTEGER * -1)).to.be.true
})
})
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export { default as isBoolean } from './is-boolean'
export { default as isBooleanish } from './is-booleanish'
export { default as isEmptyString } from './is-empty-string'
export { default as isEmptyValue } from './is-empty-value'
export { default as isFloat } from './is-float'
export { default as isFunction } from './is-function'
export { default as isInteger } from './is-integer'
export { default as isJsonPrimitive } from './is-json-primitive'
export { default as isLowercase } from './is-lowercase'
export { default as isNIL } from './is-nil'
Expand Down
21 changes: 21 additions & 0 deletions src/utils/is-float.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file Utilities - isFloat
* @module tutils/utils/isFloat
*/

import type { Float } from '#src/types'
import isNumber from './is-number'

/**
* Checks if `value` is a floating point number.
*
* @see {@linkcode Float}
*
* @param {unknown} value - Value to check
* @return {value is Float} `true` if `value` is a float
*/
function isFloat(value: unknown): value is Float {
return isNumber(value) && value % 1 !== 0
}

export default isFloat
21 changes: 21 additions & 0 deletions src/utils/is-integer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file Utilities - isInteger
* @module tutils/utils/isInteger
*/

import type { Integer } from '#src/types'
import isNumber from './is-number'

/**
* Checks if `value` is an integer.
*
* @see {@linkcode Integer}
*
* @param {unknown} value - Value to check
* @return {value is Integer} `true` if `value` is an integer
*/
function isInteger(value: unknown): value is Integer {
return isNumber(value) && value % 1 === 0
}

export default isInteger

0 comments on commit 2612779

Please sign in to comment.