From 207dee91f44423c8396d8c8f157c22d37834bb72 Mon Sep 17 00:00:00 2001 From: tomasz stryjewski Date: Tue, 17 Jun 2014 11:21:41 +0200 Subject: [PATCH] small cleanup - created and applied two utility functions (cleaner code, more minify-friendly): - `isFunction` - `isDefined` - simplified some `if` checks --- svg-injector.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/svg-injector.js b/svg-injector.js index ce0c84c..341c26b 100644 --- a/svg-injector.js +++ b/svg-injector.js @@ -14,6 +14,14 @@ var isLocal = window.location.protocol === 'file:'; var hasSvgSupport = document.implementation.hasFeature('http://www.w3.org/TR/SVG11/feature#BasicStructure', '1.1'); + function isFunction(value) { + return typeof value === 'function'; + } + + function isDefined(value){ + return typeof value !== 'undefined'; + } + // SVG Cache var svgCache = {}; @@ -48,10 +56,10 @@ }; var loadSvg = function (url, callback) { - if (svgCache[url] !== undefined) { + if (isDefined(svgCache[url])) { if (svgCache[url] instanceof SVGSVGElement) { // We already have it in cache, so use it - callback(svgCache[url].cloneNode(true)); + callback(cloneSvg(svgCache[url])); } else { // We don't have it in cache yet, but we are loading it, so queue this request @@ -173,7 +181,7 @@ // Load it up loadSvg(imgUrl, function (svg) { - if (svg === undefined || typeof svg === 'string') { + if (!isDefined(svg) || typeof svg === 'string') { callback(svg); return false; } @@ -343,19 +351,19 @@ var eachCallback = options.each; // Do the injection... - if (elements.length !== undefined) { + if (isDefined(elements.length)) { var elementsLoaded = 0; Array.prototype.forEach.call(elements, function (element) { injectElement(element, evalScripts, pngFallback, function (svg) { - if (eachCallback && typeof (eachCallback) === 'function') eachCallback(svg); + if (isFunction(eachCallback)) eachCallback(svg); if (done && elements.length === ++elementsLoaded) done(elementsLoaded); }); }); } else { - if (elements !== null) { + if (elements) { injectElement(elements, evalScripts, pngFallback, function (svg) { - if (eachCallback && typeof (eachCallback) === 'function') eachCallback(svg); + if (isFunction(eachCallback)) eachCallback(svg); if (done) done(1); }); } @@ -371,7 +379,7 @@ module.exports = exports = SVGInjector; } // AMD support - else if (typeof define === 'function' && define.amd) { + else if (isFunction(define) && define.amd) { define(function () { return SVGInjector; });