Skip to content

Commit

Permalink
Formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
willstocks authored Feb 11, 2019
1 parent 30b4573 commit bcd37e6
Showing 1 changed file with 51 additions and 70 deletions.
121 changes: 51 additions & 70 deletions dynamicpolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@ function dynamicPolyfill(tocheck, scriptToPolyfill, functionToRunonLoad) {
var checkPromises = [];
if(Array.isArray(tocheck)) {
tocheck.forEach(
function(tocheck) {
checkPromises.push(checking(tocheck))
}
)
function(tocheck) {
checkPromises.push(checking(tocheck))
}
)
} else {
checkPromises.push(checking(tocheck))
}
Promise.all(checkPromises)
.then(
function() {
loadMyScript(scriptToPolyfill, functionToRunonLoad)
}
)
.catch(
function(error) {
return error
}
)
Promise.all(checkPromises).then(
function() {
loadMyScript(scriptToPolyfill, functionToRunonLoad)
}
).catch(function(error){return error})
}

function checking(check) {
Expand All @@ -34,88 +28,75 @@ function checking(check) {

function loadPolyfill(url) {
return new Promise(
function(resolve, reject) {
var polyfill = document.createElement('script');
polyfill.src = ('https://polyfill.io/v3/polyfill.min.js?features=' + encodeURIComponent(url));
document.body.appendChild(polyfill);
polyfill.onerror = function(response) {
return reject("Loading the polyfill(s) failed!", response)
}
polyfill.onload = function() {
return resolve
}
}
)
function(resolve, reject) {
var polyfill = document.createElement('script');
polyfill.src = ('https://polyfill.io/v3/polyfill.min.js?features=' + encodeURIComponent(url));
document.body.appendChild(polyfill);
polyfill.onerror = function(response) {
return reject("Loading the polyfill(s) failed!", response)
}
polyfill.onload = function() {
return resolve
}
}
)
}

function loadMyScript(url, functionToRunonLoad) {
if(Array.isArray(url)) {
var promises = [];
url.forEach(
function(url) {
function(url) {
promises.push(nonblankURL(url))
}
);
Promise.all(promises)
.then(
function() {
initialiseMyScript(functionToRunonLoad)
}
)
.catch(
function(error) {
return error
}
)
}
else if (!Array.isArray(url) && url !== null && url !== '') {
nonblankURL(url)
.then(
function() {
initialiseMyScript(functionToRunonLoad)
}
)
.catch(
function(error) {
return error
}
)
);
Promise.all(promises).then(
function() {
initialiseMyScript(functionToRunonLoad)
}
).catch(function(error){return error})
} else if (!Array.isArray(url) && url !== null && url !== '') {
nonblankURL(url).then(
function() {
initialiseMyScript(functionToRunonLoad)
}
).catch(function(error){return error})
} else {
initialiseMyScript(functionToRunonLoad)
}
}

function nonblankURL(uri) {
return new Promise(
function(resolve, reject) {
var thescript = document.createElement('script');
thescript.src = encodeURI(uri);
document.body.appendChild(thescript);
thescript.onerror = function(response) {
return reject("Loading the script failed!", response)
}
thescript.onload = function() {
return resolve(uri)
}
}
)
function(resolve, reject) {
var thescript = document.createElement('script');
thescript.src = encodeURI(uri);
document.body.appendChild(thescript);
thescript.onerror = function(response) {
return reject("Loading the script failed!", response)
}
thescript.onload = function() {
return resolve(uri)
}
}
)
}

function initialiseMyScript(functionToRunonLoad) {
if(Array.isArray(functionToRunonLoad)) {
functionToRunonLoad.forEach(
function(functionToRunonLoad) {
initScript(functionToRunonLoad)
}
)
function(functionToRunonLoad) {
initScript(functionToRunonLoad)
}
)
} else {
initScript(functionToRunonLoad)
}
function initScript(fn) {
try {
window[fn]
}
catch (err) {
catch(err) {
console.error('There was an error: ', err, err.name, err.stack)
}
}
Expand Down

0 comments on commit bcd37e6

Please sign in to comment.