Skip to content

Instantly share code, notes, and snippets.

@ewebdev
ewebdev / overload.js
Created April 15, 2018 12:17
Overloading Javascript Functions
const overload = (() => {
const noop = () => {};
const typeOf = (p) => (Object.prototype.toString.call(p).slice(8,-1));
return (fns) => (function (...args) {
const key = args.map(typeOf).join('_');
return (fns[key] || noop).apply(this, args);
});
})();
function compile (pattern, mapping) {
pattern = pattern || '';
var i,
ch,
last,
attrName,
sequenceLength = 0,
len = pattern.length,
(function(){
var globals = [],
pageProps = {},
nativeProps = {},
v;
for (v in window) {
pageProps[v] = 1;
}
var ifrm = document.createElement("IFRAME");
document.body.appendChild(ifrm);
@ewebdev
ewebdev / RecursiveObjectPathResolver.js
Created December 24, 2014 16:05
Returns the value in within a requested path in a nested object (tree traversal), without using "eval" / "new Function".
/**
* assumption: path contains only key names and dots (.), doesn't handle function calls or array indices
*/
function getVal (obj, path) {
if (path) {
var s = path.split('.'),
cur = s.shift();
if (obj.hasOwnProperty(cur)) {
return getVal(obj[cur], s.join('.'));
}
"use strict";
define([
'jquery',
'underscore'
], function ($, _) {
$.fn.loadImg = function (options) {
// var opts = $.extend({}, $.fn.loadImg.defaults, options);
function formatFileSize(bytes) {
var val = bytes / 1024,
suffix;
if (val < 1000) {
suffix = 'KB';
} else {
val = val / 1024;
if (val < 1000) {
suffix = 'MB';
} else {
function formatPrice(val, fixedDecimals, currencyCode) {
var absVal = Math.abs(val),
parts = absVal.toString().split('.'),
res;
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
if (fixedDecimals) {
parts[1] = absVal.toFixed(fixedDecimals).split('.')[1];
}
res = parts.join(".");
return (val < 0 ? '-' : '') + (currencyCode || '') + res;
function roundNumber(val, maxDecimals) {
return (Math.round(val * Math.pow(10, maxDecimals))) / Math.pow(10, maxDecimals);
}
(function($) {
$.fn.loadSvg = function (options) {
var opts = $.extend({}, $.fn.loadSvg.defaults, options);
return this.each(function () {
var $stage = $(this);
$stage.load($stage.data('src'), function (response) {
$stage.addClass(opts.loadedCls);
(function ($) {
$.fn.linkOut = function (options) {
return this.each(function () {
$(this).on('mouseenter', options.mouseEnterToSelector, function () {
var $el = $(this),
$target = $el.find(options.targetAppendSelector);
if ($target.length) {
var $linkout = $('<a />', {class: 'linkout sprite', rel: 'nofollow', css: {opacity: 0}, href: $target.data('href')}).appendTo($target);