Skip to content

Instantly share code, notes, and snippets.

@westc
westc / String.prototype.after.js
Last active August 29, 2015 14:05
Gets the substring after the specified target.
/**
* @license Copyright 2015 - Chris West - MIT Licensed
* Gets the substring after the specified target.
* @param {string|RegExp} target
* Specifies the substring to key off in order to pull the substring that
* follows it.
* @param {number=} opt_occurrence
* Optional. The number of the occurrence to key off of. Defaults to 1.
* @return {string|null}
* If the target is found, the substring that follows will be returned.
@westc
westc / String.prototype.wrap.js
Last active August 29, 2015 14:05
Surrounds the string with the specified string(s).
(function(RGX, STR, undefined) {
/**
* Surrounds the string with the specified string(s).
* @param {string} wrap1
* The string to precede this string.
* @param {string=} opt_wrap2
* The string to follow this string. Defaults to the value of `wrap1` if
* not given.
* @param {string=} opt_wrap1rep
* The string to replace all occurrences of `wrap1` within this string.
@westc
westc / String.prototype.insert.js
Last active August 29, 2015 14:05
Easily insert a string within another string.
String.prototype.insert = function(strToInsert, num_rgx_str, opt_after) {
return ({}).toString.call(num_rgx_str) == '[object Number]'
? this.slice(0, num_rgx_str + strToInsert + this.slice(num_rgx_str)
: this.replace(num_rgx_str, function(match) {
return opt_after ? match + strToInsert : (strToInsert + match);
});
};
@westc
westc / Style Remover
Created August 26, 2014 04:01
Removes the styles and stylesheets from a web page.
(function(a,b,c){function d(a){a.style&&(a.style.cssText='');for(var b,c=0;b=a.childNodes[c++];)d(b)}a=document.styleSheets;for(c=a.length;b=a[--c];)b.disabled=!0;d(document.body)})()
@westc
westc / globalEval-using-data-uris.js
Created October 23, 2014 12:11
Global Namespace Evaluation of JavaScript Using Data URIs (DANGEROUS!!!)
/**
* Evaluate a string of JavaScript code in the global namespace.
* NOTE: This differs from eval() in that it doesn't return a value.
* @param {string} code The JavaScript code to be executed in the global namespace.
*/
function globalEval(code) {
var script = document.createElement('script');
script.src = 'data:application/javascript;charset=utf-8,' + encodeURIComponent(code);
(document.getElementsByTagName('head')[0] || document.body).appendChild(script);
}
@westc
westc / queue.js
Last active August 29, 2015 14:09
A way to rate limit function calls by queueing up the context and arguments to be called at a later time.
/**
* Copies a function and rate limits it so it will only execute once within the
* given amount of milliseconds.
* @param {!Function} fn The function to be rate limited.
* @param {number} threshold The minimum amount of milliseconds between
* subsequent function executions.
* @param {boolean=} opt_waitAfter Optional parameter defaulting to false.
* If set to true the delay doesn't start until after the function
* executes.
* @return {!Function} The rate limited version of the function passed in.
function getCall(fn) {
return function() {
return fn.call.apply(fn, arguments);
};
}
function getApply(fn) {
return function(context, args) {
return fn.apply(context, args);
};
}
@westc
westc / call.js
Created November 21, 2014 18:58
function call() {
return call.call.apply(call.call, arguments);
}
function apply(fn, context, args) {
return fn.apply(context, args);
}