Skip to content

Commit

Permalink
fix(TreeData.fromList): 优化根节点的判定逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 10, 2021
1 parent 32eb285 commit 4222744
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 33 deletions.
34 changes: 34 additions & 0 deletions src/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
closestTo,
compareAsc,
compareDesc,
daysToWeeks,
differenceInBusinessDays,
differenceInCalendarDays,
differenceInCalendarISOWeeks,
Expand All @@ -47,6 +48,7 @@ export {
differenceInYears,
eachDayOfInterval,
eachHourOfInterval,
eachMinuteOfInterval,
eachMonthOfInterval,
eachQuarterOfInterval,
eachWeekendOfInterval,
Expand Down Expand Up @@ -105,7 +107,11 @@ export {
getWeeksInMonth,
getWeekYear,
getYear,
hoursToMilliseconds,
hoursToMinutes,
hoursToSeconds,
intervalToDuration,
intlFormat,
isAfter,
isBefore,
isDate,
Expand Down Expand Up @@ -158,11 +164,32 @@ export {
lightFormat,
max,
milliseconds,
millisecondsToHours,
millisecondsToMinutes,
millisecondsToSeconds,
min,
minutesToHours,
minutesToMilliseconds,
minutesToSeconds,
monthsToQuarters,
monthsToYears,
nextDay,
nextFriday,
nextMonday,
nextSaturday,
nextSunday,
nextThursday,
nextTuesday,
nextWednesday,
parse,
parseISO,
parseJSON,
quartersToMonths,
quartersToYears,
roundToNearestMinutes,
secondsToHours,
secondsToMilliseconds,
secondsToMinutes,
set,
setDate,
setDay,
Expand Down Expand Up @@ -207,6 +234,9 @@ export {
subWeeks,
subYears,
toDate,
weeksToDays,
yearsToMonths,
yearsToQuarters,
// @endindex
} from 'date-fns/esm'

Expand All @@ -229,6 +259,7 @@ export {
cy,
da,
de,
deAT,
el,
enAU,
enCA,
Expand All @@ -251,6 +282,7 @@ export {
he,
hi,
hr,
ht,
hu,
hy,
id,
Expand All @@ -265,6 +297,7 @@ export {
lt,
lv,
mk,
mn,
ms,
mt,
nb,
Expand All @@ -277,6 +310,7 @@ export {
ru,
sk,
sl,
sq,
sr,
srLatn,
sv,
Expand Down
7 changes: 1 addition & 6 deletions src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
export {
// 手动导出以解决 cjs 下 Cannot redefine property 的问题
// @index(['../../node_modules/react-use/esm/{use,create}*.js', '!**/{useToggle,createGlobalState,useTitle,useInterval,useSearchParam,useLocalStorage,useWindowSize,useHover,createRouter,useKeyboardJs,useMeasureDirty,useSpring,useWait}.js'], (f, _) => `${f.name},`)
createBreakpoint,
createMemo,
createReducer,
createReducerContext,
createStateContext,
useAsync,
useAsyncFn,
useAsyncRetry,
Expand Down Expand Up @@ -72,7 +67,7 @@ export {
useMouseHovered,
useMouseWheel,
useMultiStateValidator,
useNetwork,
useNetworkState,
useNumber,
useObservable,
useOrientation,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/TreeData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('TreeData', () => {
[
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 3, pid: -1 },
{ id: 11, pid: 1 },
{ id: 4 },
{ id: 12, pid: 1 },
Expand Down
31 changes: 19 additions & 12 deletions src/utils/TreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ export class TreeData<TNode extends TreeDataNode> {

private searchStrategy: TreeDataSearchStrategy = 'DFS'

private cloneIgnore:
| ((value: unknown) => boolean | undefined)
| undefined = undefined
private cloneIgnore: ((value: unknown) => boolean | undefined) | undefined =
undefined

/**
* 构造函数。
Expand Down Expand Up @@ -263,9 +262,8 @@ export class TreeData<TNode extends TreeDataNode> {
let _node: TNode | undefined
while ((_node = nodes.pop())) {
if (Array.isArray(_node[this.childrenPropName])) {
const fns: Array<TreeDataTraverseFn<TNode>> = (Array.isArray(fn)
? fn
: [fn]
const fns: Array<TreeDataTraverseFn<TNode>> = (
Array.isArray(fn) ? fn : [fn]
).filter(fn => typeof fn === 'function') as any
for (let i = 0; i < fns.length; i++) {
TreeData.traverse<TNode>(
Expand Down Expand Up @@ -370,7 +368,7 @@ export class TreeData<TNode extends TreeDataNode> {
[K in keyof TNode]?: (payload: TreeDataTraverseFnPayload<TNode>) => any
} & {
[K: string]: (payload: TreeDataTraverseFnPayload<TNode>) => any
}
},
>(
props: TProps,
): TreeData<
Expand Down Expand Up @@ -623,16 +621,25 @@ export class TreeData<TNode extends TreeDataNode> {
list,
cloneIgnore,
) as any
const IS_CHILD_KEY = '__tree_data_is_child__'
const data = _list
.map(item => {
item.children = _list.filter(
item2 =>
item.children = _list.filter(item2 => {
if (item2[IS_CHILD_KEY] === true) return false
const isChild =
(item2 as any)[parentIdKey] != null &&
(item as any)[idKey] === (item2 as any)[parentIdKey],
) as any
(item as any)[idKey] === (item2 as any)[parentIdKey]
if (isChild) {
Object.defineProperty(item2, IS_CHILD_KEY, {
value: true,
enumerable: false,
})
}
return isChild
}) as any
return item
})
.filter(item => (item as any)[parentIdKey] == null)
.filter(item => !item[IS_CHILD_KEY])
return new TreeData(data)
}
}
1 change: 1 addition & 0 deletions src/utils/__snapshots__/TreeData.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,7 @@ Array [
"children": Array [],
"id": 3,
"name": "0. 3",
"pid": -1,
},
Object {
"children": Array [
Expand Down
44 changes: 30 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,9 @@
integrity sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==

"@nodelib/fs.walk@^1.2.3":
version "1.2.7"
resolved "https://registry.yarnpkg.com/@nodelib/fs.walk/-/fs.walk-1.2.7.tgz#94c23db18ee4653e129abd26fb06f870ac9e1ee2"
integrity sha512-BTIhocbPBSrRmHxOAJFtR18oLhxTtAFDAvL8hY1S3iU8k+E60W/YFs4jrixGzQjMpF4qPXxIQHcjVD9dz1C2QA==
version "1.2.8"
resolved "https://registry.yarnpkg.com/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz#e95737e8bb6746ddedf69c556953494f196fe69a"
integrity sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==
dependencies:
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"
Expand Down Expand Up @@ -1699,9 +1699,9 @@
"@types/node" "*"

"@types/glob@^7.1.1":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183"
integrity sha512-SEYeGAIQIQX8NN6LDKprLjbrd5dARM5EXsd8GI/A5l0apYI1fGMWgPHSe4ZKL4eozlAyI+doUE9XbYS4xCkQ1w==
version "7.1.4"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.4.tgz#ea59e21d2ee5c517914cb4bc8e4153b99e566672"
integrity sha512-w+LsMxKyYQm347Otw+IfBXOv9UWVjpHpCDdbBMt8Kz/xbvCYNjP+0qPh91Km3iKfSRLBB0P7fAMf0KHrPu+MyA==
dependencies:
"@types/minimatch" "*"
"@types/node" "*"
Expand Down Expand Up @@ -1778,25 +1778,30 @@
integrity sha512-bpcvu/MKHHeYX+qeEN8GE7DIravODWdACVA1ctevD8CN24RhPZIKMn9ntfAsrvLfSX3cR5RrBKAbYm9bGs0A+Q==

"@types/minimatch@*":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.4.tgz#f0ec25dbf2f0e4b18647313ac031134ca5b24b21"
integrity sha512-1z8k4wzFnNjVK/tlxvrWuK5WMt6mydWWP7+zvH5eFep4oj+UkrfiJTRtjCeBXNpwaA/FYqqtb4/QS4ianFpIRA==
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==

"@types/minimist@^1.2.0":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.1.tgz#283f669ff76d7b8260df8ab7a4262cc83d988256"
integrity sha512-fZQQafSREFyuZcdWFAExYjBiCL7AUCdgsk80iO0q4yihYYdcIiH28CcuPTGFgLOCC8RlW49GSQxdHwZP+I7CNg==

"@types/node@*", "@types/[email protected]":
version "16.0.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.0.0.tgz#067a6c49dc7a5c2412a505628e26902ae967bf6f"
integrity sha512-TmCW5HoZ2o2/z2EYi109jLqIaPIi9y/lc2LmDCWzuCi35bcaQ+OtUh6nwBiFK7SOu25FAU5+YKdqFZUwtqGSdg==
"@types/node@*":
version "16.3.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.3.1.tgz#24691fa2b0c3ec8c0d34bfcfd495edac5593ebb4"
integrity sha512-N87VuQi7HEeRJkhzovao/JviiqKjDKMVKxKMfUvSKw+MbkbW8R0nA3fi/MQhhlxV2fQ+2ReM+/Nt4efdrJx3zA==

"@types/[email protected]":
version "10.17.13"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.13.tgz#ccebcdb990bd6139cd16e84c39dc2fb1023ca90c"
integrity sha512-pMCcqU2zT4TjqYFrWtYHKal7Sl30Ims6ulZ4UFXxI4xbtQqK/qqKwkDoBFCfooRqqmRu9vY3xaJRwxSh673aYg==

"@types/[email protected]":
version "16.0.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.0.0.tgz#067a6c49dc7a5c2412a505628e26902ae967bf6f"
integrity sha512-TmCW5HoZ2o2/z2EYi109jLqIaPIi9y/lc2LmDCWzuCi35bcaQ+OtUh6nwBiFK7SOu25FAU5+YKdqFZUwtqGSdg==

"@types/node@^14.14.22":
version "14.17.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.17.4.tgz#218712242446fc868d0e007af29a4408c7765bc0"
Expand Down Expand Up @@ -4250,7 +4255,18 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

fast-glob@^3.0.3, fast-glob@^3.1.1:
fast-glob@^3.0.3:
version "3.2.7"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.7.tgz#fd6cb7a2d7e9aa7a7846111e85a196d6b2f766a1"
integrity sha512-rYGMRwip6lUMvYD3BTScMwT1HtAs2d71SMv66Vrxs0IekGZEjhM0pcMfjQPnknBt2zeCwQMEupiN02ZP4DiT1Q==
dependencies:
"@nodelib/fs.stat" "^2.0.2"
"@nodelib/fs.walk" "^1.2.3"
glob-parent "^5.1.2"
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.1.1:
version "3.2.6"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.6.tgz#434dd9529845176ea049acc9343e8282765c6e1a"
integrity sha512-GnLuqj/pvQ7pX8/L4J84nijv6sAnlwvSDpMkJi9i7nPmPxGtRPkBSStfvDW5l6nMdX9VWe+pkKWFTgD+vF2QSQ==
Expand Down

0 comments on commit 4222744

Please sign in to comment.