diff --git a/common/script/libs/appliedTags.js b/common/script/libs/appliedTags.js index 513405bfeb..c65074033e 100644 --- a/common/script/libs/appliedTags.js +++ b/common/script/libs/appliedTags.js @@ -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(', '); }; diff --git a/common/script/libs/gold.js b/common/script/libs/gold.js index 7ce971aeec..83d9531d5e 100644 --- a/common/script/libs/gold.js +++ b/common/script/libs/gold.js @@ -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'; } }; diff --git a/common/script/libs/noTags.js b/common/script/libs/noTags.js index fa47cd70d0..16a59d1d31 100644 --- a/common/script/libs/noTags.js +++ b/common/script/libs/noTags.js @@ -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; })); }; diff --git a/common/script/libs/silver.js b/common/script/libs/silver.js index 5a2b8f98f8..1d3620f602 100644 --- a/common/script/libs/silver.js +++ b/common/script/libs/silver.js @@ -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'; } }; diff --git a/tasks/gulp-eslint.js b/tasks/gulp-eslint.js index 0461636744..5e0afc3357 100644 --- a/tasks/gulp-eslint.js +++ b/tasks/gulp-eslint.js @@ -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 = [ diff --git a/test/common/libs/appliedTags.test.js b/test/common/libs/appliedTags.test.js new file mode 100644 index 0000000000..204d30f5bd --- /dev/null +++ b/test/common/libs/appliedTags.test.js @@ -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'); + }); +}); diff --git a/test/common/libs/gold.test.js b/test/common/libs/gold.test.js new file mode 100644 index 0000000000..2cdfc3ef65 --- /dev/null +++ b/test/common/libs/gold.test.js @@ -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); + }); +}); diff --git a/test/common/libs/noTags.test.js b/test/common/libs/noTags.test.js new file mode 100644 index 0000000000..dcd2481854 --- /dev/null +++ b/test/common/libs/noTags.test.js @@ -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); + }); +}); diff --git a/test/common/libs/silver.test.js b/test/common/libs/silver.test.js new file mode 100644 index 0000000000..5bd614fcd1 --- /dev/null +++ b/test/common/libs/silver.test.js @@ -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'); + }); +}); diff --git a/test/common/libs/taskDefaults.test.js b/test/common/libs/taskDefaults.test.js new file mode 100644 index 0000000000..c970634137 --- /dev/null +++ b/test/common/libs/taskDefaults.test.js @@ -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); + }); +});