Skip to content

Instantly share code, notes, and snippets.

@esnho
Created November 20, 2021 11:35
Show Gist options
  • Save esnho/ae5e4e08b54b7027c219f90c7467a903 to your computer and use it in GitHub Desktop.
Save esnho/ae5e4e08b54b7027c219f90c7467a903 to your computer and use it in GitHub Desktop.
fxhashFeatures-helper
const called = [];
// get a feature from defined
export function getHash(hash) {
if (window.$fxhashFeatures[hash] === undefined) {
const msg = `missing feature ${hash}`;
window.alert(msg);
throw new Error(msg);
}
called.push(hash);
return window.$fxhashFeatures[hash];
}
// this method is meant to be called when you think you called all your hashes
// if you forgot to call one it will throw an error and an alert message
// it's useful if you share the base html page between more than a project
// or if you add a feature and forgot to use it
export function checkHashes() {
let availables = [];
for (const hash in window.$fxhashFeatures) {
availables.push(hash);
}
const sorting = (a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
const calledCheck = [...new Set(called)];
calledCheck.sort(sorting);
availables.sort(sorting);
if (!availables.equals(calledCheck)) {
const msg = `some features has not been called`;
window.alert(msg);
throw new Error(msg);
}
}
// Utility to check if two arrays are equals
// Warn if overriding existing method
if(Array.prototype.equals)
console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "equals", {enumerable: false});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment