refactor(frontend): move utils to TypeScript (#9820)

* refactor(frontend): move utils to typescript (#9101)

* refactor(frontend): don't export interfaces

* test(frontend): update types and test for isValidChild
This commit is contained in:
Christian Murphy 2020-05-20 14:47:40 -07:00 committed by GitHub
parent ef6af935fc
commit a262ea7487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 60 additions and 21 deletions

View File

@ -31,7 +31,7 @@ import {
TAB_TYPE as TAB, TAB_TYPE as TAB,
} from 'src/dashboard/util/componentTypes'; } from 'src/dashboard/util/componentTypes';
const getIndentation = depth => const getIndentation = (depth: number) =>
Array(depth * 3) Array(depth * 3)
.fill('') .fill('')
.join('-'); .join('-');
@ -136,12 +136,14 @@ describe('isValidChild', () => {
invalidExamples.forEach((example, exampleIdx) => { invalidExamples.forEach((example, exampleIdx) => {
let childDepth = 0; let childDepth = 0;
example.forEach((childType, i) => { example.forEach((childType, i) => {
const shouldTestChild = Array.isArray(childType); // should test child
if (i > 0 && Array.isArray(childType)) {
if (i > 0 && shouldTestChild) {
const parentDepth = childDepth - 1; const parentDepth = childDepth - 1;
const parentType = example[i - 1]; const parentType = example[i - 1];
if (typeof parentType !== 'string')
throw TypeError('parent must be string');
it(`(${exampleIdx})${getIndentation( it(`(${exampleIdx})${getIndentation(
childDepth, childDepth,
)}${parentType} (depth ${parentDepth}) > ${childType} `, () => { )}${parentType} (depth ${parentDepth}) > ${childType} `, () => {
@ -149,7 +151,7 @@ describe('isValidChild', () => {
isValidChild({ isValidChild({
parentDepth, parentDepth,
parentType, parentType,
childType, childType: childType[0],
}), }),
).toBe(false); ).toBe(false);
}); });

View File

@ -18,6 +18,6 @@
*/ */
import { COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE } from './componentTypes'; 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; return [COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE].indexOf(entity.type) > -1;
} }

View File

@ -16,11 +16,20 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
export function getDashboardFilterKey({ chartId, column }) {
interface GetDashboardFilterKeyProps {
chartId: string;
column: string;
}
export function getDashboardFilterKey({
chartId,
column,
}: GetDashboardFilterKeyProps) {
return `${chartId}_${column}`; return `${chartId}_${column}`;
} }
export function getChartIdAndColumnFromFilterKey(key) { export function getChartIdAndColumnFromFilterKey(key: string) {
const [chartId, ...parts] = key.split('_'); const [chartId, ...parts] = key.split('_');
const column = parts.slice().join('_'); const column = parts.slice().join('_');
return { chartId: parseInt(chartId, 10), column }; return { chartId: parseInt(chartId, 10), column };

View File

@ -18,20 +18,33 @@
*/ */
import { getChartIdAndColumnFromFilterKey } from './getDashboardFilterKey'; import { getChartIdAndColumnFromFilterKey } from './getDashboardFilterKey';
interface FilterScopeMap {
[key: string]: number[];
}
interface GetRevertFilterScopeProps {
checked: string[];
filterFields: string[];
filterScopeMap: FilterScopeMap;
}
export default function getRevertedFilterScope({ export default function getRevertedFilterScope({
checked = [], checked = [],
filterFields = [], filterFields = [],
filterScopeMap = {}, filterScopeMap = {},
}) { }: GetRevertFilterScopeProps) {
const checkedChartIdsByFilterField = checked.reduce((map, value) => { const checkedChartIdsByFilterField = checked.reduce<FilterScopeMap>(
const [chartId, filterField] = value.split(':'); (map, value) => {
return { const [chartId, filterField] = value.split(':');
...map, return {
[filterField]: (map[filterField] || []).concat(parseInt(chartId, 10)), ...map,
}; [filterField]: (map[filterField] || []).concat(parseInt(chartId, 10)),
}, {}); };
},
{},
);
return filterFields.reduce((map, filterField) => { return filterFields.reduce<FilterScopeMap>((map, filterField) => {
const { chartId } = getChartIdAndColumnFromFilterKey(filterField); const { chartId } = getChartIdAndColumnFromFilterKey(filterField);
// force display filter_box chart as unchecked, but show checkbox as disabled // force display filter_box chart as unchecked, but show checkbox as disabled
const updatedCheckedIds = ( const updatedCheckedIds = (

View File

@ -105,12 +105,21 @@ const parentMaxDepthLookup = {
[MARKDOWN_TYPE]: {}, [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') { if (!parentType || !childType || typeof parentDepth !== 'number') {
return false; return false;
} }
const maxParentDepth = (parentMaxDepthLookup[parentType] || {})[childType]; const maxParentDepth: number | undefined = (parentMaxDepthLookup[
parentType
] || {})[childType];
return typeof maxParentDepth === 'number' && parentDepth <= maxParentDepth; return typeof maxParentDepth === 'number' && parentDepth <= maxParentDepth;
} }

View File

@ -16,17 +16,23 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
const stopPeriodicRender = refreshTimer => { const stopPeriodicRender = (refreshTimer?: number) => {
if (refreshTimer) { if (refreshTimer) {
clearInterval(refreshTimer); clearInterval(refreshTimer);
} }
}; };
interface SetPeriodicRunnerProps {
interval?: number;
periodicRender: TimerHandler;
refreshTimer?: number;
}
export default function setPeriodicRunner({ export default function setPeriodicRunner({
interval = 0, interval = 0,
periodicRender, periodicRender,
refreshTimer, refreshTimer,
}) { }: SetPeriodicRunnerProps) {
stopPeriodicRender(refreshTimer); stopPeriodicRender(refreshTimer);
if (interval > 0) { if (interval > 0) {