mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 02:44:45 +00:00
chore(packages/stdlib): fix code style, stack pop and peek jsdoc
This commit is contained in:
@@ -4,31 +4,37 @@ import { cluster } from '.';
|
|||||||
describe('cluster', () => {
|
describe('cluster', () => {
|
||||||
it('cluster an array into subarrays of a specific size', () => {
|
it('cluster an array into subarrays of a specific size', () => {
|
||||||
const result = cluster([1, 2, 3, 4, 5, 6, 7, 8], 3);
|
const result = cluster([1, 2, 3, 4, 5, 6, 7, 8], 3);
|
||||||
|
|
||||||
expect(result).toEqual([[1, 2, 3], [4, 5, 6], [7, 8]]);
|
expect(result).toEqual([[1, 2, 3], [4, 5, 6], [7, 8]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handle arrays that are not perfectly divisible by the size', () => {
|
it('handle arrays that are not perfectly divisible by the size', () => {
|
||||||
const result = cluster([1, 2, 3, 4, 5], 2);
|
const result = cluster([1, 2, 3, 4, 5], 2);
|
||||||
|
|
||||||
expect(result).toEqual([[1, 2], [3, 4], [5]]);
|
expect(result).toEqual([[1, 2], [3, 4], [5]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('return an array with each element in its own subarray if size is 1', () => {
|
it('return an array with each element in its own subarray if size is 1', () => {
|
||||||
const result = cluster([1, 2, 3, 4], 1);
|
const result = cluster([1, 2, 3, 4], 1);
|
||||||
|
|
||||||
expect(result).toEqual([[1], [2], [3], [4]]);
|
expect(result).toEqual([[1], [2], [3], [4]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('return an array with a single subarray if size is greater than the array length', () => {
|
it('return an array with a single subarray if size is greater than the array length', () => {
|
||||||
const result = cluster([1, 2, 3], 5);
|
const result = cluster([1, 2, 3], 5);
|
||||||
|
|
||||||
expect(result).toEqual([[1, 2, 3]]);
|
expect(result).toEqual([[1, 2, 3]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('return an empty array if size is less than or equal to 0', () => {
|
it('return an empty array if size is less than or equal to 0', () => {
|
||||||
const result = cluster([1, 2, 3, 4], -1);
|
const result = cluster([1, 2, 3, 4], -1);
|
||||||
|
|
||||||
expect(result).toEqual([]);
|
expect(result).toEqual([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('return an empty array if the input array is empty', () => {
|
it('return an empty array if the input array is empty', () => {
|
||||||
const result = cluster([], 3);
|
const result = cluster([], 3);
|
||||||
|
|
||||||
expect(result).toEqual([]);
|
expect(result).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -4,36 +4,43 @@ import { sum } from '.';
|
|||||||
describe('sum', () => {
|
describe('sum', () => {
|
||||||
it('return the sum of all elements in a number array', () => {
|
it('return the sum of all elements in a number array', () => {
|
||||||
const result = sum([1, 2, 3, 4, 5]);
|
const result = sum([1, 2, 3, 4, 5]);
|
||||||
|
|
||||||
expect(result).toBe(15);
|
expect(result).toBe(15);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('return 0 for an empty array', () => {
|
it('return 0 for an empty array', () => {
|
||||||
const result = sum([]);
|
const result = sum([]);
|
||||||
|
|
||||||
expect(result).toBe(0);
|
expect(result).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('return the sum of all elements using a getValue function', () => {
|
it('return the sum of all elements using a getValue function', () => {
|
||||||
const result = sum([{ value: 1 }, { value: 2 }, { value: 3 }], (item) => item.value);
|
const result = sum([{ value: 1 }, { value: 2 }, { value: 3 }], (item) => item.value);
|
||||||
|
|
||||||
expect(result).toBe(6);
|
expect(result).toBe(6);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handle arrays with negative numbers', () => {
|
it('handle arrays with negative numbers', () => {
|
||||||
const result = sum([-1, -2, -3, -4, -5]);
|
const result = sum([-1, -2, -3, -4, -5]);
|
||||||
|
|
||||||
expect(result).toBe(-15);
|
expect(result).toBe(-15);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handle arrays with mixed positive and negative numbers', () => {
|
it('handle arrays with mixed positive and negative numbers', () => {
|
||||||
const result = sum([1, -2, 3, -4, 5]);
|
const result = sum([1, -2, 3, -4, 5]);
|
||||||
|
|
||||||
expect(result).toBe(3);
|
expect(result).toBe(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handle arrays with floating point numbers', () => {
|
it('handle arrays with floating point numbers', () => {
|
||||||
const result = sum([1.5, 2.5, 3.5]);
|
const result = sum([1.5, 2.5, 3.5]);
|
||||||
|
|
||||||
expect(result).toBe(7.5);
|
expect(result).toBe(7.5);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handle arrays with a getValue function returning floating point numbers', () => {
|
it('handle arrays with a getValue function returning floating point numbers', () => {
|
||||||
const result = sum([{ value: 1.5 }, { value: 2.5 }, { value: 3.5 }], (item) => item.value);
|
const result = sum([{ value: 1.5 }, { value: 2.5 }, { value: 3.5 }], (item) => item.value);
|
||||||
|
|
||||||
expect(result).toBe(7.5);
|
expect(result).toBe(7.5);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -4,6 +4,7 @@ import { unique } from '.';
|
|||||||
describe('unique', () => {
|
describe('unique', () => {
|
||||||
it('return an array with unique numbers', () => {
|
it('return an array with unique numbers', () => {
|
||||||
const result = unique([1, 2, 3, 3, 4, 5, 5, 6]);
|
const result = unique([1, 2, 3, 3, 4, 5, 5, 6]);
|
||||||
|
|
||||||
expect(result).toEqual([1, 2, 3, 4, 5, 6]);
|
expect(result).toEqual([1, 2, 3, 4, 5, 6]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -12,16 +13,19 @@ describe('unique', () => {
|
|||||||
[{ id: 1 }, { id: 2 }, { id: 1 }],
|
[{ id: 1 }, { id: 2 }, { id: 1 }],
|
||||||
(item) => item.id,
|
(item) => item.id,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toEqual([{ id: 1 }, { id: 2 }]);
|
expect(result).toEqual([{ id: 1 }, { id: 2 }]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('return the same array if all elements are unique', () => {
|
it('return the same array if all elements are unique', () => {
|
||||||
const result = unique([1, 2, 3, 4, 5]);
|
const result = unique([1, 2, 3, 4, 5]);
|
||||||
|
|
||||||
expect(result).toEqual([1, 2, 3, 4, 5]);
|
expect(result).toEqual([1, 2, 3, 4, 5]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handle arrays with different types of values', () => {
|
it('handle arrays with different types of values', () => {
|
||||||
const result = unique([1, '1', 2, '2', 2, 3, '3']);
|
const result = unique([1, '1', 2, '2', 2, 3, '3']);
|
||||||
|
|
||||||
expect(result).toEqual([1, '1', 2, '2', 3, '3']);
|
expect(result).toEqual([1, '1', 2, '2', 3, '3']);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -29,11 +33,13 @@ describe('unique', () => {
|
|||||||
const sym1 = Symbol('a');
|
const sym1 = Symbol('a');
|
||||||
const sym2 = Symbol('b');
|
const sym2 = Symbol('b');
|
||||||
const result = unique([sym1, sym2, sym1]);
|
const result = unique([sym1, sym2, sym1]);
|
||||||
|
|
||||||
expect(result).toEqual([sym1, sym2]);
|
expect(result).toEqual([sym1, sym2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('return an empty array when given an empty array', () => {
|
it('return an empty array when given an empty array', () => {
|
||||||
const result = unique([]);
|
const result = unique([]);
|
||||||
|
|
||||||
expect(result).toEqual([]);
|
expect(result).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -84,7 +84,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Pops an element from the stack
|
* Pops an element from the stack
|
||||||
* @returns {T} The element popped from the stack
|
* @returns {T | undefined} The element popped from the stack
|
||||||
*/
|
*/
|
||||||
public pop() {
|
public pop() {
|
||||||
return this.stack.pop();
|
return this.stack.pop();
|
||||||
@@ -92,7 +92,7 @@ export class Stack<T> implements Iterable<T>, AsyncIterable<T> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Peeks at the top element of the stack
|
* Peeks at the top element of the stack
|
||||||
* @returns {T} The top element of the stack
|
* @returns {T | undefined} The top element of the stack
|
||||||
*/
|
*/
|
||||||
public peek() {
|
public peek() {
|
||||||
if (this.isEmpty)
|
if (this.isEmpty)
|
||||||
|
|||||||
Reference in New Issue
Block a user