From c99bf6d00a275e75d4f758e3943c4e4eb04ca570 Mon Sep 17 00:00:00 2001 From: Geoff Doty Date: Fri, 1 Jan 2021 22:34:37 -0500 Subject: [PATCH] never refactor just before pushing --- dist/anchor.js | 37 ++++++++------------- dist/anchor.min.js | 2 +- dist/anchor.umd.js | 38 ++++++++-------------- dist/anchor.umd.min.js | 2 +- src/index.js | 74 ++++++++++++++++++------------------------ 5 files changed, 61 insertions(+), 92 deletions(-) diff --git a/dist/anchor.js b/dist/anchor.js index d7285ec..2baa78c 100644 --- a/dist/anchor.js +++ b/dist/anchor.js @@ -1,38 +1,29 @@ +function Anchor(fasten = {}, mixins = []) { + const NAMESPACE = `_${Math.random().toString(36).slice(-6)}_`.toUpperCase(); -class Anchor { - constructor(fasten = {}, mixins = [], debug = false) { - this.namespace = `_${Math.random().toString(36).slice(-6)}_`.toUpperCase(); + const GLOBAL = window || global || {}; - this.global = window || global; - - return this.build(fasten, mixins, debug); - } - - register(key, value) { - if (this.global[this.namespace] && !this.global[this.namespace][key]) { - return this.global[this.namespace][key] = value; + const register = function register(key, value) { + if (GLOBAL[NAMESPACE] && !GLOBAL[NAMESPACE][key]) { + return GLOBAL[NAMESPACE][key] = value; } return false; - } + }.bind(GLOBAL[NAMESPACE]); - build(fasten, merges, debug) { - if (this.global[this.namespace]) { + function build(fasten, merges, register) { + if (GLOBAL[NAMESPACE]) { return false; } - this.global[this.namespace] = fasten; + GLOBAL[NAMESPACE] = fasten; + GLOBAL[NAMESPACE]['register'] = register; - merges.forEach((merge) => Object.assign(this.global[this.namespace], merge)); + merges.forEach((merge) => Object.assign(GLOBAL[NAMESPACE], merge)); - this.register('register', this.register); - - if (!!debug) { - console.log('ANCHOR:FASTEN', this.namespace, this.global[this.namespace]); - } - - return this.global[this.namespace]; + return GLOBAL[NAMESPACE]; } + return build(fasten, mixins, register); } export default Anchor; diff --git a/dist/anchor.min.js b/dist/anchor.min.js index f1b4a12..b64e723 100644 --- a/dist/anchor.min.js +++ b/dist/anchor.min.js @@ -1 +1 @@ -class Anchor{constructor(s={},e=[],t=!1){return this.namespace=`_${Math.random().toString(36).slice(-6)}_`.toUpperCase(),this.global=window||global,this.build(s,e,t)}register(s,e){return!(!this.global[this.namespace]||this.global[this.namespace][s])&&(this.global[this.namespace][s]=e)}build(s,e,t){return!this.global[this.namespace]&&(this.global[this.namespace]=s,e.forEach(s=>Object.assign(this.global[this.namespace],s)),this.register("register",this.register),t&&console.log("ANCHOR:FASTEN",this.namespace,this.global[this.namespace]),this.global[this.namespace])}}export default Anchor; \ No newline at end of file +function Anchor(n={},r=[]){const t=`_${Math.random().toString(36).slice(-6)}_`.toUpperCase(),o=window||global||{};return function(n,r,e){return!o[t]&&(o[t]=n,o[t].register=e,r.forEach(n=>Object.assign(o[t],n)),o[t])}(n,r,function(n,r){return!(!o[t]||o[t][n])&&(o[t][n]=r)}.bind(o[t]))}export default Anchor; \ No newline at end of file diff --git a/dist/anchor.umd.js b/dist/anchor.umd.js index fd32ff6..166f29a 100644 --- a/dist/anchor.umd.js +++ b/dist/anchor.umd.js @@ -4,42 +4,32 @@ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.umd = factory()); }(this, (function () { 'use strict'; + function Anchor(fasten = {}, mixins = []) { + const NAMESPACE = `_${Math.random().toString(36).slice(-6)}_`.toUpperCase(); + const GLOBAL = window || global || {}; - class Anchor { - constructor(fasten = {}, mixins = [], debug = false) { - this.namespace = `_${Math.random().toString(36).slice(-6)}_`.toUpperCase(); - - this.global = window || global; - - return this.build(fasten, mixins, debug); - } - - register(key, value) { - if (this.global[this.namespace] && !this.global[this.namespace][key]) { - return this.global[this.namespace][key] = value; + const register = function register(key, value) { + if (GLOBAL[NAMESPACE] && !GLOBAL[NAMESPACE][key]) { + return GLOBAL[NAMESPACE][key] = value; } return false; - } + }.bind(GLOBAL[NAMESPACE]); - build(fasten, merges, debug) { - if (this.global[this.namespace]) { + function build(fasten, merges, register) { + if (GLOBAL[NAMESPACE]) { return false; } - this.global[this.namespace] = fasten; + GLOBAL[NAMESPACE] = fasten; + GLOBAL[NAMESPACE]['register'] = register; - merges.forEach((merge) => Object.assign(this.global[this.namespace], merge)); + merges.forEach((merge) => Object.assign(GLOBAL[NAMESPACE], merge)); - this.register('register', this.register); - - if (!!debug) { - console.log('ANCHOR:FASTEN', this.namespace, this.global[this.namespace]); - } - - return this.global[this.namespace]; + return GLOBAL[NAMESPACE]; } + return build(fasten, mixins, register); } return Anchor; diff --git a/dist/anchor.umd.min.js b/dist/anchor.umd.min.js index cadeb6c..15b93f5 100644 --- a/dist/anchor.umd.min.js +++ b/dist/anchor.umd.min.js @@ -1 +1 @@ -!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define("umd",s):(e="undefined"!=typeof globalThis?globalThis:e||self).umd=s()}(this,function(){"use strict";return class{constructor(e={},s=[],t=!1){return this.namespace=`_${Math.random().toString(36).slice(-6)}_`.toUpperCase(),this.global=window||global,this.build(e,s,t)}register(e,s){return!(!this.global[this.namespace]||this.global[this.namespace][e])&&(this.global[this.namespace][e]=s)}build(e,s,t){return!this.global[this.namespace]&&(this.global[this.namespace]=e,s.forEach(e=>Object.assign(this.global[this.namespace],e)),this.register("register",this.register),t&&console.log("ANCHOR:FASTEN",this.namespace,this.global[this.namespace]),this.global[this.namespace])}}}); \ No newline at end of file +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define("umd",n):(e="undefined"!=typeof globalThis?globalThis:e||self).umd=n()}(this,function(){"use strict";return function(e={},n=[]){const t=`_${Math.random().toString(36).slice(-6)}_`.toUpperCase(),o=window||global||{};return function(e,n,i){return!o[t]&&(o[t]=e,o[t].register=i,n.forEach(e=>Object.assign(o[t],e)),o[t])}(e,n,function(e,n){return!(!o[t]||o[t][e])&&(o[t][e]=n)}.bind(o[t]))}}); \ No newline at end of file diff --git a/src/index.js b/src/index.js index 4e45dcd..1108213 100644 --- a/src/index.js +++ b/src/index.js @@ -1,65 +1,53 @@ -/* {Module} v{version} | MIT | https:// */ - /** * Tiny global application registry */ -class Anchor { - constructor(fasten = {}, mixins = [], debug = false) { - // create a unique namespace to avoid collisions - this.namespace = `_${Math.random().toString(36).slice(-6)}_`.toUpperCase(); +function Anchor(fasten = {}, mixins = []) { + // create a unique namespace to avoid collisions + const NAMESPACE = `_${Math.random().toString(36).slice(-6)}_`.toUpperCase(); - // find environments global object - this.global = window || global; - - // builds anchor instance - return this.build(fasten, mixins, debug); - } + // find environments global object + const GLOBAL = window || global || {}; /** - * Register - * - * Helper to extend functionality without overriding existing - * - * @param {String} key - * @param {Any} value - */ - register(key, value) { - if (this.global[this.namespace] && !this.global[this.namespace][key]) { - return this.global[this.namespace][key] = value; + * Register + * + * Helper to extend functionality without overriding existing + * + * @param {String} key + * @param {Any} value + */ + const register = function register(key, value) { + if (GLOBAL[NAMESPACE] && !GLOBAL[NAMESPACE][key]) { + return GLOBAL[NAMESPACE][key] = value; } return false; - } + }.bind(GLOBAL[NAMESPACE]); /** - * Builds the Application Anchor - * - * @param {Object} fasten - * @param {Array} merges - * @param {Boolean} debug - */ - build(fasten, merges, debug) { - if (this.global[this.namespace]) { + * Builds the Application Anchor + * + * @param {Object} fasten + * @param {Array} merges + * @param {Boolean} debug + */ + function build(fasten, merges, register) { + if (GLOBAL[NAMESPACE]) { // already initialized return false; } // add to global instance - this.global[this.namespace] = fasten; + GLOBAL[NAMESPACE] = fasten; + GLOBAL[NAMESPACE]['register'] = register; // merge with global instance - merges.forEach((merge) => Object.assign(this.global[this.namespace], merge)); + merges.forEach((merge) => Object.assign(GLOBAL[NAMESPACE], merge)); - // app global utils - this.register('register', this.register); + return GLOBAL[NAMESPACE]; + }; - if (debug) { - // eslint-disable-next-line - console.log('ANCHOR:FASTEN', this.namespace, this.global[this.namespace]); - } - - return this.global[this.namespace]; - } -} + return build(fasten, mixins, register); +}; export default Anchor;