npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

datetimesfromduration

v1.4.0

Published

For if you have a duration (of, say, a task), and you want a start time and an end time from this duration.

Downloads

5

Readme

DatetimesFromDuration

To make schedules with durations (for tasks), you need two of the three pieces of the puzzle: duration, start time, and end time. The user provides the duration, the time the duration is submitted provides the start time, and this thing creates the end time from these two. I've needed this in many projects, so I've made a separate project for it.

const expect = require('chai').expect;
const sinon = require('sinon');
const DFD = require('../index.js');

describe("Datetimes from Duration", () => {
    let getStartDatetime;
    let getEndDatetimePOSIX;

    // start time 1565242962879
    // end time 1565260962879

    beforeEach(() => {
        getStartDatetime = sinon.stub(DFD.prototype, "getStartDatetime").returns("8/8/2019, 1:42:42 AM");
        getEndDatetimePOSIX = sinon.stub(DFD.prototype, "getEndDatetimePOSIX").returns(1565260962879);
    });

    afterEach(() => {
        getStartDatetime.restore();
        getEndDatetimePOSIX.restore();
    });

    it("Returns now for the start time.", () => {
        const dfd = new DFD(5); // Duration is in hours.
        expect(dfd.start).to.equal("8/8/2019, 1:42:42 AM");
    });
    
    it("Returns five hours from now for the end time.", () => {
        const dfd = new DFD(5); 
        expect(dfd.end).to.equal("8/8/2019, 6:42:42 AM");
    });
    
    it("Returns five minutes from now for the end time.", () => {
        getEndDatetimePOSIX.restore();
        const dfd = new DFD(5).inMinutes(); 
        expect(dfd.start).to.equal("8/8/2019, 1:42:42 AM");
        expect(dfd.end).to.equal("8/8/2019, 1:47:42 AM");
    });
    
    it("Works if the number is repped as a string", () => {
        const dfd = new DFD("5"); 
        expect(dfd.start).to.equal("8/8/2019, 1:42:42 AM");
        expect(dfd.end).to.equal("8/8/2019, 6:42:42 AM");

    });

    it("Handles an array of one number", () => {
        //
        const dfd = new DFD([5]);
        expect(dfd[0].end).to.equal("8/8/2019, 6:42:42 AM");
    });

    it("Handles an array of one number repped as a string", () => {
        const dfd = new DFD(["5"]); 
        expect(dfd[0].start).to.equal("8/8/2019, 1:42:42 AM");
        expect(dfd[0].end).to.equal("8/8/2019, 6:42:42 AM");

    });

    it("Makes the start time of the second task the same as the end time of the first task", () => {
        const dfd = new DFD([5, 6]);
        expect(dfd[0].end).to.equal("8/8/2019, 6:42:42 AM");
        expect(dfd[1].start).to.equal("8/8/2019, 6:42:42 AM");
        
    });
    
    it("Makes the end time of the second task 6 hours after the end time of the first task", () => {
        getEndDatetimePOSIX.restore();
        const dfd = new DFD([5, 6]);
        expect(dfd[0].start).to.equal("8/8/2019, 1:42:42 AM");
        expect(dfd[0].end).to.equal("8/8/2019, 6:42:42 AM");
        expect(dfd[1].end).to.equal("8/8/2019, 12:42:42 PM");
        
    });
    
    it("Returns a series of start and end times if an array is passed", () => {
        getEndDatetimePOSIX.restore();
        const dfd = new DFD([5, 6, 3]);
        expect(dfd[0].end).to.equal("8/8/2019, 6:42:42 AM");
        // End time of second should be new Date(1565282562879).toLocaleString();
        expect(dfd[1].start).to.equal("8/8/2019, 6:42:42 AM");
        expect(dfd[1].end).to.equal("8/8/2019, 12:42:42 PM");
        expect(dfd[2].start).to.equal("8/8/2019, 12:42:42 PM");
        expect(dfd[2].end).to.equal("8/8/2019, 3:42:42 PM");
    });
    
    it("Returns a series of start and end times if an array of numbers repped as strings is passed", () => {
        getEndDatetimePOSIX.restore();
        const dfd = new DFD(["5", "6", "3"]);
        expect(dfd[0].end).to.equal("8/8/2019, 6:42:42 AM");
        // End time of second should be new Date(1565282562879).toLocaleString();
        expect(dfd[1].start).to.equal("8/8/2019, 6:42:42 AM");
        expect(dfd[1].end).to.equal("8/8/2019, 12:42:42 PM");
        expect(dfd[2].start).to.equal("8/8/2019, 12:42:42 PM");
        expect(dfd[2].end).to.equal("8/8/2019, 3:42:42 PM");
    });
    
    it("Batches startEndTime objects under a date", () => {
        getEndDatetimePOSIX.restore();
        const dfd = new DFD(["5", "6", "3"]).splitIt();
        let expectedResult = {
            "8/8/2019": [
                {start: "1:42:42 AM", end: "6:42:42 AM"},
                {start: "6:42:42 AM", end: "12:42:42 PM"},
                {start: "12:42:42 PM", end: "3:42:42 PM"}
            ]
        }
        expect(dfd).to.deep.include(expectedResult);
    });
    
    it("Batches startEndTime objects under a date, even if a single duration is put in", () => {
        getEndDatetimePOSIX.restore();
        const dfd = new DFD(5).splitIt();
        let expectedResult = {
            "8/8/2019": [
                {start: "1:42:42 AM", end: "6:42:42 AM"}
            ]
        }
        expect(dfd).to.deep.include(expectedResult);
    });
    
    it("Let's me get the dates from the dates property", () => {
        getEndDatetimePOSIX.restore();
        const dfd = new DFD(5).splitIt();
        let expectedResult = {
            "8/8/2019": [
                {start: "1:42:42 AM", end: "6:42:42 AM"}
            ]
        }
        expect(dfd.dates).to.deep.include(expectedResult);
    });
});