Skip to content

Commit

Permalink
feat: use an object for parameters in xSequentialFillFromStep and xSe…
Browse files Browse the repository at this point in the history
…quentialFillFromTo (#223)
  • Loading branch information
lpatiny authored Mar 4, 2024
1 parent 759cefb commit 69e6897
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
20 changes: 13 additions & 7 deletions src/x/__tests__/xSequentialFillFromStep.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { xSequentialFillFromStep } from '../xSequentialFillFromStep';

test('Default array type (Float64Array)', () => {
const result = xSequentialFillFromStep(0, 2, 6, {});
const result = xSequentialFillFromStep({ from: 0, step: 2, size: 6 }, {});
expect(result).toStrictEqual(new Float64Array([0, 2, 4, 6, 8, 10]));
});

test('Int32Array', () => {
const result = xSequentialFillFromStep(0, 2, 6, {
ArrayConstructor: Int32Array,
});
const result = xSequentialFillFromStep(
{ from: 0, step: 2, size: 6 },
{
ArrayConstructor: Int32Array,
},
);
expect(result).toStrictEqual(new Int32Array([0, 2, 4, 6, 8, 10]));
});

test('Array', () => {
const result = xSequentialFillFromStep(0, 2, 6, {
ArrayConstructor: Array,
});
const result = xSequentialFillFromStep(
{ from: 0, step: 2, size: 6 },
{
ArrayConstructor: Array,
},
);
expect(typeof result.push).toBe('function');
expect(result).toStrictEqual([0, 2, 4, 6, 8, 10]);
});
20 changes: 13 additions & 7 deletions src/x/__tests__/xSequentialFillFromTo.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { xSequentialFillFromTo } from '../xSequentialFillFromTo';

test('Default array type (Float64Array)', () => {
const result = xSequentialFillFromTo(0, 10, 6, {});
const result = xSequentialFillFromTo({ from: 0, to: 10, size: 6 }, {});
expect(result).toStrictEqual(new Float64Array([0, 2, 4, 6, 8, 10]));
});

test('Int32Array', () => {
const result = xSequentialFillFromTo(0, 10, 6, {
ArrayConstructor: Int32Array,
});
const result = xSequentialFillFromTo(
{ from: 0, to: 10, size: 6 },
{
ArrayConstructor: Int32Array,
},
);
expect(result).toStrictEqual(new Int32Array([0, 2, 4, 6, 8, 10]));
});

test('Array', () => {
const result = xSequentialFillFromTo(0, 10, 6, {
ArrayConstructor: Array,
});
const result = xSequentialFillFromTo(
{ from: 0, to: 10, size: 6 },
{
ArrayConstructor: Array,
},
);
expect(result).toStrictEqual([0, 2, 4, 6, 8, 10]);
});
10 changes: 7 additions & 3 deletions src/x/xSequentialFillFromStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ export interface XSequentialFillFromStepOptions<
*/
ArrayConstructor?: ArrayConstructorType;
}
export interface XSequentialFillFromStepParameters {
from: number;
step: number;
size: number;
}

export function xSequentialFillFromStep<
ArrayConstructorType extends NumberArrayConstructor = Float64ArrayConstructor,
>(
from: number,
step: number,
size: number,
parameters: XSequentialFillFromStepParameters,
options: XSequentialFillFromStepOptions<ArrayConstructorType> = {},
): NumberArrayType<ArrayConstructorType> {
const { from, step, size } = parameters;
const { ArrayConstructor = Float64Array as ArrayConstructorType } = options;
const result = createNumberArray(ArrayConstructor, size);
for (let i = 0; i < size; i++) {
Expand Down
13 changes: 9 additions & 4 deletions src/x/xSequentialFillFromTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ export interface XSequentialFillFromToOptions<
ArrayConstructor?: ArrayConstructorType;
}

export interface XSequentialFillFromToParameters {
from: number;
to: number;
size: number;
}

export function xSequentialFillFromTo<
ArrayConstructorType extends NumberArrayConstructor = Float64ArrayConstructor,
>(
from: number,
to: number,
size: number,
parameters: XSequentialFillFromToParameters,
options: XSequentialFillFromToOptions<ArrayConstructorType> = {},
): NumberArrayType<ArrayConstructorType> {
const { from, to, size } = parameters;
const step = (to - from) / (size - 1);
return xSequentialFillFromStep(from, step, size, options);
return xSequentialFillFromStep({ from, step, size }, options);
}

0 comments on commit 69e6897

Please sign in to comment.