nonNumericOnlyHash.js 618 B

1234567891011121314151617181920212223
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const A_CODE = "a".charCodeAt(0);
  7. /**
  8. * Returns hash that has at least one non numeric char.
  9. * @param {string} hash hash
  10. * @param {number} hashLength hash length
  11. * @returns {string} returns hash that has at least one non numeric char
  12. */
  13. module.exports = (hash, hashLength) => {
  14. if (hashLength < 1) return "";
  15. const slice = hash.slice(0, hashLength);
  16. if (/[^\d]/.test(slice)) return slice;
  17. return `${String.fromCharCode(
  18. A_CODE + (Number.parseInt(hash[0], 10) % 6)
  19. )}${slice.slice(1)}`;
  20. };