{"id":15381314,"url":"https://github.com/pshihn/key-tree","last_synced_at":"2025-04-15T19:09:22.477Z","repository":{"id":57289171,"uuid":"128260973","full_name":"pshihn/key-tree","owner":"pshihn","description":"Simple keyed tree data structure 🔑🌲","archived":false,"fork":false,"pushed_at":"2018-04-10T23:02:28.000Z","size":73,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T19:09:12.762Z","etag":null,"topics":["datastructure","hash-trees","hashtree","key-trees","tree"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pshihn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-05T20:31:37.000Z","updated_at":"2023-11-25T03:51:10.000Z","dependencies_parsed_at":"2022-08-29T12:02:37.054Z","dependency_job_id":null,"html_url":"https://github.com/pshihn/key-tree","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pshihn%2Fkey-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pshihn%2Fkey-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pshihn%2Fkey-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pshihn%2Fkey-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pshihn","download_url":"https://codeload.github.com/pshihn/key-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249135809,"owners_count":21218365,"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":["datastructure","hash-trees","hashtree","key-trees","tree"],"created_at":"2024-10-01T14:26:45.164Z","updated_at":"2025-04-15T19:09:22.454Z","avatar_url":"https://github.com/pshihn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# key-tree 🔑🌲\n* Simple keyed tree data structure\n* 831 bytes gzipped\n\n## Usage\n\nA tree where each node has a string as a key, and an array of values. Nodes can simply be refered by their qualified path.\nNodes at any level can easily be accessed/mutated using their key path. \n\n``` javascript\nconst tree = new KeyTree();\ntree.add('cars', ['toyota', 'bmw', 'honda', 'ford']);\ntree.add('cars.models.colors', ['red', 'green', 'blue']);\ntree.add('cars.models', ['sedan', 'suv']);\n```\n\nYou can get specific values at the node or all the values under the node (all children).\n``` javascript\ntree.get('cars.models'); // ['sedan', 'suv']\ntree.getSub('cars.models'); // ['sedan', 'suv', 'red', 'green', 'blue']\n```\n\nOr get all the values of the node and all parent nodes\n``` javascript\ntree.get('cars.models'); // ['sedan', 'suv']\ntree.getSup('cars.models'); // ['sedan', 'suv', 'toyota', 'bmw', 'honda', 'ford']\n```\n\n\n### Example use case\n\nI recently used this to map a set of observer callbacks. So if *_a.b.c_* is modified then invoke the appropriate callbacks. But if *_a.b_* is modified, invoke callbacks associated with _a.b_, _a.b.c_, _a.b.d_.\n\n\n## Install\n\nDownload the latest from [dist folder](https://github.com/pshihn/key-tree/tree/master/dist)\n\nor from npm:\n```\nnpm install --save key-tree\n```\n\n## API\n\nThis defines a KeyTree class\n\n### constructor([options, values])\nBasic tree\n```js\nconst tree = new KeyTree();\n```\n_options_ is an optional argument that can be used to define a custom delimiter for key paths. Default is '.'\n```js\nconst tree = new KeyTree({ separator: '|' });\ntree.add('cars|models', 'sedan');\n```\n_values_ is an optional argument to seed the tree\n```js\nlet tree = new KeyTree(null, {\n  'cars': ['toyota', 'bmw', 'honda', 'merc'],\n  'animals': 'dogs',\n  'cars.models': ['sedan', 'suv']\n})\n```\n\n### add(keyPath, value)\nAdd value(s) to the node at the specified keyPath.\n_value_ can be a single object or an array of objects.\n\n```js\ntree.add('cars.models', 'sedan');\ntree.add('cars.models', ['suv', 'atv'];\n```\n\n### get(keyPath)\nReturns an array of values at the specified keyPath. An empty array is returned if there are no values.\n\n### getSub(keyPath [, grouped])\nReturns all the values at the specified key path and the values of all the children of that node.\nBy default, the result is a combined array.\n\nif _grouped_ is specified and is set to true, the result is an object grouped by various key paths.\n\n### getSup(keyPath [, grouped])\nReturns all the values at the specified key path and the values of all its parent nodes up to the root.\nBy default, the result is a combined array.\n\nif _grouped_ is specified and is set to true, the result is an object grouped by various key paths.\n\n### remove(keyPath, value)\nRemoves the specified value from the specified keyPath.\nReturns _true_ if value existed and was removed.\n\n### removeKey(keyPath)\nRemoves the node at the specified keyPath and all its children.\nReturns _true_ if there was a node at the path\n\n### removeChildren(keyPath)\nRemove all the children of a node. \n\n### clearKey(keyPath [, clearChildren])\nClear all the values associated with the specified node. \nOptionally, if _clearChildren_ is set to true, all the children values are also cleared.\n\n## License\n[MIT License](https://github.com/pshihn/key-tree/blob/master/LICENSE) (c) [Preet Shihn](https://twitter.com/preetster)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpshihn%2Fkey-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpshihn%2Fkey-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpshihn%2Fkey-tree/lists"}