Skip to content

Commit

Permalink
refactor(frontend): move utils to TypeScript (#9820)
Browse files Browse the repository at this point in the history
* refactor(frontend): move utils to typescript (#9101)

* refactor(frontend): don't export interfaces

* test(frontend): update types and test for isValidChild
  • Loading branch information
ChristianMurphy authored May 20, 2020
1 parent ef6af93 commit a262ea7
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
TAB_TYPE as TAB,
} from 'src/dashboard/util/componentTypes';

const getIndentation = depth =>
const getIndentation = (depth: number) =>
Array(depth * 3)
.fill('')
.join('-');
Expand Down Expand Up @@ -136,20 +136,22 @@ describe('isValidChild', () => {
invalidExamples.forEach((example, exampleIdx) => {
let childDepth = 0;
example.forEach((childType, i) => {
const shouldTestChild = Array.isArray(childType);

if (i > 0 && shouldTestChild) {
// should test child
if (i > 0 && Array.isArray(childType)) {
const parentDepth = childDepth - 1;
const parentType = example[i - 1];

if (typeof parentType !== 'string')
throw TypeError('parent must be string');

it(`(${exampleIdx})${getIndentation(
childDepth,
)}${parentType} (depth ${parentDepth}) > ${childType} ❌`, () => {
expect(
isValidChild({
parentDepth,
parentType,
childType,
childType: childType[0],
}),
).toBe(false);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
*/
import { COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE } from './componentTypes';

export default function componentIsResizable(entity) {
export default function componentIsResizable(entity: { type: string }) {
return [COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE].indexOf(entity.type) > -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@
* specific language governing permissions and limitations
* under the License.
*/
export function getDashboardFilterKey({ chartId, column }) {

interface GetDashboardFilterKeyProps {
chartId: string;
column: string;
}

export function getDashboardFilterKey({
chartId,
column,
}: GetDashboardFilterKeyProps) {
return `${chartId}_${column}`;
}

export function getChartIdAndColumnFromFilterKey(key) {
export function getChartIdAndColumnFromFilterKey(key: string) {
const [chartId, ...parts] = key.split('_');
const column = parts.slice().join('_');
return { chartId: parseInt(chartId, 10), column };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,33 @@
*/
import { getChartIdAndColumnFromFilterKey } from './getDashboardFilterKey';

interface FilterScopeMap {
[key: string]: number[];
}

interface GetRevertFilterScopeProps {
checked: string[];
filterFields: string[];
filterScopeMap: FilterScopeMap;
}

export default function getRevertedFilterScope({
checked = [],
filterFields = [],
filterScopeMap = {},
}) {
const checkedChartIdsByFilterField = checked.reduce((map, value) => {
const [chartId, filterField] = value.split(':');
return {
...map,
[filterField]: (map[filterField] || []).concat(parseInt(chartId, 10)),
};
}, {});
}: GetRevertFilterScopeProps) {
const checkedChartIdsByFilterField = checked.reduce<FilterScopeMap>(
(map, value) => {
const [chartId, filterField] = value.split(':');
return {
...map,
[filterField]: (map[filterField] || []).concat(parseInt(chartId, 10)),
};
},
{},
);

return filterFields.reduce((map, filterField) => {
return filterFields.reduce<FilterScopeMap>((map, filterField) => {
const { chartId } = getChartIdAndColumnFromFilterKey(filterField);
// force display filter_box chart as unchecked, but show checkbox as disabled
const updatedCheckedIds = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,21 @@ const parentMaxDepthLookup = {
[MARKDOWN_TYPE]: {},
};

export default function isValidChild({ parentType, childType, parentDepth }) {
interface IsValidChildProps {
parentType?: string;
childType?: string;
parentDepth?: unknown;
}

export default function isValidChild(child: IsValidChildProps): boolean {
const { parentType, childType, parentDepth } = child;
if (!parentType || !childType || typeof parentDepth !== 'number') {
return false;
}

const maxParentDepth = (parentMaxDepthLookup[parentType] || {})[childType];
const maxParentDepth: number | undefined = (parentMaxDepthLookup[
parentType
] || {})[childType];

return typeof maxParentDepth === 'number' && parentDepth <= maxParentDepth;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
* specific language governing permissions and limitations
* under the License.
*/
const stopPeriodicRender = refreshTimer => {
const stopPeriodicRender = (refreshTimer?: number) => {
if (refreshTimer) {
clearInterval(refreshTimer);
}
};

interface SetPeriodicRunnerProps {
interval?: number;
periodicRender: TimerHandler;
refreshTimer?: number;
}

export default function setPeriodicRunner({
interval = 0,
periodicRender,
refreshTimer,
}) {
}: SetPeriodicRunnerProps) {
stopPeriodicRender(refreshTimer);

if (interval > 0) {
Expand Down

0 comments on commit a262ea7

Please sign in to comment.