Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

date-streaks

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

date-streaks - npm Package Compare versions

Comparing version 1.1.4 to 1.1.5

18

dist/helpers.js

@@ -1,2 +0,2 @@

"use strict";
'use strict';

@@ -8,13 +8,9 @@ Object.defineProperty(exports, "__esModule", {

var _moment = require("moment");
var _dateFns = require('date-fns');
var _moment2 = _interopRequireDefault(_moment);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var relativeDates = exports.relativeDates = function relativeDates() {
return {
today: (0, _moment2.default)().startOf("day"),
yesterday: (0, _moment2.default)().subtract(1, "days").startOf("day"),
tomorrow: (0, _moment2.default)().add(1, "days").startOf("day")
today: (0, _dateFns.startOfDay)(new Date()),
yesterday: (0, _dateFns.startOfDay)((0, _dateFns.subDays)(new Date(), 1)),
tomorrow: (0, _dateFns.startOfDay)((0, _dateFns.addDays)(new Date(), 1))
};

@@ -25,3 +21,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, _dateFns.isValid)(date) ? 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/') : new Date(date);
});

@@ -32,4 +28,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, _dateFns.startOfDay)(b) - (0, _dateFns.startOfDay)(a);
}).reverse();
};

@@ -8,8 +8,2 @@ 'use strict';

var _moment = require('moment');
var _moment2 = _interopRequireDefault(_moment);
var _helpers = require('./helpers');
var _summary = require('./summary');

@@ -16,0 +10,0 @@

@@ -7,6 +7,2 @@ 'use strict';

var _moment = require('moment');
var _moment2 = _interopRequireDefault(_moment);
var _helpers = require('./helpers');

@@ -43,6 +39,6 @@

if (days && days.length > 1) {
start = (0, _moment2.default)(days[0]).toString();
end = (0, _moment2.default)(days[days.length - 1]).toString();
start = new Date(days[0]);
end = new Date(days[days.length - 1]);
} else {
start = (0, _moment2.default)(days[0]).toString();
start = new Date(days[0]);
end = null;

@@ -49,0 +45,0 @@ }

@@ -1,2 +0,2 @@

"use strict";
'use strict';

