1package goquery
 2
 3// Each iterates over a Selection object, executing a function for each
 4// matched element. It returns the current Selection object. The function
 5// f is called for each element in the selection with the index of the
 6// element in that selection starting at 0, and a *Selection that contains
 7// only that element.
 8func (s *Selection) Each(f func(int, *Selection)) *Selection {
 9	for i, n := range s.Nodes {
10		f(i, newSingleSelection(n, s.document))
11	}
12	return s
13}
14
15// EachWithBreak iterates over a Selection object, executing a function for each
16// matched element. It is identical to Each except that it is possible to break
17// out of the loop by returning false in the callback function. It returns the
18// current Selection object.
19func (s *Selection) EachWithBreak(f func(int, *Selection) bool) *Selection {
20	for i, n := range s.Nodes {
21		if !f(i, newSingleSelection(n, s.document)) {
22			return s
23		}
24	}
25	return s
26}
27
28// Map passes each element in the current matched set through a function,
29// producing a slice of string holding the returned values. The function
30// f is called for each element in the selection with the index of the
31// element in that selection starting at 0, and a *Selection that contains
32// only that element.
33func (s *Selection) Map(f func(int, *Selection) string) (result []string) {
34	return Map(s, f)
35}
36
37// Map is the generic version of Selection.Map, allowing any type to be
38// returned.
39func Map[E any](s *Selection, f func(int, *Selection) E) (result []E) {
40	result = make([]E, len(s.Nodes))
41
42	for i, n := range s.Nodes {
43		result[i] = f(i, newSingleSelection(n, s.document))
44	}
45
46	return result
47}