mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
shared-code-tags-gold-silver (#7056)
This commit is contained in:
committed by
Matteo Pagliazzi
parent
a29dd1a1c7
commit
2f1329254e
@@ -1,21 +1,14 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
/*
|
||||
Are there tags applied?
|
||||
*/
|
||||
|
||||
// TODO move to client
|
||||
|
||||
module.exports = function(userTags, taskTags) {
|
||||
var arr;
|
||||
arr = [];
|
||||
_.each(userTags, function(t) {
|
||||
if (t == null) {
|
||||
return;
|
||||
}
|
||||
if (taskTags != null ? taskTags[t.id] : void 0) {
|
||||
return arr.push(t.name);
|
||||
}
|
||||
module.exports = function appliedTags (userTags, taskTags = {}) {
|
||||
let arr = userTags.filter(tag => {
|
||||
return taskTags[tag.id];
|
||||
}).map(tag => {
|
||||
return tag.name;
|
||||
});
|
||||
return arr.join(', ');
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// TODO move to client
|
||||
|
||||
module.exports = function(num) {
|
||||
module.exports = function gold (num) {
|
||||
if (num) {
|
||||
return Math.floor(num);
|
||||
} else {
|
||||
return "0";
|
||||
return '0';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,8 +6,8 @@ are any tags active?
|
||||
|
||||
// TODO move to client
|
||||
|
||||
module.exports = function(tags) {
|
||||
return _.isEmpty(tags) || _.isEmpty(_.filter(tags, function(t) {
|
||||
module.exports = function noTags (tags) {
|
||||
return _.isEmpty(tags) || _.isEmpty(_.filter(tags, (t) => {
|
||||
return t;
|
||||
}));
|
||||
};
|
||||
|
||||
@@ -4,10 +4,11 @@ Silver amount from their money
|
||||
|
||||
// TODO move to client
|
||||
|
||||
module.exports = function(num) {
|
||||
module.exports = function silver (num) {
|
||||
if (num) {
|
||||
return ("0" + Math.floor((num - Math.floor(num)) * 100)).slice(-2);
|
||||
let centCount = Math.floor((num - Math.floor(num)) * 100);
|
||||
return `0${centCount}`.slice(-2);
|
||||
} else {
|
||||
return "00";
|
||||
return '00';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -13,15 +13,11 @@ const COMMON_FILES = [
|
||||
'!./common/script/ops/reset.js',
|
||||
'!./common/script/fns/crit.js',
|
||||
'!./common/script/fns/randomDrop.js',
|
||||
'!./common/script/libs/appliedTags.js',
|
||||
'!./common/script/libs/countExists.js',
|
||||
'!./common/script/libs/encodeiCalLink.js',
|
||||
'!./common/script/libs/friendlyTimestamp.js',
|
||||
'!./common/script/libs/gold.js',
|
||||
'!./common/script/libs/newChatMessages.js',
|
||||
'!./common/script/libs/noTags.js',
|
||||
'!./common/script/libs/planGemLimits.js',
|
||||
'!./common/script/libs/silver.js',
|
||||
'!./common/script/public/**/*.js',
|
||||
];
|
||||
const TEST_FILES = [
|
||||
|
||||
10
test/common/libs/appliedTags.test.js
Normal file
10
test/common/libs/appliedTags.test.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import appliedTags from '../../../common/script/libs/appliedTags';
|
||||
|
||||
describe('appliedTags', () => {
|
||||
it('returns the tasks', () => {
|
||||
let userTags = [{ id: 'tag1', name: 'tag 1' }, { id: 'tag2', name: 'tag 2' }, { id: 'tag3', name: 'tag 3' }];
|
||||
let taskTags = { tag2: true, tag3: true };
|
||||
let result = appliedTags(userTags, taskTags);
|
||||
expect(result).to.eql('tag 2, tag 3');
|
||||
});
|
||||
});
|
||||
11
test/common/libs/gold.test.js
Normal file
11
test/common/libs/gold.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import gold from '../../../common/script/libs/gold';
|
||||
|
||||
describe('gold', () => {
|
||||
it('is 0', () => {
|
||||
expect(gold()).to.eql('0');
|
||||
});
|
||||
|
||||
it('is 5 in 5.2 of gold', () => {
|
||||
expect(gold(5.2)).to.eql(5);
|
||||
});
|
||||
});
|
||||
13
test/common/libs/noTags.test.js
Normal file
13
test/common/libs/noTags.test.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import noTags from '../../../common/script/libs/noTags';
|
||||
|
||||
describe('noTags', () => {
|
||||
it('returns true for no tags', () => {
|
||||
let result = noTags([]);
|
||||
expect(result).to.eql(true);
|
||||
});
|
||||
|
||||
it('returns false for some tags', () => {
|
||||
let result = noTags(['a', 'b', 'c']);
|
||||
expect(result).to.eql(false);
|
||||
});
|
||||
});
|
||||
19
test/common/libs/silver.test.js
Normal file
19
test/common/libs/silver.test.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import silver from '../../../common/script/libs/silver';
|
||||
|
||||
describe('silver', () => {
|
||||
it('is 0', () => {
|
||||
expect(silver(0)).to.eql('00');
|
||||
});
|
||||
|
||||
it('20 coins in 5.2 of gold: two decimal places', () => {
|
||||
expect(silver(5.2)).to.eql('20');
|
||||
});
|
||||
|
||||
it('4 coint in 5.04 of gold: one decimal place', () => {
|
||||
expect(silver(5.04)).to.eql('04');
|
||||
});
|
||||
|
||||
it('is no value', () => {
|
||||
expect(silver()).to.eql('00');
|
||||
});
|
||||
});
|
||||
61
test/common/libs/taskDefaults.test.js
Normal file
61
test/common/libs/taskDefaults.test.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import taskDefaults from '../../../common/script/libs/taskDefaults';
|
||||
|
||||
describe('taskDefaults', () => {
|
||||
it('applies defaults to undefined type or habit', () => {
|
||||
let task = taskDefaults();
|
||||
expect(task.type).to.eql('habit');
|
||||
expect(task._id).to.exist;
|
||||
expect(task.text).to.eql(task._id);
|
||||
expect(task.tags).to.eql([]);
|
||||
expect(task.value).to.eql(0);
|
||||
expect(task.priority).to.eql(1);
|
||||
expect(task.up).to.eql(true);
|
||||
expect(task.down).to.eql(true);
|
||||
expect(task.history).to.eql([]);
|
||||
});
|
||||
|
||||
it('applies defaults to a daily', () => {
|
||||
let task = taskDefaults({ type: 'daily' });
|
||||
expect(task.type).to.eql('daily');
|
||||
expect(task._id).to.exist;
|
||||
expect(task.text).to.eql(task._id);
|
||||
expect(task.tags).to.eql([]);
|
||||
expect(task.value).to.eql(0);
|
||||
expect(task.priority).to.eql(1);
|
||||
expect(task.history).to.eql([]);
|
||||
expect(task.completed).to.eql(false);
|
||||
expect(task.streak).to.eql(0);
|
||||
expect(task.repeat).to.eql({
|
||||
m: true,
|
||||
t: true,
|
||||
w: true,
|
||||
th: true,
|
||||
f: true,
|
||||
s: true,
|
||||
su: true,
|
||||
});
|
||||
expect(task.frequency).to.eql('weekly');
|
||||
expect(task.startDate).to.exist;
|
||||
});
|
||||
|
||||
it('applies defaults a reward', () => {
|
||||
let task = taskDefaults({ type: 'reward' });
|
||||
expect(task.type).to.eql('reward');
|
||||
expect(task._id).to.exist;
|
||||
expect(task.text).to.eql(task._id);
|
||||
expect(task.tags).to.eql([]);
|
||||
expect(task.value).to.eql(10);
|
||||
expect(task.priority).to.eql(1);
|
||||
});
|
||||
|
||||
it('applies defaults a todo', () => {
|
||||
let task = taskDefaults({ type: 'todo' });
|
||||
expect(task.type).to.eql('todo');
|
||||
expect(task._id).to.exist;
|
||||
expect(task.text).to.eql(task._id);
|
||||
expect(task.tags).to.eql([]);
|
||||
expect(task.value).to.eql(0);
|
||||
expect(task.priority).to.eql(1);
|
||||
expect(task.completed).to.eql(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user