Skip to content

Commit

Permalink
feat: add isHan, isChineseName
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 22, 2018
1 parent 5fd36ae commit 3aafede
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export { default as inWechatWebview } from './inWechatWebview'
export { default as isArray } from './isArray'
export { default as isBoolean } from './isBoolean'
export { default as isChineseIDCardNumber } from './isChineseIDCardNumber'
export { default as isChineseName } from './isChineseName'
export { default as isChinesePhoneNumber } from './isChinesePhoneNumber'
export { default as isDate } from './isDate'
export { default as isEmail } from './isEmail'
export { default as isEmpty } from './isEmpty'
export { default as isEqualArray } from './isEqualArray'
export { default as isFinite } from './isFinite'
export { default as isFunction } from './isFunction'
export { default as isHan } from './isHan'
export { default as isInteger } from './isInteger'
export { default as isNaN } from './isNaN'
export { default as isNil } from './isNil'
Expand Down
18 changes: 18 additions & 0 deletions src/isChineseName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import isHan from './isHan'

/**
* 检测 `value` 是否是中国人姓名,支持少数名族姓名中间的 `·` 号。
*
* @param value 要检测的值
* @returns 是(true)或否(false)
*/
export default function isChineseName(value: string): boolean {
return (
value
&& value.length > 1
&& value.length < 20
&& value[0] !== '\u00B7'
&& value.indexOf('\u00B7\u00B7') === -1
&& isHan(value.replace(/\u00B7/g, ''))
)
}
12 changes: 12 additions & 0 deletions src/isHan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://mothereff.in/regexpu#input=const+regex+%3D+%2F%5E%5Cp%7BScript%3DHan%7D%2B%24%2Fu%3B&unicodePropertyEscape=1
const hanRe = /* /^\p{Script=Han}+$/u */ /^(?:[\u2E80-\u2E99\u2E9B-\u2EF3\u2F00-\u2FD5\u3005\u3007\u3021-\u3029\u3038-\u303B\u3400-\u4DB5\u4E00-\u9FEF\uF900-\uFA6D\uFA70-\uFAD9]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0]|\uD87E[\uDC00-\uDE1D])+$/

/**
* 检查 `value` 是否是汉字。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
export default function isHan(value: string): boolean {
return hanRe.test(value)
}
33 changes: 33 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1353,3 +1353,36 @@ describe('EventBus', () => {
expect(bus.emit('add', 1, 2)).toEqual([])
})
})

describe('isHan', () => {
test('是', () => {
expect(vtils.isHan('我')).toBeTruthy()
expect(vtils.isHan('我们')).toBeTruthy()
expect(vtils.isHan('我的家')).toBeTruthy()
expect(vtils.isHan('畫羣飃姉')).toBeTruthy()
})
test('否', () => {
expect(vtils.isHan('我1')).toBeFalsy()
expect(vtils.isHan('我,们')).toBeFalsy()
expect(vtils.isHan('我,的家')).toBeFalsy()
expect(vtils.isHan('畫羣飃#姉')).toBeFalsy()
})
})

describe('isChineseName', () => {
test('是', () => {
expect(vtils.isChineseName('我们')).toBeTruthy()
expect(vtils.isChineseName('我的家')).toBeTruthy()
expect(vtils.isChineseName('畫羣飃姉')).toBeTruthy()
expect(vtils.isChineseName('阿·不多')).toBeTruthy()
})
test('否', () => {
expect(vtils.isChineseName('我')).toBeFalsy()
expect(vtils.isChineseName('我1')).toBeFalsy()
expect(vtils.isChineseName('我,们')).toBeFalsy()
expect(vtils.isChineseName('我,的家')).toBeFalsy()
expect(vtils.isChineseName('畫羣飃#姉')).toBeFalsy()
expect(vtils.isChineseName('阿··不多')).toBeFalsy()
expect(vtils.isChineseName('·不多')).toBeFalsy()
})
})

0 comments on commit 3aafede

Please sign in to comment.