Skip to content

Commit 4434e59

Browse files
committed
refactor: create composable from record checks
1 parent d3fed8b commit 4434e59

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

src/Input.vue

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import { computed, h, onMounted, ref, useAttrs, watch } from 'vue'
33
import DOMPurify from 'isomorphic-dompurify'
4-
import { useChildWithCursor, useCursorPosition } from './composables'
4+
import { useChildWithCursor, useCursorPosition, useRecord } from './composables'
55
66
export interface Props {
77
text: string
@@ -21,26 +21,19 @@ const emit = defineEmits<{
2121
}>()
2222
2323
const attrs = useAttrs()
24+
const record = useRecord()
2425
const input = ref<HTMLDivElement | null>(null)
2526
const TEXT = computed(() => DOMPurify.sanitize(props.text))
2627
27-
function isStringRecord(value: typeof props.special): value is Record<string, string | string[]> {
28-
return Object.values(value).every(v => typeof v === 'string' || Array.isArray(v))
29-
}
30-
31-
function isRegexRecord(value: typeof props.special): value is Record<string, RegExp | RegExp[]> {
32-
return Object.values(value).every(v => v instanceof RegExp || Array.isArray(v))
33-
}
34-
3528
function styleSpecialValues() {
3629
let regex: RegExp | undefined
3730
38-
if (isStringRecord(props.special)) {
31+
if (record.isString(props.special)) {
3932
const keys = Object.values(props.special).flat().sort((a, b) => b.length - a.length)
4033
const escapedKeys = keys.map(key => key.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'))
4134
regex = new RegExp(escapedKeys.join('|'), 'g')
4235
}
43-
else if (isRegexRecord(props.special)) {
36+
else if (record.isRegExp(props.special)) {
4437
const regexes = Object.values(props.special).flat().map(v => new RegExp(v.source))
4538
regex = new RegExp(regexes.map(r => r.source).join('|'), 'g')
4639
}

src/composables/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { useCursorPosition } from './useCursorPosition'
22
import { useChildWithCursor } from './useChildWithCursor'
3+
import { useRecord } from './useRecord'
34

45
export {
56
useCursorPosition,
67
useChildWithCursor,
8+
useRecord,
79
}

src/composables/useRecord.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { Props } from '../Input.vue'
2+
3+
export function useRecord() {
4+
function isString(value: Props['special']): value is Record<string, string | string[]> {
5+
return Object.values(value).every(v => typeof v === 'string' || Array.isArray(v))
6+
}
7+
8+
function isRegExp(value: Props['special']): value is Record<string, RegExp | RegExp[]> {
9+
return Object.values(value).every(v => v instanceof RegExp || Array.isArray(v))
10+
}
11+
12+
return {
13+
isString,
14+
isRegExp,
15+
}
16+
}

0 commit comments

Comments
 (0)