-
-
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
9 changed files
with
63 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
# EditorConfig: http://EditorConfig.org | ||
# EditorConfig is awesome: https://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.{js,d.ts,ts}] | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
# 2 space indentation | ||
[*.yaml, *.yml] | ||
[package.json,*.yaml] | ||
indent_style = space | ||
indent_size = 2 |
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 @@ | ||
* text=auto eol=lf |
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,5 +1,6 @@ | ||
# Global | ||
node_modules/ | ||
coverage | ||
|
||
# OS Generated | ||
.DS_Store* | ||
|
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,6 @@ | ||
interface Options { | ||
min?: number; | ||
max?: number; | ||
fixed?: number; | ||
} | ||
export default function float(options?: Options): 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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
/* eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}] */ | ||
export default function float(options) { | ||
options = options || {}; | ||
let fixed = Math.pow(10, ((options.fixed === undefined) ? 4 : options.fixed)); | ||
let max = (options.max === undefined) ? Number.MAX_SAFE_INTEGER / fixed : options.max; | ||
let min = (options.min === undefined) ? Number.MIN_SAFE_INTEGER / fixed : options.min; | ||
const fixed = 10 ** options.fixed === undefined ? 4 : options.fixed; | ||
const max = (options.max === undefined) ? Number.MAX_SAFE_INTEGER / fixed : options.max; | ||
const min = (options.min === undefined) ? Number.MIN_SAFE_INTEGER / fixed : options.min; | ||
if (typeof min !== 'number' || typeof max !== 'number') { | ||
throw new TypeError('Expected all arguments to be numbers.'); | ||
} | ||
throw new TypeError('Expected all arguments to be numbers.'); | ||
} | ||
|
||
if (min > max) { | ||
throw new TypeError('Min cannot be greater than Max.'); | ||
} | ||
let result = Math.random() * (max - min) + min; | ||
return Number(Number(parseFloat(result.toString())).toFixed(4)); | ||
}; | ||
throw new TypeError('Min cannot be greater than Max.'); | ||
} | ||
|
||
const result = Math.random() * ((max - min) + min); | ||
return Number(Number(Number.parseFloat(result.toString())).toFixed(4)); | ||
} |
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,7 @@ | ||
import {expectType} from 'tsd'; | ||
import float from './index.js'; | ||
|
||
expectType<number>(float()); | ||
expectType<number>(float({min: 0})); | ||
expectType<number>(float({min: 0, max: 10})); | ||
expectType<number>(float({min: 0, max: 10, fixed: 10})); |
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 |
---|---|---|
@@ -1,34 +1,39 @@ | ||
import float from './index.js'; | ||
import test from 'ava'; | ||
import float from './index.js'; | ||
|
||
test('float return type to be number', t => { | ||
t.is(typeof float(), 'number'); | ||
t.is(typeof float(), 'number'); | ||
}); | ||
|
||
test('float with number min 0 return type to be number', t => { | ||
t.is(typeof float({min: 0}), 'number'); | ||
t.is(typeof float({min: 0}), 'number'); | ||
}); | ||
|
||
test('float with number min 0 and max 10 return type to be number', t => { | ||
t.is(typeof float({min: 0, max: 10}), 'number'); | ||
t.is(typeof float({min: 0, max: 10}), 'number'); | ||
}); | ||
|
||
test('float with number min 0 and max 10 less than 11', t => { | ||
t.true(float({min: 0, max: 10}) < 11); | ||
t.true(float({min: 0, max: 10}) < 11); | ||
}); | ||
|
||
test('float with number min 0 and max 10 and fixed 10 less than 11', t => { | ||
t.true(float({min: 0, max: 10, fixed: 10}) < 11); | ||
}); | ||
|
||
test('float with string to thow error on string', t => { | ||
const error = t.throws(() => { | ||
float({min: 'string', max: 'string', fixed: 'string'}) | ||
}, {instanceOf: TypeError}); | ||
const error = t.throws(() => { | ||
// @ts-ignore | ||
float({min: 'string', max: 'string', fixed: 'string'}); | ||
}, {instanceOf: TypeError}); | ||
|
||
t.is(error.message, 'Expected all arguments to be numbers.'); | ||
t.is(error.message, 'Expected all arguments to be numbers.'); | ||
}); | ||
|
||
test('float with min and max to thow error on min > max', t => { | ||
const error = t.throws(() => { | ||
float({min: 10, max: 0}) | ||
}, {instanceOf: TypeError}); | ||
t.is(error.message, 'Min cannot be greater than Max.'); | ||
}); | ||
const error = t.throws(() => { | ||
float({min: 10, max: 0}); | ||
}, {instanceOf: TypeError}); | ||
|
||
t.is(error.message, 'Min cannot be greater than Max.'); | ||
}); |