-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from XiaoXinYo/main
新增函数:对身份证进行解析
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* 对身份证解析 | ||
* @author XiaoXinYo | ||
* @category 字符串 | ||
* @param {String} idCard 身份证 | ||
* @returns {Object} 返回解析对象,areaCode(地区码),birthDate(出生日期),gender(性别),isValid(是否有效) | ||
*/ | ||
|
||
const checkIDCardValidity = (idCard) => { | ||
const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; | ||
const parityBit = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; | ||
const sum = idCard.slice(0, 17).split('').reduce((acc, num, index) => acc + (num * factor[index]), 0); | ||
const mod = sum % 11; | ||
return parityBit[mod] === idCard.slice(17).toUpperCase(); | ||
}; | ||
|
||
export const yd_string_idCardAnalysis = (idCard) => { | ||
const areaCode = idCard.slice(0, 6); | ||
const birthDate = idCard.slice(6, 14); | ||
const sequenceCode = idCard.slice(14, 17); | ||
|
||
const year = birthDate.slice(0, 4); | ||
const month = birthDate.slice(4, 6); | ||
const day = birthDate.slice(6, 8); | ||
const birthDateFormatted = `${year}-${month}-${day}`; | ||
|
||
const gender = parseInt(sequenceCode) % 2 === 0 ? 'Female' : 'Male'; | ||
|
||
const isValid = checkIDCardValidity(idCard); | ||
|
||
return { | ||
areaCode, | ||
birthDate: birthDateFormatted, | ||
gender, | ||
isValid | ||
}; | ||
}; |