-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { read } from './read'; | ||
import { toFloat } from './utils'; | ||
|
||
export function readFloat(key: string) : number | undefined; | ||
export function readFloat<T>(key: string, alt: T) : T | number; | ||
export function readFloat<T>(key: string, alt?: T) : any { | ||
const value = read(key) as any; | ||
if (typeof value !== 'undefined') { | ||
return toFloat(value) ?? alt; | ||
} | ||
|
||
return alt; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function toFloat(value: any) : number | undefined { | ||
const num = Number.parseFloat(value); | ||
if (Number.isNaN(num) || Number.isNaN(value)) { | ||
return undefined; | ||
} | ||
|
||
return num; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './bool'; | ||
export * from './float'; | ||
export * from './int'; | ||
export * from './number'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { readFloat, write } from '../../src'; | ||
|
||
describe('src/read-float.ts', () => { | ||
it('should read float env', () => { | ||
write('foo', '1'); | ||
|
||
let result = readFloat('foo'); | ||
expect(result).toEqual(1.0); | ||
|
||
write('foo', 'foo'); | ||
|
||
result = readFloat('foo'); | ||
expect(result).toBeUndefined(); | ||
|
||
write('foo', undefined); | ||
|
||
result = readFloat('foo', 2.0); | ||
expect(result).toEqual(2.0); | ||
|
||
result = readFloat('foo'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters