'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var getAgeByBirth = _interopDefault(require('./getAgeByBirth'));
var consts = require('./consts');

/**
 * 根据身份证号获取年龄
 * @param {string} cardNb 身份证号码
 *
 * getAgeByIdCard('350424870506202');    // => 34
 * getAgeByIdCard('36232119980910337x'); // => 23
 * getAgeByIdCard('440102198001021230'); // => 41
 */
function getAgeByIdCard(cardNb) {
  cardNb = String(cardNb);

  if (!cardNb || !consts.RE_ID_CARD.test(cardNb)) {
    return -1;
  }

  var birthdayStr = '';

  if (cardNb.length === 15) {
    birthdayStr = "19".concat(cardNb.substr(6, 2), "/").concat(cardNb.substr(8, 2), "/").concat(cardNb.substr(10, 2));
  } else if (cardNb.length === 18) {
    birthdayStr = "".concat(cardNb.substr(6, 4), "/").concat(cardNb.substr(10, 2), "/").concat(cardNb.substr(12, 2));
  }

  return getAgeByBirth(birthdayStr);
}

module.exports = getAgeByIdCard;
