devimalplanet / object-traversal Goto Github PK
View Code? Open in Web Editor NEWFlexible and performant utility for traversing javascript objects.
License: MIT License
Flexible and performant utility for traversing javascript objects.
License: MIT License
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~
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);
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
"module": "dist/lib.esm.js",
does not exist, is not usable for now, should be object-traversal.esm.js
Line 5 in 161ce37
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates are pending. To force PRs open, click the checkbox below.
@typescript-eslint/eslint-plugin
, @typescript-eslint/parser
)These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
jest
, ts-jest
)@typescript-eslint/eslint-plugin
, @typescript-eslint/parser
).github/workflows/tests.yml
actions/checkout v4
actions/setup-node v4
codecov/codecov-action v4.5.0
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
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
Run:
npm ci
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:
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?
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.