Skip to content

Instantly share code, notes, and snippets.

@westc
westc / nativize-all-functions.js
Created February 26, 2014 23:13
JavaScript code to make all functions appear to be native code.
(function(strFakeNative) {
Function.prototype.toString = function() {
return strFakeNative.replace('atob', this.name || '');
};
})(atob + '');
@westc
westc / parseUrl.js
Last active August 29, 2015 13:57
Parses a given URL breaking it out into its parts.
(function (RGX_PLUS, RGX_SLASH, RGX_PARAM, decodeURIComponent, undefined) {
/**
* @license Copyright 2014 - Chris West - MIT Licensed
*
* Parses a given URL breaking it out into its parts.
* @param {string} opt_url
* Optional. The URL to be parsed and broken out into its individual
* parts. Defaults to the URL of the current page if not given.
* @return {!Object}
* Object containing the `href`, `protocol`, `host`, `hostname`, `port`,
@westc
westc / cint.js
Last active March 8, 2019 02:36
Convert numbers to integers with cint().
/**
* @license Copyright 2019 - Chris West - MIT Licensed
*
* Converts a number into an integer.
* @param {*} num
* The value to convert into an integer.
* @param {boolean|null} opt_infinityBiased
* Optional. If a true-ish value is specified the number will be rounded
* towards `Infinity` if positive or `-Infinity` if negative. If not given,
* or if `null` or `undefined` is specified the number will be rounded. If
@westc
westc / uri.js
Last active August 29, 2015 14:02 — forked from jlong/uri.js
Parsing a URL (jPaq Proof Test)
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
console.log(parser.protocol); // => "http:"
console.log(parser.hostname); // => "example.com"
console.log(parser.port); // => "3000"
console.log(parser.pathname); // => "/pathname/"
console.log(parser.search); // => "?search=test"
console.log(parser.hash); // => "#hash"
console.log(parser.host); // => "example.com:3000"
@westc
westc / typoglycemia.js
Last active August 29, 2015 14:02
Message Scrambler - Proof that typoglycemia allows us to read scrambled messages.
function scramble(input) {
return input.replace(/\b(\w)(\w{2,})(\w)\b/g, function(all, a, b, c) {
return a
+ b.split('').sort(function(){return Math.random()-.5}).join('')
+ c;
});
}
input = prompt("Enter the message you want scrambled:");
console.log(scramble(input));
@westc
westc / guess-a-number.js
Created July 7, 2014 15:52
Simple JavaScript number guessing game.
// Initial setup of variables.
var lowerLimit = 1;
var upperLimit = 25;
var tries = 5;
var answer = Math.floor(Math.random() * (upperLimit - lowerLimit + 1)) + lowerLimit;
var guess = '';
var message = 'Guess a number between ' + lowerLimit + ' and ' + upperLimit + ':';
// Keep prompting the user for a guess until the game ends.
while (tries > 0) {
@westc
westc / popProps.js
Created July 30, 2014 20:34
Takes an array (or uses all but the first parameter as an array) of strings, removes those properties from the given object and returns an object of all of the removed values.
function popProps(obj, keys) {
var ret = {};
keys = ret.toString.call(keys) == '[object Array]'
? keys
: [].slice.call(arguments, 1);
for (var key, i = keys.length; i--;) {
key = keys[i];
ret[key] = obj[key];
delete obj[key];
}
@westc
westc / getProps.js
Created July 30, 2014 20:36
Takes an array (or uses all but the first parameter as an array) of keys and returns a new object containing all of the values with the specified keys.
function getProps(obj, keys) {
var ret = {};
keys = ret.toString.call(keys) == '[object Array]'
? keys
: [].slice.call(arguments, 1);
for (var key, i = keys.length; i--;) {
key = keys[i];
ret[key] = obj[key];
}
return ret;
@westc
westc / getComments.js
Last active February 12, 2024 17:18
getComments(fn, opt_trim) returns an array of all the comments in the specified function.
(function(RGX_COMMENT, RGX_TRIM) {
getComments = function (fn, opt_trim) {
var comments = [];
(fn + '').replace(RGX_COMMENT, function(m, a, b, c) {
if (!c) {
m = a == undefined ? b : a;
comments.push(opt_trim ? m.replace(RGX_TRIM, '') : m);
}
});
return comments;
@westc
westc / String.prototype.before.js
Last active August 29, 2015 14:05
Gets the substring before the specified target.
/**
* @license Copyright 2015 - Chris West - MIT Licensed
* Gets the substring before the specified target.
* @param {string|RegExp} target
* Specifies the substring to key off in order to pull the substring that
* precedes it.
* @param {number=} opt_occurrence
* Optional. The number of the occurrence to key off of. Defaults to 1.
* @return {string=}
* If the target is found, the substring that precedes will be returned.