Skip to content

Commit

Permalink
feat(regexp): 新增 blankCharsRegExpBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 27, 2023
1 parent 235c385 commit db05dbf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/regexp/blankCharsRegExpBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { blankCharsRegExpBuilder } from './blankCharsRegExpBuilder'

describe('blankCharsRegExpBuilder', () => {
test('ok', () => {
expect(blankCharsRegExpBuilder.build().test(' ')).toBeTrue()
expect(blankCharsRegExpBuilder.build().test('我')).toBeFalse()
})
})
10 changes: 10 additions & 0 deletions src/regexp/blankCharsRegExpBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RegExpBuilder } from './RegExpBuilder'

// https://github.com/frandiox/normalize-unicode-text/blob/master/src/index.ts
// 新增 200E, 200F
const baseRegExp =
/[\s\u180E\u200B-\u200F\u2060\uFEFF \u00A0\u1680\u2000-\u200A\u202F\u205F\u3000\u2420\u2422\u2423]/

export const blankCharsRegExpBuilder = new RegExpBuilder({
baseRegExp: baseRegExp,
})
1 change: 1 addition & 0 deletions src/regexp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// @index(['./**/*.ts', '!./**/*.test.*', '!**/__*'], f => `export * from '${f.path}'`)
export * from './RegExpBuilder'
export * from './blankCharsRegExpBuilder'
export * from './emailRegExpBuilder'
export * from './emojiRegExpBuilder'
// @endindex
9 changes: 4 additions & 5 deletions src/utils/removeBlankChars.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// https://github.com/frandiox/normalize-unicode-text/blob/master/src/index.ts
// 新增 200E, 200F
const re =
/(\s+|[\u180E\u200B-\u200F\u2060\uFEFF]+|[ \u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]+|[\u2420\u2422\u2423]+)/gs
import { blankCharsRegExpBuilder } from '../regexp'

const regExp = blankCharsRegExpBuilder.build({ global: true })

/**
* 从字符串中移除空白字符。
Expand All @@ -10,5 +9,5 @@ const re =
* @returns 返回移除空白字符后的字符串
*/
export function removeBlankChars(value: string): string {
return value.replace(re, '')
return value.replace(regExp, '')
}

0 comments on commit db05dbf

Please sign in to comment.