-
Notifications
You must be signed in to change notification settings - Fork 188
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
141 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
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,19 @@ | ||
import {tickIncrement} from "./ticks.js"; | ||
|
||
export default function nice(start, stop, count) { | ||
let prestep; | ||
while (true) { | ||
const step = tickIncrement(start, stop, count); | ||
if (step === prestep || step === 0 || !isFinite(step)) { | ||
break; | ||
} else if (step > 0) { | ||
start = Math.floor(start / step) * step; | ||
stop = Math.ceil(stop / step) * step; | ||
} else if (step < 0) { | ||
start = Math.ceil(start * step) / step; | ||
stop = Math.floor(stop * step) / step; | ||
} | ||
prestep = step; | ||
} | ||
return [start, stop]; | ||
} |
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,46 @@ | ||
const tape = require("tape-await"); | ||
const array = require("../"); | ||
|
||
tape("nice(start, stop, count) returns [start, stop] if any argument is NaN", (test) => { | ||
test.deepEqual(array.nice(NaN, 1, 1), [NaN, 1]); | ||
test.deepEqual(array.nice(0, NaN, 1), [0, NaN]); | ||
test.deepEqual(array.nice(0, 1, NaN), [0, 1]); | ||
test.deepEqual(array.nice(NaN, NaN, 1), [NaN, NaN]); | ||
test.deepEqual(array.nice(0, NaN, NaN), [0, NaN]); | ||
test.deepEqual(array.nice(NaN, 1, NaN), [NaN, 1]); | ||
test.deepEqual(array.nice(NaN, NaN, NaN), [NaN, NaN]); | ||
}); | ||
|
||
tape("nice(start, stop, count) returns [start, stop] if start === stop", (test) => { | ||
test.deepEqual(array.nice(1, 1, -1), [1, 1]); | ||
test.deepEqual(array.nice(1, 1, 0), [1, 1]); | ||
test.deepEqual(array.nice(1, 1, NaN), [1, 1]); | ||
test.deepEqual(array.nice(1, 1, 1), [1, 1]); | ||
test.deepEqual(array.nice(1, 1, 10), [1, 1]); | ||
}); | ||
|
||
tape("nice(start, stop, count) returns [start, stop] if count is not positive", (test) => { | ||
test.deepEqual(array.nice(0, 1, -1), [0, 1]); | ||
test.deepEqual(array.nice(0, 1, 0), [0, 1]); | ||
}); | ||
|
||
tape("nice(start, stop, count) returns [start, stop] if count is infinity", (test) => { | ||
test.deepEqual(array.nice(0, 1, Infinity), [0, 1]); | ||
}); | ||
|
||
tape("nice(start, stop, count) returns the expected values", (test) => { | ||
test.deepEqual(array.nice(0.132, 0.876, 1000), [0.132, 0.876]); | ||
test.deepEqual(array.nice(0.132, 0.876, 100), [0.13, 0.88]); | ||
test.deepEqual(array.nice(0.132, 0.876, 30), [0.12, 0.88]); | ||
test.deepEqual(array.nice(0.132, 0.876, 10), [0.1, 0.9]); | ||
test.deepEqual(array.nice(0.132, 0.876, 6), [0.1, 0.9]); | ||
test.deepEqual(array.nice(0.132, 0.876, 5), [0, 1]); | ||
test.deepEqual(array.nice(0.132, 0.876, 1), [0, 1]); | ||
test.deepEqual(array.nice(132, 876, 1000), [132, 876]); | ||
test.deepEqual(array.nice(132, 876, 100), [130, 880]); | ||
test.deepEqual(array.nice(132, 876, 30), [120, 880]); | ||
test.deepEqual(array.nice(132, 876, 10), [100, 900]); | ||
test.deepEqual(array.nice(132, 876, 6), [100, 900]); | ||
test.deepEqual(array.nice(132, 876, 5), [0, 1000]); | ||
test.deepEqual(array.nice(132, 876, 1), [0, 1000]); | ||
}); |
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,62 @@ | ||
const tape = require("tape-await"); | ||
const array = require("../"); | ||
|
||
tape("tickIncrement(start, stop, count) returns NaN if any argument is NaN", (test) => { | ||
test.ok(isNaN(array.tickIncrement(NaN, 1, 1))); | ||
test.ok(isNaN(array.tickIncrement(0, NaN, 1))); | ||
test.ok(isNaN(array.tickIncrement(0, 1, NaN))); | ||
test.ok(isNaN(array.tickIncrement(NaN, NaN, 1))); | ||
test.ok(isNaN(array.tickIncrement(0, NaN, NaN))); | ||
test.ok(isNaN(array.tickIncrement(NaN, 1, NaN))); | ||
test.ok(isNaN(array.tickIncrement(NaN, NaN, NaN))); | ||
}); | ||
|
||
tape("tickIncrement(start, stop, count) returns NaN or -Infinity if start === stop", (test) => { | ||
test.ok(isNaN(array.tickIncrement(1, 1, -1))); | ||
test.ok(isNaN(array.tickIncrement(1, 1, 0))); | ||
test.ok(isNaN(array.tickIncrement(1, 1, NaN))); | ||
test.equal(array.tickIncrement(1, 1, 1), -Infinity); | ||
test.equal(array.tickIncrement(1, 1, 10), -Infinity); | ||
}); | ||
|
||
tape("tickIncrement(start, stop, count) returns 0 or Infinity if count is not positive", (test) => { | ||
test.equal(array.tickIncrement(0, 1, -1), Infinity); | ||
test.equal(array.tickIncrement(0, 1, 0), Infinity); | ||
}); | ||
|
||
tape("tickIncrement(start, stop, count) returns -Infinity if count is infinity", (test) => { | ||
test.equal(array.tickIncrement(0, 1, Infinity), -Infinity); | ||
}); | ||
|
||
tape("tickIncrement(start, stop, count) returns approximately count + 1 tickIncrement when start < stop", (test) => { | ||
test.equal(array.tickIncrement( 0, 1, 10), -10); | ||
test.equal(array.tickIncrement( 0, 1, 9), -10); | ||
test.equal(array.tickIncrement( 0, 1, 8), -10); | ||
test.equal(array.tickIncrement( 0, 1, 7), -5); | ||
test.equal(array.tickIncrement( 0, 1, 6), -5); | ||
test.equal(array.tickIncrement( 0, 1, 5), -5); | ||
test.equal(array.tickIncrement( 0, 1, 4), -5); | ||
test.equal(array.tickIncrement( 0, 1, 3), -2); | ||
test.equal(array.tickIncrement( 0, 1, 2), -2); | ||
test.equal(array.tickIncrement( 0, 1, 1), 1); | ||
test.equal(array.tickIncrement( 0, 10, 10), 1); | ||
test.equal(array.tickIncrement( 0, 10, 9), 1); | ||
test.equal(array.tickIncrement( 0, 10, 8), 1); | ||
test.equal(array.tickIncrement( 0, 10, 7), 2); | ||
test.equal(array.tickIncrement( 0, 10, 6), 2); | ||
test.equal(array.tickIncrement( 0, 10, 5), 2); | ||
test.equal(array.tickIncrement( 0, 10, 4), 2); | ||
test.equal(array.tickIncrement( 0, 10, 3), 5); | ||
test.equal(array.tickIncrement( 0, 10, 2), 5); | ||
test.equal(array.tickIncrement( 0, 10, 1), 10); | ||
test.equal(array.tickIncrement(-10, 10, 10), 2); | ||
test.equal(array.tickIncrement(-10, 10, 9), 2); | ||
test.equal(array.tickIncrement(-10, 10, 8), 2); | ||
test.equal(array.tickIncrement(-10, 10, 7), 2); | ||
test.equal(array.tickIncrement(-10, 10, 6), 5); | ||
test.equal(array.tickIncrement(-10, 10, 5), 5); | ||
test.equal(array.tickIncrement(-10, 10, 4), 5); | ||
test.equal(array.tickIncrement(-10, 10, 3), 5); | ||
test.equal(array.tickIncrement(-10, 10, 2), 10); | ||
test.equal(array.tickIncrement(-10, 10, 1), 20); | ||
}); |