diff --git a/src/common/utils/__tests__/arrays.test.ts b/src/common/utils/__tests__/arrays.test.ts new file mode 100644 index 0000000000..8670db4d04 --- /dev/null +++ b/src/common/utils/__tests__/arrays.test.ts @@ -0,0 +1,31 @@ +import { splitArray } from "../splitArray"; + +describe("split array on element tests", () => { + test("empty array", () => { + expect(splitArray([], 10)).toStrictEqual([[], [], false]); + }); + + test("one element, not in array", () => { + expect(splitArray([1], 10)).toStrictEqual([[1], [], false]); + }); + + test("ten elements, not in array", () => { + expect(splitArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10)).toStrictEqual([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [], false]); + }); + + test("one elements, in array", () => { + expect(splitArray([1], 1)).toStrictEqual([[], [], true]); + }); + + test("ten elements, in front array", () => { + expect(splitArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 0)).toStrictEqual([[], [1, 2, 3, 4, 5, 6, 7, 8, 9], true]); + }); + + test("ten elements, in middle array", () => { + expect(splitArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4)).toStrictEqual([[0, 1, 2, 3], [5, 6, 7, 8, 9], true]); + }); + + test("ten elements, in end array", () => { + expect(splitArray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 9)).toStrictEqual([[0, 1, 2, 3, 4, 5, 6, 7, 8], [], true]); + }); +}); \ No newline at end of file diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index cbcfb4b3b8..35b65207ac 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -1,5 +1,5 @@ -// Common utils bundle +// Common utils (main/renderer) -export * from "./app-version" export * from "./base64" export * from "./camelCase" +export * from "./splitArray" diff --git a/src/common/utils/splitArray.ts b/src/common/utils/splitArray.ts new file mode 100644 index 0000000000..4b96d28864 --- /dev/null +++ b/src/common/utils/splitArray.ts @@ -0,0 +1,19 @@ +// Restored from dashboard/client/utils/arrays.ts +/** + * This function splits an array into two sub arrays on the first instance of + * element (from the left). If the array does not contain the element. The + * return value is defined to be `[array, [], false]`. If the element is in + * the array then the return value is `[left, right, true]` where `left` is + * the elements of `array` from `[0, index)` and `right` is `(index, length)` + * @param array the full array to split into two sub-arrays + * @param element the element in the middle of the array + * @returns the left and right sub-arrays which when conjoined with `element` + * is the same as `array`, and `true` + */ +export function splitArray(array: T[], element: T): [T[], T[], boolean] { + const index = array.indexOf(element); + if (index < 0) { + return [array, [], false]; + } + return [array.slice(0, index), array.slice(index + 1, array.length), true] +} diff --git a/src/renderer/api/kube-api.ts b/src/renderer/api/kube-api.ts index be8016c68c..029a75b6bb 100644 --- a/src/renderer/api/kube-api.ts +++ b/src/renderer/api/kube-api.ts @@ -7,7 +7,7 @@ import { IKubeObjectRef, KubeJsonApi, KubeJsonApiData, KubeJsonApiDataList } fro import { apiKube } from "./index"; import { kubeWatchApi } from "./kube-watch-api"; import { apiManager } from "./api-manager"; -import { split } from "../utils/arrays"; +import { splitArray } from "../../common/utils"; export interface IKubeApiOptions { kind: string; // resource type within api-group, e.g. "Namespace" @@ -45,7 +45,7 @@ export class KubeApi { const [, prefix, ...parts] = apiPath.split("/"); const apiPrefix = `/${prefix}`; - const [left, right, found] = split(parts, "namespaces"); + const [left, right, found] = splitArray(parts, "namespaces"); let apiGroup, apiVersion, namespace, resource, name; if (found) { diff --git a/src/renderer/components/chart/chart.tsx b/src/renderer/components/chart/chart.tsx index 45510b40f9..92e3619031 100644 --- a/src/renderer/components/chart/chart.tsx +++ b/src/renderer/components/chart/chart.tsx @@ -1,6 +1,6 @@ import "./chart.scss"; import React from "react"; -import ChartJS, {ChartData, ChartOptions} from "chart.js"; +import ChartJS from "chart.js"; import { isEqual, remove } from "lodash"; import { cssNames } from "../../utils"; import { StatusBrick } from "../status-brick"; @@ -17,7 +17,7 @@ export interface ChartDataSets extends ChartJS.ChartDataSets { export interface ChartProps { data: ChartData; - options?: ChartOptions; // Passed to ChartJS instance + options?: ChartJS.ChartOptions; // Passed to ChartJS instance width?: number | string; height?: number | string; type?: ChartKind; @@ -126,8 +126,7 @@ export class Chart extends React.Component { ...datasets[index], ...props } - } - else { + } else { datasets[datasetIndex] = next } })