Skip to content

Commit

Permalink
feat: 新增函数
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Feb 2, 2024
1 parent af7ccd5 commit 6231140
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/array/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { findIndex, move } from '../index'
import { findIndex, move, shuffle } from '../index'

describe('findIndex', () => {
it('should return the index of the first element that satisfies the provided testing function', () => {
Expand Down Expand Up @@ -40,3 +40,17 @@ describe('move', () => {
expect(() => move([1, 2, 3], 5, 0)).toThrowError('Index out of bounds')
})
})

describe('shuffle', () => {
it('should return an array with the same length', () => {
const ary = [1, 2, 3, 4, 5]
const shuffled = shuffle(ary)
expect(shuffled.length).toBe(ary.length)
})

it('should return an array with the same elements', () => {
const ary = [1, 2, 3, 4, 5]
const shuffled = shuffle(ary)
expect(shuffled.sort()).toEqual(ary.sort())
})
})
14 changes: 14 additions & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@ export const move = <T = any>(ary: T[], fromIndex: number, toIndex: number): T[]
;[ary[fromIndex], ary[toIndex]] = [ary[toIndex], ary[fromIndex]]
return ary
}

/**
* 数组乱序
* @category Array
* @param ary 查找的数组
* @returns 返回乱序后的数组
* @example
* ``` typescript
* shuffle([1, 2, 3])
* ```
*/
export const shuffle = <T = any>(ary: T[]): T[] => {
return ary.sort(() => Math.random() - 0.5)
}
14 changes: 14 additions & 0 deletions src/number/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, it, expect } from 'vitest'
import { numberToChinese } from '../index'

describe('numberToChinese', () => {
it('should convert numbers to Chinese currency format', () => {
expect(numberToChinese(123456.78)).toBe('壹拾贰万叁仟肆佰伍拾陆元柒角捌分')
expect(numberToChinese(100000000)).toBe('壹亿元整')
expect(numberToChinese(0)).toBe('零元整')
})

it('should handle negative numbers', () => {
expect(numberToChinese(-123456.78)).toBe('欠壹拾贰万叁仟肆佰伍拾陆元柒角捌分')
})
})
41 changes: 41 additions & 0 deletions src/number/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 数字转化为大写金额
* @category Number
* @param {number} num
* @returns {string}
* @example
* ``` typescript
* numberToChinese(100)
* ```
*/
export const numberToChinese = (num: number): string => {
const fraction = ['角', '分']
const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
const unit = [
['元', '万', '亿'],
['', '拾', '佰', '仟']
]
const head = num < 0 ? '欠' : ''
num = Math.abs(num)
let s = ''
for (let i = 0; i < fraction.length; i++) {
s += (digit[Math.floor(num * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '')
}
s = s || '整'
num = Math.floor(num)
for (let i = 0; i < unit[0].length && num > 0; i++) {
let p = ''
for (let j = 0; j < unit[1].length && num > 0; j++) {
p = digit[num % 10] + unit[1][j] + p
num = Math.floor(num / 10)
}
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
}
return (
head +
s
.replace(/(零.)*零元/, '元')
.replace(/(零.)+/g, '零')
.replace(/^整$/, '零元整')
)
}
54 changes: 53 additions & 1 deletion src/string/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { describe, it, expect } from 'vitest'
import { trim, underlineToHump, humpToUnderline, replace } from '../index'
import {
trim,
underlineToHump,
humpToUnderline,
replace,
phoneToAsterisk,
toCDB,
toDBC
} from '../index'

describe('trim', () => {
it('should remove spaces from both ends', () => {
Expand Down Expand Up @@ -28,3 +36,47 @@ describe('replace', () => {
expect(result).toBe('bbcdefg')
})
})

describe('phoneToAsterisk', () => {
it('should replace the middle four digits with asterisks', () => {
expect(phoneToAsterisk('12345678901')).toBe('123****8901')
expect(phoneToAsterisk('98765432109')).toBe('987****2109')
})

it('should return the original string if it does not match the pattern', () => {
expect(phoneToAsterisk('12345')).toBe('12345')
expect(phoneToAsterisk('abcdefg')).toBe('abcdefg')
})
})

describe('toCDB', () => {
it('should convert full-width characters to half-width', () => {
expect(toCDB('123456')).toBe('123456')
expect(toCDB('ABCDEF')).toBe('ABCDEF')
})

it('should convert full-width space to half-width', () => {
expect(toCDB(' ')).toBe(' ')
})

it('should leave half-width characters unchanged', () => {
expect(toCDB('123456')).toBe('123456')
expect(toCDB('ABCDEF')).toBe('ABCDEF')
})
})

describe('toDBC', () => {
it('should convert half-width characters to full-width', () => {
expect(toDBC('123456')).toBe('123456')
expect(toDBC('ABCDEF')).toBe('ABCDEF')
})

it('should convert half-width space to full-width', () => {
expect(toDBC(' ')).toBe(' ')
})

it('should leave full-width characters unchanged', () => {
expect(toDBC('123456')).toBe('123456')
expect(toDBC('ABCDEF')).toBe('ABCDEF')
})
})
64 changes: 63 additions & 1 deletion src/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const underlineToHump = (name: string): string => {
}

/**
* 驼峰字符串转下划线
* 驼峰字符串转中划线
* @category String
* @param str 驼峰字符串
* @example
Expand All @@ -59,3 +59,65 @@ export const replace = (str: string, findText: string, repText: string) => {
const regExp = new RegExp(findText, 'g')
return str.replace(regExp, repText)
}

/**
* 手机号码中间四位替换成*
* @category String
* @param phone 手机号码
* @example
* ``` typescript
* phoneToAsterisk('12345678901')
* ```
* @returns 123****8901
*/
export const phoneToAsterisk = (phone: string): string => {
return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}

/**
* 全角转换为半角
* @category String
* @param s 需要转换的字符串
* @example
* ``` typescript
* toCDB('123456')
* ```
* @returns 123456
*/
export const toCDB = (s: string): string => {
const result = s.split('').map((char: string) => {
const code = char.charCodeAt(0)
if (code >= 65281 && code <= 65374) {
return String.fromCharCode(code - 65248)
}
if (code === 12288) {
return String.fromCharCode(32)
}
return char
})
return result.join('')
}

/**
* 半角转换为全角
* @category String
* @param s 需要转换的字符串
* @example
* ``` typescript
* toDBC('123456')
* ```
* @returns 123456
*/
export const toDBC = (s: string): string => {
const result = s.split('').map((char: string) => {
const code = char.charCodeAt(0)
if (code >= 33 && code <= 126) {
return String.fromCharCode(code + 65248)
}
if (code === 32) {
return String.fromCharCode(12288)
}
return char
})
return result.join('')
}

0 comments on commit 6231140

Please sign in to comment.