Skip to content

Commit 083c7a5

Browse files
committed
refactor: extracted object filtering to function
1 parent 68a2eca commit 083c7a5

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/SearchInput.vue

+16-20
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,22 @@
5050
</template>
5151

5252
<script lang="ts">
53-
import { defineComponent, PropType, ref, computed, onBeforeUnmount, StyleValue } from 'vue'
53+
import { defineComponent, PropType, ref, computed, onBeforeUnmount } from 'vue'
5454
import { fieldType, FieldType } from './SearchInput.types'
5555
56+
const filterObject = (obj: { [key: string]: unknown }, properties: (string | number)[], remove = true) => {
57+
const res: { [key: string]: unknown } = {}
58+
59+
Object.keys(obj).forEach((objAttr) => {
60+
const condition = remove ? properties.indexOf(objAttr) === -1 : properties.indexOf(objAttr) >= 0
61+
if (condition) {
62+
res[objAttr] = obj[objAttr]
63+
}
64+
})
65+
66+
return res
67+
}
68+
5669
export default defineComponent({
5770
inheritAttrs: false,
5871
props: {
@@ -71,26 +84,9 @@ export default defineComponent({
7184
const hasFocus = ref(false)
7285
const inputRef = ref<null | HTMLInputElement>(null)
7386
74-
const attrsWithoutStyles = computed(() => {
75-
const toOmit = ['class', 'style']
76-
const res = {} as Record<string, unknown>
77-
78-
Object.keys(attrs).forEach((attr) => {
79-
if (toOmit.indexOf(attr) === -1) {
80-
res[attr] = attrs[attr]
81-
}
82-
})
87+
const attrsWithoutStyles = computed(() => filterObject(attrs, ['class', 'style']))
8388
84-
return res
85-
})
86-
87-
const attrsStyles = computed(() => {
88-
const style = attrs.style as StyleValue | undefined
89-
return {
90-
...(attrs.class ? { class: attrs.class } : {}),
91-
...(style ? { style } : {})
92-
}
93-
})
89+
const attrsStyles = computed(() => filterObject(attrs, ['class', 'style'], false))
9490
9591
const clear = () => {
9692
emit('update:modelValue', '')

0 commit comments

Comments
 (0)