Skip to content

Commit 9aece38

Browse files
authored
Refactor validators to reduce repeated conditionals (#8032)
2 parents 657e0d5 + fdbb0ca commit 9aece38

File tree

11 files changed

+31
-42
lines changed

11 files changed

+31
-42
lines changed

packages/components/src/schema/props/accordion-callbacks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Generic } from 'adopted-style-sheets';
22
import type { Events } from '../enums';
33
import type { EventValueOrEventCallback } from '../types/callbacks';
44
import { watchValidator } from '../utils';
5+
import { isObject } from '../validators';
56

67
/* types */
78
export type AccordionCallbacksPropType<T> = {
@@ -17,5 +18,5 @@ export type PropAccordionCallbacks<T> = {
1718

1819
/* validator */
1920
export const validateAccordionCallbacks = (component: Generic.Element.Component, value?: AccordionCallbacksPropType<boolean>): void => {
20-
watchValidator(component, `_on`, (value) => typeof value === 'object' && value !== null, new Set(['AccordionCallbacksPropType {Events.onClick}']), value);
21+
watchValidator(component, `_on`, (value) => isObject(value), new Set(['AccordionCallbacksPropType {Events.onClick}']), value);
2122
};

packages/components/src/schema/props/button-callbacks.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Events } from '../enums';
22
import type { EventCallback, EventValueOrEventCallback } from '../types/callbacks';
33
import type { StencilUnknown } from '../types/unknown';
44
import { watchValidator } from '../utils';
5+
import { isObject } from '../validators';
56

67
import type { Generic } from 'adopted-style-sheets';
78
/* types */
@@ -19,11 +20,5 @@ export type PropButtonCallbacks<T> = {
1920

2021
/* validator */
2122
export const validateButtonCallbacks = (component: Generic.Element.Component, value?: ButtonCallbacksPropType<StencilUnknown>): void => {
22-
watchValidator(
23-
component,
24-
`_on`,
25-
(value) => typeof value === 'object' && value !== null,
26-
new Set(['ButtonCallbacksPropType {Events.onClick, Events.onMouseDown}']),
27-
value,
28-
);
23+
watchValidator(component, `_on`, (value) => isObject(value), new Set(['ButtonCallbacksPropType {Events.onClick, Events.onMouseDown}']), value);
2924
};

packages/components/src/schema/props/details-callbacks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Generic } from 'adopted-style-sheets';
22
import type { Events } from '../enums';
33
import type { EventValueOrEventCallback } from '../types/callbacks';
44
import { watchValidator } from '../utils';
5+
import { isObject } from '../validators';
56

67
/* types */
78
export type DetailsCallbacksPropType<T> = {
@@ -17,5 +18,5 @@ export type PropDetailsCallbacks<T> = {
1718

1819
/* validator */
1920
export const validateDetailsCallbacks = (component: Generic.Element.Component, value?: DetailsCallbacksPropType<boolean>): void => {
20-
watchValidator(component, `_on`, (value) => typeof value === 'object' && value !== null, new Set(['DetailsCallbacksPropType {Events.onToggle}']), value);
21+
watchValidator(component, `_on`, (value) => isObject(value), new Set(['DetailsCallbacksPropType {Events.onToggle}']), value);
2122
};

packages/components/src/schema/props/icons.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Generic } from 'adopted-style-sheets';
22

3-
import type { AnyIconFontClass, KoliBriCustomIcon, KoliBriHorizontalIconsProp, KoliBriIconsProp, KoliBriIconsState } from '../types';
3+
import type { AnyIconFontClass, KoliBriAllIcons, KoliBriCustomIcon, KoliBriHorizontalIconsProp, KoliBriIconsProp, KoliBriIconsState } from '../types';
44
import type { Stringified } from '../types/common';
55
import type { WatchOptions } from '../utils';
66
import { objectObjectHandler, parseJson, watchValidator } from '../utils';
@@ -26,7 +26,7 @@ export type PropHorizontalIcons = {
2626

2727
const mapCustomIcon = (state: KoliBriIconsState, alignment: AlignPropType, icon?: AnyIconFontClass | KoliBriCustomIcon) => {
2828
if (isObject(icon)) {
29-
state[alignment] = icon as KoliBriCustomIcon;
29+
state[alignment] = icon;
3030
} else if (isString(icon, 1)) {
3131
state[alignment] = {
3232
icon: icon as AnyIconFontClass,
@@ -42,11 +42,12 @@ export const mapIconProp2State = (icon: KoliBriIconsProp): KoliBriIconsState =>
4242
icon: icon as AnyIconFontClass,
4343
},
4444
};
45-
} else if (typeof icon === 'object' && icon !== null) {
46-
mapCustomIcon(state, 'top', icon.top);
47-
mapCustomIcon(state, 'right', icon.right);
48-
mapCustomIcon(state, 'bottom', icon.bottom);
49-
mapCustomIcon(state, 'left', icon.left);
45+
} else if (isObject(icon)) {
46+
const icons: KoliBriAllIcons = icon;
47+
mapCustomIcon(state, 'top', icons.top);
48+
mapCustomIcon(state, 'right', icons.right);
49+
mapCustomIcon(state, 'bottom', icons.bottom);
50+
mapCustomIcon(state, 'left', icons.left);
5051
}
5152
return state;
5253
};
@@ -59,8 +60,7 @@ const beforePatchIcon = (component: Generic.Element.Component): void => {
5960
};
6061

6162
export const isIcon = (value?: unknown): boolean =>
62-
typeof value === 'object' &&
63-
value !== null &&
63+
isObject(value) &&
6464
(typeof (value as KoliBriCustomIcon).style === 'undefined' || isStyle((value as KoliBriCustomIcon).style)) &&
6565
(typeof (value as KoliBriCustomIcon).label === 'undefined' || isString((value as KoliBriCustomIcon).label)) &&
6666
isString((value as KoliBriCustomIcon).icon, 1);
@@ -76,13 +76,12 @@ export const validateIcons = (component: Generic.Element.Component, value?: Icon
7676
component,
7777
'_icons',
7878
(value): boolean => {
79-
const valueIsEmptyObject = typeof value === 'object' && value !== null && Object.keys(value).length === 0;
79+
const valueIsEmptyObject = isObject(value) && Object.keys(value).length === 0;
8080
return (
8181
value === null ||
8282
valueIsEmptyObject ||
8383
isString(value, 1) ||
84-
(typeof value === 'object' &&
85-
value !== null &&
84+
(isObject(value) &&
8685
(isString(value.left, 1) ||
8786
isIcon(value.left) ||
8887
isString(value.right, 1) ||

packages/components/src/schema/props/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { RadioOption, Optgroup, Option, StencilUnknown } from '../types';
44
import type { Stringified } from '../types/common';
55
import type { WatchOptions } from '../utils';
66
import { watchJsonArrayString } from '../utils';
7-
import { validateInputSelectOptions } from '../validators';
7+
import { isObject, validateInputSelectOptions } from '../validators';
88

99
/* types */
1010

@@ -36,7 +36,7 @@ export const validateOptions = (component: Generic.Element.Component, value: Opt
3636
watchJsonArrayString(
3737
component,
3838
'_options',
39-
(item: Option<StencilUnknown>) => typeof item === 'object' && item !== null && typeof item.label === 'string' && item.label.length > 0,
39+
(item: Option<StencilUnknown>) => isObject(item) && typeof item.label === 'string' && item.label.length > 0,
4040
value,
4141
undefined,
4242
options,

packages/components/src/schema/props/popover-callbacks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Generic } from 'adopted-style-sheets';
22
import type { Events } from '../enums';
33
import type { EventCallback } from '../types/callbacks';
44
import { watchValidator } from '../utils';
5+
import { isObject } from '../validators';
56

67
/* types */
78
export type PopoverCallbacksPropType = {
@@ -17,5 +18,5 @@ export type PropPopoverCallbacks = {
1718

1819
/* validator */
1920
export const validatePopoverCallbacks = (component: Generic.Element.Component, value?: PopoverCallbacksPropType): void => {
20-
watchValidator(component, `_on`, (value) => typeof value === 'object' && value !== null, new Set(['PopoverCallbacksPropType {Events.onClose}']), value);
21+
watchValidator(component, `_on`, (value) => isObject(value), new Set(['PopoverCallbacksPropType {Events.onClose}']), value);
2122
};

packages/components/src/schema/props/table-callbacks.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Events } from '../enums';
22
import type { EventValueOrEventCallback } from '../types/callbacks';
33
import { watchValidator } from '../utils';
4+
import { isObject } from '../validators';
45

56
import type { Generic } from 'adopted-style-sheets';
67
import type { KoliBriSortDirection, KoliBriTableDataType } from '../types';
@@ -34,21 +35,9 @@ export type StatefulPropTableCallbacks = {
3435

3536
/* validator */
3637
export const validateTableCallbacks = (component: Generic.Element.Component, value?: TableCallbacksPropType): void => {
37-
watchValidator(
38-
component,
39-
`_on`,
40-
(value) => typeof value === 'object' && value !== null,
41-
new Set(['TableCallbacksPropType {Events.onSort, Events.onSelectionChange}']),
42-
value,
43-
);
38+
watchValidator(component, `_on`, (value) => isObject(value), new Set(['TableCallbacksPropType {Events.onSort, Events.onSelectionChange}']), value);
4439
};
4540

4641
export const validateTableStatefulCallbacks = (component: Generic.Element.Component, value?: TableStatefulCallbacksPropType): void => {
47-
watchValidator(
48-
component,
49-
`_on`,
50-
(value) => typeof value === 'object' && value !== null,
51-
new Set(['TableStatefulCallbacksPropType {Events.onSelectionChange}']),
52-
value,
53-
);
42+
watchValidator(component, `_on`, (value) => isObject(value), new Set(['TableStatefulCallbacksPropType {Events.onSelectionChange}']), value);
5443
};

packages/components/src/schema/props/table-data-foot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Generic } from 'adopted-style-sheets';
22

33
import type { SetStateHooks } from '../utils';
44
import { emptyStringByArrayHandler, objectObjectHandler, parseJson, setState } from '../utils';
5+
import { isObject } from '../validators';
56
import type { KoliBriTableDataType, Stringified } from '../types';
67

78
/* types */
@@ -27,7 +28,7 @@ export const validateTableDataFoot = (component: Generic.Element.Component, valu
2728
} catch (e) {
2829
// value keeps the original data
2930
}
30-
if (Array.isArray(value) && value.every((data: KoliBriTableDataType) => typeof data === 'object' && data !== null)) {
31+
if (Array.isArray(value) && value.every((data: KoliBriTableDataType) => isObject(data))) {
3132
setState(component, '_dataFoot', value, setStateHooks);
3233
}
3334
});

packages/components/src/schema/props/table-data.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Generic } from 'adopted-style-sheets';
22

33
import type { SetStateHooks } from '../utils';
44
import { emptyStringByArrayHandler, objectObjectHandler, parseJson, setState } from '../utils';
5+
import { isObject } from '../validators';
56
import type { KoliBriTableDataType, Stringified } from '../types';
67

78
/* types */
@@ -27,7 +28,7 @@ export const validateTableData = (component: Generic.Element.Component, value?:
2728
} catch (e) {
2829
// value keeps the original data
2930
}
30-
if (Array.isArray(value) && value.every((data: KoliBriTableDataType) => typeof data === 'object' && data !== null)) {
31+
if (Array.isArray(value) && value.every((data: KoliBriTableDataType) => isObject(data))) {
3132
setState(component, '_data', value, setStateHooks);
3233
}
3334
});

packages/components/src/schema/props/toolbar-items.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Generic } from 'adopted-style-sheets';
22

33
import { emptyStringByArrayHandler, objectObjectHandler, parseJson, setState } from '../utils';
4+
import { isObject } from '../validators';
45
import type { ButtonProps, LinkProps } from '../../schema';
56

67
/* types */
@@ -27,7 +28,7 @@ export const validateToolbarItems = (component: Generic.Element.Component, value
2728
} catch (e) {
2829
// value keeps the original items
2930
}
30-
if (Array.isArray(value) && value.every((items: ToolbarItemPropType) => typeof items === 'object' && items !== null)) {
31+
if (Array.isArray(value) && value.every((items: ToolbarItemPropType) => isObject(items))) {
3132
setState(component, '_items', value);
3233
}
3334
});

0 commit comments

Comments
 (0)