{"_id":"@4i4/registry","name":"@4i4/registry","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@4i4/registry","version":"1.0.0","description":"Simple Registry class","author":{"name":"Petyo Stoyanov","email":"petyosv@gmail.com"},"repository":{"type":"git","url":"git+https://gitlab.com/4i4/registry.git"},"bugs":{"url":"https://gitlab.com/4i4/registry/-/issues"},"license":"MIT","main":"./dist/index.js","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.js","import":"./dist/index.mjs","types":"./dist/index.d.ts"}},"scripts":{"build":"rollup -c","start":"rollup -c -w","lint":"eslint --fix --ext .ts ./src","format":"prettier --write './**/*.ts' --config ./.prettierrc"},"devDependencies":{"@rollup/plugin-typescript":"^9.0.2","@typescript-eslint/eslint-plugin":"^5.41.0","@typescript-eslint/parser":"^5.41.0","eslint":"^8.26.0","eslint-config-prettier":"^8.5.0","eslint-plugin-prettier":"^4.2.1","prettier":"^2.7.1","rollup":"^3.2.3","rollup-plugin-dts":"^5.0.0","rollup-plugin-terser":"^7.0.2","tslib":"^2.4.0","typescript":"^4.8.4"},"gitHead":"868a8821c2b14a3e90d820641735f86633019116","homepage":"https://gitlab.com/4i4/registry#readme","_id":"@4i4/registry@1.0.0","_nodeVersion":"14.18.3","_npmVersion":"6.14.15","dist":{"integrity":"sha512-GKyHjX7ummRIeoCuWww/7Cl6trt3wz3kgRcT8fdgdIwz6uUaxf4+3UZVgvBWQE6/cqFj1z2EuAA4QAUKuT3/Jw==","shasum":"8073777742d89b775a47bdfe1714163f2372b69b","tarball":"https://registry.npmjs.org/@4i4/registry/-/registry-1.0.0.tgz","fileCount":7,"unpackedSize":19152,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD9RujcsB2rBNEhd3LlFjq3BIeyeAmdFSaDgv7jUwqH9wIgL3DcepngG+bLc3PP5NYM0gwyFDbsgVPH+TTbL+SM6W0="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjWPlGACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoERhAAmuF8cUYi406vYkL+ZWGcJpAy8JemFzL8j/rarW5oH7ocbq8x\r\nn8LZQTLRrBLgCFRixFTHDqCgNO8ab8UbfK7Eeqc3L3x2gF6ZLli/b+O7a/Qd\r\nJe0KKRG2QsFcSBeVlxTphSwejuoAbSFuItgicFREiJ7/X9g9nYZ6Mks/rK1G\r\nlSkaB2ZiwSxIMnOob5Sps31+WBFeVNIGbK0tnGPJ7LzSEdN4eDuv0MuCUDUl\r\neIAQbO7u+0puJ1J7aeRetLurbfXQkpDISUYoS+d6UeGWpwWkQ8t1Lpp10p9k\r\nBbErk0YFD3jArKU005vQ5o3apmcVbGE9zyXCCs+c4Wy27QqbtR7Fa+ULdLh0\r\no6Ov53aqUKJ0BzIZM8BSeoHkgdEugJ0vg9xnx+zSJR72zggzZ5TtPmb4YASD\r\n6mn18J9xUMdBSetqy1PNZ3sU2RhzPaxks8oqVqrCKO2d1fVc/2QllnsP9QnV\r\nIgVQjwm8W2mImQFt8WIB4EanMk1JtRDgA5nhRrNkCUc/p/9YJHDg4R8Gahda\r\nP6EaB4nalpMRjBVnvMWnDGZWpnXuPBPvBmx+Pdnb7G9lbSU5dePLyYWqfQXO\r\nsoihz0AROQG5nG0CYNxMhzYT4ldZsDnLWUk6Y5D+U6m10rfb44QLdaERIMnR\r\nVlFY5P8yYmPE5UanEocHn2r5euFIAts9bmQ=\r\n=kiCv\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"petyosv","email":"petyosv@gmail.com"},"directories":{},"maintainers":[{"name":"petyosv","email":"petyosv@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/registry_1.0.0_1666775366605_0.7122348891521577"},"_hasShrinkwrap":false}},"time":{"created":"2022-10-26T09:09:26.604Z","1.0.0":"2022-10-26T09:09:26.822Z","modified":"2022-10-26T09:09:27.177Z"},"maintainers":[{"name":"petyosv","email":"petyosv@gmail.com"}],"description":"Simple Registry class","homepage":"https://gitlab.com/4i4/registry#readme","repository":{"type":"git","url":"git+https://gitlab.com/4i4/registry.git"},"author":{"name":"Petyo Stoyanov","email":"petyosv@gmail.com"},"bugs":{"url":"https://gitlab.com/4i4/registry/-/issues"},"license":"MIT","readme":"# Registry\n\nYou can create your own registry of any kind, separate the items in the registry by scopes, and search by multiple suggestions.\n\n## Why not use a simple object?\n\nIn the end this registry is just normal object that contains key:value pairs separated by scopes.\n\n#### The need to search by multiple suggestions.\nThis **\"tool\"** was created by the needs of a project that was storing all rendable components that has as templates (paths to the components) in a single object.\nEach rendable item of the project can use multiple templates depending on a context and if a template is missing for the special context it can use the closest or a default one.\\\nSo the suggestion search was created as function. \n\n#### The need for better maintenance.\nWith the extending the project the templates has multiply and the object was getting bigger and difficult to maintain.\nTo skip duplication of the template names we introduced one more level of the object - we separated the templates by scopes.\nThat made the object event longer. We needed a way to update the object as easy as can be and to keep it in order.\\\nSo the set function was created.\n\n#### The need for consistency.\nWith one successful usage of this template registry the requests for new projects with same functionality was growing.\nNow we needed to maintain not only one object in a single project, but multiple objects in multiple projects.\nUpdating a single project with a new functionality or fixing a bug for it meant that we need to go through all the projects.\\\nSo the registry **\"tool\"** was in order to be created.\\\nAnd once we had it, we found multiple cases outside the template registry that we could to use it.\n\n## Pros & Cons\n\n**Pros:**\n - search by multiple suggestions\n - easy to maintain big objects\n - easy to separate items by scopes\n\n**Cons:**\n - one more library to install\n\n## Installing:\n\nUsing npm:\n```shell\n$ npm install @4i4/registry\n```\n\nUsing yarn:\n```shell\n$ yarn add @4i4/registry\n```\n\nUsing pnpm:\n```shell\n$ pnpm add @4i4/registry\n```\n\n## Methods:\n\n### Constructor\n\n**Params**\n\n| **Param**       | **Type** | **Optional** | **Default value** | **Description**                          |\n|-----------------|----------|--------------|-------------------|------------------------------------------|\n| `data`          | `object` | Yes          | {}                | Predefined registry data.                |\n| `default_scope` | `string` | Yes          | _                 | The default scope. Default value is `_`. |\n\n**Example**\n```javascript\nconst data = {\n  // Each scope is an object of key:value pairs\n  \"scope_1\": {\n    \"key_1\": \"value_1\",\n    \"key_2\": \"value_2\"\n  },\n  \"scope_2\": {\n    \"key_3\": \"value_3\",\n    \"key_4\": \"value_4\"\n  }\n};\n\nconst registry_1 = new Registry();\nconst registry_2 = new Registry(data);\nconst registry_3 = new Registry(data, \"scope_1\");\n```\n\n### .set\n**Params**\n\n| **Param** | **Type** | **Optional** | **Description**                                                |\n|-----------|----------|--------------|----------------------------------------------------------------|\n| `key`     | `string` | No           | The key for the item.                                          |\n| `item`    | `any`    | No           | The item it self.                                              |\n| `scope`   | `string` | Yes          | The scope in which this pair of key:value will be assigned to. |\n\n**Example**\n```javascript\nregistry.set(\"key_1\", \"value_1\");\nregistry.set(\"key_2\", \"value_2\", \"scope_1\");\n```\n\n### .get\n**Params**\n\n| **Param**       | **Type** | **Optional** | **Description**                                    |\n|-----------------|----------|--------------|----------------------------------------------------|\n| `key`           | `string` | No           | The key for the item.                              |\n| `default_value` | `any`    | Yes          | Return this value if the item is missing.          |\n| `scope`         | `string` | Yes          | The scope in which this key will be retrieve from. |\n\n**Example**\n```javascript\nconst key_1 = registry.get(\"key_1\");\nconst key_2 = registry.get(\"key_2\", \"default_value_2\");\nconst key_3 = registry.get(\"key_3\", null, \"scope_1\");\n```\n\n### .getAll\n**Params**\n\n| **Param**       | **Type** | **Optional** | **Description**                                                                               |\n|-----------------|----------|--------------|-----------------------------------------------------------------------------------------------|\n| `scope`         | `string` | Yes          | Retrieve all items from this scope. If non provided the whole registry data will be returned. |\n\n**Example**\n```javascript\nconst registry = new Registry({\n  \"scope_1\": {\n    \"key_1\": \"value_1\",\n    \"key_2\": \"value_2\"\n  },\n  \"scope_2\": {\n    \"key_3\": \"value_3\",\n    \"key_4\": \"value_4\"\n  }\n});\n\nconst scope_1 = registry.getAll(\"scope_1\");\nconst data = registry.getAll();\n\n// This will print { \"key_1\": \"value_1\", \"key_2\": \"value_2\" }.\nconsole.log(scope_1);\n// This will print { \"scope_1\": { \"key_1\": \"value_1\", \"key_2\": \"value_2\" }, \"scope_2\": { \"key_3\": \"value_3\", \"key_4\": \"value_4\" }}.\nconsole.log(data);\n```\n\n### .clone\n\n**Example**\n```javascript\nconst registry_1 = new Registry();\nconst registry_2 = registry_1.clone();\n```\n\n## Typescript:\nSimple:\n```typescript\nimport Registry from \"@4i4/registry\";\n\nconst registry = new Registry<string | number | boolean>();\n\nregistry.set(\"string_value\", \"This is example of string\");\nregistry.set(\"number_value\", 300);\nregistry.set(\"boolean_value\", false);\n```\nUse predefined data:\n```typescript\nimport Registry, { RegistryType } from \"@4i4/registry\";\n\nconst data: RegistryType<string | number | boolean> = {\n  \"scope_1\": {\n    \"string_value\": \"This is example of string\",\n    \"number_value\": 300,\n    \"boolean_value\": false\n  }\n}\n\nconst registry = new Registry<string | number | boolean>(data);\n```\n\n## Examples:\nSimple:\n```javascript\nimport Registry from \"@4i4/registry\";\n\nconst settings = new Registry();\n\nsettings.set(\"site_name\", \"This is the name of the site\");\nsettings.set(\"homepage\", \"/home\");\nsettings.set(\"languages\", { \"en\": \"English\", \"de\": \"Deutsch\" });\n\nconst siteName = settings.get(\"site_name\");\nconst languages = settings.get(\"languages\");\n```\n\nUsing the scopes:\n```javascript\nimport Registry from \"@4i4/registry\";\n\nconst settings = new Registry();\n\n// This setting is part of the default scope.\nsettings.set(\"cache_enabled\", false);\n// Next two settings are part of scope \"site_info\".\nsettings.set(\"site_name\", \"This is the name of the site\", \"site_info\");\nsettings.set(\"homepage\", \"/home\", \"site_info\");\n// Next two settings are part of scope \"languages\".\nsettings.set(\"en\", \"English\", \"languages\");\nsettings.set(\"de\", \"Deutsch\", \"languages\");\n\n// Retrieve setting from the default scope.\nconst isCacheEnabled = settings.get(\"cached_enabled\");\n// Retrieve setting from scope \"site_info\".\nconst siteName = settings.get(\"site_name\", \"\", \"site_info\");\n// Retrieve all settings from scope \"languages\".\nconst languages = settings.getAll(\"languages\");\n```\n\nUse predefined data:\n```javascript\nimport Registry from \"@4i4/registry\";\n\nconst data = {\n  \"site_info\": {\n    \"site_name\": \"This is the name of the site\",\n    \"homepage\": \"/home\"\n  },\n  \"languages\": {\n    \"en\": \"English\",\n    \"de\": \"Deutsch\"\n  }\n};\n// Creating new registry with two custom scopes - \"site_info\", \"languages\"\nconst settings = new Registry(data);\n// This setting is part of the default scope.\nsettings.set(\"cache_enabled\", false);\n\nconst siteName = settings.get(\"site_name\", \"\", \"site_info\");\n```\n\nChanging the default scope:\n```javascript\nimport Registry from \"@4i4/registry\";\n\nconst data = {\n  \"site_info\": {\n    \"site_name\": \"This is the name of the site\",\n    \"homepage\": \"/home\"\n  },\n  \"languages\": {\n    \"en\": \"English\",\n    \"de\": \"Deutsch\"\n  }\n};\n// Set the default scope to \"site_info\"\nconst settings = new Registry(data, \"site_info\");\n\n// This setting will be assinged to the default scope - \"site_info\".\nsettings.set(\"description\", \"This is the description of the site\");\n// Retrieve setting from the default scope - \"site_info\".\nconst siteName = settings.get(\"site_name\");\n```\n\nRetrieve single settings from multiple suggestions:\n```javascript\nimport Registry from \"@4i4/registry\";\n\nconst data = {\n  \"page\": {\n    \"title\": \"This is the default page title\",\n    \"title-tablet\": \"This is the tablet page title\"\n  }\n}\n// Set the default scope to \"page\"\nconst settings = new Registry(data, \"page\");\n\n// Search for one of the following settings. \nconst search = ['title', 'title-tablet', 'title-mobile'];\n// This will retrieve the last found item from the search array.\nconst pageTitle = settings.get(search);\n// This will print the tablet title. \nconsole.log(pageTitle);\n```\n\nCloning the registry:\n```javascript\nimport Registry from \"@4i4/registry\";\n\nconst data = {\n  \"site_info\": {\n    \"site_name\": \"This is the name of site 1\",\n    \"homepage\": \"/home\"\n  },\n  \"languages\": {\n    \"en\": \"English\",\n    \"de\": \"Deutsch\"\n  }\n};\n// Set the default scope to \"site_info\"\nconst site_1 = new Registry(data, \"site_info\");\nconst site_2 = site_1.clone();\n\nsite_2.set(\"site_name\", \"This is the name of site 2\");\n\nconst siteName1 = site_1.get(\"site_name\");\nconst siteName2 = site_2.get(\"site_name\");\n\n// This will print \"This is the name of site 1\"\nconsole.log(siteName1);\n// This will print \"This is the name of site 2\"\nconsole.log(siteName2);\n```\n","readmeFilename":"README.md"}