3

I want to be able to use HashIDs in my project. With HashIDs you can create hash IDs from either one or n-number of integers, like so:

encrypt(1);
encrypt(1,2,3,4);
encrypt(1,2,...n);

This works fine for one variable already, but unfortunately I'm sometimes receiving a string of comma separated numbers that I'd wish to encode. So it would be "1,2,3,4,5".

Of course I can easily turn that into an array, but an array doesn't help, as I'd still need to pass the valuables individually to encrypt. And since I do not know how many integers there are in the string, I do not know how I can implement that.

Coincidentally, PHP has a way for dealing with that exact same problem.

How could I achieve this in JavaScript?

Community
  • 1
  • 1
dengar81
  • 2,485
  • 3
  • 18
  • 23
  • I looked at the PHP solution and it looks like it's an array map function, have you looked at the JS [array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2Fmap) yet? And if you have a comma separated string, I think you could just `string.split(",");` to turn it into an array and then use the `.map()` – zero298 Apr 16 '15 at 16:51

3 Answers3

2

You can split into an array and convert the values to integers, then use the Function.prototype.apply method to call your encrypt() passing the array values as arguments.

Here's a solution:

var myArgs = "1,2,3,4,5"; // etc.

myArgs = myArgs.split(",").map(function(i) {return parseInt(i);});
encrypt.apply(this, myArgs);

Explaination: the first line is just an assignment; the second line uses the .split() method to turn your string into an array of characters, which gets parsed into an array of integers using .map(...); finally the third line uses the .apply() method to apply your array values as arguments of your function, and calls encrypt(1, 2, 3, 4, 5); like you want.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • thanks for the hint, Marco. I do only seem to get the first number into myArgs on the third line of your example. myArgs after "myArgs = myArgs.split(",").map(parseInt);" looks like [1,Null,Null,...]. – dengar81 Apr 16 '15 at 22:30
  • @dengar81 sorry, for that, I edited my code! Now it works fine. – Marco Bonelli Apr 16 '15 at 22:41
  • Hi Marco, sure this all makes sense to me. However, I still have an issue on the apply method: I get the error message: "TypeError: Cannot find function _encode in object [object Object]." – dengar81 Apr 16 '15 at 23:03
  • @dengar81 that looks like an unrelated error. Perhaps your error is referring to a function that doesn't exist on the local object. By the way it seems unrelated, you should ask another question for that. – Marco Bonelli Apr 16 '15 at 23:04
  • was just a minor typo. the function was called encrypt, not encode :D! – dengar81 Apr 16 '15 at 23:18
1

You should try using the arguments object (it contains all the arguments passed to a function):

   function callEncrypt() {
       return this.apply(encrypt, arguments);
   }

Then you can use it with as many arguments as you want:

callEncrypt(1);
callEncrypt(1, 2);
callEncrypt(1, 2, 3);
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
taxicala
  • 21,408
  • 7
  • 37
  • 66
  • `this.apply(encrypt, arguments);` this is wrong, it will result in a type error saying "undefined is not a function", because you're calling the apply method of the `Window` object, which hasn't got it at all. – Marco Bonelli Apr 16 '15 at 17:01
  • Plus, this still isn't of any help, because now using `callEncrypt` will result in the same problem the OP had before with `encrypt`... – Marco Bonelli Apr 16 '15 at 17:19
0

Pass the arguments using the ES6 spread operator:

var myArgs = "1,2,3,4,5"; // etc.
myArgs = myArgs . split(',') . map(parseInt);

encrypt(...myArgs);
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • This is hasn't been implemented yet and fails in most of the current browsers, plus you would have to use strict mode on some browsers. – Marco Bonelli Apr 16 '15 at 17:10
  • This answer was not intended to be a tutorial on ES6 and its toolings, but suffice it to say that there are a rich variety of ways to use ES6 in almost any environment. It's completely incorrect to say that it's not implemented. For instance, it's completely implemented in node. It's implemented in a well-known, well-supported transpiler called Babel, which fits perfectly well into a browser-based workflow. In any case, the intent of this answer was merely to point out the possibility for those who are looking forward as opposed to backward. –  Apr 16 '15 at 17:25
  • Actually I said *"hasn't been implemented yet and fails in most of the current browsers"*, so yes, I know it's implemented in *some* platforms, but, like I said, not in *all* the current browsers. – Marco Bonelli Apr 16 '15 at 17:29
  • OK, let's beat a dead horse here. It does **not** fail in any current browser, because you transpile it into ES5 which all browsers support. –  Apr 16 '15 at 18:07