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?

1
  • I looked at the PHP solution and it looks like it's an array map function, have you looked at the JS array.map() 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() Commented Apr 16, 2015 at 16:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 sorry, for that, I edited my code! Now it works fine.
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 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.
was just a minor typo. the function was called encrypt, not encode :D!
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);

2 Comments

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.
Plus, this still isn't of any help, because now using callEncrypt will result in the same problem the OP had before with encrypt...
0

Pass the arguments using the ES6 spread operator:

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

encrypt(...myArgs);

4 Comments

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.
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.
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.