@opencreek/ext
Advanced tools
Comparing version 1.7.0--canary.12.1829903665.0 to 1.7.0--canary.12.1829969022.0
/** | ||
* range will make an array from start to end (exclusive), with the given step | ||
* | ||
* @param start first value in the array | ||
@@ -4,0 +5,0 @@ * @param end last value + step of the array |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.range = void 0; | ||
const _1 = require("."); | ||
/** | ||
* range will make an array from start to end (exclusive), with the given step | ||
* | ||
* @param start first value in the array | ||
@@ -11,4 +13,14 @@ * @param end last value + step of the array | ||
function range(start, end, step = 1) { | ||
if (step === 0) { | ||
(0, _1.error)("step must not be 0"); | ||
} | ||
const ret = []; | ||
let i = 0; | ||
step = start > end && step > 0 ? -step : step; | ||
if (step < 0) { | ||
for (let n = start; n > end; n += step) { | ||
ret[i++] = n; | ||
} | ||
return ret; | ||
} | ||
for (let n = start; n < end; n += step) { | ||
@@ -15,0 +27,0 @@ ret[i++] = n; |
@@ -16,2 +16,9 @@ "use strict"; | ||
}); | ||
(0, ava_1.default)("range should produce negative ranges", (t) => { | ||
t.deepEqual((0, range_1.range)(6, 3), [6, 5, 4]); | ||
t.deepEqual((0, range_1.range)(6, 1, 2), [6, 4, 2]); | ||
t.deepEqual((0, range_1.range)(6, 1, -2), [6, 4, 2]); | ||
t.deepEqual((0, range_1.range)(8, 3, -0.5), [3.5, 4, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0].reverse()); | ||
t.deepEqual((0, range_1.range)(8, 3, 0.5), [3.5, 4, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0].reverse()); | ||
}); | ||
//# sourceMappingURL=range.test.js.map |
{ | ||
"name": "@opencreek/ext", | ||
"version": "1.7.0--canary.12.1829903665.0", | ||
"version": "1.7.0--canary.12.1829969022.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -13,1 +13,15 @@ import test from "ava" | ||
}) | ||
test("range should produce negative ranges", (t) => { | ||
t.deepEqual(range(6, 3), [6, 5, 4]) | ||
t.deepEqual(range(6, 1, 2), [6, 4, 2]) | ||
t.deepEqual(range(6, 1, -2), [6, 4, 2]) | ||
t.deepEqual( | ||
range(8, 3, -0.5), | ||
[3.5, 4, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0].reverse() | ||
) | ||
t.deepEqual( | ||
range(8, 3, 0.5), | ||
[3.5, 4, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0].reverse() | ||
) | ||
}) |
@@ -0,3 +1,6 @@ | ||
import { error } from "." | ||
/** | ||
* range will make an array from start to end (exclusive), with the given step | ||
* | ||
* @param start first value in the array | ||
@@ -8,5 +11,16 @@ * @param end last value + step of the array | ||
export function range(start: number, end: number, step = 1): Array<number> { | ||
if (step === 0) { | ||
error("step must not be 0") | ||
} | ||
const ret = [] | ||
let i = 0 | ||
step = start > end && step > 0 ? -step : step | ||
if (step < 0) { | ||
for (let n = start; n > end; n += step) { | ||
ret[i++] = n | ||
} | ||
return ret | ||
} | ||
for (let n = start; n < end; n += step) { | ||
@@ -13,0 +27,0 @@ ret[i++] = n |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66665
1096