@@ -9,10 +9,6 @@ Object.defineProperty(exports, "__esModule", {

var _moment = require("moment");
var _dateFns = require('date-fns');
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; }

@@ -28,4 +24,3 @@

today = _relativeDates.today,
yesterday = _relativeDates.yesterday,
tomorrow = _relativeDates.tomorrow;
yesterday = _relativeDates.yesterday;

@@ -36,8 +31,8 @@ var allDates = (0, _helpers.filterInvalidDates)(dates);

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 = 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;
var first = new Date(date);
var second = sortedDates[index + 1] ? new Date(sortedDates[index + 1]) : first;
var diff = (0, _dateFns.differenceInDays)(second, first);
var isToday = acc.isToday || (0, _dateFns.differenceInDays)(date, today) === 0;
var isYesterday = acc.isYesterday || (0, _dateFns.differenceInDays)(date, yesterday) === 0;
var isInFuture = acc.isInFuture || (0, _dateFns.differenceInDays)(today, date) < 0;

@@ -74,3 +69,3 @@ if (diff === 0) {

isInFuture = result.isInFuture,
rest = _objectWithoutProperties(result, ["isToday", "isYesterday", "isInFuture"]);
rest = _objectWithoutProperties(result, ['isToday', 'isYesterday', 'isInFuture']);

@@ -77,0 +72,0 @@ return rest;

@@ -9,10 +9,6 @@ 'use strict';

var _moment = require('moment');
var _dateFns = require('date-fns');
var _moment2 = _interopRequireDefault(_moment);
var _helpers = require('./helpers');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

@@ -28,12 +24,11 @@

var pastDates = [].concat(_toConsumableArray(Array(length))).map(function (n, i) {
return (0, _moment2.default)().subtract(i, 'days').startOf('day');
var pastDates = [].concat(_toConsumableArray(Array(length))).map(function (_, i) {
return (0, _dateFns.startOfDay)((0, _dateFns.subDays)(new Date(), i));
});
var sortedDates = (0, _helpers.sortDates)(dates);
var momentDates = sortedDates.map(function (date) {
return (0, _moment2.default)(date).toString();
var sortedDates = (0, _helpers.sortDates)(dates).map(function (date) {
return (0, _dateFns.startOfDay)(date).getTime();
});
var result = pastDates.reduce(function (acc, date) {
acc = _extends({}, acc, _defineProperty({}, (0, _moment2.default)(date), momentDates.includes(date.toString())));
var result = pastDates.reduce(function (acc, pastDate) {
acc = _extends({}, acc, _defineProperty({}, pastDate, sortedDates.includes(pastDate.getTime())));
return acc;

@@ -40,0 +35,0 @@ }, {});

{
"name": "date-streaks",
"version": "1.1.4",
"version": "1.1.5",
"description": "Find a variety of streak metrics from a list of dates.",

@@ -26,3 +26,3 @@ "main": "./dist/index.js",

"dependencies": {
"moment": "2.21.0"
"date-fns": "1.30.1"
},

@@ -29,0 +29,0 @@ "devDependencies": {

@@ -1,20 +0,18 @@

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 { startOfDay, subDays, addDays } = require('date-fns');
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", () => {
var today = moment()
.startOf("day")
.toString();
describe('Date Streaks', () => {
describe('Summary', () => {
it('should report a summary of streaks', () => {
var today = startOfDay(new Date());
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'),
],
});

@@ -30,9 +28,7 @@

it("should report withinCurrentStreak when yesterday is true", () => {
var yesterday = moment()
.subtract(1, "days")
.startOf("day")
.toString();
it('should report withinCurrentStreak when yesterday is true', () => {
var today = startOfDay(new Date());
var yesterday = subDays(today, 1);
var result = summary({
dates: [new Date(yesterday)]
dates: [yesterday],
});

@@ -45,16 +41,8 @@ expect(result.currentStreak).to.equal(1);

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();
it('should report withinCurrentStreak when tomorrow is true', () => {
var today = startOfDay(new Date());
var tomorrow = addDays(today, 1);
var yesterday = subDays(today, 1);
var result = summary({
dates: [new Date(yesterday), new Date(today), new Date(tomorrow)]
dates: [yesterday, today, tomorrow],
});

@@ -67,16 +55,8 @@ expect(result.currentStreak).to.equal(3);

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();
it('should report withinCurrentStreak when future unconnected dates are reported', () => {
var today = startOfDay(new Date());
var futureDate = addDays(today, 3);
var yesterday = subDays(today, 1);
var result = summary({
dates: [new Date(yesterday), new Date(today), new Date(futureDate)]
dates: [yesterday, today, futureDate],
});

@@ -89,13 +69,8 @@ expect(result.currentStreak).to.equal(1);

it("should report todayInStreak false", () => {
var futureDate = moment()
.add(3, "days")
.startOf("day")
.toString();
var yesterday = moment()
.subtract(1, "days")
.startOf("day")
.toString();
it('should report todayInStreak false', () => {
var today = startOfDay(new Date());
var futureDate = addDays(today, 3);
var yesterday = subDays(today, 1);
var result = summary({
dates: [new Date(yesterday), new Date(futureDate)]
dates: [yesterday, futureDate],
});

@@ -108,9 +83,7 @@ expect(result.currentStreak).to.equal(1);

it("should report withinCurrentStreak false", () => {
var dateInPast = moment()
.subtract(3, "days")
.startOf("day")
.toString();
it('should report withinCurrentStreak false', () => {
var today = startOfDay(new Date());
var dateInPast = subDays(today, 3);
var result = summary({
dates: [new Date(dateInPast)]
dates: [dateInPast],
});

@@ -123,5 +96,5 @@ expect(result.currentStreak).to.equal(0);

it("should report a streak of zero", () => {
it('should report a streak of zero', () => {
var result = summary({
dates: []
dates: [],
});

@@ -135,34 +108,52 @@ expect(result.currentStreak).to.equal(0);

it("should report a streak longer than 10 days", () => {
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'),
],
});
expect(longStreak.longestStreak).to.equal(11);
});
it('should report correct streak summary with unordered dates', () => {
var longStreak = summary({
dates: [
new Date('08/19/2018'),
new Date('08/10/2018'),
new Date('08/17/2018'),
new Date('08/18/2018'),
new Date('08/15/2018'),
new Date('08/14/2018'),
new Date('08/16/2018'),
new Date('08/12/2018'),
new Date('08/13/2018'),
new Date('08/11/2018'),
new Date('08/09/2018'),
],
});
expect(longStreak.longestStreak).to.equal(11);
});
});
describe("Track record", () => {
it("should report a track record", () => {
var today = moment()
.startOf("day")
.toString();
var result = trackRecord({ dates: [new Date(today)] });
describe('Track record', () => {
it('should report a track record', () => {
var today = startOfDay(new Date());
var result = trackRecord({ dates: [today] });
expect(Object.keys(result).includes(today)).to.equal(true);
expect(result[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);

@@ -172,22 +163,24 @@ });

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'),
],
streaks: [2, 1]
streaks: [2, 1],
});
expect(result[0].start).to.equal(
moment(new Date("01/04/2018")).toString()
expect(result[0].start.getTime()).to.equal(
new Date('01/04/2018').getTime()
);
expect(result[0].end).to.equal(null);
expect(result[0].duration).to.equal(1);
expect(result[1].start).to.equal(
moment(new Date("01/01/2018")).toString()
expect(result[1].start.getTime()).to.equal(
new Date('01/01/2018').getTime()
);
expect(result[1].end).to.equal(moment(new Date("01/02/2018")).toString());
expect(result[1].end.getTime()).to.equal(
new Date('01/02/2018').getTime()
);
expect(result[1].duration).to.equal(2);

@@ -194,0 +187,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc