Skip to content

Commit

Permalink
feat(utils): 新增 removeBlankChars
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 2, 2022
1 parent fc0569d commit fbf58db
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export * from './placeKitten'
export * from './pMap'
export * from './prepareData'
export * from './readFile'
export * from './removeBlankChars'
export * from './RichUrl'
export * from './rot13'
export * from './roundTo'
Expand Down
23 changes: 23 additions & 0 deletions src/utils/removeBlankChars.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { removeBlankChars } from './removeBlankChars'

describe('removeBlankChars', () => {
test('半角空格', () => {
expect(removeBlankChars('o k')).toBe('ok')
})

test('全角空格', () => {
expect(removeBlankChars('o k')).toBe('ok')
})

test('半角空格+全角空格', () => {
expect(removeBlankChars(' o  k ')).toBe('ok')
})

test('换行', () => {
expect(removeBlankChars('o\r\nk')).toBe('ok')
})

test('特殊', () => {
expect(removeBlankChars('o\u180E\u200Bk')).toBe('ok')
})
})
13 changes: 13 additions & 0 deletions src/utils/removeBlankChars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// https://github.com/frandiox/normalize-unicode-text/blob/master/src/index.ts
const re =
/(\s+|[\u180E\u200B-\u200D\u2060\uFEFF]+|[ \u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]+|[\u2420\u2422\u2423]+)/gs

/**
* 从字符串中移除空白字符。
*
* @param value 要操作的字符串
* @returns 返回移除空白字符后的字符串
*/
export function removeBlankChars(value: string): string {
return value.replace(re, '')
}

0 comments on commit fbf58db

Please sign in to comment.