Skip to content

Instantly share code, notes, and snippets.

@phoenisx
Created October 5, 2018 05:23
Show Gist options
  • Save phoenisx/821379521d672b6edf779078e88ecafd to your computer and use it in GitHub Desktop.
Save phoenisx/821379521d672b6edf779078e88ecafd to your computer and use it in GitHub Desktop.
Some Useful Compose Functions for day-to-day uses
/**
* A DOM Node Caching Compose Function, that can be used in a Classes for Old Legacy Codes
* that didn't use any UI Frameworks, like React
*
* @param {HTMLElement} ref - A parent reference that will be used to query, to fetch inner nodes
* DEFAULT: `document`
*/
nodeCache = (ref = document) => {
const node = ref;
const cache = {};
return (key) => {
if (!cache.hasOwnProperty(key)) {
const childNode = node.querySelectorAll(key);
if (childNode.length) cache[key] = childNode;
}
return cache[key] || [];
};
};
/**
* Usage Example:
*
* constructor(options) {
* this.nodeCache = nodeCache(options.node);
* this.nodeCache('.className');
* this.nodeCache('#idName');
* }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment