syntax-tree / unist-util-parents Goto Github PK
View Code? Open in Web Editor NEWunist utility to add references to parents on nodes in a tree
Home Page: https://unifiedjs.com
License: MIT License
unist utility to add references to parents on nodes in a tree
Home Page: https://unifiedjs.com
License: MIT License
Hi !
Me again suggesting something on a possible misunderstanding ! :)
Do unist trees can be fully used with "node instance" referenced in multiple branch of the tree ?
If so, one specific node can have multiple parents.
This is the subject of this suggestion.
.parent
property in wrapped tree should return multiples parents if the same node instance is referenced in multiple parentNode.children
.
.parent
is not usable is this kind of tree as it return only the first one find.
return 'parents' as an array.
jest test
here. I can translate it to a tape
one if there is interest / value added.import parents from 'unist-util-parents'
describe('unist-util-parents feature for node with 2 or more parents', () => {
it('Return all parents of a node instance in multiple parentNode.children', () => {
const predicateTree = { type : 'predicateTree', children:[]}
const parent1 = { type: 'predicate', children:[], id:'http://p/1'}
const parent2 = { type: 'predicate', children:[], id:'http://p/2'}
const objectOneParent = { type: 'object', id:'http://o/1', value:'I only have one parent'}
const objectTwoParents = { type: 'object', id:'http://o/2', value:'I have 2 parents !'}
//build the tree
parent1.children.push(objectTwoParents)
parent2.children.push(objectTwoParents, objectOneParent)
predicateTree.children.push(parent1, parent2)
// wrap it
const treeWithParents = parents(predicateTree)
// w = wrapped
const [wparent1, wparent2] = treeWithParents.children
expect(wparent1.children.length).toBe(1)
// objectTwoParents is under his 2 parents
expect(wparent1.children[0]).toStrictEqual(objectTwoParents)
expect(wparent2.children[0]).toStrictEqual(objectTwoParents)
// get the wrapped object wO2P = object with 2 parents
const [wO2P] = wparent2.children
// but the parent relationship only return one parent
console.log(wO2P.parent)
expect(Array.isArray(wO2P.parent)).toBe(true)
})
})
Thanks and regards.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.