{"id":19621308,"url":"https://github.com/commenthol/hashtree","last_synced_at":"2025-10-24T07:47:07.601Z","repository":{"id":14218209,"uuid":"16925052","full_name":"commenthol/hashtree","owner":"commenthol","description":"Build up \"hash of hashes\" in javascript and access the data contained","archived":false,"fork":false,"pushed_at":"2019-10-25T16:19:40.000Z","size":47,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-09T12:42:51.997Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/commenthol.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":"2014-02-17T20:06:48.000Z","updated_at":"2019-10-25T16:19:42.000Z","dependencies_parsed_at":"2022-09-03T10:00:41.907Z","dependency_job_id":null,"html_url":"https://github.com/commenthol/hashtree","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fhashtree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fhashtree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fhashtree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/commenthol%2Fhashtree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/commenthol","download_url":"https://codeload.github.com/commenthol/hashtree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240914937,"owners_count":19878066,"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-11-11T11:22:18.845Z","updated_at":"2025-10-24T07:47:02.582Z","avatar_url":"https://github.com/commenthol.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hashtree [![Build Status](https://secure.travis-ci.org/commenthol/hashtree.png?branch=master)](https://travis-ci.org/commenthol/hashtree)\n\n\u003e Build up \"hash of hashes\" in javascript and access the data contained.\n\nFor documentation of the methods provided see the [Doc][documentation]\n\nThe module can be used in Node, AMD / RequireJS or straight in a browser.\n\n## Motivation\n\nI'm a big fan of perl hashes. Unfortunately in javascript building up and accessing hash-of-hashes is not as straightforward as in perl. This lib tries to solve this by providing methods to easily build up those.\n\n## Interface\n\nTwo interfaces are offered either using a prototype function `HashTree` or a functional interface using `hashTree` both offering the same functionality.\n\nTo access the values in the hashtree you use the `keys` either with a dot concatenated string or an array.\nE.g. `'key.subkey'` or `[ 'key', 'subkey' ]` .\n\n## Basic Operations\n\nBasic operations use the methods\n* `set` - Sets a value in the hashtree obj according to keys. \n* `get` - Gets a value in the hashtree obj according to keys.\n* `delete` - Deletes a value in the hashtree obj according to keys.\n\n``` javascript\nvar ht = require('./hashtree').hashTree;\n\nvar r, \n\tobj = { \"one\": 1 };\n\n/// add a new branch `obj.two.three` and set its value to 3 using dot-notation for keys\nht.set(obj, 'two.three', 3);\n// =\u003e obj = { one: 1, two: { three: 3 } }\n\n/// add a new branch `obj.four.five` and set its value to 5 using array notation for keys\nht.set(obj, [ 'four', 'five' ], 5);\n// =\u003e obj = { one: 1, two: { three: 3 }, four: { five: 5 } }\n\n/// set objects - note this replaces existing branches\nht.set(obj, 'two', { six: 6 } );\n// =\u003e obj = { one: 1, two: { six: 6 }, four: { five: 5 } }\n\n/// get the value stored in `obj.two` using dot-notation for keys\nr = ht.get(obj, 'two');\n// =\u003e r = { six: 6 }\n\n/// get the value stored in `obj.four.five` using array notation for keys\nr = ht.get(obj, [ 'four', 'five' ]);\n// =\u003e r = 5\n\n/// you can also set arrays\nht.set(obj, 'arr', [ 26, 27, 28 ]);\n// =\u003e obj = { one: 1, two: { three: 3 }, four: { five: 5 }, arr: [ 26, 27, 28 ] }\n\n/// and delete branches\nht.delete(obj, 'four');\n// =\u003e obj = { one: 1, two: { three: 3 }, arr: [ 26, 27, 28 ] }\n\n/// or items from arrays\nht.delete(obj, 'arr.1');\n// =\u003e obj = { one: 1, two: { three: 3 }, arr: [ 26, , 28 ] }\n```\n\n## Manipulate Values in the hashtree\n\nTo manipulate values in the hashtree use the methods\n* `setAll` - Sets all leafes of the hashtree on obj to value.\n* `use` - use a reference from the hashtree to make operations upon.\n* `sort` - sort the hashtree.\n\n``` javascript\nvar ht = require('./hashtree').hashTree;\n\nvar r, \n\tobj = { one: { a: 1, b: 2, c: 3 } };\nr = ht.setAll(obj, 'one', 0);\n// =\u003e r = true \n// =\u003e obj = { one: { a: 0, b: 0, c: 0 } }\n```\n\nTo manipulate values in the hashtree without having to constantly use get and set methods use the `use` method.\nAll operations can be chained.\n\n``` javascript\nvar ht = require('./hashtree').hashTree;\n\nvar r, obj = {};\nr = ht.use(obj, 'one.a', 7) // sets the value to '7'\n        .inc()              // increments by one\n        .dec()              // decrements by one\n        .add(10)            // add 10\n        .sub(5)             // subtract 5\n        .mul(200)           // multiply by 2\n        .div(3)             // divide by 3\n        .mod(70)            // modulo 7\n        .get();             // finally get the value        \n// =\u003e r = 30\n// =\u003e obj = { one: { a: 30 } }\n```\n\nSorting a hash tree does not make a lot of sense. It does not change anything. But for human eyes, e.g. on exporting to YAML or JSON, sorted patterns are easier to read (at least for me).\n\n``` javascript\nvar ht = require('./hashtree').hashTree;\n\n// sort in decending order\nfunction descSorter(a, b) { \n    return (a \u003e b) ? -1 : ( a == b ? 0 : 1);\n}\n\nvar r, \n    obj = { \n        a: { a: 1, b: 2, c: 2 }, \n        e: { a: 1, b: 2, c: 2 }, \n        d: { a: 1, b: 2, z: 3 }\n    };\n// sort in decending order\nr = ht.sort(obj, descSorter);\n// =\u003e obj = { \n//  e: { c: 2, b: 2, a: 1 },\n//  d: { z: 3, b: 2, a: 1 },\n//  a: { c: 2, b: 2, a: 1 } }\n```\n\nAdditionally two hashtrees can be compared with `diff` or `diffToBase`.\n\n```js\nvar ht = require('hashtree').hashTree;\n\nht.diff({one:{two:{a:1,b:2}},two:2}, {one:{two:{a:1,b:3}},two:2});\n// =\u003e { diff1: {one:{two:{b:2}}}, diff2: {one:{two:{b:3}}} }\n\nht.diffToBase({one:{two:{a:1,b:2}},two:2}, {one:{two:{a:1,b:3}},two:2});\n// =\u003e {one:{two:{b:3}}}\n```\n\n## HashTree\n\nAll methods are offered also as a prototype function.\n\n``` javascript\nvar HashTree = require('hashtree').HashTree;\n\nvar r;\nvar ht = new HashTree({ 'one': 1 }); \nht.set('two.three', 3);\n\nr = ht.get('two');\n// =\u003e r = { three: 3 }\nht.delete('two');\n\nht.use('three.four').inc().add(3);\n\n/// return the hashtree object\nr = ht.tree();\n// =\u003e r = { one: 1, three: { four: 4 } }\n``` \n\n## License\n\nCopyright (c) 2014- commenthol \n\nSoftware is released under [MIT][license].\n\n\n[license]: ./LICENSE\n[documentation]: ./docs/documentation.md\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fhashtree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcommenthol%2Fhashtree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommenthol%2Fhashtree/lists"}