Skip to content

Instantly share code, notes, and snippets.

@d-simon
d-simon / string_padding.js
Created March 7, 2014 00:42
String Padding / Leading Zeros
function pad (n, width, z) {
z = z || '0';
n = n + '';
while (n.length < width) { n = z + n; }
return n;
}
@d-simon
d-simon / iterator_async_callback.js
Created March 7, 2014 00:49
For-Loop / Pass iterator into async callback
for (var i = 0; i < array.length; i++) (function (i, arrayElement) {
// this will create a new variables i and arrayElement for each loop iteration
// so we can use it in an async callback
doSomeAsync(function callback () {
console.log(i, arrayElement);
});
}) (i, array[i]);
@d-simon
d-simon / shuffle_array.js
Created March 7, 2014 13:04
Shuffle Array
shuffle = function(v){
for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
return v;
};
@d-simon
d-simon / math_random.js
Created March 7, 2014 21:27
Random number in range
/**
* Returns a random number between min and max
*/
function getRandomArbitary (min, max) {
return Math.random() * (max - min) + min;
}
/**
* Returns a random integer between min and max
* Using Math.round() will give you a non-uniform distribution!
@d-simon
d-simon / emoji_regex.js
Created March 8, 2014 01:43
Emoji Regex
// http://crocodillon.com/blog/parsing-emoji-unicode-in-javascript
var ranges = [
'\ud83c[\udf00-\udfff]', // U+1F300 to U+1F3FF
'\ud83d[\udc00-\ude4f]', // U+1F400 to U+1F64F
'\ud83d[\ude80-\udeff]' // U+1F680 to U+1F6FF
];
var regexp = new RegExp(ranges.join('|'), 'g');
@d-simon
d-simon / double_not_operator.md
Created March 9, 2014 17:17
Double NOT Operator ! (convert value to boolean)

Taken from [Double exclamation points?][1]

Also: [What is the !! (not not) operator in JavaScript?][2]

#Double NOT Operator

This converts a value to a boolean and ensures a boolean type.

"foo"      =    "foo"

!"foo" = false

@d-simon
d-simon / double_bitwise_not_operator.md
Created March 9, 2014 17:25
Double Bitwise NOT Operator (~~, double tilde)

Taken from [Tilde or the Floor? Javascript bitwise operators in Practice][1]

#Bitwise NOT - The ~ operator

Apart from "inverting the bits of its operand" bitwise NOT in JavaScript is actually very useful not only when it comes to binary. Firstly, it has a very interesting effect on integers - it converts the integer to -(N+1) value.

For example:

~2 === -3; //true

~1 === -2; //true

@d-simon
d-simon / string_repeat.js
Created March 12, 2014 14:11
String Repeat
// http://stackoverflow.com/a/5450113
// String prototyped
String.prototype.repeat = function(count) {
if (count < 1) return '';
var result = '', pattern = this.valueOf();
while (count > 0) {
if (count & 1) result += pattern;
count >>= 1, pattern += pattern;
}
@d-simon
d-simon / .jshintrc
Created March 22, 2014 14:40
.jshintrc - Personal Node.js Coding Style JSHint
{
"curly": false,
"eqeqeq": true,
"eqnull": true,
"bitwise": true,
"camelcase": false,
"forin": true,
"immed": true,
"indent": 4,
"latedef": true,
ALTER TABLE users
ADD COLUMN `count` SMALLINT(6) NOT NULL AFTER `lastname`,
ADD COLUMN `log` VARCHAR(12) NOT NULL AFTER `count`,
ADD COLUMN `status` INT(10) UNSIGNED NOT NULL AFTER `log`;