mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Merge pull request #7051 from piousbox/shared-code-percent
shared-code-percent
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
// TODO move to client
|
// TODO move to client
|
||||||
|
|
||||||
module.exports = function(x, y, dir) {
|
module.exports = function percent (x, y, dir) {
|
||||||
var roundFn;
|
let roundFn;
|
||||||
switch (dir) {
|
switch (dir) {
|
||||||
case "up":
|
case 'up':
|
||||||
roundFn = Math.ceil;
|
roundFn = Math.ceil;
|
||||||
break;
|
break;
|
||||||
case "down":
|
case 'down':
|
||||||
roundFn = Math.floor;
|
roundFn = Math.floor;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
module.exports = function(s) {
|
|
||||||
|
module.exports = function splitWhitespace (s) {
|
||||||
return s.split(' ');
|
return s.split(' ');
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,51 +1,40 @@
|
|||||||
import {
|
import {
|
||||||
shouldDo
|
shouldDo,
|
||||||
} from '../cron';
|
} from '../cron';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Task classes given everything about the class
|
Task classes given everything about the class
|
||||||
*/
|
*/
|
||||||
module.exports = function(task, filters, dayStart, lastCron, showCompleted, main) {
|
module.exports = function taskClasses (task, filters = [], dayStart = 0, lastCron = Number(new Date()), showCompleted = false, main = false) {
|
||||||
var classes, completed, enabled, filter, priority, ref, repeat, type, value;
|
|
||||||
if (filters == null) {
|
|
||||||
filters = [];
|
|
||||||
}
|
|
||||||
if (dayStart == null) {
|
|
||||||
dayStart = 0;
|
|
||||||
}
|
|
||||||
if (lastCron == null) {
|
|
||||||
lastCron = +(new Date);
|
|
||||||
}
|
|
||||||
if (showCompleted == null) {
|
|
||||||
showCompleted = false;
|
|
||||||
}
|
|
||||||
if (main == null) {
|
|
||||||
main = false;
|
|
||||||
}
|
|
||||||
if (!task) {
|
if (!task) {
|
||||||
return;
|
return '';
|
||||||
}
|
}
|
||||||
type = task.type, completed = task.completed, value = task.value, repeat = task.repeat, priority = task.priority;
|
let type = task.type;
|
||||||
if (main) {
|
let classes = task.type;
|
||||||
if (!task._editing) {
|
let completed = task.completed;
|
||||||
for (filter in filters) {
|
let value = task.value;
|
||||||
enabled = filters[filter];
|
let priority = task.priority;
|
||||||
if (enabled && !((ref = task.tags) != null ? ref[filter] : void 0)) {
|
|
||||||
return 'hidden';
|
if (main && !task._editing) {
|
||||||
}
|
for (let filter in filters) {
|
||||||
|
let enabled = filters[filter];
|
||||||
|
if (!task.tags) task.tags = {};
|
||||||
|
if (enabled && !task.tags[filter]) {
|
||||||
|
return 'hidden';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
classes = type;
|
classes = task.type;
|
||||||
if (task._editing) {
|
if (task._editing) {
|
||||||
classes += " beingEdited";
|
classes += ' beingEdited';
|
||||||
}
|
}
|
||||||
if (type === 'todo' || type === 'daily') {
|
if (type === 'todo' || type === 'daily') {
|
||||||
if (completed || (type === 'daily' && !shouldDo(+(new Date), task, {
|
if (completed || (type === 'daily' && !shouldDo(Number(new Date()), task, { // eslint-disable-line no-extra-parens
|
||||||
dayStart: dayStart
|
dayStart,
|
||||||
}))) {
|
}))) {
|
||||||
classes += " completed";
|
classes += ' completed';
|
||||||
} else {
|
} else {
|
||||||
classes += " uncompleted";
|
classes += ' uncompleted';
|
||||||
}
|
}
|
||||||
} else if (type === 'habit') {
|
} else if (type === 'habit') {
|
||||||
if (task.down && task.up) {
|
if (task.down && task.up) {
|
||||||
|
|||||||
@@ -21,11 +21,8 @@ const COMMON_FILES = [
|
|||||||
'!./common/script/libs/gold.js',
|
'!./common/script/libs/gold.js',
|
||||||
'!./common/script/libs/newChatMessages.js',
|
'!./common/script/libs/newChatMessages.js',
|
||||||
'!./common/script/libs/noTags.js',
|
'!./common/script/libs/noTags.js',
|
||||||
'!./common/script/libs/percent.js',
|
|
||||||
'!./common/script/libs/planGemLimits.js',
|
'!./common/script/libs/planGemLimits.js',
|
||||||
'!./common/script/libs/silver.js',
|
'!./common/script/libs/silver.js',
|
||||||
'!./common/script/libs/splitWhitespace.js',
|
|
||||||
'!./common/script/libs/taskClasses.js',
|
|
||||||
'!./common/script/public/**/*.js',
|
'!./common/script/public/**/*.js',
|
||||||
];
|
];
|
||||||
const TEST_FILES = [
|
const TEST_FILES = [
|
||||||
|
|||||||
19
test/common/libs/percent.test.js
Normal file
19
test/common/libs/percent.test.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import percent from '../../../common/script/libs/percent';
|
||||||
|
|
||||||
|
describe('percent', () => {
|
||||||
|
it('with direction "up"', () => {
|
||||||
|
expect(percent(1, 10, 'up')).to.eql(10);
|
||||||
|
expect(percent(1, 20, 'up')).to.eql(5);
|
||||||
|
expect(percent(1.22, 10.99, 'up')).to.eql(12);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('with direction "down"', () => {
|
||||||
|
expect(percent(1, 10, 'down')).to.eql(10);
|
||||||
|
expect(percent(1, 20, 'down')).to.eql(5);
|
||||||
|
expect(percent(1.22, 10.99, 'down')).to.eql(11);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('with no direction', () => {
|
||||||
|
expect(percent(1.22, 10.99)).to.eql(11);
|
||||||
|
});
|
||||||
|
});
|
||||||
7
test/common/libs/splitWhitespace.test.js
Normal file
7
test/common/libs/splitWhitespace.test.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import splitWhitespace from '../../../common/script/libs/splitWhitespace';
|
||||||
|
|
||||||
|
describe('splitWhitespace', () => {
|
||||||
|
it('returns an array', () => {
|
||||||
|
expect(splitWhitespace('a b')).to.eql(['a', 'b']);
|
||||||
|
});
|
||||||
|
});
|
||||||
82
test/common/libs/taskClasses.test.js
Normal file
82
test/common/libs/taskClasses.test.js
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import taskClasses from '../../../common/script/libs/taskClasses';
|
||||||
|
|
||||||
|
describe('taskClasses', () => {
|
||||||
|
let task = {};
|
||||||
|
let filters = {};
|
||||||
|
let result;
|
||||||
|
|
||||||
|
describe('a todo task', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
task = { type: 'todo', _editing: false, tags: { a: false } };
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is hidden', () => {
|
||||||
|
filters = { a: true };
|
||||||
|
result = taskClasses(task, filters, 0, Number(new Date()), false, true);
|
||||||
|
expect(result).to.eql('hidden');
|
||||||
|
});
|
||||||
|
it('is beingEdited', () => {
|
||||||
|
task._editing = true;
|
||||||
|
result = taskClasses(task, filters);
|
||||||
|
expect(result.split(' ').indexOf('beingEdited')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
it('is completed', () => {
|
||||||
|
task.completed = true;
|
||||||
|
result = taskClasses(task, filters);
|
||||||
|
expect(result.split(' ').indexOf('completed')).to.not.eql(-1);
|
||||||
|
task.completed = false;
|
||||||
|
result = taskClasses(task, filters);
|
||||||
|
expect(result.split(' ').indexOf('completed')).to.eql(-1);
|
||||||
|
expect(result.split(' ').indexOf('uncompleted')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('a daily task', () => {
|
||||||
|
it('is completed', () => {
|
||||||
|
task = { type: 'daily' };
|
||||||
|
result = taskClasses(task);
|
||||||
|
expect(result.split(' ').indexOf('completed')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is uncompleted'); // this requires stubbing the internal dependency shouldDo in taskClasses
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('a habit', () => {
|
||||||
|
it('that is wide', () => {
|
||||||
|
task = { type: 'habit', up: true, down: true };
|
||||||
|
result = taskClasses(task);
|
||||||
|
expect(result.split(' ').indexOf('habit-wide')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
it('that is narrow', () => {
|
||||||
|
task = { type: 'habit' };
|
||||||
|
result = taskClasses(task);
|
||||||
|
expect(result.split(' ').indexOf('habit-narrow')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('varies based on priority', () => {
|
||||||
|
it('trivial', () => {
|
||||||
|
task.priority = 0.1;
|
||||||
|
result = taskClasses(task);
|
||||||
|
expect(result.split(' ').indexOf('difficulty-trivial')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
it('hard', () => {
|
||||||
|
task.priority = 2;
|
||||||
|
result = taskClasses(task);
|
||||||
|
expect(result.split(' ').indexOf('difficulty-hard')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('varies based on value', () => {
|
||||||
|
it('color-worst', () => {
|
||||||
|
task.value = -30;
|
||||||
|
result = taskClasses(task);
|
||||||
|
expect(result.split(' ').indexOf('color-worst')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
it('color-neutral', () => {
|
||||||
|
task.value = 0;
|
||||||
|
result = taskClasses(task);
|
||||||
|
expect(result.split(' ').indexOf('color-neutral')).to.not.eql(-1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user