Skip to content

Commit

Permalink
feat: add isChineseIDCardNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 2, 2018
1 parent e706334 commit 9ea0525
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"url": "https://github.com/fjc0k"
},
"scripts": {
"testOnly": "jest",
"test": "jest --coverage",
"build": "bili",
"release": "standard-version -a",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as Disposer } from './Disposer'
export { default as base64Decode } from './base64Decode'
export { default as base64Encode } from './base64Encode'
export { default as base64UrlDecode } from './base64UrlDecode'
Expand All @@ -7,7 +8,6 @@ export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as cssTransform } from './cssTransform'
export { default as currencyFormat } from './currencyFormat'
export { default as Disposer } from './Disposer'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as getType } from './getType'
Expand All @@ -17,6 +17,7 @@ export { default as inWechatMiniProgram } from './inWechatMiniProgram'
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 isDate } from './isDate'
export { default as isEqualArray } from './isEqualArray'
export { default as isFinite } from './isFinite'
Expand Down
43 changes: 43 additions & 0 deletions src/isChineseIDCardNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const isValidDate = (year: number, month: number, day: number): boolean => {
const date = new Date(year, month - 1, day)
return (
date.getFullYear() === year
&& date.getMonth() + 1 === month
&& date.getDate() === day
)
}

/**
* 检测 `value` 是否是中国大陆身份证号码。
*
* @param value 要检测的值
* @returns 是(true)或否(false)
* @see https://my.oschina.net/labrusca/blog/306116
*/
export default function isChineseIDCardNumber(value: string): boolean {
const len = value.length

// 长度错误
if (len !== 15 && len !== 18) {
return false
}

// 15 位
if (len === 15) {
return isValidDate(+`19${value.substr(6, 2)}`, +value.substr(8, 2), +value.substr(10, 2))
}

// 18 位
if (!isValidDate(+value.substr(6, 4), +value.substr(10, 2), +value.substr(12, 2))) {
return false
}

// 校验码
const weightMap = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
const codeMap = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
return codeMap[
value.split('').slice(0, 17).reduce((s, num, index) => {
return s += +num * weightMap[index]
}, 0) % 11
] === value[17].toUpperCase()
}
26 changes: 26 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,3 +943,29 @@ describe('toPath', () => {
expect(vtils.toPath('dee[3.2][hello.333]')).toEqual(['dee', '3.2', 'hello.333'])
})
})

describe('isChineseIDCardNumber', () => {
test('是', () => {
[
'110101881101231',
'110101198811014398',
'11010119881101331X',
'469001199208187005',
'46900119920818180x'
].forEach(item => {
expect(vtils.isChineseIDCardNumber(item)).toBeTruthy()
})
})
test('不是', () => {
[
'110101881301231',
'110101198811214398',
'11010119881101331a',
'469001399208187005',
'46900119925818180x',
'530627199508918277'
].forEach(item => {
expect(vtils.isChineseIDCardNumber(item)).toBeFalsy()
})
})
})

0 comments on commit 9ea0525

Please sign in to comment.