Last active
November 15, 2016 15:38
-
-
Save spiralx/d07f54579d60a9994061 to your computer and use it in GitHub Desktop.
jQuery extras for use in user scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
'use strict'; | |
if (typeof $.format === 'function') { | |
return; | |
} | |
// -------------------------------------------------------------------- | |
// Defined variables and constants. | |
var _types = new Set('Object Array Function String Number Boolean Date RegExp Error Set WeakSet Map WeakMap Symbol'.toLowerCase().split(' ')); | |
// -------------------------------------------------------------------- | |
// Helper functions! | |
var | |
isValue = function(o) { | |
return o !== undefined && o !== null; | |
}, | |
getType = function(o) { | |
if (!isValue(o)) { | |
return String(o); | |
} | |
var c = Object.prototype.toString.call(o).replace(/^\[object |\]$/g, '').toLowerCase(); | |
return _types.has(c) ? c : typeof o; | |
}, | |
isArray = Array.isArray, | |
isFunction = function(o) { | |
return getType(o) === 'function'; | |
}, | |
isConstructor = function(C) { | |
try { | |
new C(); | |
return true; | |
} | |
catch (ex) { | |
return false; | |
} | |
}, | |
isNumber = function(o) { | |
return !isArray(o) && o - parseFloat(o) >= 0; | |
}, | |
toInteger = function(value) { | |
var number = Number(value); | |
if (isNaN(number)) { | |
return 0; | |
} | |
if (number === 0 || !isFinite(number)) { | |
return number; | |
} | |
return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number)); | |
}; | |
// -------------------------------------------------------------------- | |
// Method to attach functions to objects as properties. | |
function attachMethods(obj, map) { | |
for (var k in map) { | |
if (obj.hasOwnProperty(k)) { | |
continue; | |
} | |
Object.defineProperty(obj, k, { | |
value: map[k], | |
configurable: true, | |
writable: true | |
}); | |
} | |
} | |
attachMethods(Object, { | |
type: getType, | |
assign: function(target) { | |
if (!isValue(target)) { | |
throw new TypeError('Object.assign cannot be called with null or undefined'); | |
} | |
var res = Object(target); | |
[].slice.call(arguments, 1).forEach(function(src) { | |
Object.keys(Object(src)).forEach(function(k) { | |
res[k] = src[k]; | |
}); | |
}); | |
return res; | |
} | |
}); | |
attachMethods(String.prototype, { | |
/** | |
* Simple string substution function.Obj | |
* | |
* $.format('x={x}, y={y}', { x: 12, y: 4 }) -> 'x=12, y=4' | |
* $.format('x={x}, y={{moo}}', { x: 12, y: 4 }) -> 'x=12, y={moo}' | |
* $.format('{x}: {y.thing}', { x: 'foo', y: { thing: 'bar' }}) -> 'foo: bar' | |
* $.format('{x}: {y.a[1]}', { x: 'foo', y: { thing: 'bar', a: [6, 7] }}) -> 'foo: 7' | |
* $.format('{0[2]}, {0[-2]}', [{ x: 12, y: 4 }, 7, 120, 777, 999]) -> '120, 777' | |
* $.format('{0[-5].y}', [{ x: 12, y: 4 }, 7, 120, 777, 999]) -> '4' | |
* $.format('{a[-5].x}', {a: [{ x: 12, y: 4 }, 7, 120, 777, 999]}) -> '12' | |
* | |
* @param {String} format | |
* @param {Object|Object+} data | |
* @return {String} | |
*/ | |
format: function(data) { | |
data = arguments.length === 1 && getType(data) === "object" && !isArray(data) | |
? data | |
: [].slice.call(arguments); | |
return this | |
.replace(/\{\{/g, String.fromCharCode(0)) | |
.replace(/\}\}/g, String.fromCharCode(1)) | |
.replace(/\{([^}]+)\}/g, function(match, path) { | |
try { | |
var p = path.replace(/\[(-?\w+)\]/g, '.$1').split('.'); | |
//console.log('path="%s" (%s), data=%s', path, p.toSource(), data.toSource()); | |
return String(p.reduce(function(o, n) { | |
return o.slice && !isNaN(n) ? o.slice(n).shift() : o[n]; | |
}, data)); | |
} | |
catch (ex) { | |
return match; | |
} | |
}) | |
.replace(/\x00/g, "{") | |
.replace(/\x01/g, "}"); | |
}, | |
includes: function(search) { | |
if (!isValue(this)) { | |
throw new TypeError(); | |
} | |
var string = String(this); | |
if (search && Object.type(search) === 'regexp') { | |
throw new TypeError(); | |
} | |
var stringLength = string.length, | |
searchString = String(search), | |
searchLength = searchString.length, | |
position = arguments.length > 1 ? Number(arguments[1]) : 0; | |
// `ToInteger` | |
if (position != position) { // better `isNaN` | |
position = 0; | |
} | |
var start = Math.min(Math.max(position, 0), stringLength); | |
// Avoid the `indexOf` call if no match is possible | |
return searchLength + start > stringLength ? false : indexOf.call(string, searchString, position) !== -1; | |
}, | |
repeatWith: function(num) { | |
if (!isNumber(num)) { | |
throw new TypeError('Parameter should be a number'); | |
} | |
var result = ''; | |
while (num--) { | |
result += this; | |
} | |
return result; | |
}, | |
codePointAt : function(pos) { | |
if (!isNumber(pos)) { | |
throw new TypeError("Parameter should be a number"); | |
} | |
var size = this.size; | |
if (pos < 0 || pos > size) { | |
return undefined; | |
} | |
var first = this.charCodeAt(pos); | |
var second; | |
if ( first >= 0xD800 && first <= 0xDBFF && | |
size > pos + 1 ) { | |
second = this.charCodeAt(pos + 1); | |
if (second >= 0xDC00 && second <= 0xDFFF) { | |
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; | |
} | |
} | |
return first; | |
} | |
}); | |
$.fn.updateForm = function(props) { | |
var $el = $(this); | |
for (var k in props) { | |
$el.append('<input type="hidden" name="{0}" id="{0}" value="{1}">'.format(k, props[k])); | |
} | |
return this; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment