Skip to content

Commit

Permalink
feat: unit test of range with step
Browse files Browse the repository at this point in the history
  • Loading branch information
RoxaneBurri committed Mar 24, 2023
1 parent e75eab1 commit 18c3754
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
slideWords,
getWordSlide,
boundParent,
rangeWithStep,
} from "./utils";

const origin: Coordinate = {
Expand Down Expand Up @@ -310,3 +311,18 @@ describe("BoundParent", () => {
).toEqual({ x: 1, y: 3, width: 8, height: 7 });
});
});

describe("Range with step", () => {
it("Start equal to end", () => {
expect(rangeWithStep(1, 1, 2)).toEqual([1]);
});
it("End bigger than start", () => {
expect(rangeWithStep(4, 1, 2)).toEqual([]);
});
it("No step", () => {
expect(rangeWithStep(0, 10, 1)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});
it("Step of 2", () => {
expect(rangeWithStep(0, 9, 2)).toEqual([0, 2, 4, 6, 8]);
});
});
17 changes: 14 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,19 @@ export const cumulativeBins = (bin: number[]): number[] => {

// https://stackoverflow.com/questions/36947847/how-to-generate-range-of-numbers-from-0-to-n-in-es2015-only
// range(0, 9, 2) => [0, 2, 4, 6, 8]
export const range = (from: number, to: number, step: number) =>
[...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step);
// No negative step
export const rangeWithStep = (
from: number,
to: number,
step: number
): number[] => {
if (to < from) {
return [];
}
return [...Array(Math.floor((to - from) / step) + 1)].map(
(_, i) => from + i * step
);
};

// This function puts the word in a random place on a circle
export const placeWordOnOuterCircle = (
Expand Down Expand Up @@ -150,7 +161,7 @@ export const placeWordOnOuterCircle = (
const ratio = 360 / NUMBER_OF_INTERVALS;

// create the intervals
const rangeInterval = range(0, 360, ratio);
const rangeInterval = rangeWithStep(0, 360, ratio);

let angleInter;

Expand Down

0 comments on commit 18c3754

Please sign in to comment.