{"id":20806339,"url":"https://github.com/nodesource/grama","last_synced_at":"2025-05-11T17:31:48.161Z","repository":{"id":57253038,"uuid":"83348603","full_name":"nodesource/grama","owner":"nodesource","description":"Pass grama an array of nodes with parent/child relationships and ask her questions about their ancestry.","archived":true,"fork":false,"pushed_at":"2017-03-01T20:33:16.000Z","size":1210,"stargazers_count":4,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T14:17:09.391Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://nodesource.github.io/grama","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/nodesource.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":"2017-02-27T19:33:40.000Z","updated_at":"2025-01-30T21:37:24.000Z","dependencies_parsed_at":"2022-08-31T22:11:25.369Z","dependency_job_id":null,"html_url":"https://github.com/nodesource/grama","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodesource%2Fgrama","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodesource%2Fgrama/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodesource%2Fgrama/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodesource%2Fgrama/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nodesource","download_url":"https://codeload.github.com/nodesource/grama/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253561096,"owners_count":21927762,"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-17T19:19:25.435Z","updated_at":"2025-05-11T17:31:47.914Z","avatar_url":"https://github.com/nodesource.png","language":"JavaScript","readme":"# grama [![build status](https://secure.travis-ci.org/nodesource/grama.png?branch=master)](http://travis-ci.org/nodesource/grama)\n\nPass grama an array of nodes with parent/child relationships and ask her questions about their ancestry.\n\n```js\nconst askGrama = require('grama')\n\nconst grama = askGrama({\n    nodes: [\n        { id: 10, tid: 3 }\n      , { id: 11, tid: 3 }\n      , { id: 12, tid: 3 }\n      , { id: 13, tid: 11 }\n      , { id: 14, tid: 11 }\n      , { id: 15, tid: 13 }\n      , { id: 16, tid: 13 } ]\n    , id: 'id'\n    , parentId: 'tid'\n})\n\nconsole.log('Closest ancestor of 15 and 16 is', grama.closestCommonAncestor(15, 16))\nconsole.log('Closest ancestor of 15 and 11 is', grama.closestCommonAncestor(15, 11))\nconsole.log('Closest ancestor of 16 and 13 is', grama.closestCommonAncestor(16, 13))\n```\n\n    Closest ancestor of 15 and 16 is 13\n    Closest ancestor of 15 and 11 is 3\n    Closest ancestor of 16 and 13 is 11\n\n## Installation\n\n    npm install grama\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n-   [API](#api)\n    -   [grama.commonAncestors](#gramacommonancestors)\n    -   [grama.closestCommonAncestor](#gramaclosestcommonancestor)\n    -   [grama.furthestCommonAncestor](#gramafurthestcommonancestor)\n    -   [grama.closestAncestor](#gramaclosestancestor)\n    -   [grama.allAncestors](#gramaallancestors)\n    -   [grama.closestDescendant](#gramaclosestdescendant)\n    -   [grama.allDescendants](#gramaalldescendants)\n    -   [askGrama](#askgrama)\n-   [License](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## [API](https://nodesource.github.io/grama)\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n### grama.commonAncestors\n\nReturns all common ancestors of id1 and id2.\nIf either id1 or id2 are not found in the ancestry an error is thrown.\n\n**Parameters**\n\n-   `id1` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the first id\n-   `id2` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the second id\n\n### grama.closestCommonAncestor\n\nReturns the id of the closest ancestor of id1 and id2.\nIf either id1 or id2 are not found in the ancestry an error is thrown.\n\nWhen using the predicate a return value of `false` will cause grama to look\nfor the next closest common ancestor, i.e. the returned id is not of the actual\nclosest ancestor, but the closest one that matches the predicate.\n\n**Parameters**\n\n-   `id1` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the first id\n-   `id2` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the second id\n-   `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options\n    -   `$0.predicate` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)?** a function that if supplied needs to\n         return `true` in order to accept the common ancestor.\n         If not supplied the actual closest common ancestor is accepted. (optional, default `null`)\n\nReturns **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the closest common ancestor or `null` if it doesn't exist\n\n### grama.furthestCommonAncestor\n\nReturns the id of the furthest ancestor of id1 and id2.\nIf either id1 or id2 are not found in the ancestry an error is thrown.\n\nWhen using the predicate a return value of `false` will cause grama to look\nfor the next furthest common ancestor, i.e. the returned id is not of the actual\nfurthest ancestor, but the furthest one that matches the predicate.\n\n**Parameters**\n\n-   `id1` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the first id\n-   `id2` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the second id\n-   `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options\n    -   `$0.predicate` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)?** a function that if supplied needs to\n         return `true` in order to accept the common ancestor.\n         If not supplied the actual furthest common ancestor is accepted. (optional, default `null`)\n\nReturns **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the furthest common ancestor or `null` if it doesn't exist\n\n### grama.closestAncestor\n\nFinds the closes ancestor that matches the predicate.\nIf two ancestors match the predicate and have the same distance, the\nfirst one found is returned.\n\nTherefore this function is non-deterministic since it depends on the order\nin which ancestors were added.\n\n**Parameters**\n\n-   `id` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the node whose ancestors to evaluate\n-   `predicate` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a function that needst to return `true` if the\n    ancestor satisfies the criteria\n\nReturns **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the first ancestor matching the predicate\n\n### grama.allAncestors\n\nFinds all ancestors that match the predicate.\n\n**Parameters**\n\n-   `id` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the node whose ancestors to evaluate\n-   `predicate` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a function that needst to return `true` if the\n    ancestor satisfies the criteria\n\nReturns **[Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)\u0026lt;([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))\u003e** the ids of all ancestors matching the predicate\n\n### grama.closestDescendant\n\nFinds the closes descendant that matches the predicate.\nIf two descendants match the predicate and have the same distance, the\nfirst one found is returned.\n\nTherefore this function is non-deterministic since it depends on the order\nin which descendants were added.\n\n**Parameters**\n\n-   `id` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the node whose descendants to evaluate\n-   `predicate` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a function that needst to return `true` if the\n    descendant satisfies the criteria\n\nReturns **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the first descendant matching the predicate\n\n### grama.allDescendants\n\nFinds all descendants that match the predicate.\n\n**Parameters**\n\n-   `id` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the node whose descendants to evaluate\n-   `predicate` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a function that needst to return `true` if the\n    descendant satisfies the criteria\n\nReturns **[Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)\u0026lt;([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))\u003e** the ids of all descendants matching the predicate\n\n### grama.closestSibling\n\nFinds the closest sibling to the node with the provided id that matches the predicate.\n\nIt's not exactly a sibling but any node that is a descendant of an\nancestor of the node with the provided `id`.\n\nWe consider the siblings closest if the distance from the node at `id` to\nthe common ancestor is shortest.\n\nFor instance in the example below we are trying to find the closest sibling of WriteStream:Close.\nOur predicate looks for anything that is a `WriteStream:Write`.\n\n`WriteStream:Write2` is considered a closer sibling since the common ancestor `Read2` is at a shorter\ndistance to `WriteStream:Close` than `Read1` which is the ancestor of the other `WriteStream:Write1`.\n\n               -- ReadStream:Open -- Read1 -- Read2 -- Read3 -- WriteStream:Close\n             /                           \\          \\\n    Parent                                \\           -- WriteStream:Write2\n             \\                              -- WriteStream:Write1\n               -- WriteStream:Open\n\n**Parameters**\n\n-   `id` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the node whose closest sibling we are trying to find\n-   `predicate` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** needs to return `true` in order to determine a node as a sibling,\n     it is invoked with `({ descendantId, descendantDistance, ancestorId, ancestorDistance })`.\n\nReturns **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the closest sibling matching the predicate\n\n### grama.allSiblings\n\nFinds the all siblings to the node with the provided id that match the predicate.\n\nIt's not exactly a sibling but any node that is a descendant of an\nancestor of the node with the provided `id`.\n\nFor instance in the example below we are trying to find all siblings to\n`WriteStream:Close` that start with `WriteStream`.\nThe result would include `WriteStream:Write2`, `WriteStream:Write1` and `WriteStream:Open`.\n\n               -- ReadStream:Open -- Read1 -- Read2 -- Read3 -- WriteStream:Close\n             /                           \\          \\\n    Parent                                \\           -- WriteStream:Write2\n             \\                              -- WriteStream:Write1\n               -- WriteStream:Open\n\n**Parameters**\n\n-   `id` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id of the node whose siblings we are trying to find\n-   `predicate` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** needs to return `true` in order to determine a node as a sibling,\n     it is invoked with `({ descendantId, descendantDistance, ancestorId, ancestorDistance })`.\n\nReturns **[Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)\u0026lt;([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))\u003e** the ids of the siblings matching the predicate\n\n### grama.has\n\nDetermines if the given id is part of the nodes that were passed to grama.\n\n**Parameters**\n\n-   `id` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id we are verifying\n\nReturns **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` if so otherwise `false`\n\n### grama.get\n\nRetrieves the node with the given id from the nodes that were passed to grama.\n\n**Parameters**\n\n-   `id` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number))** the id we are trying to get\n\nReturns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the node with the supplied id or `null` if not found\n\n### askGrama\n\nCreates grama who will tell you about the ancestry of the nodes you passed.\n\n**Parameters**\n\n-   `$0` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** options\n    -   `$0.nodes` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)\u0026lt;[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** the nodes to be added to the ancestry\n    -   `$0.id` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the name of the property that returns the id of each node\n    -   `$0.parentId` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the name of the property that returns the id of the parent of each node\n\nReturns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** an instance of Grama\n\n## License\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodesource%2Fgrama","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnodesource%2Fgrama","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodesource%2Fgrama/lists"}