nonNumericOnlyHash.js 556 B

12345678910111213141516171819202122
  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. * @param {string} hash hash
  9. * @param {number} hashLength hash length
  10. * @returns {string} returns hash that has at least one non numeric char
  11. */
  12. module.exports = (hash, hashLength) => {
  13. if (hashLength < 1) return "";
  14. const slice = hash.slice(0, hashLength);
  15. if (slice.match(/[^\d]/)) return slice;
  16. return `${String.fromCharCode(
  17. A_CODE + (parseInt(hash[0], 10) % 6)
  18. )}${slice.slice(1)}`;
  19. };