test: cover sliceFragment

This commit is contained in:
max 2022-07-23 16:21:53 +03:00
parent 19d7f239c1
commit ffea4d15cb
7 changed files with 417 additions and 11 deletions

View file

@ -1,5 +1,5 @@
// detect device
export const MOBILE = window.innerWidth < 600 || window.navigator.userAgentData?.mobile;
export const MOBILE = globalThis.innerWidth < 600 || globalThis.navigator?.userAgentData?.mobile;
// typed arrays max values
export const INT8_MAX = 127;

View file

@ -0,0 +1,29 @@
import {describe, it, expect} from "vitest";
import {sliceFragment} from "./arrayUtils";
describe("sliceFragment", () => {
const array = ["a", "b", "c", "d", "e", "f"];
it("should return expected fragment within array length", () => {
expect(sliceFragment(array, 2, 2)).toEqual(["a", "b", "c", "d", "e"]);
expect(sliceFragment(array, 2, 1)).toEqual(["b", "c", "d"]);
expect(sliceFragment(array, 2, 0)).toEqual(["c"]);
});
it("should cycle array and return expected fragment if input is beyond array length", () => {
expect(sliceFragment(array, 1, 2)).toEqual(["f", "a", "b", "c", "d"]);
expect(sliceFragment(array, 0, 1)).toEqual(["f", "a", "b"]);
expect(sliceFragment(array, 5, 1)).toEqual(["e", "f", "a"]);
});
it("should return original array if boundaries exceed the array", () => {
expect(sliceFragment(array, 2, 10)).toEqual(array);
expect(sliceFragment([], 2, 10)).toEqual([]);
});
it("should throw an error if input is incorrect", () => {
expect(() => sliceFragment(array, -2, 2)).toThrow();
expect(() => sliceFragment(array, 2, -1)).toThrow();
});
});

View file

@ -1,4 +1,4 @@
import {UINT16_MAX, UINT32_MAX, UINT8_MAX} from "../config/constants";
import {UINT16_MAX, UINT32_MAX, UINT8_MAX} from "config/constants";
export function last<T>(array: T[]) {
return array[array.length - 1];
@ -9,15 +9,18 @@ export function unique<T>(array: T[]) {
}
export function sliceFragment<T>(array: T[], index: number, zone: number) {
if (index < 0) throw new Error("Index must not be negative");
if (zone < 0) throw new Error("Zone must not be negative");
if (zone + 1 + zone >= array.length) {
return array.slice();
}
const start = index - zone;
const end = index + zone;
const end = index + zone + 1;
if (start < 0) {
return array.slice(0, end).concat(array.slice(start));
return array.slice(start).concat(array.slice(0, end));
}
if (end >= array.length) {
@ -27,8 +30,6 @@ export function sliceFragment<T>(array: T[], index: number, zone: number) {
return array.slice(start, end);
}
window.sliceFragment = sliceFragment;
interface ICreateTypesArrayLength {
maxValue: number;
length: number;