Giter VIP home page Giter VIP logo

unist-util-parents's Issues

Node instance in multiple parentNode.children

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.

Subject of the feature

.parent property in wrapped tree should return multiples parents if the same node instance is referenced in multiple parentNode.children.

Problem

.parent is not usable is this kind of tree as it return only the first one find.

Expected behaviour

return 'parents' as an array.

show code

  • an illustrating 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.