{"size": 2160, "ext": "js", "max_stars_repo_path": "lib/number.js", "max_stars_repo_name": "justinsisley/cardy", "max_stars_repo_stars_event_min_datetime": "2017-08-19T07:12:27.000Z", "max_stars_repo_stars_event_max_datetime": "2017-08-19T07:12:27.000Z", "max_issues_repo_path": "lib/number.js", "max_issues_repo_name": "justinsisley/cardy", "max_issues_repo_issues_event_min_datetime": "2017-08-25T02:59:20.000Z", "max_issues_repo_issues_event_max_datetime": "2018-04-16T04:02:01.000Z", "max_forks_repo_path": "lib/number.js", "max_forks_repo_name": "justinsisley/cardy", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 22.2680412371, "max_line_length": 95, "alphanum_fraction": 0.687962963}
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.validate = validate;
exports.format = format;
exports.mask = mask;

var _cardValidator = require('card-validator');

var _cardValidator2 = _interopRequireDefault(_cardValidator);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function validate(input) {
  var result = {
    type: undefined,
    niceType: undefined,
    codeName: undefined,
    codeSize: undefined,
    isPotentiallyValid: false,
    isValid: false
  };

  var cardValidatorResult = _cardValidator2.default.number(input);

  result.isPotentiallyValid = cardValidatorResult.isPotentiallyValid;

  if (cardValidatorResult.card) {
    result.type = cardValidatorResult.card.type;
    result.niceType = cardValidatorResult.card.niceType;

    result.codeName = cardValidatorResult.card.code.name;
    result.codeSize = cardValidatorResult.card.code.size;
  }

  if (!cardValidatorResult.isValid) {
    return result;
  }

  result.isValid = true;

  return result;
}

function format(input) {
  var cardValidatorResult = _cardValidator2.default.number(input);

  // Not valid, just return the original input
  if (!cardValidatorResult.isValid) {
    return input;
  }

  var gaps = cardValidatorResult.card.gaps;


  var condensed = input.replace(/\W/g, '');
  var split = condensed.split('');

  gaps.forEach(function (gap, index) {
    split.splice(gap + index, 0, ' ');
  });

  return split.join('');
}

function mask(input) {
  var cardValidatorResult = _cardValidator2.default.number(input);

  if (!cardValidatorResult.isPotentiallyValid) {
    return input.slice(0, -1);
  }

  if (!cardValidatorResult.card) {
    return input;
  }

  var gaps = cardValidatorResult.card.gaps;


  var condensed = input.replace(/\W/g, '');
  var split = condensed.split('');

  gaps.forEach(function (gap, index) {
    if (split.length > gap + index) {
      split.splice(gap + index, 0, ' ');
    }
  });

  // Input was already valid, with appropriate spacing, so return it
  if (input.trim() === split.join('')) {
    return input;
  }

  return split.join('');
}