UNPKG

utilite

Version:

Powerful utility library for JS

210 lines (209 loc) 8.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const index_1 = require("../../utility/ArrayUtils/index"); describe("Sample Test Suite", () => { it("Sample Test Case-1", () => { const value = true; (0, chai_1.expect)(value).to.equal(true); }); }); describe("fillArray", () => { it("should create an array of the specified length filled with the specified value", () => { const result = (0, index_1.fillArray)(5, "test"); (0, chai_1.expect)(result).to.be.an("array"); (0, chai_1.expect)(result).to.have.lengthOf(5); (0, chai_1.expect)(result.every((v) => v === "test")).to.equal(true); }); }); describe("filterFalsy", () => { it("should filter out falsy values", () => { const inputList = [ 1, 0, null, undefined, NaN, "", "hello", ]; const result = (0, index_1.filterFalsy)(inputList); // Only non-falsy values should remain (0, chai_1.expect)(result).to.deep.equal([1, "hello"]); }); it("should handle an empty list", () => { const inputList = []; const result = (0, index_1.filterFalsy)(inputList); // Empty list, so the result should also be an empty list (0, chai_1.expect)(result).to.be.an("array"); (0, chai_1.expect)(result).to.have.lengthOf(0); }); }); describe("Filter Functions", () => { const numbers = [1, 2, 3, 4, 5]; it("should filter even numbers", () => { const isEven = (num) => num % 2 === 0; const evenNumbers = (0, index_1.filter)(numbers, isEven); (0, chai_1.expect)(evenNumbers).to.deep.equal([2, 4]); }); it("should filterNegate even numbers to get odd numbers", () => { const isEven = (num) => num % 2 === 0; const oddNumbers = (0, index_1.filterNegate)(numbers, isEven); (0, chai_1.expect)(oddNumbers).to.deep.equal([1, 3, 5]); }); }); describe("findFirstMatching", () => { const list = [1, 2, 3, 4, 5]; it("should find the first matching element", () => { const predicate = (item) => item % 2 === 0; const result = (0, index_1.findFirstMatching)(list, predicate); (0, chai_1.expect)(result).to.equal(2); }); it("should return undefined if no match is found", () => { const predicate = (item) => item > 10; const result = (0, index_1.findFirstMatching)(list, predicate); (0, chai_1.expect)(result).to.equal(undefined); }); it("should return the default value if no match is found", () => { const predicate = (item) => item > 10; const defaultValue = -1; const result = (0, index_1.findFirstMatching)(list, predicate, defaultValue); (0, chai_1.expect)(result).to.equal(defaultValue); }); }); describe("getTopN", () => { const sampleArray = [10, 20, 30, 40, 50]; it("should take the first N elements from the array", () => { const result = (0, index_1.getHeadN)(sampleArray, 3); (0, chai_1.expect)(result).to.deep.equal([10, 20, 30]); }); it("should take the first element if N is 1", () => { const result = (0, index_1.getHeadN)(sampleArray, 1); (0, chai_1.expect)(result).to.deep.equal([10]); }); it("should take an empty array if N is 0", () => { const result = (0, index_1.getHeadN)(sampleArray, 0); (0, chai_1.expect)(result).to.deep.equal([]); }); it("should take the entire array if N is greater than the array length", () => { const result = (0, index_1.getHeadN)(sampleArray, 10); (0, chai_1.expect)(result).to.deep.equal(sampleArray); }); }); describe("getTailN", () => { const sampleArray = [10, 20, 30, 40, 50]; it("should retrieve the last N elements from the array", () => { const result = (0, index_1.getTailN)(sampleArray, 3); (0, chai_1.expect)(result).to.deep.equal([30, 40, 50]); }); it("should retrieve the last element if N is 1", () => { const result = (0, index_1.getTailN)(sampleArray, 1); (0, chai_1.expect)(result).to.deep.equal([50]); }); it("should retrieve an empty array if N is 0", () => { const result = (0, index_1.getTailN)(sampleArray, 0); (0, chai_1.expect)(result).to.deep.equal([]); }); it("should retrieve the entire array if N is greater than the array length", () => { const result = (0, index_1.getTailN)(sampleArray, 10); (0, chai_1.expect)(result).to.deep.equal(sampleArray); }); }); describe("isAnyMatchingLazy", () => { const sampleArray = [1, 2, 3, 4, 5]; it("should return true if any property matches the predicate", () => { const result = (0, index_1.isAnyMatchingLazy)(sampleArray, (item) => item > 3); (0, chai_1.expect)(result).to.be.equal(true); }); it("should return false if no property matches the predicate", () => { const result = (0, index_1.isAnyMatchingLazy)(sampleArray, (item) => item > 10); (0, chai_1.expect)(result).to.be.equal(false); }); }); describe("removeValuesFrom", () => { it("should remove specified values from the array", () => { const array = [1, 2, 3, 1, 4, 1, 5]; const valuesToRemove = [1, 5]; const result = (0, index_1.removeValuesFrom)(array, valuesToRemove); (0, chai_1.expect)(result).to.deep.equal([2, 3, 4]); }); it("should handle an empty array", () => { const array = []; const valuesToRemove = [1, 5]; const result = (0, index_1.removeValuesFrom)(array, valuesToRemove); (0, chai_1.expect)(result).to.deep.equal([]); }); }); describe("getUniqueElements", () => { it("should return an array with unique elements", () => { const originalArray = [1, 2, 3, 3, 2, 1]; const uniqueArray = (0, index_1.getUniqueElements)(originalArray); (0, chai_1.expect)(uniqueArray).to.deep.equal([1, 2, 3]); }); it("should handle an empty array", () => { const originalArray = []; const uniqueArray = (0, index_1.getUniqueElements)(originalArray); (0, chai_1.expect)(uniqueArray).to.deep.equal([]); }); it("should handle an array with a single element", () => { const originalArray = [42]; const uniqueArray = (0, index_1.getUniqueElements)(originalArray); (0, chai_1.expect)(uniqueArray).to.deep.equal([42]); }); }); describe("copyWithin", () => { it("should copy a sequence of elements within the array", () => { const originalArray = [1, 2, 3, 4, 5]; const newArray = (0, index_1.copyWithin)(originalArray, 2, 0, 3); (0, chai_1.expect)(newArray).to.deep.equal([1, 2, 1, 2, 3]); }); it("should handle an empty array", () => { const originalArray = []; const newArray = (0, index_1.copyWithin)(originalArray, 0, 0); (0, chai_1.expect)(newArray).to.deep.equal([]); }); }); describe("Set Operation Methods", () => { describe("difference", () => { it("should return the difference of two arrays", () => { const arr1 = [1, 2, 3, 4]; const arr2 = [3, 4, 5, 6]; const result = (0, index_1.difference)(arr1, arr2); (0, chai_1.expect)(result).to.deep.equal([1, 2]); }); it("should handle empty arrays", () => { const arr1 = []; const arr2 = []; const result = (0, index_1.difference)(arr1, arr2); (0, chai_1.expect)(result).to.deep.equal([]); }); }); describe("union", () => { it("should return the union of two arrays", () => { const arr1 = [1, 2, 3, 4]; const arr2 = [3, 4, 5, 6]; const result = (0, index_1.union)(arr1, arr2); (0, chai_1.expect)(result).to.deep.equal([1, 2, 3, 4, 5, 6]); }); it("should handle empty arrays", () => { const arr1 = []; const arr2 = []; const result = (0, index_1.union)(arr1, arr2); (0, chai_1.expect)(result).to.deep.equal([]); }); }); describe("intersection", () => { it("should return the intersection of two arrays", () => { const arr1 = [1, 2, 3, 4]; const arr2 = [3, 4, 5, 6]; const result = (0, index_1.intersection)(arr1, arr2); (0, chai_1.expect)(result).to.deep.equal([3, 4]); }); it("should handle empty arrays", () => { const arr1 = []; const arr2 = []; const result = (0, index_1.intersection)(arr1, arr2); (0, chai_1.expect)(result).to.deep.equal([]); }); }); });