Skip to content

Commit

Permalink
add $if expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario L Gutierrez committed Mar 18, 2013
1 parent fd2524a commit 4446d61
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
24 changes: 23 additions & 1 deletion dist/lib/run/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,29 @@ Filter = (function() {
};

Filter.prototype.canProcess = function(asset) {
var filename;
var comparison, filename, prop, truthy, value, _ref;
if (this.processOptions.$if) {
truthy = true;
_ref = this.processOptions.$if;
for (prop in _ref) {
comparison = _ref[prop];
value = asset[prop];
if (_.isString(comparison)) {
truthy && (truthy = asset[prop] === comparison);
} else if (val instanceof RegExp) {
truthy && (truthy = comparison.test(value));
} else if (typeof val === "boolean") {
truthy && (truthy = val);
} else {
this.log.warn("Unrecognized $if asset property: '" + prop + "'");
truthy = false;
}
if (!truthy) {
return false;
}
}
return truthy;
}
if (this.extnames.indexOf("*") >= 0) {
return true;
}
Expand Down
21 changes: 21 additions & 0 deletions src/lib/run/filter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ class Filter
# a filter unless it can handle the asset's text.
#
canProcess: (asset) ->

# Simple if expression on the filter
#
# f.addHeader(text: "...", $if: {extname: ".txt"})
if @processOptions.$if
truthy = true
for prop, comparison of @processOptions.$if
value = asset[prop]
if _.isString(comparison)
truthy &&= asset[prop] == comparison
else if val instanceof RegExp
truthy &&= comparison.test(value)
else if typeof val == "boolean"
truthy &&= val
else
@log.warn "Unrecognized $if asset property: '#{prop}'"
truthy = false
return false unless truthy
return truthy


return true if @extnames.indexOf("*") >= 0

# must hanlde cases like ".coffee.md"
Expand Down

0 comments on commit 4446d61

Please sign in to comment.