Skip to content

Instantly share code, notes, and snippets.

@wizard04wsu
wizard04wsu / stringPrune.js
Last active August 29, 2015 13:56
String.prototype.prune() is the opposite of String.prototype.slice(). It removes the specified portion of the string and returns what's left, without modifying the original string.
//returns a copy of a string with a portion removed
//(essentially the opposite of `slice`)
//the `end` argument is optional
//str.prune(begin[, end])
if(!String.prototype.prune){
String.prototype.prune = function prune(begin, end){
"use strict";
var newStr = this.slice(0); //make a copy of the string
@wizard04wsu
wizard04wsu / encodeURI.js
Last active August 29, 2015 13:56
Fixes encodeURI() to comply with RFC 3986 where "[" and "]" are reserved characters.
(function (){
"use strict";
var oldEncodeURI = encodeURI;
encodeURI = function encodeURI(str){ return oldEncodeURI(str).replace(/%5B/ig, "[").replace(/%5D/ig, "]"); };
})();
@wizard04wsu
wizard04wsu / encodeStrings.js
Last active August 29, 2015 13:56
Functions to encode/decode text for use in HTML 4.01.
//encode reserved characters in a string for use in HTML
//if `keepValidEntities` is true, the amphersands for valid character entity references will not be encoded
function textToHTML(str, keepValidEntities){
"use strict";
var validEntityNames, rxp;
if(keepValidEntities){
//see http://www.w3.org/TR/html401/sgml/entities.html
validEntityNames = ""+
//markup-significant and internationalization characters
@wizard04wsu
wizard04wsu / bind.js
Last active August 29, 2015 13:56
Cross-browser workaround for function binding
//creates a new function that, when called, has its `this` keyword set to the provided object, with a given sequence of arguments
// preceding any provided when the new function is called
//note: this is only a partial implementation; it may not work as expected in some scenarios.
// For instance, the resulting function cannot be used as a contructor.
if(!Function.prototype.bind){
Function.prototype.bind = function bind(toObject){
"use strict";
var originalFn, boundArgs;
@wizard04wsu
wizard04wsu / objectKeys.js
Created February 5, 2014 21:13
Cross-browser Object.keys()
//returns an array of the properties & methods that are direct properties of an object (i.e., those that have not been inherited)
//see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
if(!Object.keys){
Object.keys = (function (){
"use strict";
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable("toString"),
dontEnums = ["toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "constructor"];
@wizard04wsu
wizard04wsu / trim.js
Created February 5, 2014 21:18
JavaScript trim functions
//trim functions were tested in Firefox 4, 8, & 20; IE 6 & 9; Chrome 17; Safari 5.1; and Opera 11.5
//built-in trim() is much faster in Firefox & IE, slower in Opera, and slower for short strings but much faster for long strings in Chrome & Safari
if(!String.prototype.trim /*|| window.opera*/){
if( window.navigator.mozIsLocallyAvailable || ((/safari/i).test(window.navigator.userAgent) && !window.chrome) ) //Firefox & Safari
String.prototype.trim = function trim(){
var str = /\S+(?:\s+\S+)*/.exec(this);
return str ? str[0] : "";
};
else //other browsers
@wizard04wsu
wizard04wsu / ordinalOf.js
Last active August 29, 2015 13:56
Get the ordinal indicator of a number (English only)
function ordinalOf(num){
var n;
return ((n = num%100) === 11 || n === 12 || n === 13) ? "th" : ((n = num%10) === 1) ? "st" : (n === 2) ? "nd" : (n === 3) ? "rd" : (num%1 === 0) ? "th" : "";
}
@wizard04wsu
wizard04wsu / dates.js
Created February 6, 2014 21:30
JavaScript Date methods
//month names, weekday names, and ordinal indicators are in English
// Date.now()
// Date.fromISOString(isoStr)
// Date.timeBetween(date1, date2)
// dat.isLeapYear()
// dat.isUTCLeapYear()
// dat.getDaysInMonth()
// dat.getUTCDaysInMonth()
// dat.getMonthName()
@wizard04wsu
wizard04wsu / cookies.js
Last active May 12, 2021 09:32
Methods for working with cookies
//If cookies are not supported by the browser, `Cookies` will be undefined
//Cookies.set(name, value[, options])
// creates, modifies, or removes a cookie
// `options` may be an object including `expires`, `path`, `domain`, and/or `secure`
// `options.expires` can be either a Date object or the number of seconds until it should expire
// (0 or undefined indicates end of session, <0 removes the cookie, Infinity sets it to 50 years)
//
//Cookies.get(name)
// returns the value of the first cookie with that name
@wizard04wsu
wizard04wsu / nthRoot.js
Last active August 29, 2015 13:56
Get the nth root of a number, where n is the degree of the root. For example, get the cube root of 27 using Math.root(27, 3).
if(!Math.root){
Math.root = function root(num, degree){
return Math.pow(num, (1/degree));
};
}