From 6fb509a021ba1846efcf2561bdbce0cf2c4b0501 Mon Sep 17 00:00:00 2001 From: Behind The Math Date: Mon, 29 Jan 2018 23:20:00 -0500 Subject: [PATCH] Code cleanup --- index.js | 7 +++---- lib/clone.js | 2 +- lib/eval-script.js | 6 +++--- lib/proto/attach-form.js | 4 ++-- lib/proto/attach-link.js | 2 +- lib/proto/parse-options.js | 1 + lib/send-request.js | 2 +- lib/switches.js | 2 +- tests/lib/abort-request.js | 4 ++-- tests/lib/proto/attach-form.js | 7 +++---- tests/lib/proto/attach-link.js | 7 +++---- tests/lib/send-request.js | 8 ++++---- 12 files changed, 25 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index 64b933a..61bf465 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,7 @@ var Pjax = function(options) { } var parseOptions = require("./lib/proto/parse-options.js"); - parseOptions.apply(this,[options]) + parseOptions.call(this,options) this.log("Pjax options", this.options) if (this.options.scrollRestoration && "scrollRestoration" in history) { @@ -168,7 +168,7 @@ Pjax.prototype = { this.request = this.doRequest(href, options.requestOptions, function(html, request) { // Fail if unable to load HTML via AJAX if (html === false) { - trigger(document,"pjax:complete pjax:error", options) + trigger(document, "pjax:complete pjax:error", options) return } @@ -218,8 +218,7 @@ Pjax.prototype = { if (console && console.error) { console.error("Pjax switch fail: ", e) } - this.latestChance(href) - return + return this.latestChance(href) } else { throw e diff --git a/lib/clone.js b/lib/clone.js index a6b9ae6..674baa3 100644 --- a/lib/clone.js +++ b/lib/clone.js @@ -1,5 +1,5 @@ module.exports = function(obj) { - if (null === obj || "object" != typeof obj) { + if (null === obj || "object" !== typeof obj) { return obj } var copy = obj.constructor() diff --git a/lib/eval-script.js b/lib/eval-script.js index 382e43e..3e9f516 100644 --- a/lib/eval-script.js +++ b/lib/eval-script.js @@ -13,12 +13,12 @@ module.exports = function(el) { script.type = "text/javascript" - if (src != "") { + if (src !== "") { script.src = src; script.async = false; // force synchronous loading of peripheral js } - if (code != "") { + if (code !== "") { try { script.appendChild(document.createTextNode(code)) } @@ -31,7 +31,7 @@ module.exports = function(el) { // execute parent.appendChild(script); // avoid pollution only in head or body tags - if (["head","body"].indexOf(parent.tagName.toLowerCase()) > 0) { + if (["head", "body"].indexOf(parent.tagName.toLowerCase()) > 0) { parent.removeChild(script) } diff --git a/lib/proto/attach-form.js b/lib/proto/attach-form.js index f6775a2..5ec846e 100644 --- a/lib/proto/attach-form.js +++ b/lib/proto/attach-form.js @@ -13,7 +13,7 @@ var formAction = function(el, event) { // Initialize requestOptions options.requestOptions = { requestUrl: el.getAttribute("action") || window.location.href, - requestMethod: el.getAttribute("method") || "GET", + requestMethod: el.getAttribute("method") || "GET" } // create a testable virtual link of the form action @@ -93,7 +93,7 @@ module.exports = function(el) { } - if (event.keyCode == 13) { + if (event.keyCode === 13) { formAction.call(that, el, event) } }.bind(this)) diff --git a/lib/proto/attach-link.js b/lib/proto/attach-link.js index 69945bc..b0af05c 100644 --- a/lib/proto/attach-link.js +++ b/lib/proto/attach-link.js @@ -91,7 +91,7 @@ module.exports = function(el) { return } - if (event.keyCode == 13) { + if (event.keyCode === 13) { linkAction.call(that, el, event) } }.bind(this)) diff --git a/lib/proto/parse-options.js b/lib/proto/parse-options.js index 358b8ff..6c35d0d 100644 --- a/lib/proto/parse-options.js +++ b/lib/proto/parse-options.js @@ -3,6 +3,7 @@ var defaultSwitches = require("../switches") module.exports = function(options) { + options = options || {} options.elements = options.elements || "a[href], form[action]" options.selectors = options.selectors || ["title", ".js-Pjax"] options.switches = options.switches || {} diff --git a/lib/send-request.js b/lib/send-request.js index 9a4e5b8..a474e05 100644 --- a/lib/send-request.js +++ b/lib/send-request.js @@ -35,7 +35,7 @@ module.exports = function(location, options, callback) { request.setRequestHeader("X-PJAX", "true") // Add the request payload if available - if (options.requestPayloadString != undefined && options.requestPayloadString != "") { + if (options.requestPayloadString !== undefined && options.requestPayloadString !== "") { // Send the proper header information along with the request request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); } diff --git a/lib/switches.js b/lib/switches.js index 405d6bf..51daf6b 100644 --- a/lib/switches.js +++ b/lib/switches.js @@ -40,7 +40,7 @@ module.exports = { var animationEventNames = "animationend webkitAnimationEnd MSAnimationEnd oanimationend" var animatedElsNumber = 0 var sexyAnimationEnd = function(e) { - if (e.target != e.currentTarget) { + if (e.target !== e.currentTarget) { // end triggered by an animation on a child return } diff --git a/tests/lib/abort-request.js b/tests/lib/abort-request.js index 8fa257a..7661de0 100644 --- a/tests/lib/abort-request.js +++ b/tests/lib/abort-request.js @@ -18,8 +18,8 @@ if (!("responseURL" in XMLHttpRequest.prototype)) { tape("test aborting xhr request", function(t) { var requestCacheBust = sendRequest.bind({ options: { - cacheBust: true, - }, + cacheBust: true + } }) t.test("- pending request is aborted", function(t) { diff --git a/tests/lib/proto/attach-form.js b/tests/lib/proto/attach-form.js index ab89543..86ecdbc 100644 --- a/tests/lib/proto/attach-form.js +++ b/tests/lib/proto/attach-form.js @@ -80,12 +80,11 @@ tape("test attach form preventDefaulted events", function(t) { tape("test options are not modified by attachForm", function(t) { var form = document.createElement("form") var options = {foo: "bar"} - var loadUrl = () => {} + var loadUrl = function() {} - attachForm.call({options, loadUrl}, form) + attachForm.call({options: options, loadUrl: loadUrl}, form) - var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search - form.action = internalUri + form.action = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search form.method = "GET" trigger(form, "submit") diff --git a/tests/lib/proto/attach-link.js b/tests/lib/proto/attach-link.js index fbaae4e..3a5141c 100644 --- a/tests/lib/proto/attach-link.js +++ b/tests/lib/proto/attach-link.js @@ -79,12 +79,11 @@ tape("test attach link preventDefaulted events", function(t) { tape("test options are not modified by attachLink", function(t) { var a = document.createElement("a") var options = {foo: "bar"} - var loadUrl = () => {}; + var loadUrl = function() {}; - attachLink.call({options, loadUrl}, a) + attachLink.call({options: options, loadUrl: loadUrl}, a) - var internalUri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search - a.href = internalUri + a.href = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search trigger(a, "click") diff --git a/tests/lib/send-request.js b/tests/lib/send-request.js index f33b28c..cbc3b36 100644 --- a/tests/lib/send-request.js +++ b/tests/lib/send-request.js @@ -20,8 +20,8 @@ tape("test xhr request", function(t) { t.test("- request is made, gets a result, and is cache-busted", function(t) { var requestCacheBust = sendRequest.bind({ options: { - cacheBust: true, - }, + cacheBust: true + } }); var r = requestCacheBust(url, {}, function(result) { t.equal(r.responseURL.indexOf("?"), url.length, "XHR URL is cache-busted when configured to be") @@ -38,8 +38,8 @@ tape("test xhr request", function(t) { t.test("- request is not cache-busted when configured not to be", function(t) { var requestNoCacheBust = sendRequest.bind({ options: { - cacheBust: false, - }, + cacheBust: false + } }); var r = requestNoCacheBust(url, {}, function() { t.equal(r.responseURL, url, "XHR URL is left untouched")