Ни в одном из примеров не было тестов, реализация для шага с опцией получения убывающих значений.
export function range(start = 0, end = 0, step = 1) {
if (start === end || step === 0) {
return [];
}
const diff = Math.abs(end - start);
const length = Math.ceil(diff / step);
return start > end
? Array.from({length}, (value, key) => start - key * step)
: Array.from({length}, (value, key) => start + key * step);
}
Тесты:
import range from './range'
describe('Range', () => {
it('default', () => {
expect(range()).toMatchObject([]);
})
it('same values', () => {
expect(range(1,1)).toMatchObject([]);
})
it('step=0', () => {
expect(range(0,1,0)).toMatchObject([]);
})
describe('step=1', () => {
it('normal', () => {
expect(range(6,12)).toMatchObject([6, 7, 8, 9, 10, 11]);
})
it('reversed', () => {
expect(range(12,6)).toMatchObject([12, 11, 10, 9, 8, 7]);
})
})
describe('step=5', () => {
it('start 0 end 60', () => {
expect(range(0, 60, 5)).toMatchObject([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]);
})
it('reversed start 60 end -1', () => {
expect(range(55, -1, 5)).toMatchObject([55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0]);
})
})
})