idcard-util

1.0.2 • Public • Published

idcard-util

Build Status Coverage Status

中国公民身份证工具类

Installation

npm i idcard-util

Usage

var IdCard = require('idcard');
var cardInfo = IdCard('42110219910512018X');
 
console.log(cardInfo);

If this is a valid card, it will show the information about this card.

IdCard {
  _card: '42110219910512018X',
  _isValid: { result: true, reason: '' },
  _areaCode: '421102',
  _yearCode: '1991',
  _monthCode: '05',
  _dayCode: '12',
  _sequenceCode: '018',
  _parityCode: 'X' }

Otherwise the error message will be displayed

IdCard {
  _card: '421102199155120188',
  _isValid: { result: false, reason: '生日校验失败' } }

Methods

getParityCode() {Number|String}
Calculate the parity code according to the first 17 digits

console.log(cardInfo.getParityCode());
 
> X

getSex() {Object}
Return the sex of the card owner

console.log(cardInfo.getSex());
 
> { sex: 0, desc: 'female', desc_cn: '' }

getBirthDay(format) {Date}
Return the birthday of the card owner

console.log(cardInfo.getBirthDay());
 
> '1991-05-11T16:00:00.000Z'
 
console.log(cardInfo.getBirthDay('yyyy年MM月dd日'));
 
> '1991年05月12日'

getAge() {Number}
Return the age of the card owner

console.log(cardInfo.getAge());
 
> 26

genRandom() {String}
Generate a random idcard number

console.log(cardInfo.genRandom());
 
> 440300197505035517

repair() {String}

  1. Update the card number form first generation to the second generation.
  2. Fill the 18th digit if it is missing.
  3. Fix the 18th digit if it is wrong.
console.log(cardInfo.repair());
 
> {
>   result: 'success',
>   reason: '修复成功',
>   value: '42110219910512018X'
> }

Readme

Keywords

Package Sidebar

Install

npm i idcard-util

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

19.8 kB

Total Files

11

Last publish

Collaborators

  • ares_cn