If we are dealing with smaller number then we can leverage bit-wise operators such as bit-wise negation (
~). The reason we can only use this solution for smaller numbers is because bit-wise operations can only be executed on a number in the range of
231 - 1. If the number is outside of that range it will be converted to a number within that range (eg.
There is actually a function that will convert strings into numbers called
parseInt(). We can use this function and specify the base to ensure that numbers starting with
0 will not be parsed as octals (eg.
8 in older versions of ECMAScript).
We can also use the equivalent function to
parseInt() for floats which is
parseFloat(). After that we can use
Number.prototype.toFixed() to make the number a string representation of the truncated number. Finally we can prefix this expression with a plus sign (
+) to convert the string to a number again.
We can use the newer
Math.trunc() function which should in theory just do what we want 😆 .
Below are the results of using your browser to test out each solution:
If you have looked through the results above on a modern browser (one implementing
Math.trunc()) you’ll notice that for all of our test cases, it is the only solution that works for them all. Therefore, if you are looking to convert floating-point numbers to integers or strings to integers, the most convenient and straightforward way may in fact be ES6’s new
Math.trunc() function. Let me know what you think and as always, happy coding! 😎
Now Available in YourJS
The above function takes the string in question and returns
true if the string can be used a variable name. If the string can not be used as a variable name
false is returned.
Some may wonder why I’m doing the following:
The reason I included the above replacement is to avoid false-positives in the case of an empty string, extra spacing, commas, and forward slashes.
Others have attempted to make the same function using the evil
eval() function which allows for JS injection. Even though the
Function constructor can also be used for evil, when supplying arguments it does prevent you from doing JS injection by making sure the arguments don’t have parentheses.
The following is an example of what will happen when running for the function for a number of strings:
console.log(isValidVarName('')); // -> false
console.log(isValidVarName('3')); // -> false
console.log(isValidVarName('3d')); // -> false
console.log(isValidVarName('D3')); // -> true
console.log(isValidVarName('D3 ')); // -> false
console.log(isValidVarName('D3,Q')); // -> false
console.log(isValidVarName('D3/*Qs*/')); // -> false
console.log(isValidVarName('D3Q')); // -> true
console.log(isValidVarName('var')); // -> false
console.log(isValidVarName('true')); // -> false
console.log(isValidVarName('undefined')); // -> true
console.log(isValidVarName('null')); // -> false
console.log(isValidVarName('coolio.pop')); // -> false
console.log(isValidVarName('coolio')); // -> true
console.log(isValidVarName('coolio_pop')); // -> true
console.log(isValidVarName('$')); // -> true
console.log(isValidVarName('$á')); // -> true
console.log(isValidVarName('áÑ')); // -> true
console.log(isValidVarName('_')); // -> true
Here is a similar example hosted on JSBin:
JS Bin on jsbin.com
I finally have the 1st pass of YourJS available to the public!!!
What is YourJS?
Who controls the codebase?
Everyone does! All you have to do is sign up and you can start writing code that can be included in YourJS.
How do I design and download my own library?
As of right now there is a section called Snippets which is a repo of code that can be included in your custom JS library. You can browse the snippets, add the ones you like to the build and then go to the build page to build the library from the selected snippets. Finally, you will have the opportunity to name your library (this will be the variable name added to the global namespace for your utility library).
What development plans are there?
- Port most functionality available in jPaq over to YourJS.
- Add an examples section similar to the one that exists on jPaq.org.
- Include a buildable version of the library which includes documentation.
- A link share for all other JS sites.
I have a lot of ideas for YourJS so the sky is the limit! Let me know if you have any suggestions and perhaps I will be able to include them in the next iteration of changes. 😎