Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NS-98 #68

Merged
merged 3 commits into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cypress/integration/unit/helpers/array.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/// <reference types="cypress" />
import { ArrayHelper } from "../../../../src/helpers/array.helper";

type TestCase = { arr1: number[]; arr2: number[]; exp: [number, number][] };
var testCases: TestCase[] = [
{
arr1: [1, 2],
arr2: [1, 2, 3],
exp: [
[1, 1],
[2, 2],
[undefined, 3],
],
},
{
arr1: [1, 2],
arr2: [1, 2],
exp: [
[1, 1],
[2, 2],
],
},
{
arr1: [1, 2, 6],
arr2: [1, 2],
exp: [
[1, 1],
[2, 2],
[6, undefined],
],
},
];

describe("ArrayHelper", () => {
testCases.forEach((testCase) => {
describe("zip", () => {
it(`should return ${testCase.exp} for arrays ${testCase.arr1} and ${testCase.arr2}`, () => {
const result = ArrayHelper.zip(testCase.arr1, testCase.arr2);
expect(result).to.eql(testCase.exp);
});
});
});
});
16 changes: 8 additions & 8 deletions src/helpers/array.helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
type Primitive = undefined | null | boolean | number | string | bigint | symbol;
export class ArrayHelper {
public static zip<T>(array1: T[], array2: T[]): [T, T][] {
let longer: T[];
let shorter: T[];
if (array1.length >= array2.length) {
[longer, shorter] = [array1, array2];
} else {
[longer, shorter] = [array2, array1];
public static zip<T1, T2>(
array1: (T1 | null)[],
array2: (T2 | null)[]
): [T1 | null, T2 | null][] {
if (array1.length < array2.length) {
const count = array2.length - array1.length;
array1 = [...array1, ...Array.from(Array(count))];
}
return longer.map((v: T, index: number) => [v, shorter[index]]);
return array1.map((v, index: number) => [v, array2[index]]);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down