Skip to content

Commit a4ea5f2

Browse files
paparentyyx990803
authored andcommitted
Reuse the sort by key function for sub elements in DataField component (#375)
1 parent 6398334 commit a4ea5f2

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/devtools/components/DataField.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import {
3939
UNDEFINED,
4040
INFINITY,
4141
NAN,
42-
isPlainObject
42+
isPlainObject,
43+
sortByKey
4344
} from 'src/util'
4445
4546
const rawTypeRE = /^\[object (\w+)]$/
@@ -125,11 +126,10 @@ export default {
125126
value: item
126127
}))
127128
} else if (typeof value === 'object') {
128-
value = Object.keys(value).map(key => ({
129+
value = sortByKey(Object.keys(value).map(key => ({
129130
key,
130131
value: value[key]
131-
}))
132-
value = value.slice().sort((a, b) => a.key > b.key)
132+
})))
133133
}
134134
return value
135135
},

src/devtools/views/components/ComponentInspector.vue

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import ScrollPane from 'components/ScrollPane.vue'
3434
import ActionHeader from 'components/ActionHeader.vue'
3535
import StateInspector from 'components/StateInspector.vue'
36-
import { searchDeepInObject } from 'src/util'
36+
import { searchDeepInObject, sortByKey } from 'src/util'
3737
import groupBy from 'lodash.groupby'
3838
3939
const isChrome = typeof chrome !== 'undefined' && chrome.devtools
@@ -57,7 +57,7 @@ export default {
5757
return this.target.id != null
5858
},
5959
filteredState () {
60-
return groupBy(sort(this.target.state.filter(el => {
60+
return groupBy(sortByKey(this.target.state.filter(el => {
6161
return searchDeepInObject({
6262
[el.key]: el.value
6363
}, this.filter)
@@ -77,12 +77,4 @@ export default {
7777
}
7878
}
7979
}
80-
81-
function sort (state) {
82-
return state && state.slice().sort((a, b) => {
83-
if (a.key < b.key) return -1
84-
if (a.key > b.key) return 1
85-
return 0
86-
})
87-
}
8880
</script>

src/util.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,11 @@ function searchInArray (arr, searchTerm) {
155155
}
156156
return found
157157
}
158+
159+
export function sortByKey (state) {
160+
return state && state.slice().sort((a, b) => {
161+
if (a.key < b.key) return -1
162+
if (a.key > b.key) return 1
163+
return 0
164+
})
165+
}

0 commit comments

Comments
 (0)