{"id":15731679,"url":"https://github.com/tuchk4/base-storage","last_synced_at":"2025-03-31T02:52:37.070Z","repository":{"id":74997898,"uuid":"50419761","full_name":"tuchk4/base-storage","owner":"tuchk4","description":" base-storage component. Provides easy way to get / set and check the existence of a paths in the objects","archived":false,"fork":false,"pushed_at":"2017-07-24T10:00:35.000Z","size":6,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-09T08:18:46.559Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tuchk4.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-26T09:54:18.000Z","updated_at":"2021-08-19T15:10:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"c220be52-a7fb-4cde-8952-c1cc170e27b2","html_url":"https://github.com/tuchk4/base-storage","commit_stats":{"total_commits":14,"total_committers":1,"mean_commits":14.0,"dds":0.0,"last_synced_commit":"50e6f7a2a6d5c726d667961f8bb6b5dc99c17cf1"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fbase-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fbase-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fbase-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fbase-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuchk4","download_url":"https://codeload.github.com/tuchk4/base-storage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246408104,"owners_count":20772230,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-04T00:03:51.147Z","updated_at":"2025-03-31T02:52:37.032Z","avatar_url":"https://github.com/tuchk4.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Base storage\n\nProvides easy way to get / set and check the existence of a paths in the objects\n\n`npm install --save base-storage`\n\n### How to use\n```js\nimport BaseStorage from 'base-storage';\n\n\nconst storage = new BaseStorage({\n  a: {\n    b: {\n      c: {\n        d: 1,\n        e: 2\n      }\n    }\n  },\n  f: 2\n});\n\n// has method\nexpect(storage.has('a')).to.be.equal(true);\nexpect(storage.has('A')).to.be.equal(false);\n\n// get method\nexpect(storage.get('f')).to.be.equal(2);\nexpect(storage.get('a.b.c.d')).to.be.equal(1);\nexpect(storage.get('a.b.c')).to.be.equal({\n    d: 1,\n    e: 2\n});\n\n// set method\n\nstorage.set('x.y.z', 100500);\nexpect(storage.get('x')).to.be.equal({\n  y: {\n    z: 100500\n  }\n});\n```\n\n### Extends\n\n```js\nimport BaseStorage from 'base-storage';\n\nclass CustomStorage extends BaseStorage {\n  constructor(config) {\n    super(config);\n  }\n  \n  set() {\n    throw new Error('\"set\" method is denied')\n  }\n}\n```\n\nVery useful for using as config storage with presets (shortcuts)\n\n```js\nimport BaseStorage from 'base-storage';\n\nclass AppConfig extends BaseStorage {\n  \n  routing: {\n    add: (path, action) =\u003e {\n      let routes = this.get('routing.actions', []);\n      routes.push({path, action});\n      \n      this.set('routing.actions', routes);\n    },\n    \n    enableHistoryApi: () =\u003e {\n      this.set('routing.isHistoryApiEnabled', true);\n    },\n    \n    disableHistoryApi: () =\u003e {\n      this.set('routing.isHistoryApiEnabled', false);\n    }\n  };\n  \n  exceptions: {\n    handler: (handler) =\u003e {\n      let handlers = this.get('exceptions.handlers', []);\n      handlers.push(handler);\n      \n      this.set('exceptions.handlers', handlers);\n    }\n  };\n  \n  api: {\n    setHost: host =\u003e this.set('api.host', host),\n    setPort: port =\u003e this.set('api.port', port),\n    setPrefix: prefix =\u003e this.set('api.prefix', prefix),\n  };\n  \n  google: {\n    setAuthClientId: clientId =\u003e this.set('google.auth.clientId', clientId),\n    getAuthClientId: () =\u003e this.get('google.auth.clientId'),\n    \n    setAnalyticsId: analyticsId =\u003e this.set('google.analyticsId.id', analyticsId),\n  }\n  \n  constructor(config) {\n    super(config);\n  }\n}\n\nlet config = new AppConfig();\n\n// ....\n\nconfig.google.setAuthClientId('xxx');\nconfig.google.setAnalyticsId('zzz');\n\nexpect(config.get('google')).to.be.equal({\n  auth: {\n    clientId: 'xxx',\n  },\n  analyticsId: {\n    id: 'zzz'\n  }\n});\n\n\nexpect(config.get('google.auth.clientId')).to.be.equal('xxx');\nexpect(config.getAuthClientId()).to.be.equal('xxx');\n```\n\n### Available methods\n\n- `constructor(object)` - constructor takes **object** with which  get / set / has methods will work\n\n- `get(path, defaultValue)` - gets the value at path of object. If the resolved value is undefined the defaultValue is used in its place. ([lodash/get](https://lodash.com/docs#get))\n- `set(path, value)` - sets the value at path of object. ([lodash/set](https://lodash.com/docs#set)) \n- `has(path)` - returns **true** is path exists and **false** - if not. ([lodash/has](https://lodash.com/docs#has))\n\n### Community\nYou are always welcome for ideas and pull requests :)\n\n\n### TODO\n\n- [ ] Feature request: freeze (make immutable) whole stored object and its parts\n- [ ] Feature request: Support data structures. (likely to use [tcomb](https://github.com/gcanti/tcomb)) \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuchk4%2Fbase-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuchk4%2Fbase-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuchk4%2Fbase-storage/lists"}