From 948b88c36fe38a85ee3d092fe0f7b71b517a0c3b Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Fri, 24 May 2013 11:25:42 -0400 Subject: [PATCH] add EachWithBreak(), fixes #13 --- LICENSE | 2 +- bench/v0.1.1-v0.2.1-go1.1rc1.svg | 405 +++++++++++++++++++++++++++++ bench/v0.2.0-v0.2.1-go1.1rc1.svg | 420 +++++++++++++++++++++++++++++++ doc.go | 3 +- iteration.go | 13 + iteration_test.go | 15 ++ 6 files changed, 856 insertions(+), 2 deletions(-) create mode 100644 bench/v0.1.1-v0.2.1-go1.1rc1.svg create mode 100644 bench/v0.2.0-v0.2.1-go1.1rc1.svg diff --git a/LICENSE b/LICENSE index 5b0fbd5..e460c34 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2012, Martin Angers & Contributors +Copyright (c) 2012-2013, Martin Angers & Contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/bench/v0.1.1-v0.2.1-go1.1rc1.svg b/bench/v0.1.1-v0.2.1-go1.1rc1.svg new file mode 100644 index 0000000..849a70b --- /dev/null +++ b/bench/v0.1.1-v0.2.1-go1.1rc1.svg @@ -0,0 +1,405 @@ + + + + + + + + ++0.10% + +BenchmarkFirst + + +-0.10% + +BenchmarkLast + + ++2.86% + +BenchmarkEq + + ++2.67% + +BenchmarkSlice + + +-10.82% + +BenchmarkGet + + +-44.06% + +BenchmarkIndex + + +-52.26% + +BenchmarkIndexSelector + + +-9.32% + +BenchmarkIndexOfNode + + +-2.48% + +BenchmarkIndexOfSelection + + +-34.96% + +BenchmarkMetalReviewExample + + +-59.16% + +BenchmarkAdd + + ++0.00% + +BenchmarkAddSelection + + ++3.47% + +BenchmarkAddNodes + + +-2.51% + +BenchmarkAndSelf + + +-13.96% + +BenchmarkFilter + + +-14.53% + +BenchmarkNot + + +-3.81% + +BenchmarkFilterFunction + + +-2.83% + +BenchmarkNotFunction + + +-4.63% + +BenchmarkFilterNodes + + +-2.32% + +BenchmarkNotNodes + + +-4.23% + +BenchmarkFilterSelection + + +-3.31% + +BenchmarkNotSelection + + +-33.69% + +BenchmarkHas + + +-4.53% + +BenchmarkHasNodes + + +-5.30% + +BenchmarkHasSelection + + ++11.35% + +BenchmarkEnd + + +-3.33% + +BenchmarkEach + + +-9.16% + +BenchmarkMap + + +-19.15% + +BenchmarkAttr + + +-3.24% + +BenchmarkText + + +-61.25% + +BenchmarkLength + + +-11.92% + +BenchmarkHtml + + +-16.46% + +BenchmarkIs + + +-27.31% + +BenchmarkIsPositional + + +-13.49% + +BenchmarkIsFunction + + +-1.71% + +BenchmarkIsSelection + + +-3.03% + +BenchmarkIsNodes + + +-20.30% + +BenchmarkHasClass + + +-11.29% + +BenchmarkContains + + +-50.88% + +BenchmarkFind + + +-45.28% + +BenchmarkFindWithinSelection + + ++22.95% + +BenchmarkFindSelection + + ++22.68% + +BenchmarkFindNodes + + +-65.08% + +BenchmarkContents + + +-59.77% + +BenchmarkContentsFiltered + + +-64.31% + +BenchmarkChildren + + +-39.56% + +BenchmarkChildrenFiltered + + +-6.61% + +BenchmarkParent + + +-10.92% + +BenchmarkParentFiltered + + +-1.72% + +BenchmarkParents + + +-2.37% + +BenchmarkParentsFiltered + + +-47.60% + +BenchmarkParentsUntil + + +-36.39% + +BenchmarkParentsUntilSelection + + +-35.99% + +BenchmarkParentsUntilNodes + + +-48.84% + +BenchmarkParentsFilteredUntil + + +-32.75% + +BenchmarkParentsFilteredUntilSelection + + +-33.12% + +BenchmarkParentsFilteredUntilNodes + + +-29.78% + +BenchmarkSiblings + + +-30.89% + +BenchmarkSiblingsFiltered + + +-68.19% + +BenchmarkNext + + +-66.59% + +BenchmarkNextFiltered + + +-31.08% + +BenchmarkNextAll + + +-30.03% + +BenchmarkNextAllFiltered + + +-68.32% + +BenchmarkPrev + + +-66.65% + +BenchmarkPrevFiltered + + +-43.33% + +BenchmarkPrevAll + + +-43.24% + +BenchmarkPrevAllFiltered + + +-54.27% + +BenchmarkNextUntil + + +-37.90% + +BenchmarkNextUntilSelection + + +-43.21% + +BenchmarkNextUntilNodes + + +-49.33% + +BenchmarkPrevUntil + + +-37.02% + +BenchmarkPrevUntilSelection + + +-44.59% + +BenchmarkPrevUntilNodes + + +-57.79% + +BenchmarkNextFilteredUntil + + +-36.61% + +BenchmarkNextFilteredUntilSelection + + +-37.81% + +BenchmarkNextFilteredUntilNodes + + +-59.21% + +BenchmarkPrevFilteredUntil + + +-36.20% + +BenchmarkPrevFilteredUntilSelection + + +-36.05% + +BenchmarkPrevFilteredUntilNodes + + diff --git a/bench/v0.2.0-v0.2.1-go1.1rc1.svg b/bench/v0.2.0-v0.2.1-go1.1rc1.svg new file mode 100644 index 0000000..eaabc61 --- /dev/null +++ b/bench/v0.2.0-v0.2.1-go1.1rc1.svg @@ -0,0 +1,420 @@ + + + + + + + + ++2.12% + +BenchmarkFirst + + ++1.06% + +BenchmarkLast + + ++3.63% + +BenchmarkEq + + ++2.67% + +BenchmarkSlice + + +-24.26% + +BenchmarkGet + + +-41.71% + +BenchmarkIndex + + +-50.42% + +BenchmarkIndexSelector + + ++5.94% + +BenchmarkIndexOfNode + + ++8.26% + +BenchmarkIndexOfSelection + + +-32.94% + +BenchmarkMetalReviewExample + + +-56.84% + +BenchmarkAdd + + ++6.63% + +BenchmarkAddSelection + + ++9.42% + +BenchmarkAddNodes + + ++2.93% + +BenchmarkAndSelf + + +-13.75% + +BenchmarkFilter + + +-14.90% + +BenchmarkNot + + ++1.07% + +BenchmarkFilterFunction + + ++4.91% + +BenchmarkNotFunction + + ++1.03% + +BenchmarkFilterNodes + + ++4.22% + +BenchmarkNotNodes + + +-1.00% + +BenchmarkFilterSelection + + ++4.00% + +BenchmarkNotSelection + + +-32.12% + +BenchmarkHas + + ++1.59% + +BenchmarkHasNodes + + ++1.56% + +BenchmarkHasSelection + + ++15.54% + +BenchmarkEnd + + ++1.77% + +BenchmarkEach + + +-3.61% + +BenchmarkMap + + +-16.48% + +BenchmarkAttr + + ++1.33% + +BenchmarkText + + +-59.21% + +BenchmarkLength + + +-9.01% + +BenchmarkHtml + + +-16.73% + +BenchmarkIs + + +-25.08% + +BenchmarkIsPositional + + +-11.55% + +BenchmarkIsFunction + + ++1.28% + +BenchmarkIsSelection + + ++2.10% + +BenchmarkIsNodes + + +-15.99% + +BenchmarkHasClass + + +-2.65% + +BenchmarkContains + + +-48.54% + +BenchmarkFind + + +-42.71% + +BenchmarkFindWithinSelection + + ++28.77% + +BenchmarkFindSelection + + ++28.47% + +BenchmarkFindNodes + + +-62.84% + +BenchmarkContents + + +-58.06% + +BenchmarkContentsFiltered + + +-63.13% + +BenchmarkChildren + + +-38.24% + +BenchmarkChildrenFiltered + + +-3.66% + +BenchmarkParent + + +-6.06% + +BenchmarkParentFiltered + + ++3.64% + +BenchmarkParents + + ++1.44% + +BenchmarkParentsFiltered + + +-44.35% + +BenchmarkParentsUntil + + +-31.99% + +BenchmarkParentsUntilSelection + + +-32.62% + +BenchmarkParentsUntilNodes + + +-45.83% + +BenchmarkParentsFilteredUntil + + +-30.05% + +BenchmarkParentsFilteredUntilSelection + + +-29.97% + +BenchmarkParentsFilteredUntilNodes + + +-25.96% + +BenchmarkSiblings + + +-26.67% + +BenchmarkSiblingsFiltered + + +-66.40% + +BenchmarkNext + + +-64.46% + +BenchmarkNextFiltered + + +-26.50% + +BenchmarkNextAll + + +-26.27% + +BenchmarkNextAllFiltered + + +-66.84% + +BenchmarkPrev + + +-64.35% + +BenchmarkPrevFiltered + + +-40.05% + +BenchmarkPrevAll + + +-39.74% + +BenchmarkPrevAllFiltered + + +-51.73% + +BenchmarkNextUntil + + +-33.42% + +BenchmarkNextUntilSelection + + +-40.42% + +BenchmarkNextUntilNodes + + +-46.87% + +BenchmarkPrevUntil + + +-33.14% + +BenchmarkPrevUntilSelection + + +-41.19% + +BenchmarkPrevUntilNodes + + +-55.92% + +BenchmarkNextFilteredUntil + + +-32.49% + +BenchmarkNextFilteredUntilSelection + + +-33.00% + +BenchmarkNextFilteredUntilNodes + + +-58.54% + +BenchmarkPrevFilteredUntil + + +-33.80% + +BenchmarkPrevFilteredUntilSelection + + +-33.44% + +BenchmarkPrevFilteredUntilNodes + + +-24.82% + +BenchmarkClosest + + +-34.45% + +BenchmarkClosestSelection + + +-35.30% + +BenchmarkClosestNodes + + diff --git a/doc.go b/doc.go index e98eb50..edc21c3 100644 --- a/doc.go +++ b/doc.go @@ -1,4 +1,4 @@ -// Copyright (c) 2012, Martin Angers & Contributors +// Copyright (c) 2012-2013, Martin Angers & Contributors // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, @@ -60,6 +60,7 @@ The various methods are split into files based on the category of behavior: * iteration.go : methods to loop over the selection's nodes. - Each() + - EachWithBreak() - Map() * property.go : methods that inspect and get the node's properties values. diff --git a/iteration.go b/iteration.go index ff07f29..fd70b90 100644 --- a/iteration.go +++ b/iteration.go @@ -9,6 +9,19 @@ func (this *Selection) Each(f func(int, *Selection)) *Selection { return this } +// EachWithBreak() iterates over a Selection object, executing a function for each +// matched element. It is identical to `Each()` except that it is possible to break +// out of the loop by returning `false` in the callback function. It returns the +// current Selection object. +func (this *Selection) EachWithBreak(f func(int, *Selection) bool) *Selection { + for i, n := range this.Nodes { + if !f(i, newSingleSelection(n, this.document)) { + return this + } + } + return this +} + // Map() passes each element in the current matched set through a function, // producing a slice of string holding the returned values. func (this *Selection) Map(f func(int, *Selection) string) (result []string) { diff --git a/iteration_test.go b/iteration_test.go index a662ad2..d1bb748 100644 --- a/iteration_test.go +++ b/iteration_test.go @@ -19,6 +19,21 @@ func TestEach(t *testing.T) { AssertLength(t, sel.Nodes, 6) } +func TestEachWithBreak(t *testing.T) { + var cnt int + + sel := Doc().Find(".hero-unit .row-fluid").EachWithBreak(func(i int, n *Selection) bool { + cnt++ + t.Logf("At index %v, node %v", i, n.Nodes[0].Data) + return false + }).Find("a") + + if cnt != 1 { + t.Errorf("Expected Each() to call function 1 time, got %v times.", cnt) + } + AssertLength(t, sel.Nodes, 6) +} + func TestEachEmptySelection(t *testing.T) { var cnt int