-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package composition | ||
|
||
import "strings" | ||
|
||
// Initialization: Sets the default deduplication strategy to be used. | ||
func init() { | ||
SetStrategy(new(IdentityDeduplicationStrategy)) | ||
} | ||
|
||
// Set another deduplication strategy. | ||
func SetStrategy(strategy StylesheetDeduplicationStrategy) { | ||
stylesheetDeduplicationStrategy = strategy | ||
} | ||
|
||
// NOOP strategy. | ||
// This stragegy will insert all found stylesheets w/o any filtering. | ||
type IdentityDeduplicationStrategy struct { | ||
} | ||
|
||
func (strategy *IdentityDeduplicationStrategy) Deduplicate(hrefs []string) []string { | ||
return hrefs | ||
} | ||
|
||
// Simple strategy | ||
// Implements a very simple deduplication stragegy. That is, it filters out | ||
// stylesheets with duplicate href value. | ||
type SimpleDeduplicationStrategy struct { | ||
} | ||
|
||
// Remove duplicate entries from hrefs. | ||
func (strategy *SimpleDeduplicationStrategy) Deduplicate(hrefs []string) []string { | ||
var knownHrefs string | ||
var result []string | ||
const delimiter = "-|-" | ||
for _, href := range hrefs { | ||
if !strings.Contains(knownHrefs, href) { | ||
result = append(result, href) | ||
knownHrefs += delimiter + href | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// Variable to hold the active strategy | ||
var stylesheetDeduplicationStrategy StylesheetDeduplicationStrategy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package composition | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_DefaultDeduplicationStrategy(t *testing.T) { | ||
a := assert.New(t) | ||
stylesheets := []string{"a", "b"} | ||
result := stylesheetDeduplicationStrategy.Deduplicate(stylesheets) | ||
a.EqualValues(stylesheets, result) | ||
} | ||
|
||
func Test_SimpleDeduplicationStrategy(t *testing.T) { | ||
a := assert.New(t) | ||
stylesheets := []string{"a", "b", "a", "b", "c", "a"} | ||
deduper := new(SimpleDeduplicationStrategy) | ||
result := deduper.Deduplicate(stylesheets) | ||
a.EqualValues([]string{"a", "b", "c"}, result) | ||
} | ||
|
||
type Strategy struct { | ||
collecttion []string | ||
} | ||
|
||
func (strategy *Strategy) Deduplicate(hrefs []string) []string { | ||
newhrefs := []string{} | ||
for _, href := range hrefs { | ||
newhrefs = append(newhrefs, href+"?abcdef") | ||
strategy.collecttion = append(strategy.collecttion, href) | ||
} | ||
return newhrefs | ||
} | ||
|
||
func Test_OwnDeduplicationStrategy(t *testing.T) { | ||
strategy := new(Strategy) | ||
SetStrategy(strategy) | ||
|
||
a := assert.New(t) | ||
stylesheets := []string{"a", "b"} | ||
expected := []string{"a?abcdef", "b?abcdef"} | ||
result := stylesheetDeduplicationStrategy.Deduplicate(stylesheets) | ||
a.EqualValues(expected, result) | ||
a.EqualValues(strategy.collecttion, stylesheets) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters