Coder Social home page Coder Social logo

devimalplanet / object-traversal Goto Github PK

View Code? Open in Web Editor NEW
22.0 1.0 1.0 764 KB

Flexible and performant utility for traversing javascript objects.

License: MIT License

JavaScript 16.64% TypeScript 83.36%
javascript typescript traversal graph search

object-traversal's People

Contributors

devimal avatar kepelrs avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

kepelrs

object-traversal's Issues

Just stumbled onto this library!

Hello fellow coder =)

Was just looking through npm to find competitors to a library I wrote and found yours. One feature that I'm currently lacking that you have is "breadth-first" search. Adds a whole layer of complexity and memory consumption, so I haven't tackled it.

Are you doing this for fun or for a work project? What are your spec requirements?

Feel free to take a look at mine and get some inspiration as well!
https://github.com/blackflux/object-scan

Happy coding! L~

Include traversed object with different key

Hello, nice and fast traversing lib. I am trying to filter an large hierarchiel Open API schema object, beforehand I dereferenced the schema's to one root object. I am using this object to search and filter properties. So far traversing is working but it excludes properties with same values. I believe it has to do with line 86 in traverse.ts where it skips nodes (values) that have already been visited.

Is there a workaround to include nodes that have different keys but are using the same objects/ models which are visited?

Example
Lets say I have a Person model with two address properties that have the same Address model (see input code). When I run the code below, I will only get access to person.address, but not person.foreignAddress because it is not included. I would like to all the nodes included when traversing.

When I set cycleHandling option to off, it works but I need this otherwise I will have performance issues where the maximum size will exceed.

Input

let address = {
      address: {
          street: {
              type: "string"
          },
          houseNumber: {
              type: "integer"
          },
          postalCode: {
              type: "string"
          }
      }
  };
  const person = {
      name: "John Doe",
      address: address,
      foreignAddress: address
  }

Code

let nodes = []
function filterData(n) {
    nodes.push(n)
}

traverse(person, filterData);

Accessing parent properties on arrays (grandparent)

Nice lib!
I just have a quick question of something i was not able to do. Imagine you have at some point a node like this:

{ 
 id: "1", 
 name: "Peter", 
 books: [ 
  {name: "Funny book"} , 
  {name: "Sad book"} 
  ]
 }

When the current value is the node {name:"funny Book"} accessing the parent gets the parent array and not the parent node. Is there a way to access the "id" property from the children?

πŸ“It is possible to do it with the original object using getNodeByPath() but id is not there from the begining, I create it while I traverse the tree so i need a reference to the parent node that has been already extended

πŸ“Also no way to get it from the array, because when the value is the array itself, key and parent are null

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • chore(deps): update dependency ts-jest to v26.5.6
  • chore(deps): update typescript-eslint monorepo to v7.18.0 (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/tests.yml
  • actions/checkout v4
  • actions/setup-node v4
  • codecov/codecov-action v4.5.0
npm
package.json
  • @size-limit/preset-small-lib 8.2.6
  • @types/benchmark 2.1.5
  • @types/traverse 0.6.37
  • benchmark 2.1.4
  • husky 8.0.3
  • playwright-test 9.2.0
  • size-limit 8.2.6
  • traverse 0.6.9
  • tsdx 0.14.1
  • tslib 2.6.3
  • typescript 5.5.4
  • node >=10
  • @typescript-eslint/eslint-plugin ^7.0.0
  • @typescript-eslint/parser ^7.0.0
  • jest ^26.6.3
  • ts-jest ^26.4.4
  • typescript 5.5.4

  • Check this box to trigger a request for Renovate to run again on this repository

inflight and glob deprecations

Hi bud,

Got a couple of deprecations, not sure where they are from. I can jump in and issue a PR if you're fine with it.

npm warn 
 [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.

npm warn deprecated [email protected]: Glob versions prior to v9 are no longer supported

Repro

Run:

npm ci 

Mutate root object

Hello! Still using your lib a lot :)

I was wondering if you have any idea in how to mutate the root object without throwing weird javascript errors.

Lets imagine we have this format of nested objects, which could start as arrays or single objects at the root

{ $thing: 'Employee', $id: ['e1','e2'], companies: [{$id: ['c1','c2'], $op: 'link'}]}

Now imagine we want to split the $ids when they are arrays to have this output:

[
   {
      $thing: 'Employee
      $id: 'e1'
      companies: { $op: 'link' , $id: 'c1'} , { $op: 'link', $id: 'c2'}
    },
     {
      $thing: 'Employee
      $id: '21'
      companies: { $op: 'link' , $id: 'c1'} , { $op: 'link', $id: 'c2'}
    }
]

We could imagine something like this:

return produce(blocks, (draft) =>
   traverse(draft, ({ value, key, parent }: TraversalCallbackContext) => {
      if (isObject(val) && isArray(val.$id) {
        parent[key] = value.$ids.map((id) => ({...value, $id: id})
       }
     }
    );

This has obvious issues:

  1. Not working for the root (no parent)
  2. Is actually adding nested arrays in things that are already arrays (not a flatMap)

In general I realized it is cleaner to modify this objects from their parent, but then i need to create two different logics, one for the root, and then other for the rest.

Also being able to do transformation directly at the value level is convinient, because we can delete, update and add keys of that value easily

Do you have any idea in how could we do a wrapper around the traverse to manage this cases in a simple way, ensuring no JS object mutation errors and being able to avoid doing the particular cases for the root levels?

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.