Permalink
Browse files

filters now work

  • Loading branch information...
1 parent ca5fc74 commit 1c0ed3ea85a6584b57a1cbff6b0b5c0f28d6b69f @drewfish committed Apr 11, 2012
Showing with 34 additions and 5 deletions.
  1. +2 −2 Readme.md
  2. +32 −3 lib/crank.js
View
@@ -93,15 +93,15 @@ updates changelog. {file} defaults to `Changelog.md`
* `changes`:
* `dateformat`: string
* format to use for change dates
- * `filters`: list of regexes to modify changes
+ * `filters`: list of objects describing how to modify changes
* `subject`: "message", "author", "date", "changeid"
* `regexp`: string
* `replace`: "string"
* if results in `--CRANK:SKIP--` then change is skipped
* `versions`:
* `dateformat`: string
* format to use for revision dates
- * `filters`: list of regeses to modify versions
+ * `filters`: list of objects describing how to modify versions
* `subject`: "version", "date", "changeid", "changes"
* `regexp`: string
* `replace`: "string"
View
@@ -63,6 +63,9 @@ function objectMerge(a, b) {
if (typeof a !== typeof b) {
return b;
}
+ if ('[object Array]' === Object.prototype.toString.call(b)) {
+ return b;
+ }
if ('object' === typeof a) {
keys = {};
for (key in a) {
@@ -83,7 +86,6 @@ function objectMerge(a, b) {
}
return out;
}
- // FUTURE: merge arrays?
return b;
}
@@ -160,12 +162,39 @@ Base.prototype.fileWriteJSON = function(path, content) {
// filter a list of JSON structures
Base.prototype.filter = function(list, filters) {
+ var regexps = {},
+ f, filter,
+ i, item,
+ skip,
+ out = [];
+
if (! filters.length) {
// nothing to do
return list;
}
- // TODO
- return list;
+
+ // slight optimization
+ for (f = 0; f < filters.length; f++) {
+ filter = filters[f];
+ regexps[filter.regexp] = new RegExp(filter.regexp);
+ }
+
+ for (i = 0; i < list.length; i++) {
+ skip = false;
+ item = list[i];
+ for (f = 0; f < filters.length; f++) {
+ filter = filters[f];
+ item[filter.subject] = item[filter.subject].replace(regexps[filter.regexp], filter.replace);
+ if (item[filter.subject] === '--CRANK:SKIP--') {
+ skip = true;
+ break;
+ }
+ }
+ if (! skip) {
+ out.push(item);
+ }
+ }
+ return out;
};

0 comments on commit 1c0ed3e

Please sign in to comment.