Skip to content

Instantly share code, notes, and snippets.

@ianpgall
ianpgall / string-camelcase.js
Last active December 20, 2015 21:59
JavaScript functions that converts strings to camel case, or to a hyphenated version
var Format = (function () {
"use strict";
var toCamelCase, toHyphenated;
toCamelCase = (function () {
var re, ret;
re = /-([a-z])/g;
ret = function (str) {
str = str.replace(re, function (match, lower) {
@ianpgall
ianpgall / array-removedupes.js
Last active December 20, 2015 21:59
JavaScript functions that remove duplicates from an array
var RemoveDuplicates = (function () {
"use strict";
var inPlace, getCopy;
inPlace = function (arr) {
var i, j, cur, found;
for (i = arr.length - 1; i >= 0; i--) {
cur = arr[i];
found = false;
@ianpgall
ianpgall / element-inviewport.js
Last active December 20, 2015 21:59
JavaScript function that determines if an element is in the window's viewport
var IsInViewport = (function () {
"use strict";
var func;
func = function (el, par) {
var ret, elRect, parRect;
elRect = el.getBoundingClientRect();
if (par) {
parRect = par.getBoundingClientRect();
@ianpgall
ianpgall / date-leapyear.js
Last active December 20, 2015 21:59
JavaScript function that determines if a specific year is a leap year
var IsLeapYear = (function () {
"use strict";
var func;
func = function (year) {
return (year % 400) ? ((year % 100) ? ((year % 4) ? false : true) : false) : true;
};
return func;
@ianpgall
ianpgall / string-pad.js
Last active December 20, 2015 17:19
JavaScript function that pads the left side of a string/number with a specific prefix, up to a specific length
var PadText = (function () {
"use strict";
var func;
func = function (text, prefix, finalLength) {
return (new Array(finalLength).join(prefix.charAt(0)) + text).slice(-finalLength);
};
return func;
@ianpgall
ianpgall / object-navigate.js
Last active December 20, 2015 15:59
JavaScript function that allows the deep navigation of an Object, invoking a callback upon every single property
var Navigate = (function () {
"use strict";
var O, func;
O = {};
func = function (obj, callback) {
var iterator = function (o) {
var k, cur;
@ianpgall
ianpgall / element-animate.js
Last active December 20, 2015 13:39
JavaScript function that animates an element's numeric style in a certain amount of time
var animate = (function () {
"use strict";
var ret;
ret = function (elem, style, unit, from, to, time) {
var start, timer;
if (!elem) {
return;
}
@ianpgall
ianpgall / next-sibling.js
Last active December 20, 2015 12:59
JavaScript function that gets an Element's first following sibling (that matches a selector, optionally). If a selector is provided, it searches through all following siblings for the element that matches the selector (if any)
var NextElementSibling = (function () {
"use strict";
var E, supportsNativeNext, is, traverseAcross, ret;
E = document.createElement("div");
supportsNativeNext = ("nextElementSibling" in E && !E.hasOwnProperty("nextElementSibling"));
is = (function () {
var prefixes, i, j, cur, matchProp, matchFunc;
@ianpgall
ianpgall / regex-escape.js
Last active December 20, 2015 12:38
JavaScript RegExp method to escape a string that will be used in a RegExp object constructor, so the characters aren't interpreted as regex special characters
RegExp.quote = (function () {
"use strict";
var ret, re;
re = /([.?*+^$[\]\\(){}|-])/g;
ret = function (str) {
return ("" + str).replace(re, "\\$1");
};
@ianpgall
ianpgall / string-format.js
Last active December 20, 2015 03:19
JavaScript function to format strings with the format {#}
var Format = (function () {
"use strict";
var refToString, toString, re, slice, ret;
refToString = {}.toString;
toString = function (p) {
return refToString.call(p);
};
re = /\{(\d+)\}/g;