Comments (7)
Hey @wooorm ! Cool, thanks for the response. Well, good to hear my understanding matches yours. :)
We ended up using visit-dynamic
variant I put above, and it's been working fine for our use cases. I'm not sure if I'll have bandwidth to publish/maintain it myself right now — perhaps we can set it up under syntax-tree
as an alternative to this visitor, if you think it's useful? You're welcome to the code above!
Also just PR'ed a doc clarification.
from unist-util-visit.
Sweet, thanks! I added you to the org, so feel free to create a utility here and we’ll help maintain it!
How much code would it add in this utility to have this added here though, without breaking current things?
from unist-util-visit.
Hey Joshua! I’m extremely sorry for the long wait. I’ve been busy with holiday, and then super busy creating a new course (which is actually paid, so I’d like to make that great too). I know, I know, excuses. Anyway, thanks for waiting!
It may be worth putting in the docs some thoughts on what the contract is wrt mutating the tree. In particular, modifying individual nodes is fine, but unist-util-visit assumes the visitor does not mutate the number of children of the parent.
Correct. If we’re talking about docs here. What do you suggest?
More generally, I'd be interested to know what approach is best to handle the fully mutating use case. https://github.com/syntax-tree/unist-util-map and https://github.com/eush77/unist-util-filter are listed but cover different use cases. One easy option (but may not be the best) is to make a visit-dynamic variant, something like:
That looks good! I think that makes sense to have, so I’d support a utility that does that!
I had a similar problem building natural language parsers, and wrapped it in array-iterate
. Maybe that can be useful too?
I hope you’re still interested in this, cheers,
Titus
from unist-util-visit.
@jlevy Ping! :)
I know I know, I took way longer 😛
from unist-util-visit.
Cool, thanks. Hm on second thought, yes, I believe you can just add support here by modifying your existing all() to something like (code not tested!):
/* Visit children in `parent`, possibly adding or removing children. */
function all(children, parent) {
var sign = reverse ? -1 : 1;
var index = reverse ? children.length - 1 : 0;
var child;
while (index >= 0 && index < children.length) {
child = children[index];
let delta = 0;
if (child && (delta = one(child, index, parent)) === false) {
return false;
}
index += sign * (1 + (delta || 0));
}
return true;
}
This should then support forward/reverse, abort, and adding/removing nodes. Luckily the check you currently have for false (abort) is exact (===) so it won't conflate 0 and false, and so should be backwards compatible with previous uses of this lib. :)
from unist-util-visit.
Done! Only difference is that this quires absolute index
es, instead of your proposed delta
s.
from unist-util-visit.
cool!
from unist-util-visit.
Related Issues (12)
- Is the API synchronous? HOT 2
- Can transformations be immutable? HOT 6
- Special character parsing HOT 2
- Add types HOT 7
- [Types]: type of parent should be Parent HOT 3
- Standalone documentation HOT 3
- Error [ERR_REQUIRE_ESM]: Must use import to load ES Module HOT 14
- NPM readme example still uses require() HOT 3
- Type doesn't work when using array of tests HOT 7
- Causes tsc to stall indefinitely HOT 16
- Cannot find module 'unist-util-is/lib/index.js' or its corresponding type declarations HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unist-util-visit.