1.0.2 • Published 6 years ago

idcard-util v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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'
> }
1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago