Skip to content

Commit 75a85ee

Browse files
committed
feat: add Astx.some, Astx.every
1 parent a068f86 commit 75a85ee

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Super powerful structural search and replace for JavaScript and TypeScript to au
7474
- [`.match` (`Match`)](#match-match)
7575
- [`.paths` (`NodePath[]`)](#paths-nodepath)
7676
- [`.nodes` (`Node[]`)](#nodes-node)
77+
- [`.some(predicate)` (`boolean`)](#somepredicate-boolean)
78+
- [`.every(predicate)` (`boolean`)](#everypredicate-boolean)
7779
- [`.filter(iteratee)` (`Astx`)](#filteriteratee-astx)
7880
- [`.map<T>(iteratee)` (`T[]`)](#maptiteratee-t)
7981
- [`.at(index)` (`Astx`)](#atindex-astx)
@@ -757,6 +759,18 @@ Returns the nodes that `.find()` and `.closest()` will search within.
757759
If this instance was returned by `.find()` or `.closest()`, these are
758760
the nodes that matched the search pattern.
759761

762+
### `.some(predicate)` (`boolean`)
763+
764+
Returns `false` unless `predicate` returns truthy for at least one match.
765+
766+
`iteratee` is function that will be called with `match: Astx, index: number, parent: Astx` and returns `true` or `false`.
767+
768+
### `.every(predicate)` (`boolean`)
769+
770+
Returns `true` unelss `predicate` returns falsy for at least one match.
771+
772+
`iteratee` is function that will be called with `match: Astx, index: number, parent: Astx` and returns `true` or `false`.
773+
760774
### `.filter(iteratee)` (`Astx`)
761775

762776
Filters the matches.

src/Astx.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,26 @@ export default class Astx extends ExtendableProxy implements Iterable<Astx> {
241241
)
242242
}
243243

244+
some(
245+
iteratee: (astx: Astx, index: number, parent: Astx) => boolean
246+
): boolean {
247+
let index = 0
248+
for (const astx of this) {
249+
if (iteratee(astx, index++, this)) return true
250+
}
251+
return false
252+
}
253+
254+
every(
255+
iteratee: (astx: Astx, index: number, parent: Astx) => boolean
256+
): boolean {
257+
let index = 0
258+
for (const astx of this) {
259+
if (!iteratee(astx, index++, this)) return false
260+
}
261+
return true
262+
}
263+
244264
filter(iteratee: (astx: Astx, index: number, parent: Astx) => boolean): Astx {
245265
const filtered = []
246266
let index = 0

0 commit comments

Comments
 (0)