date-streaks
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -8,3 +8,3 @@ Object.defineProperty(exports, "__esModule", { | ||
var _moment = require('moment'); | ||
var _moment = require("moment"); | ||
@@ -16,4 +16,5 @@ var _moment2 = _interopRequireDefault(_moment); | ||
var relativeDates = exports.relativeDates = { | ||
today: (0, _moment2.default)().startOf('day'), | ||
yesterday: (0, _moment2.default)().subtract(1, 'days').startOf('day') | ||
today: (0, _moment2.default)().startOf("day"), | ||
yesterday: (0, _moment2.default)().subtract(1, "days").startOf("day"), | ||
tomorrow: (0, _moment2.default)().add(1, "days").startOf("day") | ||
}; | ||
@@ -23,3 +24,3 @@ | ||
return dates.filter(function (date) { | ||
return !(0, _moment2.default)(date).isValid() ? console.error('The date \'' + date + '\' is not in a valid date format and date-streaks is ignoring it. Browsers do not consistently support this and this package\'s results may fail. Verify the array of dates you\'re passing to date-streaks are all valid date strings. http://momentjs.com/docs/#/parsing/string/') : (0, _moment2.default)(date); | ||
return !(0, _moment2.default)(date).isValid() ? console.error("The date '" + date + "' is not in a valid date format and date-streaks is ignoring it. Browsers do not consistently support this and this package's results may fail. Verify the array of dates you're passing to date-streaks are all valid date strings. http://momentjs.com/docs/#/parsing/string/") : (0, _moment2.default)(date); | ||
}); | ||
@@ -30,4 +31,4 @@ }; | ||
return dates.sort(function (a, b) { | ||
return (0, _moment2.default)((0, _moment2.default)(b).startOf('day')).format('X') - (0, _moment2.default)((0, _moment2.default)(a).startOf('day')).format('X'); | ||
return (0, _moment2.default)((0, _moment2.default)(b).startOf("day")).format("X") - (0, _moment2.default)((0, _moment2.default)(a).startOf("day")).format("X"); | ||
}).reverse(); | ||
}; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -9,10 +9,12 @@ Object.defineProperty(exports, "__esModule", { | ||
var _moment = require('moment'); | ||
var _moment = require("moment"); | ||
var _moment2 = _interopRequireDefault(_moment); | ||
var _helpers = require('./helpers'); | ||
var _helpers = require("./helpers"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
@@ -24,3 +26,4 @@ | ||
var today = _helpers.relativeDates.today, | ||
yesterday = _helpers.relativeDates.yesterday; | ||
yesterday = _helpers.relativeDates.yesterday, | ||
tomorrow = _helpers.relativeDates.tomorrow; | ||
@@ -30,8 +33,9 @@ var allDates = (0, _helpers.filterInvalidDates)(dates); | ||
return sortedDates.reduce(function (acc, date, index) { | ||
var result = sortedDates.reduce(function (acc, date, index) { | ||
var first = (0, _moment2.default)(date); | ||
var second = sortedDates[index + 1] ? (0, _moment2.default)(sortedDates[index + 1]) : first; | ||
var diff = second.diff(first, 'days'); | ||
var isToday = (0, _moment2.default)(date).diff(today) === 0; | ||
var isYesterday = (0, _moment2.default)(date).diff(yesterday) === 0; | ||
var diff = second.diff(first, "days"); | ||
var isToday = acc.isToday || (0, _moment2.default)(date).diff(today) === 0; | ||
var isYesterday = acc.isYesterday || (0, _moment2.default)(date).diff(yesterday) === 0; | ||
var isInFuture = acc.isInFuture || (0, _moment2.default)(today).diff(date) < 0; | ||
@@ -48,4 +52,7 @@ if (diff === 0) { | ||
longestStreak: Math.max.apply(Math, _toConsumableArray(acc.streaks)), | ||
withinCurrentStreak: acc.withinCurrentStreak ? true : isToday || isYesterday, | ||
currentStreak: isToday || isYesterday ? acc.streaks[acc.streaks.length - 1] : 0 | ||
withinCurrentStreak: acc.isToday || acc.isYesterday || acc.isInFuture || isToday || isYesterday || isInFuture, | ||
currentStreak: isToday || isYesterday || isInFuture ? acc.streaks[acc.streaks.length - 1] : 0, | ||
isInFuture: isInFuture, | ||
isYesterday: isYesterday, | ||
isToday: isToday | ||
}); | ||
@@ -57,6 +64,16 @@ }, { | ||
todayInStreak: false, | ||
withinCurrentStreak: false | ||
withinCurrentStreak: false, | ||
isInFuture: false, | ||
isToday: false, | ||
isYesterday: false | ||
}); | ||
var isToday = result.isToday, | ||
isYesterday = result.isYesterday, | ||
isInFuture = result.isInFuture, | ||
rest = _objectWithoutProperties(result, ["isToday", "isYesterday", "isInFuture"]); | ||
return rest; | ||
} | ||
exports.default = summary; |
{ | ||
"name": "date-streaks", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Find a variety of streak metrics from a list of dates.", | ||
"main": "./dist/index.js", | ||
"scripts": { | ||
"test": "mocha test/index.js", | ||
"test": "npm run build && mocha test/index.js", | ||
"build": "babel src --presets babel-preset-env --out-dir dist", | ||
@@ -9,0 +9,0 @@ "prepublish": "npm run build" |
@@ -1,56 +0,143 @@ | ||
var moment = require('moment'); | ||
var expect = require('chai').expect; | ||
var summary = require('../dist/summary').default; | ||
var trackRecord = require('../dist/trackRecord').default; | ||
var streakRanges = require('../dist/streakRanges').default; | ||
var moment = require("moment"); | ||
var expect = require("chai").expect; | ||
var summary = require("../dist/summary").default; | ||
var trackRecord = require("../dist/trackRecord").default; | ||
var streakRanges = require("../dist/streakRanges").default; | ||
describe('Date Streaks', () => { | ||
describe('Summary', () => { | ||
it('should report a summary of streaks', () => { | ||
describe("Date Streaks", () => { | ||
describe("Summary", () => { | ||
it("should report a summary of streaks", () => { | ||
var today = moment() | ||
.startOf('day') | ||
.startOf("day") | ||
.toString(); | ||
var resultWithToday = summary({ | ||
var result = summary({ | ||
dates: [ | ||
new Date(today), | ||
new Date('01/01/2018'), | ||
new Date('01/02/2018'), | ||
new Date('01/03/2018') | ||
new Date("01/01/2018"), | ||
new Date("01/02/2018"), | ||
new Date("01/03/2018") | ||
] | ||
}); | ||
expect(resultWithToday.currentStreak).to.equal(1); | ||
expect(resultWithToday.longestStreak).to.equal(3); | ||
expect(resultWithToday.streaks[0]).to.equal(3); | ||
expect(resultWithToday.streaks[1]).to.equal(1); | ||
expect(resultWithToday.todayInStreak).to.equal(true); | ||
expect(resultWithToday.withinCurrentStreak).to.equal(true); | ||
expect(result.currentStreak).to.equal(1); | ||
expect(result.longestStreak).to.equal(3); | ||
expect(result.streaks[0]).to.equal(3); | ||
expect(result.streaks[1]).to.equal(1); | ||
expect(result.todayInStreak).to.equal(true); | ||
expect(result.withinCurrentStreak).to.equal(true); | ||
}); | ||
it('should report withinCurrentStreak when yesterday is true', () => { | ||
it("should report withinCurrentStreak when yesterday is true", () => { | ||
var yesterday = moment() | ||
.subtract(1, 'days') | ||
.startOf('day') | ||
.subtract(1, "days") | ||
.startOf("day") | ||
.toString(); | ||
var resultWithToday = summary({ | ||
var result = summary({ | ||
dates: [new Date(yesterday)] | ||
}); | ||
expect(resultWithToday.todayInStreak).to.equal(false); | ||
expect(resultWithToday.withinCurrentStreak).to.equal(true); | ||
expect(result.currentStreak).to.equal(1); | ||
expect(result.longestStreak).to.equal(1); | ||
expect(result.todayInStreak).to.equal(false); | ||
expect(result.withinCurrentStreak).to.equal(true); | ||
}); | ||
it('should report a streak longer than 10 days', () => { | ||
it("should report withinCurrentStreak when tomorrow is true", () => { | ||
var today = moment() | ||
.startOf("day") | ||
.toString(); | ||
var tomorrow = moment() | ||
.add(1, "days") | ||
.startOf("day") | ||
.toString(); | ||
var yesterday = moment() | ||
.subtract(1, "days") | ||
.startOf("day") | ||
.toString(); | ||
var result = summary({ | ||
dates: [new Date(yesterday), new Date(today), new Date(tomorrow)] | ||
}); | ||
expect(result.currentStreak).to.equal(3); | ||
expect(result.longestStreak).to.equal(3); | ||
expect(result.todayInStreak).to.equal(true); | ||
expect(result.withinCurrentStreak).to.equal(true); | ||
}); | ||
it("should report withinCurrentStreak when future unconnected dates are reported", () => { | ||
var today = moment() | ||
.startOf("day") | ||
.toString(); | ||
var futureDate = moment() | ||
.add(3, "days") | ||
.startOf("day") | ||
.toString(); | ||
var yesterday = moment() | ||
.subtract(1, "days") | ||
.startOf("day") | ||
.toString(); | ||
var result = summary({ | ||
dates: [new Date(yesterday), new Date(today), new Date(futureDate)] | ||
}); | ||
expect(result.currentStreak).to.equal(1); | ||
expect(result.longestStreak).to.equal(2); | ||
expect(result.todayInStreak).to.equal(true); | ||
expect(result.withinCurrentStreak).to.equal(true); | ||
}); | ||
it("should report todayInStreak false", () => { | ||
var futureDate = moment() | ||
.add(3, "days") | ||
.startOf("day") | ||
.toString(); | ||
var yesterday = moment() | ||
.subtract(1, "days") | ||
.startOf("day") | ||
.toString(); | ||
var result = summary({ | ||
dates: [new Date(yesterday), new Date(futureDate)] | ||
}); | ||
expect(result.currentStreak).to.equal(1); | ||
expect(result.longestStreak).to.equal(1); | ||
expect(result.todayInStreak).to.equal(false); | ||
expect(result.withinCurrentStreak).to.equal(true); | ||
}); | ||
it("should report withinCurrentStreak false", () => { | ||
var dateInPast = moment() | ||
.subtract(3, "days") | ||
.startOf("day") | ||
.toString(); | ||
var result = summary({ | ||
dates: [new Date(dateInPast)] | ||
}); | ||
expect(result.currentStreak).to.equal(0); | ||
expect(result.longestStreak).to.equal(1); | ||
expect(result.todayInStreak).to.equal(false); | ||
expect(result.withinCurrentStreak).to.equal(false); | ||
}); | ||
it("should report a streak of zero", () => { | ||
var result = summary({ | ||
dates: [] | ||
}); | ||
expect(result.currentStreak).to.equal(0); | ||
expect(result.longestStreak).to.equal(0); | ||
expect(result.streaks[0]).to.equal(1); | ||
expect(result.todayInStreak).to.equal(false); | ||
expect(result.withinCurrentStreak).to.equal(false); | ||
}); | ||
it("should report a streak longer than 10 days", () => { | ||
var longStreak = summary({ | ||
dates: [ | ||
new Date('08/19/2018'), | ||
new Date('08/18/2018'), | ||
new Date('08/17/2018'), | ||
new Date('08/16/2018'), | ||
new Date('08/15/2018'), | ||
new Date('08/14/2018'), | ||
new Date('08/13/2018'), | ||
new Date('08/12/2018'), | ||
new Date('08/11/2018'), | ||
new Date('08/10/2018'), | ||
new Date('08/09/2018') | ||
new Date("08/19/2018"), | ||
new Date("08/18/2018"), | ||
new Date("08/17/2018"), | ||
new Date("08/16/2018"), | ||
new Date("08/15/2018"), | ||
new Date("08/14/2018"), | ||
new Date("08/13/2018"), | ||
new Date("08/12/2018"), | ||
new Date("08/11/2018"), | ||
new Date("08/10/2018"), | ||
new Date("08/09/2018") | ||
] | ||
@@ -62,14 +149,14 @@ }); | ||
describe('Track record', () => { | ||
it('should report a track record', () => { | ||
describe("Track record", () => { | ||
it("should report a track record", () => { | ||
var today = moment() | ||
.startOf('day') | ||
.startOf("day") | ||
.toString(); | ||
var resultWithToday = trackRecord({ dates: [new Date(today)] }); | ||
var result = trackRecord({ dates: [new Date(today)] }); | ||
expect(Object.keys(resultWithToday).includes(today)).to.equal(true); | ||
expect(Object.keys(result).includes(today)).to.equal(true); | ||
}); | ||
it('should take a custom length of days', () => { | ||
var result = trackRecord({ dates: [new Date('3/19/2018')], length: 10 }); | ||
it("should take a custom length of days", () => { | ||
var result = trackRecord({ dates: [new Date("3/19/2018")], length: 10 }); | ||
expect(Object.keys(result).length).to.equal(10); | ||
@@ -79,9 +166,9 @@ }); | ||
describe('Streak Ranges', () => { | ||
it('should report ranges of streaks', () => { | ||
describe("Streak Ranges", () => { | ||
it("should report ranges of streaks", () => { | ||
var result = streakRanges({ | ||
dates: [ | ||
new Date('01/01/2018'), | ||
new Date('01/02/2018'), | ||
new Date('01/04/2018') | ||
new Date("01/01/2018"), | ||
new Date("01/02/2018"), | ||
new Date("01/04/2018") | ||
], | ||
@@ -92,3 +179,3 @@ streaks: [2, 1] | ||
expect(result[0].start).to.equal( | ||
moment(new Date('01/04/2018')).toString() | ||
moment(new Date("01/04/2018")).toString() | ||
); | ||
@@ -98,5 +185,5 @@ expect(result[0].end).to.equal(null); | ||
expect(result[1].start).to.equal( | ||
moment(new Date('01/01/2018')).toString() | ||
moment(new Date("01/01/2018")).toString() | ||
); | ||
expect(result[1].end).to.equal(moment(new Date('01/02/2018')).toString()); | ||
expect(result[1].end).to.equal(moment(new Date("01/02/2018")).toString()); | ||
expect(result[1].duration).to.equal(2); | ||
@@ -103,0 +190,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19787
340
10