A couple of random methods in JS, some are pretty useful, some are pretty damn far from useful. This is the result of an internship where they didn't give me much to do, so I started making random code!
Contents
Uncategorized methods
- bigIf Multi if statements.
- equals Alternative switch statement.
- RNG Methods using random numbers.
- Eventor Micro global event utility.
- windowOrientation Get window orientation changes.
- repeat Extended interval function.
DOM utility methods
- makeFragment HTML string to document fragment.
Array and Object methods
- iterate Loop over a string, array or object.
- contains Check if the array or string contains a value.
- removeVal Remove a value from the array or string.
String manipulation
- ucfirst Transform the first character of a string to uppercase.
- camelCase Transform a string to camelCase.
- toDashed Transform all spaces and underscores in a string to dashes
- toUnderscore Transform all spaces and dashes in a string to underscores.
- filterType Filter a string, allowing only some characters (e.g. letters, numbers, special)
bigIf
Try multiple if statements, if one fails act accordingly.
// In this particular case the second statement fails.
bigIf([(1 == 1), ('derp' == 'herp'), (true == true)], function(){
console.log('Success! All statements were valid');
},
function(whichCheckFailed){
// whichCheckFailed == 2
console.log('Statement '+whichCheckFailed+' failed');
})
equals
Alternative for switch statements
'chicken'.equals({
chicken : function(){
// Will call this case
console.log("I'm a chicken!");
},
lion : function(){
console.log("I'm a lion!");
},
'chicken, lion' : function(){ // NOTE: Multi cases have to be a string, seperate the cases by a comma.
// Will also call this case
console.log("I'm an animal!");
},
default : function(){
console.log("Default case called!");
}
}
})
TIP: equal and bigIf's fail callback work really well together.
RNG
Random number methods using web.crypto() instead of Math.random().
Regular random number method.
var randomInt = RNG.number(1, 1000); // Number between 1 and 1000
Generate a random string.
var randomStr = RNG.string(['letters', 'numbers'], 30); // Random string using letters and numbers, 30 chars long.
You can use the options letters, numbers, special, all or define your own set of characters.
var randomStr = RNG.string(['!$@?', 'numbers'], 10) // Random string using the defined chars and numbers, 10 chars long.
Picks one of the given keys, the values define the odds.
var food = {
// All these values equal 10
"Pizza" : 3, // e.g. this would have a 30% chance to get chosen
"Spagetti" : 1,
"Burgers" : 1,
"Sushi" : 2, // and this 20% chance
"Chinese" : 2,
"Pussy" : 1
}
var chosenFood = RNG.pick(food);
Eventor
Micro global event utility, self explanatory.
var events = new Eventor();
// Attaches the event
events.on('testzor', function(){
// Actions when event 'testzor' gets called
});
// Attach an event, once called it'll remove itself
events.once('onceTest', function(){
// Actions
})
setTimeout(function(){
// Fires the event
events.emit('testzor');
// Second emit will fail
events.emit('onceTest').emit('onceTest');
// Removes the event and tries to fire it (which will not work, you'll get notified in the dev console)
events.off('testzor').emit('testzor');
}, 1500);
NOTE: You can attatch multiple functions to the same event name.
windowOrientation
Get current window orientation and orientation changes.
windowOrientation({
portrait : function(){
console.log('PORTRAIT');
},
landscape : function(){
console.log('LANDSCAPE');
}
})
repeat
Fire a function in intervals until it hits the given amount, then fire the callback
var repeatExample = function(){ console.log('Fired repeatExample()') }
repeat(repeatExample, 5, 250, function(){
console.log('Done');
})
// Will fire repeatExample() 5 times with 250ms pauses, will fire the callback after 1250 ms.
Setting the amount to 0 will set amount to infinite, to clear the interval you have to bind repeat to a variable.
var thisRepeat =
repeat(function(){console.log('No one shall stop me!')}, 0, 500, function(){
console.log('I will not get fired :c');
});
clearInterval(thisRepeat);
Setting the delay to 0 will simply loop the given method.
makeFragment
Prepare a HTML string for DOM insertion.
var exampleHTML = '<div class="example" id="test"><h2 class="title">This is some example HTML</h2><p class="description">The output of this will be a document fragment</p></div>';
var exampleFrag = makeFragment(exampleHTML);
// You can now use exampleFrag in lightweight methods like .appendChild()
iterate
Loop over a string, object or array
var loopObj = {one : 'test', two : 'test', three : 'test'};
iterate(loopObj, function(value, key){
// value == each value in the object
// key == each key in the object
})
var loopArr = ['herp', 'derp', 'ferp'];
iterate(loopArr, function(value, index){
// value == each value in the array
})
var loopStr = 'string';
iterate(loopStr, function(value, index){
// value == each letter in the string
})
contains
Check if an array or string contains the given value
var containArr = ['one', 'two', 'three'];
if(containArr.contains('two')) console.log('Array contains "two"');
var containStr = 'some random string';
if(containStr.contains('uwot')) console.log('String contains "uwot"');
else console.log('String doesn\'t contain "uwot"');
removeVal
Remove the given value from an array or string, returns false if the value
var spliceArr = ['one', 'two', 'three'];
spliceArr.removeVal('two'); // ['one', 'three']
var spliceStr = 'some random string';
spliceStr.removeVal('some'); // 'random string'
ucfirst
Transform the first letter of the given string to uppercase.
'ducks'.ucfirst(); // Ducks
camelCase
Transform a string to camelCase (removing all "-", "_" and " " characters and replacing the character following the stripped character by uppercase)
'weird-looking string with spaces and_some_underscores'.camelCase(); // weirdLookingStringWithSpacesAndSomeUnderscores
toDashed
Transform all spaces and underscores in a string to dashes
'weird-looking string with spaces and_some_underscores'.toDashed(); // weird-looking-string-with-spaces-and-some-underscores
toUnderscore
Transform all spaces and dashes to underscores
'weird-looking string with spaces and_some_underscores'.toUnderscore(); // weird_looking_string_with_spaces_and_some_underscores
filterType
Filter a string for numbers, letters and special characters.
'!^#*!@#t123%!@#&e45^&#@$678s$!@9%^#@t'.filterType() // {letters: "test", numbers: "123456789", special: "!^#*!@#%!@#&^&#@$$!@%^#@"}
'!^#*!@#t123%!@#&e45^&#@$678s$!@9%^#@t'.filterType('letters') // test