Skip to content

Commit

Permalink
Use arrays for all feature lists instead of having an array here and …
Browse files Browse the repository at this point in the history
…a hash there
  • Loading branch information
raucao committed Mar 27, 2012
1 parent d2357f7 commit 60df393
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion data/features.json

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion lib/trufflepig/feature_list.rb
Expand Up @@ -10,13 +10,24 @@ def self.build
caniuse = JSON.parse File.read("#{File.dirname(__FILE__)}/../../data/caniuse.json")
patterns = JSON.parse File.read("#{File.dirname(__FILE__)}/../../data/patterns.json")
features = caniuse["data"]
output = []

# Merge detection patterns into feature list
patterns.keys.each do |key|
features[key].merge!({"detection_pattern" => patterns[key]})
end

# Transform feature list to an array
features.each do |key, feature|
output << feature.merge({"id" => key})
end

# Sort array by feature id
output.sort!{|a, b| a["id"] <=> b["id"] }

# Write new features file
of = File.open "#{File.dirname(__FILE__)}/../../data/features.json", "w"
of.syswrite caniuse["data"].to_json
of.syswrite output.to_json
of.close
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/trufflepig/search.rb
Expand Up @@ -26,9 +26,9 @@ def perform
def scan(file_path)
content = File.read file_path

features.each do |key, feature|
features.each do |feature|
next unless feature["detection_pattern"]
results << feature.merge({"slug" => key}) if content.match(/#{feature["detection_pattern"]}/)
results << feature if content.match(/#{feature["detection_pattern"]}/)
end
end

Expand Down
13 changes: 7 additions & 6 deletions spec/trufflepig/feature_list_spec.rb
Expand Up @@ -7,11 +7,12 @@
end

it "returns a list of features" do
@features.keys.must_include "video"
@features.keys.must_include "audio"
@features.keys.must_include "canvas"
feature_keys = @features.collect{|f| f["id"] }
feature_keys.must_include "video"
feature_keys.must_include "audio"
feature_keys.must_include "canvas"

feature = @features.values_at("video").first
feature = @features.select{|f| f["id"] == "video"}.first
feature["categories"].must_equal ["HTML5"]
feature["title"].must_equal "Video element"
end
Expand All @@ -24,10 +25,10 @@
end

it "adds detection patterns to the features" do
feature = @features.values_at("video").first
feature = @features.select{|f| f["id"] == "video"}.first
feature["detection_pattern"].must_equal "<video.*>.*<\\/video>"

feature = @features.values_at("offline-apps").first
feature = @features.select{|f| f["id"] == "offline-apps"}.first
feature["detection_pattern"].must_equal "<html.*\\smanifest=.*>"
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/trufflepig/search_spec.rb
Expand Up @@ -29,7 +29,7 @@
end

it "finds a truffle" do
@search.results.collect{|f| f["slug"] }.must_include "video"
@search.results.collect{|f| f["id"] }.must_include "video"
end
end

Expand All @@ -40,7 +40,7 @@
end

it "finds a lot of truffles" do
feature_keys = @search.results.collect{|f| f["slug"] }
feature_keys = @search.results.collect{|f| f["id"] }

feature_keys.must_include "video" # html
feature_keys.must_include "stream" # js
Expand Down

0 comments on commit 60df393

Please sign in to comment.