Skip to content

Commit

Permalink
feat: 字段命名转换函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Ares-Chang committed Sep 5, 2024
1 parent 5a6f239 commit 2791f35
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* 将字符串中的 \ 转换为 /
*
* @param str
*/
export function slash(str: string): string {
Expand All @@ -8,6 +9,7 @@ export function slash(str: string): string {

/**
* 确保字符串以 prefix 开头
*
* @param prefix
* @param str
* @example ensurePrefix('http://', 'www.baidu.com') // => 'http://www.baidu.com'
Expand All @@ -20,6 +22,7 @@ export function ensurePrefix(prefix: string, str: string): string {

/**
* 确保字符串以 suffix 结尾
*
* @param suffix
* @param str
* @example ensureSuffix('.html', 'file') // => 'file.html'
Expand All @@ -32,6 +35,7 @@ export function ensureSuffix(suffix: string, str: string): string {

/**
* 去除字符串两边空格
*
* @param str
*/
export function trim(str: string): string {
Expand All @@ -40,6 +44,7 @@ export function trim(str: string): string {

/**
* 去除字符串左边空格
*
* @param str
*/
export function trimLeft(str: string): string {
Expand All @@ -48,6 +53,7 @@ export function trimLeft(str: string): string {

/**
* 去除字符串右边空格
*
* @param str
*/
export function trimRight(str: string): string {
Expand All @@ -56,8 +62,100 @@ export function trimRight(str: string): string {

/**
* 去除字符串中所有空格
*
* @param str
*/
export function trimAll(str: string): string {
return str.replace(/\s+/g, '')
}

/**
* 全部小写(lowerCase)
*
* @param str
* @example lowerCase('UPPERCASE') // => 'uppercase'
*/
export function lowerCase(str: string): string {
return str.toLowerCase()
}

/**
* 全部大写(upperCase)
*
* @param str
* @example upperCase('upperCase') // => 'UPPERCASE'
*/
export function upperCase(str: string): string {
return str.toUpperCase()
}

/**
* 首字母大写(capitalize)
*
* @param str
* @example capitalize('capitalize') // => 'Capitalize'
*/
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()
}

/**
* 短横线(kebabCase)
*
* @param str
* @example kebabCase('kebabCase') // => 'kebab-case'
*/
export function kebabCase(str: string): string {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/[\s_]+/g, '-').toLowerCase()
}

/**
* 标题大小写(titleCase)
*
* @param str
* @example titleCase('titleCase') // => 'Title Case'
*/
export function titleCase(str: string): string {
return kebabCase(str).split('-').map(capitalize).join(' ')
}

/**
* 下划线(snakeCase)
*
* @param str
* @example snakeCase('snakeCase') // => 'snake_case'
*/
export function snakeCase(str: string): string {
return kebabCase(str).replace(/-/g, '_')
}

/**
* 小驼峰(lowerCamelCase)
*
* @param str
* @example lowerCamelCase('LOWER_CAMEL_CASE') // => 'lowerCamelCase'
*/
export function lowerCamelCase(str: string): string {
return kebabCase(str).replace(/-./g, x => x[1].toUpperCase())
}

/**
* 大驼峰(upperCamelCase)
*
* @param str
* @example upperCamelCase('upper_camel_case') // => 'UpperCamelCase'
*/
export function upperCamelCase(str: string): string {
const val = lowerCamelCase(str)
return val.charAt(0).toUpperCase() + val.slice(1)
}

/**
* 转换为驼峰命名, 默认小驼峰
*
* @param str
* @example camelCase('CAMEL_CASE') // => 'camelCase'
*/
export function camelCase(str: 'lowerCamelCase' | 'upperCamelCase' = 'lowerCamelCase'): string {
return str === 'lowerCamelCase' ? lowerCamelCase(str) : upperCamelCase(str)
}
72 changes: 72 additions & 0 deletions test/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { describe, expect, it } from 'vitest'
import {
camelCase,
capitalize,
ensurePrefix,
ensureSuffix,
kebabCase,
lowerCamelCase,
lowerCase,
slash,
snakeCase,
titleCase,
trim,
trimAll,
trimLeft,
trimRight,
upperCamelCase,
upperCase,
} from '../src/string'

describe('string', () => {
Expand Down Expand Up @@ -51,4 +60,67 @@ describe('string', () => {
expect(trimRight(' hello ')).toBe(' hello')
})
})

describe('字符转换', () => {
it('全部小写(lowerCase)', () => {
expect(lowerCase('HELLO')).toBe('hello')
expect(lowerCase('HELLO-b')).toBe('hello-b')
expect(lowerCase('Hello c')).toBe('hello c')
})

it('全部大写(upperCase)', () => {
expect(upperCase('hello')).toBe('HELLO')
expect(upperCase('hello-b')).toBe('HELLO-B')
expect(upperCase('Hello c')).toBe('HELLO C')
})

it('首字母大写(capitalize)', () => {
expect(capitalize('hello')).toBe('Hello')
expect(capitalize('hello-b')).toBe('Hello-b')
expect(capitalize('Hello c')).toBe('Hello c')
})

it('标题大小写(titleCase)', () => {
expect(titleCase('hello')).toBe('Hello')
expect(titleCase('hello-b')).toBe('Hello B')
expect(titleCase('Hello c')).toBe('Hello C')
expect(titleCase('HelloD')).toBe('Hello D')
})

it('短横线(kebabCase)', () => {
expect(kebabCase('HELLO')).toBe('hello')
expect(kebabCase('hello-b')).toBe('hello-b')
expect(kebabCase('Hello c')).toBe('hello-c')
expect(kebabCase('HelloD')).toBe('hello-d')
expect(kebabCase('Hello_E')).toBe('hello-e')
})

it('下划线(snakeCase)', () => {
expect(snakeCase('HELLO')).toBe('hello')
expect(snakeCase('hello-b')).toBe('hello_b')
expect(snakeCase('hello C')).toBe('hello_c')
expect(snakeCase('HelloD')).toBe('hello_d')
})

it('小驼峰(lowerCamelCase)', () => {
expect(lowerCamelCase('HELLO')).toBe('hello')
expect(lowerCamelCase('hello-b')).toBe('helloB')
expect(lowerCamelCase('hello C')).toBe('helloC')
expect(lowerCamelCase('HelloD')).toBe('helloD')
expect(lowerCamelCase('Hello_e')).toBe('helloE')
})

it('大驼峰(upperCamelCase)', () => {
expect(upperCamelCase('HELLO')).toBe('Hello')
expect(upperCamelCase('hello-b')).toBe('HelloB')
expect(upperCamelCase('hello C')).toBe('HelloC')
expect(upperCamelCase('HelloD')).toBe('HelloD')
expect(upperCamelCase('Hello_e')).toBe('HelloE')
})

it('驼峰(camelCase)', () => {
expect(camelCase('lowerCamelCase')).toBe('lowerCamelCase')
expect(camelCase('upperCamelCase')).toBe('UpperCamelCase')
})
})
})

0 comments on commit 2791f35

Please sign in to comment.