Comparing version 2.0.0 to 2.0.1
@@ -58,11 +58,11 @@ (function (global, factory) { | ||
if (typeof minLength !== 'number') { | ||
throw new Error("Hashids: Provided 'minLength' has to be a number (is ".concat(_typeof(minLength), ")")); | ||
throw new TypeError("Hashids: Provided 'minLength' has to be a number (is ".concat(_typeof(minLength), ")")); | ||
} | ||
if (typeof salt !== 'string') { | ||
throw new Error("Hashids: Provided 'salt' has to be a string (is ".concat(_typeof(salt), ")")); | ||
throw new TypeError("Hashids: Provided 'salt' has to be a string (is ".concat(_typeof(salt), ")")); | ||
} | ||
if (typeof alphabet !== 'string') { | ||
throw new Error("Hashids: Provided alphabet has to be a string (is ".concat(_typeof(alphabet), ")")); | ||
throw new TypeError("Hashids: Provided alphabet has to be a string (is ".concat(_typeof(alphabet), ")")); | ||
} | ||
@@ -69,0 +69,0 @@ |
@@ -1,2 +0,2 @@ | ||
!function(t,e){if("function"==typeof define&&define.amd)define("Hashids",["exports"],e);else if("undefined"!=typeof exports)e(exports);else{var n={exports:{}};e(n.exports),t.Hashids=n.exports}}(this,function(t){"use strict";function e(t){return function(t){if(Array.isArray(t))return t}(t)||r(t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function n(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||r(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function r(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}function i(t){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}Object.defineProperty(t,"__esModule",{value:!0}),t.unicodeSubstr=t.onlyChars=t.withoutChars=t.keepUniqueChars=t.default=void 0;var a=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"cfhistuCFHISTU";if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.salt=e,this.minLength=r,"number"!=typeof r)throw new Error("Hashids: Provided 'minLength' has to be a number (is ".concat(i(r),")"));if("string"!=typeof e)throw new Error("Hashids: Provided 'salt' has to be a string (is ".concat(i(e),")"));if("string"!=typeof o)throw new Error("Hashids: Provided alphabet has to be a string (is ".concat(i(o),")"));var p=l(o);if(p.length<s)throw new Error("Hashids: alphabet must contain at least ".concat(s," unique characters, provided: ").concat(p));this.alphabet=c(p,a);var d,v,y=f(a,p);this.seps=b(y,e),(0===n(this.seps).length||n(this.alphabet).length/n(this.seps).length>u)&&(d=Math.ceil(n(this.alphabet).length/u))>n(this.seps).length&&(v=d-n(this.seps).length,this.seps+=g(this.alphabet,0,v),this.alphabet=g(this.alphabet,v)),this.alphabet=b(this.alphabet,e);var m=Math.ceil(n(this.alphabet).length/h);n(this.alphabet).length<3?(this.guards=g(this.seps,0,m),this.seps=g(this.seps,m)):(this.guards=g(this.alphabet,0,m),this.alphabet=g(this.alphabet,m))}var r,a,w;return r=t,(a=[{key:"encode",value:function(t){for(var e=arguments.length,r=new Array(e>1?e-1:0),i=1;i<e;i++)r[i-1]=arguments[i];var o="";return(r=Array.isArray(t)?t:[].concat(n(null!=t?[t]:[]),n(r))).length?(r.every(p)||(r=r.map(function(t){return"bigint"==typeof t||"number"==typeof t?t:A(String(t))})),r.every(d)?this._encode(r):o):o}},{key:"decode",value:function(t){return t&&"string"==typeof t&&0!==t.length?this._decode(t,this.alphabet):[]}},{key:"encodeHex",value:function(t){switch(i(t)){case"bigint":t=t.toString(16);break;case"string":if(!/^[0-9a-fA-F]+$/.test(t))return"";break;default:throw new Error("Hashids: The provided value is neither a string, nor a BigInt (got: ".concat(i(t),")"))}var e=I(t,12,function(t){return parseInt("1".concat(t),16)});return this.encode(e)}},{key:"decodeHex",value:function(t){return this.decode(t).map(function(t){return t.toString(16).slice(1)}).join("")}},{key:"_encode",value:function(t){var e,r=this,i=this.alphabet,o=t.reduce(function(t,e,n){return t+("bigint"==typeof e?Number(e%BigInt(n+100)):e%(n+100))},0),a=e=n(i)[o%n(i).length],s=n(this.seps),u=n(this.guards);if(t.forEach(function(n,o){var u=a+r.salt+i;i=b(i,g(u,0));var h=v(n,i);if(e+=h,o+1<t.length){var l=h.codePointAt(0)+o,c="bigint"==typeof n?Number(n%BigInt(l)):n%l;e+=s[c%s.length]}}),n(e).length<this.minLength){var h=(o+n(e)[0].codePointAt(0))%u.length;if(n(e=u[h]+e).length<this.minLength){var l=(o+n(e)[2].codePointAt(0))%u.length;e+=u[l]}}for(var c=Math.floor(n(i).length/2);n(e).length<this.minLength;){i=b(i,i);var f=n(e=g(i,c)+e+g(i,0,c)).length-this.minLength;f>0&&(e=g(e,f/2,this.minLength))}return e}},{key:"_decode",value:function(t,r){var i=this,o=m(t,function(t){return i.guards.includes(t)}),a=n(o[3===o.length||2===o.length?1:0]);if(0===a.length)return[];var s=e(a),u=s[0],h=s.slice(1).join(""),l=m(h,function(t){return i.seps.includes(t)}).reduce(function(t,e){var r=t.result,o=t.lastAlphabet,a=u+i.salt+o,s=b(o,g(a,0,n(o).length));return{result:[].concat(n(r),[y(e,s)]),lastAlphabet:s}},{result:[],lastAlphabet:r}).result;return this._encode(l)!==t?[]:l}}])&&o(r.prototype,a),w&&o(r,w),t}();t.default=a;var s=16,u=3.5,h=12,l=function(t){return Array.from(new Set(t)).join("")};t.keepUniqueChars=l;var c=function(t,n){var r=e(t).slice(0),i=e(n).slice(0);return r.filter(function(t){return!i.includes(t)}).join("")};t.withoutChars=c;var f=function(t,n){var r=e(t).slice(0),i=e(n).slice(0);return r.filter(function(t){return i.includes(t)}).join("")};t.onlyChars=f;var g=function(t,n,r){return e(t).slice(0).slice(n,void 0===r?void 0:n+r).join("")};t.unicodeSubstr=g;var p=function(t){return"bigint"==typeof t||!Number.isNaN(Number(t))&&Math.floor(Number(t))===t},d=function(t){return"bigint"==typeof t||t>=0&&Number.isSafeInteger(t)};function b(t,r){var i,o=e(r).slice(0);if(!o.length)return t;for(var a=n(t),s=a.length-1,u=0,h=0;s>0;s--,u++){h+=i=o[u%=o.length].codePointAt(0);var l=(i+u+h)%s,c=[a[s],a[l]];a[l]=c[0],a[s]=c[1]}return a.join("")}var v=function(t,n){var r=e(n).slice(0),i="";if("bigint"==typeof t){var o=BigInt(r.length);do{i=r[Number(t%o)]+i,t/=o}while(t)}else do{i=r[t%r.length]+i,t=Math.floor(t/r.length)}while(t);return i},y=function(t,n){var r=e(t).slice(0),i=e(n).slice(0);return r.map(function(t){return i.indexOf(t)}).reduce(function(t,e){if("bigint"==typeof t)return t*BigInt(i.length)+BigInt(e);var n=t*i.length+e;if(Number.isSafeInteger(n))return n;if("function"==typeof BigInt)return BigInt(t)*BigInt(i.length)+BigInt(e);throw new Error("Unable to decode the provided string, due to lack of support for BigInt numbers in the current environment")},0)},m=function(t,r){return e(t).slice(0).reduce(function(t,e){return r(e)?[].concat(n(t),[""]):[].concat(n(t.slice(0,-1)),[t[t.length-1]+e])},[""])},w=/^\+?[0-9]+$/,A=function(t){return w.test(t)?parseInt(t,10):NaN},I=function(t,e,n){return Array.from({length:Math.ceil(t.length/e)},function(r,i){return n(t.slice(i*e,(i+1)*e))})}}); | ||
!function(t,e){if("function"==typeof define&&define.amd)define("Hashids",["exports"],e);else if("undefined"!=typeof exports)e(exports);else{var n={exports:{}};e(n.exports),t.Hashids=n.exports}}(this,function(t){"use strict";function e(t){return function(t){if(Array.isArray(t))return t}(t)||r(t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function n(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||r(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function r(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}function i(t){return(i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}Object.defineProperty(t,"__esModule",{value:!0}),t.unicodeSubstr=t.onlyChars=t.withoutChars=t.keepUniqueChars=t.default=void 0;var a=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"cfhistuCFHISTU";if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.salt=e,this.minLength=r,"number"!=typeof r)throw new TypeError("Hashids: Provided 'minLength' has to be a number (is ".concat(i(r),")"));if("string"!=typeof e)throw new TypeError("Hashids: Provided 'salt' has to be a string (is ".concat(i(e),")"));if("string"!=typeof o)throw new TypeError("Hashids: Provided alphabet has to be a string (is ".concat(i(o),")"));var p=l(o);if(p.length<s)throw new Error("Hashids: alphabet must contain at least ".concat(s," unique characters, provided: ").concat(p));this.alphabet=c(p,a);var d,v,y=f(a,p);this.seps=b(y,e),(0===n(this.seps).length||n(this.alphabet).length/n(this.seps).length>u)&&(d=Math.ceil(n(this.alphabet).length/u))>n(this.seps).length&&(v=d-n(this.seps).length,this.seps+=g(this.alphabet,0,v),this.alphabet=g(this.alphabet,v)),this.alphabet=b(this.alphabet,e);var m=Math.ceil(n(this.alphabet).length/h);n(this.alphabet).length<3?(this.guards=g(this.seps,0,m),this.seps=g(this.seps,m)):(this.guards=g(this.alphabet,0,m),this.alphabet=g(this.alphabet,m))}var r,a,w;return r=t,(a=[{key:"encode",value:function(t){for(var e=arguments.length,r=new Array(e>1?e-1:0),i=1;i<e;i++)r[i-1]=arguments[i];var o="";return(r=Array.isArray(t)?t:[].concat(n(null!=t?[t]:[]),n(r))).length?(r.every(p)||(r=r.map(function(t){return"bigint"==typeof t||"number"==typeof t?t:A(String(t))})),r.every(d)?this._encode(r):o):o}},{key:"decode",value:function(t){return t&&"string"==typeof t&&0!==t.length?this._decode(t,this.alphabet):[]}},{key:"encodeHex",value:function(t){switch(i(t)){case"bigint":t=t.toString(16);break;case"string":if(!/^[0-9a-fA-F]+$/.test(t))return"";break;default:throw new Error("Hashids: The provided value is neither a string, nor a BigInt (got: ".concat(i(t),")"))}var e=I(t,12,function(t){return parseInt("1".concat(t),16)});return this.encode(e)}},{key:"decodeHex",value:function(t){return this.decode(t).map(function(t){return t.toString(16).slice(1)}).join("")}},{key:"_encode",value:function(t){var e,r=this,i=this.alphabet,o=t.reduce(function(t,e,n){return t+("bigint"==typeof e?Number(e%BigInt(n+100)):e%(n+100))},0),a=e=n(i)[o%n(i).length],s=n(this.seps),u=n(this.guards);if(t.forEach(function(n,o){var u=a+r.salt+i;i=b(i,g(u,0));var h=v(n,i);if(e+=h,o+1<t.length){var l=h.codePointAt(0)+o,c="bigint"==typeof n?Number(n%BigInt(l)):n%l;e+=s[c%s.length]}}),n(e).length<this.minLength){var h=(o+n(e)[0].codePointAt(0))%u.length;if(n(e=u[h]+e).length<this.minLength){var l=(o+n(e)[2].codePointAt(0))%u.length;e+=u[l]}}for(var c=Math.floor(n(i).length/2);n(e).length<this.minLength;){i=b(i,i);var f=n(e=g(i,c)+e+g(i,0,c)).length-this.minLength;f>0&&(e=g(e,f/2,this.minLength))}return e}},{key:"_decode",value:function(t,r){var i=this,o=m(t,function(t){return i.guards.includes(t)}),a=n(o[3===o.length||2===o.length?1:0]);if(0===a.length)return[];var s=e(a),u=s[0],h=s.slice(1).join(""),l=m(h,function(t){return i.seps.includes(t)}).reduce(function(t,e){var r=t.result,o=t.lastAlphabet,a=u+i.salt+o,s=b(o,g(a,0,n(o).length));return{result:[].concat(n(r),[y(e,s)]),lastAlphabet:s}},{result:[],lastAlphabet:r}).result;return this._encode(l)!==t?[]:l}}])&&o(r.prototype,a),w&&o(r,w),t}();t.default=a;var s=16,u=3.5,h=12,l=function(t){return Array.from(new Set(t)).join("")};t.keepUniqueChars=l;var c=function(t,n){var r=e(t).slice(0),i=e(n).slice(0);return r.filter(function(t){return!i.includes(t)}).join("")};t.withoutChars=c;var f=function(t,n){var r=e(t).slice(0),i=e(n).slice(0);return r.filter(function(t){return i.includes(t)}).join("")};t.onlyChars=f;var g=function(t,n,r){return e(t).slice(0).slice(n,void 0===r?void 0:n+r).join("")};t.unicodeSubstr=g;var p=function(t){return"bigint"==typeof t||!Number.isNaN(Number(t))&&Math.floor(Number(t))===t},d=function(t){return"bigint"==typeof t||t>=0&&Number.isSafeInteger(t)};function b(t,r){var i,o=e(r).slice(0);if(!o.length)return t;for(var a=n(t),s=a.length-1,u=0,h=0;s>0;s--,u++){h+=i=o[u%=o.length].codePointAt(0);var l=(i+u+h)%s,c=[a[s],a[l]];a[l]=c[0],a[s]=c[1]}return a.join("")}var v=function(t,n){var r=e(n).slice(0),i="";if("bigint"==typeof t){var o=BigInt(r.length);do{i=r[Number(t%o)]+i,t/=o}while(t)}else do{i=r[t%r.length]+i,t=Math.floor(t/r.length)}while(t);return i},y=function(t,n){var r=e(t).slice(0),i=e(n).slice(0);return r.map(function(t){return i.indexOf(t)}).reduce(function(t,e){if("bigint"==typeof t)return t*BigInt(i.length)+BigInt(e);var n=t*i.length+e;if(Number.isSafeInteger(n))return n;if("function"==typeof BigInt)return BigInt(t)*BigInt(i.length)+BigInt(e);throw new Error("Unable to decode the provided string, due to lack of support for BigInt numbers in the current environment")},0)},m=function(t,r){return e(t).slice(0).reduce(function(t,e){return r(e)?[].concat(n(t),[""]):[].concat(n(t.slice(0,-1)),[t[t.length-1]+e])},[""])},w=/^\+?[0-9]+$/,A=function(t){return w.test(t)?parseInt(t,10):NaN},I=function(t,e,n){return Array.from({length:Math.ceil(t.length/e)},function(r,i){return n(t.slice(i*e,(i+1)*e))})}}); | ||
//# sourceMappingURL=hashids.min.js.map |
@@ -38,11 +38,11 @@ function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); } | ||
if (typeof minLength !== 'number') { | ||
throw new Error("Hashids: Provided 'minLength' has to be a number (is ".concat(_typeof(minLength), ")")); | ||
throw new TypeError("Hashids: Provided 'minLength' has to be a number (is ".concat(_typeof(minLength), ")")); | ||
} | ||
if (typeof salt !== 'string') { | ||
throw new Error("Hashids: Provided 'salt' has to be a string (is ".concat(_typeof(salt), ")")); | ||
throw new TypeError("Hashids: Provided 'salt' has to be a string (is ".concat(_typeof(salt), ")")); | ||
} | ||
if (typeof alphabet !== 'string') { | ||
throw new Error("Hashids: Provided alphabet has to be a string (is ".concat(_typeof(alphabet), ")")); | ||
throw new TypeError("Hashids: Provided alphabet has to be a string (is ".concat(_typeof(alphabet), ")")); | ||
} | ||
@@ -49,0 +49,0 @@ |
@@ -15,3 +15,3 @@ type NumberLike = number | bigint | ||
if (typeof minLength !== 'number') { | ||
throw new Error( | ||
throw new TypeError( | ||
`Hashids: Provided 'minLength' has to be a number (is ${typeof minLength})`, | ||
@@ -21,3 +21,3 @@ ) | ||
if (typeof salt !== 'string') { | ||
throw new Error( | ||
throw new TypeError( | ||
`Hashids: Provided 'salt' has to be a string (is ${typeof salt})`, | ||
@@ -27,3 +27,3 @@ ) | ||
if (typeof alphabet !== 'string') { | ||
throw new Error( | ||
throw new TypeError( | ||
`Hashids: Provided alphabet has to be a string (is ${typeof alphabet})`, | ||
@@ -30,0 +30,0 @@ ) |
@@ -17,3 +17,3 @@ { | ||
], | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"homepage": "http://hashids.org/javascript", | ||
@@ -20,0 +20,0 @@ "repository": { |
@@ -7,2 +7,3 @@ [![hashids](http://hashids.org/public/img/hashids.gif 'Hashids')](http://hashids.org/) | ||
[![NPM version][npm-version-image]][npm-url] | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/niieani/hashids.js.svg)](https://greenkeeper.io/) | ||
[![License][license-image]][license-url] | ||
@@ -140,3 +141,3 @@ [![Chat][chat-image]][chat-url] | ||
For example `hashids.encodeHex('00000000') would encode to`qExOgK7`and decode back to`'00000000'` (length information is preserved). | ||
For example `hashids.encodeHex('00000000')` would encode to `qExOgK7` and decode back to `'00000000'` (length information is preserved). | ||
@@ -193,6 +194,13 @@ ## Pitfalls | ||
This code was written with the intent of placing created ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other: | ||
This code was written with the intent of placing created ids in visible places, like the URL. Therefore, by default the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other: | ||
c, f, h, i, s, t, u | ||
You may customize the chars that shouldn't be placed next to each other by providing a 4th argument to the Hashids constructor: | ||
```js | ||
// first 4 arguments will fallback to defaults (empty salt, no minimum length, default alphabet) | ||
const hashids = new Hashids(undefined, undefined, undefined, 'zyxZYX') | ||
``` | ||
## BigInt | ||
@@ -199,0 +207,0 @@ |
@@ -12,2 +12,8 @@ import Hashids from '../lib/hashids' | ||
test(`should throw an error when alphabet not a string`, () => { | ||
expect(() => { | ||
void new Hashids('', 0, 7) | ||
}).toThrow(TypeError) | ||
}) | ||
test(`should not throw an error when alphabet has spaces`, () => { | ||
@@ -14,0 +20,0 @@ expect(() => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
145447
42
1753
227