Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: toInt and toFloat symbol input #67

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/number/toFloat.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
export function toFloat<T extends number | null = number>(
import { isSymbol } from 'radashi'

export function toFloat(value: unknown): number

export function toFloat(value: unknown, defaultValue: number): number

export function toFloat<T>(
value: unknown,
defaultValue: T,
): number | Exclude<T, undefined>

export function toFloat<T>(
value: any,
defaultValue?: T,
): number | T {
const def = defaultValue === undefined ? 0.0 : defaultValue
if (value === null || value === undefined) {
return def
}
const result = Number.parseFloat(value)
return Number.isNaN(result) ? def : result
): number | Exclude<T, undefined> {
// Symbols throw on string coercion, which parseFloat does.
const parsedValue = isSymbol(value) ? Number.NaN : Number.parseFloat(value)
return Number.isNaN(parsedValue)
? defaultValue !== undefined
? (defaultValue as Exclude<T, undefined>)
: 0
: parsedValue
}
28 changes: 20 additions & 8 deletions src/number/toInt.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
export function toInt<T extends number | null = number>(
import { isSymbol } from 'radashi'

export function toInt(value: unknown): number

export function toInt(value: unknown, defaultValue: number): number

export function toInt<T>(
value: unknown,
defaultValue: T,
): number | Exclude<T, undefined>

export function toInt<T>(
value: any,
defaultValue?: T,
): number | T {
const def = defaultValue === undefined ? 0 : defaultValue
if (value === null || value === undefined) {
return def
}
const result = Number.parseInt(value)
return Number.isNaN(result) ? def : result
): number | Exclude<T, undefined> {
// Symbols throw on string coercion, which parseInt does.
const parsedValue = isSymbol(value) ? Number.NaN : Number.parseInt(value)
return Number.isNaN(parsedValue)
? defaultValue !== undefined
? (defaultValue as Exclude<T, undefined>)
: 0
: parsedValue
}
27 changes: 16 additions & 11 deletions tests/number/toFloat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ import * as _ from 'radashi'

describe('toFloat', () => {
test('handles null', () => {
const result = _.toFloat(null)
expect(result).toBe(0.0)
expect(_.toFloat(null)).toBe(0.0)
})
test('handles undefined', () => {
const result = _.toFloat(undefined)
expect(result).toBe(0.0)
expect(_.toFloat(undefined)).toBe(0.0)
})
test('uses null default', () => {
const result = _.toFloat('x', null)
expect(result).toBeNull()
expect(_.toFloat('x', null)).toBeNull()
})
test('handles bad input', () => {
const result = _.toFloat({})
expect(result).toBe(0.0)
expect(_.toFloat({})).toBe(0.0)
})
test('converts 20.00 correctly', () => {
const result = _.toFloat('20.00')
expect(result).toBe(20.0)
test('do not throw on symbols', () => {
expect(_.toFloat(Symbol())).toBe(0)
})
test('convert "12.34" to 12.34', () => {
expect(_.toFloat('12.34')).toBe(12.34)
})
test('preserve infinite values', () => {
expect(_.toFloat(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
expect(_.toFloat(Number.NEGATIVE_INFINITY)).toBe(Number.NEGATIVE_INFINITY)
})
test('convert NaN to default', () => {
expect(_.toFloat(Number.NaN, 1)).toBe(1)
})
})
51 changes: 18 additions & 33 deletions tests/number/toInt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,31 @@ import * as _ from 'radashi'

describe('toInt', () => {
test('handles null', () => {
const result = _.toInt(null)
expect(result).toBe(0)
expect(_.toInt(null)).toBe(0)
})
test('uses null default', () => {
const result = _.toInt('x', null)
expect(result).toBeNull()
expect(_.toInt('x', null)).toBeNull()
})
test('handles undefined', () => {
const result = _.toInt(undefined)
expect(result).toBe(0)
expect(_.toInt(undefined)).toBe(0)
})
test('handles bad input', () => {
const result = _.toInt({})
expect(result).toBe(0)
expect(_.toInt({})).toBe(0)
})
test('converts 20 correctly', () => {
const result = _.toInt('20')
expect(result).toBe(20)
test('do not throw on symbols', () => {
expect(_.toInt(Symbol())).toBe(0)
})
})

describe('isInt', () => {
class Data {}
test('returns false for non-number values', () => {
expect(_.isInt(undefined)).toBeFalsy()
expect(_.isInt(null)).toBeFalsy()
expect(_.isInt(false)).toBeFalsy()
expect(_.isInt(new Data())).toBeFalsy()
expect(_.isInt(Number.NaN)).toBeFalsy()
expect(_.isInt([1, 2, 3])).toBeFalsy()
expect(_.isInt({})).toBeFalsy()
expect(_.isInt('abc')).toBeFalsy()
expect(_.isInt(String('abc'))).toBeFalsy()
})
test('returns true for int', () => {
const result = _.isInt(22)
expect(result).toBeTruthy()
})
test('returns false for float', () => {
const result = _.isInt(22.0567)
expect(result).toBeFalsy()
test('convert "20" to 20', () => {
expect(_.toInt('20')).toBe(20)
})
test('convert 1.23 to 1', () => {
expect(_.toInt(1.23)).toBe(1)
})
test('convert infinite values to default', () => {
expect(_.toInt(Number.POSITIVE_INFINITY, 1)).toBe(1)
expect(_.toInt(Number.NEGATIVE_INFINITY, 1)).toBe(1)
})
test('convert NaN to default', () => {
expect(_.toInt(Number.NaN, 1)).toBe(1)
})
})