Skip to content

useChangeCase

Category
Export Size
385 B
Package
@vueuse/integrations
Last Changed
15 minutes ago

change-case 的响应式封装。

替代 useCamelCaseusePascalCaseuseSnakeCaseuseSentenceCaseuseCapitalize 等。

Demo

helloWorld
Available in the @vueuse/integrations add-on.

安装

bash
npm i change-case@^5

用法

ts
import { useChangeCase } from '@vueuse/integrations/useChangeCase'

// `changeCase` 将是一个 computed
const changeCase = useChangeCase('hello world', 'camelCase')
changeCase.value // helloWorld
changeCase.value = 'vue use'
changeCase.value // vueUse
// 支持的方法
// export {
//   camelCase,
//   capitalCase,
//   constantCase,
//   dotCase,
//   headerCase,
//   noCase,
//   paramCase,
//   pascalCase,
//   pathCase,
//   sentenceCase,
//   snakeCase,
// } from 'change-case'

或者将一个 ref 传递给它,返回的 computed 将随着源 ref 的变化而变化。

可以将其传递给 options 进行定制化

ts
import { useChangeCase } from '@vueuse/integrations/useChangeCase'
import { ref } from 'vue'

const input = ref('helloWorld')
const changeCase = useChangeCase(input, 'camelCase', {
  delimiter: '-',
})
changeCase.value // hello-World
ref.value = 'vue use'
changeCase.value // vue-Use

类型声明

typescript
type EndsWithCase<T> = T extends `${infer _}Case` ? T : never
type FilterKeys<T> = {
  [K in keyof T as K extends string ? K : never]: EndsWithCase<K>
}
type ChangeCaseKeys = FilterKeys<typeof changeCase>
export type ChangeCaseType = ChangeCaseKeys[keyof ChangeCaseKeys]
export declare function useChangeCase(
  input: MaybeRef<string>,
  type: MaybeRefOrGetter<ChangeCaseType>,
  options?: MaybeRefOrGetter<Options> | undefined,
): WritableComputedRef<string>
export declare function useChangeCase(
  input: MaybeRefOrGetter<string>,
  type: MaybeRefOrGetter<ChangeCaseType>,
  options?: MaybeRefOrGetter<Options> | undefined,
): ComputedRef<string>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
David Vallejo
wheat
Doctorwu
Toni Engelhardt
sun0day
Curt Grimes
马灿

变更日志

v11.0.0-beta.3 on 8/14/2024
5c598 - fix: use the exported key for filter (#4130)
v11.0.0-beta.1 on 6/12/2024
df343 - feat!: migrate to change-case@v5, closes #3457 (#3460)
v10.0.0-beta.5 on 4/13/2023
cb644 - refactor!: remove isFunction and isString utils
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue

Released under the MIT License.