Skip to content

Commit fe9edb4

Browse files
authored
Merge pull request #4604 from imsunhao/feat/ts
refactor: improve toBoxArray readability and type safety
2 parents 2a20afa + 7f8b0c5 commit fe9edb4

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

packages/vtable/src/tools/helper.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,22 @@ function getIgnoreCase(obj: any, name: string): any {
236236
return undefined;
237237
}
238238

239-
export function toBoxArray<T>(obj: T | T[]): [T, T, T, T] {
239+
type BoxTuple<T> = [top: T, right: T, bottom: T, left: T];
240+
export function toBoxArray<T>(obj: T | T[]): BoxTuple<T> {
240241
if (!Array.isArray(obj)) {
241-
return [obj /*top*/, obj /*right*/, obj /*bottom*/, obj /*left*/];
242+
return [obj, obj, obj, obj];
242243
}
243244
if (obj.length === 3) {
244-
return [obj[0] /*top*/, obj[1] /*right*/, obj[2] /*bottom*/, obj[1] /*left*/];
245+
return [obj[0], obj[1], obj[2], obj[1]];
245246
}
246247
if (obj.length === 2) {
247-
return [obj[0] /*top*/, obj[1] /*right*/, obj[0] /*bottom*/, obj[1] /*left*/];
248+
return [obj[0], obj[1], obj[0], obj[1]];
248249
}
249250
if (obj.length === 1) {
250-
return [obj[0] /*top*/, obj[0] /*right*/, obj[0] /*bottom*/, obj[0] /*left*/];
251+
return [obj[0], obj[0], obj[0], obj[0]];
251252
}
252253
// return obj as [T, T, T, T];//原先这种返回方式,会造成修改引用问题
253-
return [obj[0] /*top*/, obj[1] /*right*/, obj[2] /*bottom*/, obj[3] /*left*/];
254+
return [obj[0], obj[1], obj[2], obj[3]];
254255
}
255256

256257
export { isNode, getChainSafe, applyChainSafe, getOrApply, getIgnoreCase, array };

0 commit comments

Comments
 (0)