Skip to content

Commit

Permalink
Revert "Re-orangize code for compitable"
Browse files Browse the repository at this point in the history
This reverts commit 6802fc5.
  • Loading branch information
mna committed Sep 6, 2024
1 parent 6802fc5 commit eec8e87
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 109 deletions.
48 changes: 0 additions & 48 deletions bench_iteration123_test.go

This file was deleted.

14 changes: 7 additions & 7 deletions bench_iteration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ func BenchmarkEachIter(b *testing.B) {
sel := DocW().Find("td")
b.StartTimer()
for i := 0; i < b.N; i++ {
sel.EachIter()(func(i int, s *Selection) bool {
for range sel.EachIter() {
tmp++
return true
})
}
if n == 0 {
n = tmp
}
Expand All @@ -53,11 +52,12 @@ func BenchmarkEachIterWithBreak(b *testing.B) {
b.StartTimer()
for i := 0; i < b.N; i++ {
tmp = 0
sel.EachIter()(func(i int, s *Selection) bool {
for range sel.EachIter() {
tmp++
return tmp < 10
})

if tmp >= 10 {
break
}
}
if n == 0 {
n = tmp
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ require (
golang.org/x/net v0.27.0
)

go 1.18
go 1.23
4 changes: 3 additions & 1 deletion iteration.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package goquery

import "iter"

// Each iterates over a Selection object, executing a function for each
// matched element. It returns the current Selection object. The function
// f is called for each element in the selection with the index of the
Expand All @@ -14,7 +16,7 @@ func (s *Selection) Each(f func(int, *Selection)) *Selection {

// EachIter returns an iterator that yields the Selection object in order.
// The implementation is similar to Each, but it returns an iterator instead.
func (s *Selection) EachIter() func(yield func(int, *Selection) bool) {
func (s *Selection) EachIter() iter.Seq2[int, *Selection] {
return func(yield func(int, *Selection) bool) {
for i, n := range s.Nodes {
if !yield(i, newSingleSelection(n, s.document)) {
Expand Down
42 changes: 0 additions & 42 deletions iteration123_test.go

This file was deleted.

22 changes: 12 additions & 10 deletions iteration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ func TestEachIter(t *testing.T) {
var cnt int

sel := Doc().Find(".hero-unit .row-fluid")
sel.EachIter()(func(i int, n *Selection) bool {

for i, s := range sel.EachIter() {
cnt++
t.Logf("At index %v, node %v", i, n.Nodes[0].Data)
return true
})
t.Logf("At index %v, node %v", i, s.Nodes[0].Data)
}

sel = sel.Find("a")

if cnt != 4 {
t.Errorf("Expected EachIter() to call function 4 time, got %v times.", cnt)
t.Errorf("Expected Each() to call function 4 times, got %v times.", cnt)
}
assertLength(t, sel.Nodes, 6)
}
Expand All @@ -127,15 +128,16 @@ func TestEachIterWithBreak(t *testing.T) {
var cnt int

sel := Doc().Find(".hero-unit .row-fluid")
sel.EachIter()(func(i int, n *Selection) bool {
for i, s := range sel.EachIter() {
cnt++
t.Logf("At index %v, node %v", i, n.Nodes[0].Data)
return false
})
t.Logf("At index %v, node %v", i, s.Nodes[0].Data)
break
}

sel = sel.Find("a")

if cnt != 1 {
t.Errorf("Expected EachIter() to call function 1 time, got %v times.", cnt)
t.Errorf("Expected Each() to call function 1 time, got %v times.", cnt)
}
assertLength(t, sel.Nodes, 6)
}

0 comments on commit eec8e87

Please sign in to comment.