Adjustments to PR#5494

* Update filter name to be more accurate
* Add additional tests
* Simplify checklist checking logic
* Prevent errors from multiple matches in checklist
This commit is contained in:
Blade Barringer 2015-06-27 22:21:50 -05:00
parent 8ba621795a
commit 4abd7f85f8
3 changed files with 60 additions and 33 deletions

View file

@ -29,36 +29,66 @@ describe('Task Ordering Filters', function() {
});
});
describe('filterByTextAndNotes', function () {
describe('filterByTaskInfo', function () {
it('returns undefined when no input given', function () {
expect(filter('filterByTextAndNotes')()).to.eql(undefined);
expect(filter('filterByTaskInfo')()).to.eql(undefined);
});
it('returns input if term is not a string', function () {
var input = [1, 2, 3];
expect(filter('filterByTextAndNotes')(input, '')).to.eql(input);
expect(filter('filterByTextAndNotes')(input, undefined)).to.eql(input);
expect(filter('filterByTextAndNotes')(input, [])).to.eql(input);
expect(filter('filterByTextAndNotes')(input, new Date())).to.eql(input);
it('returns all tasks if term is not a string', function () {
var tasks = [1, 2, 3];
expect(filter('filterByTaskInfo')(tasks, undefined)).to.eql(tasks);
expect(filter('filterByTaskInfo')(tasks, [])).to.eql(tasks);
expect(filter('filterByTaskInfo')(tasks, new Date())).to.eql(tasks);
});
it('filters items by notes and text', function () {
it('returns tasks if term is an empty string', function () {
var tasks = [1, 2, 3];
expect(filter('filterByTaskInfo')(tasks, '')).to.eql(tasks);
});
it('filters items by text', function () {
var tasks = [
{ text: 'foo' },
{ text: 'foo', notes: 'bar' }
{ text: 'some text that contains foo' },
{ text: 'some text that should not be matched' }
];
expect(filter('filterByTextAndNotes')(tasks, 'bar')).to.eql([tasks[1]]);
expect(filter('filterByTextAndNotes')(tasks, 'foo')).to.eql([tasks[0], tasks[1]]);
expect(filter('filterByTaskInfo')(tasks, 'foo')).to.eql([tasks[0], tasks[1]]);
});
it('filters items by notes', function () {
var tasks = [
{ text: 'some text', notes: 'foo' },
{ text: 'some text', notes: 'a note that contains foo' },
{ text: 'some text', notes: 'some text' },
{ text: 'some text' }
];
expect(filter('filterByTaskInfo')(tasks, 'foo')).to.eql([tasks[0], tasks[1]]);
});
it('filters items by checklists', function () {
var tasks = [
{ text: 'foo' },
{ text: 'foo', notes: 'bar', checklist: [ {text: "checkListToFind"} ] }
{ text: 'foo', notes: 'bar', checklist: [ {text: "checkListToFind"} ] },
{ text: 'foo', notes: 'bar', checklist: [ {text: "checkListToNotFind"} ] }
];
expect(filter('filterByTextAndNotes')(tasks, 'checkListToFind')).to.eql([tasks[1]]);
expect(filter('filterByTaskInfo')(tasks, 'checkListToFind')).to.eql([tasks[1]]);
});
it('only includes task once, even with multiple matches in checklist', function() {
var tasks = [
{
text: 'foo', notes: 'bar', checklist: [
{text: "checkListToFind"},
{text: "checkListToFind"},
{text: "checkListToFind"}
]
}
];
expect(filter('filterByTaskInfo')(tasks, 'checkListToFind')).to.eql([tasks[0]]);
});
});
});

View file

@ -7,28 +7,25 @@ angular.module('habitrpg')
return array;
};
}])
.filter('filterByTextAndNotes', ['$filter', function($filter) {
return function (input, term) {
if (!input) return;
.filter('filterByTaskInfo', ['$filter', function($filter) {
return function (tasks, term) {
if (!tasks) return;
if (!angular.isString(term) || term.legth === 0) {
return input;
return tasks;
}
term = new RegExp(term, 'i');
var result = [];
for (var i = 0; i < input.length; i++) {
if (term.test(input[i].text) || term.test(input[i].notes)) {
result.push(input[i]);
}else if (input[i].checklist) {
var checkListLen = input[i].checklist.length;
for (var j = 0; j < checkListLen; j++) {
if ( term.test(input[i].checklist[j].text) ) {
result.push(input[i]);
}
}
for (var i = 0; i < tasks.length; i++) {
var checklist = tasks[i].checklist;
if (term.test(tasks[i].text) || term.test(tasks[i].notes)) {
result.push(tasks[i]);
} else if (checklist) {
var found = _.find(checklist, function(box) { return term.test(box.text); });
if (found) { result.push(tasks[i]) }
}
}

View file

@ -1,8 +1,8 @@
li(id='task-{{::task.id}}',
ng-repeat='task in obj[list.type+"s"] | filterByTextAndNotes: obj.filterQuery | conditionalOrderBy: list.view=="dated":"date"',
class='task {{Shared.taskClasses(task, user.filters, user.preferences.dayStart, user.lastCron, list.showCompleted, main)}}',
ng-class='{"cast-target":spell && (list.type != "reward"), "locked-task":obj._locked === true}',
ng-click='spell && (list.type != "reward") && castEnd(task, "task", $event)',
li(id='task-{{::task.id}}',
ng-repeat='task in obj[list.type+"s"] | filterByTaskInfo: obj.filterQuery | conditionalOrderBy: list.view=="dated":"date"',
class='task {{Shared.taskClasses(task, user.filters, user.preferences.dayStart, user.lastCron, list.showCompleted, main)}}',
ng-class='{"cast-target":spell && (list.type != "reward"), "locked-task":obj._locked === true}',
ng-click='spell && (list.type != "reward") && castEnd(task, "task", $event)',
ng-show='shouldShow(task, list, user.preferences)',
popover-trigger='mouseenter', popover-placement="top", popover-append-to-body='{{::modal ? "false":"true"}}',
data-popover-html="{{task.popoverOpen ? '' : task.notes | markdown}}")