Skip to content

Commit

Permalink
feat: reactiveStr
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 5, 2021
1 parent 9efe347 commit 20e9ba3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { computed, unref } from 'vue-demi'
import { reactifyString } from '../utils'

export * from './generated'

/*@__PURE__*/
export const toString = reactifyString(Object.prototype.toString)

export function reactiveStr(strings: TemplateStringsArray, ...args: any[]) {
return computed(() => String.raw(strings, ...args.map(unref)))
}

/*@__PURE__*/
export const rs = reactiveStr
5 changes: 0 additions & 5 deletions test/index.test.ts

This file was deleted.

17 changes: 17 additions & 0 deletions test/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ref } from 'vue'
import { multiply } from '../src/math'
import { rs } from '../src/string'

describe('reactiveStr', () => {
it('exported', () => {
const a = ref(2)
const b = ref(3)
const c = rs`${a} x ${b} = ${multiply(a, b)}`

expect(c.value).toBe('2 x 3 = 6')

a.value = 8
b.value = 5
expect(c.value).toBe('8 x 5 = 40')
})
})

0 comments on commit 20e9ba3

Please sign in to comment.