Skip to content

Commit

Permalink
style: fix type error
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangpaopao0609 committed Feb 2, 2025
1 parent 83b2107 commit 1558147
Show file tree
Hide file tree
Showing 27 changed files with 290 additions and 230 deletions.
1 change: 1 addition & 0 deletions js/color-picker/cmyk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const cmykInputToColor = (input: string) => {
if (/cmyk/i.test(input)) {
const str = input.replace(/\s/g, '');
const match = str.match(REG_CMYK_STRING);
if (!match) return input;
const c = toNumber(match[1]);
const m = toNumber(match[2]);
const y = toNumber(match[3]);
Expand Down
23 changes: 11 additions & 12 deletions js/color-picker/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface ColorStates {
interface GradientStates {
colors: GradientColorPoint[];
degree: number;
selectedId: string;
selectedId: string | null;
css?: string;
}

Expand All @@ -48,8 +48,8 @@ const hsv2hsla = (states: ColorStates): tinyColor.ColorFormats.HSLA => tinyColor
export const gradientColors2string = (object: GradientColors): string => {
const { points, degree } = object;
const colorsStop = points
.sort((pA, pB) => pA.left - pB.left)
.map((p) => `${p.color} ${Math.round(p.left * 100) / 100}%`);
.sort((pA, pB) => (pA?.left ?? 0) - (pB?.left ?? 0))
.map((p) => `${p.color} ${Math.round((p.left ?? 0) * 100) / 100}%`);

return `linear-gradient(${degree}deg,${colorsStop.join(',')})`;
};
Expand Down Expand Up @@ -84,9 +84,9 @@ export class Color {
a: 1,
};

originColor: string;
originColor!: string;

isGradient: boolean;
isGradient!: boolean;

gradientStates: GradientStates = {
colors: [],
Expand Down Expand Up @@ -117,14 +117,14 @@ export class Color {
if (gradientColors) {
this.isGradient = true;
const object = gradientColors as GradientColors;
const points = object.points.map((c) => genGradientPoint(c.left, c.color));
const points = object.points.map((c) => genGradientPoint(c.left ?? 0, c.color ?? ''));
this.gradientStates = {
colors: points,
degree: object.degree,
selectedId: points[0]?.id || null,
};
this.gradientStates.css = this.linearGradient;
colorInput = this.gradientSelectedPoint?.color;
colorInput = this.gradientSelectedPoint?.color ?? '';
}

this.updateStates(colorInput);
Expand Down Expand Up @@ -242,15 +242,15 @@ export class Color {
}

get gradientSelectedId() {
return this.gradientStates.selectedId;
return this.gradientStates.selectedId ?? '';
}

set gradientSelectedId(id: string) {
if (id === this.gradientSelectedId) {
return;
}
this.gradientStates.selectedId = id;
this.updateStates(this.gradientSelectedPoint?.color);
this.updateStates(this.gradientSelectedPoint?.color ?? '');
}

get gradientDegree() {
Expand Down Expand Up @@ -473,13 +473,12 @@ const COLOR_OBJECT_OUTPUT_KEYS = [
* @param color
* @returns
*/
export const getColorObject = (color: Color): ColorObject => {
export const getColorObject = (color: Color): ColorObject | null => {
if (!color) {
return null;
}
const colorObject = Object.create(null);
// eslint-disable-next-line no-return-assign
COLOR_OBJECT_OUTPUT_KEYS.forEach((key) => (colorObject[key] = color[key]));
COLOR_OBJECT_OUTPUT_KEYS.forEach((key) => (colorObject[key] = (color as { [key: string]: any })[key]));

Check failure on line 481 in js/color-picker/color.ts

View workflow job for this annotation

GitHub Actions / test

Arrow function should not return assignment
if (color.isGradient) {
colorObject.linearGradient = color.linearGradient;
}
Expand Down
19 changes: 9 additions & 10 deletions js/color-picker/draggable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-use-before-define */
export interface Coordinate {
x: number;
y: number;
Expand All @@ -17,16 +16,16 @@ export interface DraggableProps {
}

interface DraggableHandles {
start: (this: Draggable, event: DraggableEvent) => {};
drag: (this: Draggable, event: DraggableEvent) => {};
end: (this: Draggable, event: DraggableEvent) => {};
start: (event: DraggableEvent) => void;
drag: (event: DraggableEvent) => void;
end: (event: DraggableEvent) => void;
}

// 配置项
const defaultsOptions: DraggableProps = {
start: (coordinate: Coordinate, event: DraggableEvent) => {},
drag: (coordinate: Coordinate, event: DraggableEvent) => {},
end: (coordinate: Coordinate, event: DraggableEvent) => {},
start: (coordinate: Coordinate, event?: DraggableEvent) => {},
drag: (coordinate: Coordinate, event?: DraggableEvent) => {},
end: (coordinate: Coordinate, event?: DraggableEvent) => {},
};

export class Draggable {
Expand Down Expand Up @@ -58,20 +57,20 @@ export class Draggable {
window.addEventListener('mouseup', this.handles.end, false);
window.addEventListener('contextmenu', this.handles.end, false);
this.dragging = true;
this.props.start(this.#getCoordinate(event), event);
this.props.start?.(this.#getCoordinate(event), event);
}

#drag(event: DraggableEvent) {
if (!this.dragging) {
return;
}
this.props.drag(this.#getCoordinate(event), event);
this.props.drag?.(this.#getCoordinate(event), event);
}

#dragEnd(event: DraggableEvent) {
setTimeout(() => {
this.dragging = false;
this.props.end(this.#getCoordinate(event), event);
this.props.end?.(this.#getCoordinate(event), event);
}, 0);
window.removeEventListener('mousemove', this.handles.drag, false);
window.removeEventListener('mouseup', this.handles.end, false);
Expand Down
19 changes: 10 additions & 9 deletions js/color-picker/gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const generateRegExp = (): RegExpLib => {
* @returns {object|undefined}
*/
const parseGradient = (regExpLib: RegExpLib, input: string) => {
let result: ParseGradientResult;
let result: ParseGradientResult | undefined = undefined;

Check failure on line 103 in js/color-picker/gradient.ts

View workflow job for this annotation

GitHub Actions / test

It's not necessary to initialize 'result: ParseGradientResult | undefined' to undefined
let matchColorStop: any;
let stopResult: ColorStop;

Expand Down Expand Up @@ -145,7 +145,7 @@ const parseGradient = (regExpLib: RegExpLib, input: string) => {
// eslint-disable-next-line prefer-destructuring
stopResult.position = matchColorStop[2];
}
result.colorStopList.push(stopResult);
result.colorStopList?.push(stopResult);

// Continue searching from previous position.
matchColorStop = regExpLib.colorStopSearch.exec(matchGradient[4]);
Expand Down Expand Up @@ -181,7 +181,7 @@ export const isGradientColor = (input: string): null | RegExpExecArray => {
};

// 边界字符串和角度关系
const sideCornerDegreeMap = {
const sideCornerDegreeMap: Record<string, number> = {
top: 0,
right: 90,
bottom: 180,
Expand Down Expand Up @@ -211,20 +211,21 @@ export const parseGradientString = (input: string): GradientColors | boolean =>
degree: 0,
};

const result: ParseGradientResult = parseGradient(REGEXP_LIB, match[1]);
const result: ParseGradientResult | undefined = parseGradient(REGEXP_LIB, match[1]);
if (!result) return false;
if (result.original.trim() !== match[1].trim()) {
return false;
}
const points: GradientColorPoint[] = result.colorStopList.map(({ color, position }) => {
const points: GradientColorPoint[] = result.colorStopList?.map(({ color, position }) => {
const point = Object.create(null);
point.color = tinyColor(color).toRgbString();
point.left = parseFloat(position);
point.left = parseFloat(position ?? '0');
return point;
});
}) ?? [];
gradientColors.points = points;
let degree = parseInt(result.angle, 10);
let degree = parseInt(result.angle ?? '90', 10);
if (Number.isNaN(degree)) {
degree = sideCornerDegreeMap[result.sideCorner] || 90;
degree = sideCornerDegreeMap[result.sideCorner ?? ''] ?? 90;
}
gradientColors.degree = degree;

Expand Down
4 changes: 2 additions & 2 deletions js/date-picker/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function formatRange({
}) {
if (!newDate || !Array.isArray(newDate)) return [];

let dayjsDateList = newDate.map((d) => d && parseToDayjs(d, format).locale(dayjsLocale));
let dayjsDateList = newDate.map((d) => d && parseToDayjs(d, format).locale(dayjsLocale ?? ''));

// 保证后面的时间大于前面的时间
if (
Expand Down Expand Up @@ -142,7 +142,7 @@ function formatSingle({
}) {
if (!newDate) return '';

const dayJsDate = parseToDayjs(newDate, format).locale(dayjsLocale);
const dayJsDate = parseToDayjs(newDate, format).locale(dayjsLocale ?? '');

// 格式化失败提示
if (!dayJsDate.isValid()) {
Expand Down
8 changes: 4 additions & 4 deletions js/date-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function isSame(date1: Date, date2: Date, type = 'date', dayjsLocale = 'z
isSameMonth,
isSameWeek,
isSameDate,
};
} as Record<string, (date1: Date, date2: Date, dayjsLocale?: string) => boolean>;
return func[`isSame${firstUpperCase(type)}`](date1, date2, dayjsLocale);
}

Expand Down Expand Up @@ -166,7 +166,7 @@ export function setDateTime(
.hour(hours)
.minute(minutes)
.second(seconds)
.millisecond(milliseconds)
.millisecond(milliseconds ?? 0)
.toDate();
}

Expand Down Expand Up @@ -310,7 +310,7 @@ export function getQuarters(
now: isSame(date, today, 'quarter'),
disabled: (isFunction(disableDate) && disableDate(date)) || (!cancelRangeSelectLimit && outOfRanges(date, minDate, maxDate)),
active: false,
text: quarterLocal[i - 1],
text: quarterLocal?.[i - 1] ?? '',
dayjsObj: dayjs(date).locale(dayjsLocale),
});
}
Expand Down Expand Up @@ -366,7 +366,7 @@ export function getMonths(year: number, params: OptionsType) {
now: isSame(date, today, 'month'),
disabled: (isFunction(disableDate) && disableDate(date)) || (!cancelRangeSelectLimit && outOfRanges(date, minDate, maxDate)),
active: false,
text: monthLocal[date.getMonth()], // `${date.getMonth() + 1} ${monthText || '月'}`,
text: monthLocal?.[date.getMonth()] ?? '', // `${date.getMonth() + 1} ${monthText || '月'}`,
dayjsObj: dayjs(date).locale(dayjsLocale),
});
}
Expand Down
8 changes: 4 additions & 4 deletions js/input-number/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export function getStepValue(p: {
}
}
if (isUndefined(lastValue)) {
newVal = putInRangeNumber(newVal, { max, min, lastValue, largeNumber });
newVal = putInRangeNumber(newVal ?? '', { max, min, lastValue, largeNumber });
}
return largeNumber ? newVal : Number(newVal);
}
Expand Down Expand Up @@ -211,9 +211,9 @@ export function canInputNumber(number: string, largeNumber: boolean) {
// 不能出现空格
if (number.match(/\s/g)) return false;
// 只能出现一个点(.)
if (number.match(/\./g)?.length > 1) return false;
if ((number.match(/\./g)?.length ?? 0) > 1) return false;
// 只能出现一个e(e)
if (number.match(/e/g)?.length > 1) return false;
if ((number.match(/e/g)?.length ?? 0) > 1) return false;
// 只能出现一个负号(-)或 一个正号(+),并且在第一个位置;但允许 3e+10 这种形式
const tmpNumber = number.slice(1);
if (/(\+|-)/.test(tmpNumber) && !/e+/i.test(tmpNumber)) return false;
Expand Down Expand Up @@ -243,7 +243,7 @@ export function formatUnCompleteNumber(
largeNumber?: boolean;
isToFixed?: boolean;
} = {}
): number | string {
): number | string | undefined {
if (['', null, undefined].includes(number) || !/\d+/.test(number)) return undefined;
const { decimalPlaces, largeNumber, isToFixed } = extra;
let newNumber = number.replace(/[.|+|\-|e]$/, '');
Expand Down
2 changes: 1 addition & 1 deletion js/statistic/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function getFormatValue(value:number | undefined | string, decimalPlaces:
useGrouping: !!separator,
};
// replace的替换的方案仅能应对大部分地区
return value.toLocaleString(undefined, options).replace(/,|/g, separator);
return value?.toLocaleString(undefined, options).replace(/,|/g, separator) ?? '';
}
Loading

0 comments on commit 1558147

Please sign in to comment.