Skip to content

Commit 58a481f

Browse files
authored
refactor: remove dep group-items (#193)
* refactor: remove dep `group-items` * fix: add test
1 parent 3c992b9 commit 58a481f

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"@emotion/styled": "^11.10.5",
6464
"@mui/material": "^5.11.4",
6565
"copy-to-clipboard": "^3.3.3",
66-
"group-items": "^2.2.0",
6766
"zustand": "^4.1.5"
6867
},
6968
"lint-staged": {

rollup.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const external = [
3232
'zustand',
3333
'zustand/context',
3434
'zustand/middleware',
35-
'group-items',
3635
'react',
3736
'react/jsx-runtime',
3837
'react-dom',

src/components/DataTypes/Object.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Box } from '@mui/material'
2-
import { group } from 'group-items'
32
import React, { useMemo, useState } from 'react'
43

54
import { useTextColor } from '../../hooks/useColor'
65
import { useIsCycleReference } from '../../hooks/useIsCycleReference'
76
import { useJsonViewerStore } from '../../stores/JsonViewerStore'
87
import type { DataItemProps } from '../../type'
9-
import { getValueSize } from '../../utils'
8+
import { getValueSize, segmentArray } from '../../utils'
109
import { DataKeyPair } from '../DataKeyPair'
1110
import { CircularArrowsIcon } from '../Icons'
1211
import { DataBox } from '../mui/DataBox'
@@ -176,9 +175,7 @@ export const ObjectType: React.FC<DataItemProps<object>> = (props) => {
176175
return elements
177176
}
178177

179-
const elements: unknown[][] = group<unknown>(value)
180-
.by((_, index) => Math.floor(index / groupArraysAfterLength))
181-
.asArrays()
178+
const elements: unknown[][] = segmentArray(value, groupArraysAfterLength)
182179

183180
return elements.map((list, index) => {
184181
const path = [...props.path]

src/utils/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,13 @@ export function getValueSize (value: any): number {
140140
}
141141
return 1
142142
}
143+
144+
export function segmentArray<T> (arr: T[], size: number): T[][] {
145+
const result: T[][] = []
146+
let index = 0
147+
while (index < arr.length) {
148+
result.push(arr.slice(index, index + size))
149+
index += size
150+
}
151+
return result
152+
}

tests/util.test.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { describe, expect, test } from 'vitest'
44

55
import type { DataItemProps } from '../src'
66
import { applyValue, createDataType, isCycleReference } from '../src'
7+
import { segmentArray } from '../src/utils'
78

89
describe('function applyValue', () => {
910
const patches: any[] = [{}, undefined, 1, '2', 3n, 0.4]
@@ -204,3 +205,40 @@ describe('function createDataType', () => {
204205
expect(dataType.PostComponent).toBeTypeOf('function')
205206
})
206207
})
208+
209+
describe('function segmentArray', () => {
210+
test('case 1', () => {
211+
const array = [1, 2, 3, 4, 5]
212+
const result = segmentArray(array, 2)
213+
expect(result).to.deep.eq([
214+
[1, 2],
215+
[3, 4],
216+
[5]
217+
])
218+
})
219+
220+
test('case 2', () => {
221+
const array = [1, 2, 3, 4, 5]
222+
const result = segmentArray(array, 3)
223+
expect(result).to.deep.eq([
224+
[1, 2, 3],
225+
[4, 5]
226+
])
227+
})
228+
229+
test('case 3', () => {
230+
const array = [1, 2, 3, 4, 5]
231+
const result = segmentArray(array, 5)
232+
expect(result).to.deep.eq([
233+
[1, 2, 3, 4, 5]
234+
])
235+
})
236+
237+
test('case 4', () => {
238+
const array = [1, 2, 3, 4, 5]
239+
const result = segmentArray(array, 6)
240+
expect(result).to.deep.eq([
241+
[1, 2, 3, 4, 5]
242+
])
243+
})
244+
})

yarn.lock

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,6 @@ __metadata:
18321832
eslint-plugin-simple-import-sort: ^9.0.0
18331833
eslint-plugin-unused-imports: ^2.0.0
18341834
expect-type: ^0.15.0
1835-
group-items: ^2.2.0
18361835
husky: ^8.0.3
18371836
jsdom: ^21.0.0
18381837
lint-staged: ^13.1.0
@@ -3420,15 +3419,6 @@ __metadata:
34203419
languageName: node
34213420
linkType: hard
34223421

3423-
"deep-eql@npm:^4.0.0":
3424-
version: 4.1.1
3425-
resolution: "deep-eql@npm:4.1.1"
3426-
dependencies:
3427-
type-detect: ^4.0.0
3428-
checksum: e14ec4065a38d89e48dee9c79c43c45c48bb0932e2be01898874517892ea15e20619d9c570019d6086ed74ea5557cc03ba4fab11878d20922d548221979b8d94
3429-
languageName: node
3430-
linkType: hard
3431-
34323422
"deep-eql@npm:^4.1.2":
34333423
version: 4.1.3
34343424
resolution: "deep-eql@npm:4.1.3"
@@ -5012,15 +5002,6 @@ __metadata:
50125002
languageName: node
50135003
linkType: hard
50145004

5015-
"group-items@npm:^2.2.0":
5016-
version: 2.2.0
5017-
resolution: "group-items@npm:2.2.0"
5018-
dependencies:
5019-
deep-eql: ^4.0.0
5020-
checksum: 920d4b231712205cf169631053414b3e870c481f7042d14d79f601f21d29dc720686f8a3f1e65bc019ffaf9dd9c4e83f4a5520cdcb009eb29bb980052874d8db
5021-
languageName: node
5022-
linkType: hard
5023-
50245005
"hard-rejection@npm:^2.1.0":
50255006
version: 2.1.0
50265007
resolution: "hard-rejection@npm:2.1.0"

0 commit comments

Comments
 (0)