{"_id":"global-const","_rev":"3-71ab8a4b2dfde52f6a1f197fbb141a8d","name":"global-const","dist-tags":{"latest":"0.1.3"},"versions":{"0.1.0":{"name":"global-const","version":"0.1.0","keywords":["global","singleton","global-state","state","instance"],"author":{"name":"Chris A James","email":"chris@webglowit.net"},"license":"MIT","_id":"global-const@0.1.0","maintainers":[{"name":"chrisajames","email":"chris@webglowit.net"}],"homepage":"https://github.com/cajames/global-const","bugs":{"url":"https://github.com/cajames/global-const/issues"},"dist":{"shasum":"e4a63b48d1c508fa97db0b739f607315963e458c","tarball":"https://registry.npmjs.org/global-const/-/global-const-0.1.0.tgz","fileCount":8,"integrity":"sha512-QLqQap4H7W0dXz9x0+8jY6p7Q53UdOC1qZBXHzYoP8+qIiQYoyLTJGStnfHAHuWFWNpJaaX8iOPm/VKXkT9ZpA==","signatures":[{"sig":"MEYCIQDy6Mkx6tXI2ZXulBhd/oVOLfJEyx2p4TQt8w7jD5oUgwIhAPrDWpbaePAnHROcUdZTMH93/1ywA+L3owFFmIXTg/Yh","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":9260},"main":"dist/lib.js","types":"dist/lib.d.ts","scripts":{"test":"yarn test:node && yarn test:browser","build":"rm -r -f ./dist &&  tsc","release":"yarn build && np","test:node":"jest --coverage","test:browser":"jest --config=./jest.config.browser.js --coverage"},"wallaby":{"autoDetect":true},"_npmUser":{"name":"chrisajames","email":"chris@webglowit.net"},"description":"A lightweight utility to create global singletons (on browser and node)","directories":{},"licenseText":"MIT License\n\nCopyright (c) 2024 Chris James\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","resolutions":{},"_hasShrinkwrap":false,"devDependencies":{"np":"^9.2.0","jest":"^29.7.0","ts-jest":"^29.1.2","typescript":"^5.3.3","@types/jest":"^29.5.11","jest-environment-node":"^29.7.0","jest-environment-jsdom":"^29.7.0"},"_npmOperationalInternal":{"tmp":"tmp/global-const_0.1.0_1707282576160_0.0863460812486605","host":"s3://npm-registry-packages"}},"0.1.2":{"name":"global-const","version":"0.1.2","keywords":["global","singleton","global-state","state","instance","global function","global object","singleton function","singleton object","reference"],"author":{"name":"Chris A James","email":"chris@webglowit.net"},"license":"MIT","_id":"global-const@0.1.2","maintainers":[{"name":"chrisajames","email":"chris@webglowit.net"}],"homepage":"https://github.com/cajames/global-const","bugs":{"url":"https://github.com/cajames/global-const/issues"},"dist":{"shasum":"eab7dbe59dfcdfd71e5029e8603729bfb8453e87","tarball":"https://registry.npmjs.org/global-const/-/global-const-0.1.2.tgz","fileCount":8,"integrity":"sha512-yb8pTRSbWcdjmKhRfdB1+s7oU9UXTPPcRwd0oPal0WHta7B/3roXz7yGLMU+KhgByoeX/1QOFKY8aCTETexKAg==","signatures":[{"sig":"MEQCIEgEcZad2N8krxVCL6rtl1qWaYoXBlRRB9/j2dinc7jxAiBqgVHzqBKJaIkckwRORkKuH7pnroyLgHDTjmkvDAzeRg==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":9366},"main":"dist/lib.js","types":"dist/lib.d.ts","scripts":{"test":"yarn test:node && yarn test:browser","build":"rm -r -f ./dist &&  tsc","release":"yarn build && np","test:node":"jest --coverage","test:browser":"jest --config=./jest.config.browser.js --coverage"},"wallaby":{"autoDetect":true},"_npmUser":{"name":"chrisajames","email":"chris@webglowit.net"},"description":"A lightweight utility to create global singletons (on browser and node)","directories":{},"licenseText":"MIT License\n\nCopyright (c) 2024 Chris James\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","resolutions":{},"_hasShrinkwrap":false,"devDependencies":{"np":"^9.2.0","jest":"^29.7.0","ts-jest":"^29.1.2","typescript":"^5.3.3","@types/jest":"^29.5.11","jest-environment-node":"^29.7.0","jest-environment-jsdom":"^29.7.0"},"_npmOperationalInternal":{"tmp":"tmp/global-const_0.1.2_1707282975728_0.15277276088741543","host":"s3://npm-registry-packages"}},"0.1.3":{"name":"global-const","version":"0.1.3","description":"A lightweight utility to create global singletons (on browser and node)","keywords":["global","singleton","global-state","state","instance","global function","global object","singleton function","singleton object","reference"],"main":"dist/lib.js","types":"dist/lib.d.ts","license":"MIT","author":{"name":"Chris A James","email":"chris@webglowit.net"},"homepage":"https://github.com/cajames/global-const","bugs":{"url":"https://github.com/cajames/global-const/issues"},"scripts":{"test":"yarn test:node && yarn test:browser","test:node":"jest --coverage","test:browser":"jest --config=./jest.config.browser.js --coverage","build":"rm -r -f ./dist &&  tsc","release":"yarn build && np"},"devDependencies":{"@jest/globals":"^30.3.0","jest":"^30.3.0","jest-environment-jsdom":"^30.3.0","jest-environment-node":"^30.3.0","np":"^11.2.0","ts-jest":"^29.4.9","typescript":"^6.0.3"},"wallaby":{"autoDetect":true},"resolutions":{"tmp":"^0.2.4"},"licenseText":"MIT License\n\nCopyright (c) 2024 Chris James\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","_id":"global-const@0.1.3","dist":{"shasum":"577232b97bbdb6e5b3b3afe07eba93617258c55c","integrity":"sha512-d90bxH8xi9/h0In5M8U7/Cyhu1cTDxDRf1LXe7m7GxLO7f9jAudOxdpkoZ76DzAUm2x+w8wfzfDuYq56mhBvRg==","tarball":"https://registry.npmjs.org/global-const/-/global-const-0.1.3.tgz","fileCount":8,"unpackedSize":9391,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQC8Rcdw1W9LVwn0xFu+Zu1UxZEyQDaVc0eP313gYcHVDwIhAIqasOwfELvbQSuMxZ2ygjFGTP/QX31kiYrUKGOQ957q"}]},"_npmUser":{"name":"chrisajames","email":"chris@webglowit.net"},"directories":{},"maintainers":[{"name":"chrisajames","email":"chris@webglowit.net"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/global-const_0.1.3_1777365847149_0.45974035430467364"},"_hasShrinkwrap":false}},"time":{"created":"2024-02-07T05:09:36.159Z","modified":"2026-04-28T08:44:07.402Z","0.1.0":"2024-02-07T05:09:36.318Z","0.1.2":"2024-02-07T05:16:15.909Z","0.1.3":"2026-04-28T08:44:07.309Z"},"bugs":{"url":"https://github.com/cajames/global-const/issues"},"author":{"name":"Chris A James","email":"chris@webglowit.net"},"license":"MIT","homepage":"https://github.com/cajames/global-const","keywords":["global","singleton","global-state","state","instance","global function","global object","singleton function","singleton object","reference"],"description":"A lightweight utility to create global singletons (on browser and node)","maintainers":[{"name":"chrisajames","email":"chris@webglowit.net"}],"readme":"![Build](https://github.com/cajames/global-const/workflows/Build%20and%20Test/badge.svg)[![Coverage Status](https://coveralls.io/repos/github/cajames/global-const/badge.svg?branch=main)](https://coveralls.io/github/cajames/global-const?branch=main)\n\n> A lightweight utility to create global singletons (on browser and node)\n\n> [!WARNING]\n> Attaching items on globals should be a last resort, so only do this if you must... But sometimes you've got to do what you've got to do, and for that, this package exists.\n\n## Basic features\n\n- Works in Node and the browser\n- Namespace in global object to prevent collision if multiple projects use this.\n- Globally reference any kind of variable, object/function\n- TypeScript support\n\n## Installation\n\nTo install `global-const`, use npm or yarn as follows:\n\n```bash\nnpm install global-const\n# or\nyarn add global-const\n```\n\n## Usage\n\n```ts\nimport { getGlobalisedValue, clearGlobalNamespace } from \"global-const\";\n\nconst targetFunction = (someParams) => {\n  // some function that maintains state, or works\n  // within a closure.\n};\n\nconst singletonInstance = getGlobalisedValue(\n  \"namespace\",\n  \"targetFunction\",\n  targetFunction\n);\n\n// ...\nsingletonInstance(\"...\");\n\n// ... somewhere else in code\n\n// this will get the same function instance as above\nconst anotherInstance = getGlobalisedValue(\n  \"namespace\",\n  \"targetFunction\",\n  targetFunction\n);\n\nexpect(singletonInstance).toBe(anotherInstance); // true\n```\n\n## Why would you need this library?\n\nWhile it's generally not advised to add code to the global-state (`window`/`globalThis`), you might need to. This library comes handle to manage this.\n\nAnd example is when you have many packages, each using a \"metrics\" module. When a customer individually instantiates each module, each module will probably instantiate it's own metrics tracking module. However if you wish for all the features to use the same metrics tracking function, instead of being siloed, this feature will help ensure that each time the metrics functions are instantiated, they'll check if there's one that's been globalised, and if so return that one instead.\n\n> [!CAUTION]\n> This assumes that each instantiation is the same as the other, so be careful to ensure that your customers aren't likely to mix different versions of the function.\n\n## API Reference\n\n### `getGlobalisedValue(namespace, key, value)`\n\n- `namespace`: A unique string identifier to prevent collisions when considering other packages using this module.\n- `key`: The name of the value to retrieve or set.\n- `value`: An object/function to be globalised if it doesn't already exist.\n\nReturns the globalised value or stores the value passed in if not initialised, and returns it.\n\n```ts\nconst logger = getGlobalisedValue(\"myApp\", \"logger\", new Logger());\n```\n\n### `clearGlobalNamespace(namespace)`\n\n- `namespace`: A unique string identifier to prevent collisions when considering other packages using this module.\n\nThis function is used for an application to clear out it's namespace.\n","readmeFilename":"readme.md"}