Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useArrayJoin): new function (#1904)
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
category: Utilities | ||
--- | ||
|
||
# useArrayJoin | ||
|
||
Reactive `Array.join` | ||
|
||
## Usage | ||
|
||
### Use with array of multiple refs | ||
|
||
```js | ||
import { useArrayJoin } from '@vueuse/core' | ||
const item1 = ref('foo') | ||
const item2 = ref(0) | ||
const item3 = ref({ prop: 'val' }) | ||
const list = [item1, item2, item3] | ||
const result = useArrayJoin(list) | ||
// result.value: foo,0,[object Object] | ||
item1.value = 'bar' | ||
// result.value: bar,0,[object Object] | ||
``` | ||
|
||
### Use with reactive array | ||
|
||
```js | ||
import { useArrayJoin } from '@vueuse/core' | ||
const list = ref(['string', 0, { prop: 'val' }, false, [1], [[2]], null, undefined, []]) | ||
const result = useArrayJoin(list) | ||
// result.value: string,0,[object Object],false,1,2,,, | ||
list.value.push(true) | ||
// result.value: string,0,[object Object],false,1,2,,,,true | ||
list.value = [null, 'string', undefined] | ||
// result.value: ,string, | ||
``` | ||
|
||
### Use with reactive separator | ||
|
||
```js | ||
import { useArrayJoin } from '@vueuse/core' | ||
const list = ref(['string', 0, { prop: 'val' }]) | ||
const separator = ref() | ||
const result = useArrayJoin(list, separator) | ||
// result.value: string,0,[object Object] | ||
separator.value = '' | ||
// result.value: string0[object Object] | ||
separator.value = '--' | ||
// result.value: string--0--[object Object] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ref } from 'vue-demi' | ||
import { useArrayJoin } from '../useArrayJoin' | ||
|
||
describe('useArrayJoin', () => { | ||
it('should be defined', () => { | ||
expect(useArrayJoin).toBeDefined() | ||
}) | ||
|
||
it('should work with array of refs', () => { | ||
const item1 = ref('foo') | ||
const item2 = ref(0) | ||
const item3 = ref({ prop: 'val' }) | ||
const list = [item1, item2, item3] | ||
const result = useArrayJoin(list) | ||
expect(result.value).toBe('foo,0,[object Object]') | ||
item1.value = 'bar' | ||
expect(result.value).toBe('bar,0,[object Object]') | ||
}) | ||
|
||
it('should work with reactive array', () => { | ||
const list = ref(['string', 0, { prop: 'val' }, false, [1], [[2]], null, undefined, []]) | ||
const result = useArrayJoin(list) | ||
expect(result.value).toBe('string,0,[object Object],false,1,2,,,') | ||
list.value.push(true) | ||
expect(result.value).toBe('string,0,[object Object],false,1,2,,,,true') | ||
list.value = [null, 'string', undefined, 0, [], [1], [[2]], { prop: 'val' }] | ||
expect(result.value).toBe(',string,,0,,1,2,[object Object]') | ||
}) | ||
|
||
it('should work with reactive separator', () => { | ||
const list = ref(['string', 0, { prop: 'val' }, [1], [[2]], null, undefined, []]) | ||
const separator = ref() | ||
const result = useArrayJoin(list, separator) | ||
expect(result.value).toBe('string,0,[object Object],1,2,,,') | ||
separator.value = '' | ||
expect(result.value).toBe('string0[object Object]12') | ||
separator.value = '-' | ||
expect(result.value).toBe('string-0-[object Object]-1-2---') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { MaybeComputedRef } from '@vueuse/shared' | ||
import { resolveUnref } from '@vueuse/shared' | ||
import type { ComputedRef } from 'vue-demi' | ||
import { computed } from 'vue-demi' | ||
|
||
export function useArrayJoin( | ||
list: MaybeComputedRef<MaybeComputedRef<any>[]>, | ||
separator?: MaybeComputedRef<string>, | ||
): ComputedRef<string> { | ||
return computed(() => resolveUnref(list).map(i => resolveUnref(i)).join(resolveUnref(separator))) | ||
} |