Skip to content

Instantly share code, notes, and snippets.

@ianpgall
ianpgall / element-dataset.js
Last active December 20, 2015 00:18
JavaScript function to manipulate an Element's dataset/data-* attributes (detects native browser support)
var DataSet = (function () {
"use strict";
var E, toCamelCase, getter, setter;
E = document.createElement("div");
if (E.dataset && !E.hasOwnProperty("dataset")) {
toCamelCase = (function () {
var re = /-([a-z])/g;
@ianpgall
ianpgall / function-bind.js
Last active December 20, 2015 00:19
JavaScript function that binds the context of a Function, returning a new one (detects native browser support)
var Bind = (function () {
"use strict";
var A, F, ret;
A = [];
F = function () { return undefined; };
if (F.bind && !F.hasOwnProperty("bind")) {
ret = function (func, thisArg /*, args */) {
@ianpgall
ianpgall / function-once.js
Last active December 20, 2015 00:19
JavaScript function that allows the execution of a Function once
var Once = (function () {
"use strict";
var A, F, func;
A = [];
F = function () { return undefined; };
func = function (f) {
var ret, ran, retValue;
@ianpgall
ianpgall / object-clone.js
Last active December 20, 2015 00:19
JavaScript function that deep clones an Object or Array
var Clone = (function () {
"use strict";
var O, toString,
objType, arrType,
isObject, isArray,
each, iterator;
O = {};
toString = function (p) {
@ianpgall
ianpgall / string-trim.js
Last active December 20, 2015 00:28
JavaScript function to trim Strings (detects native browser support)
var Trim = (function () {
"use strict";
var S, both, left, right;
S = "";
if (S.trim && !S.hasOwnProperty("trim")) {
both = function (str) {
return S.trim.call(str);
@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;
@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 / 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 / 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 / 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;