mickmcc / node-caltime

Node.js Module to create, add and subtract time-spans
MIT License
3 stars 1 forks source link

Proposed intersectDateSpans() example doesn't work #6

Open kler opened 5 years ago

kler commented 5 years ago

Observed Behaviour

When executing th example code for the intersectDateSpans(), there is an error:

TypeError: Cannot read property '0' of undefined
    at run (/app/logic.js:155:9)
    at Command.<anonymous> (/app/contact.js:50:17)
    at Command.listener (/app/node_modules/commander/index.js:291:8)
    at Command.emit (events.js:209:13)
    at Command.parseArgs (/app/node_modules/commander/index.js:672:12)
    at Command.parse (/app/node_modules/commander/index.js:459:21)
    at Object.<anonymous> (/app/contact.js:101:9)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)

Expected Behaviour

result.length; // 1 overlap between spanB and spanC
result[0].getBegin(); // 10:00am
result[0].getEnd(); // 10:30am

Steps to Reproduce

From https://github.com/mickmcc/node-caltime#intersectdatespans

const caltime = require('caltime');
const datespanCtor = caltime.dateSpan;
const intersectDateSpans = caltime.intersectDateSpans;
let spanListA = null;
let spanListB = null;
// DateSpan object which represents 09:00am - 10:00am.
var beginDate = new Date(2017, 10, 15, 9, 0, 0, 0);
var spanA = datespanCtor(beginDate, 60, 0, 0);
// create a DateSpan object which represents 10:00am - 11:00am.
beginDate = new Date(2017, 10, 15, 10, 0, 0, 0);
var spanB = datespanCtor(beginDate, 60, 0, 0);
// create a DateSpan object which represents 10:00am - 10:30am.
beginDate = new Date(2017, 10, 15, 10, 0, 0, 0);
var spanC = datespanCtor(beginDate, 15, 0, 0);
// create a DateSpan object which represents 14:00 - 15:00.
beginDate = new Date(2017, 10, 15, 14, 0, 0, 0);
var spanD = datespanCtor(beginDate, 60, 0, 0);
// populate the arrays
spanListA = [ spanA, spanB ];
spanListB = [ spanC, spanD ];
// sort in descending order
let result = intersectDateSpans(spanListA, spanListB);
result.length; // 1 overlap between spanB and spanC
result[0].getBegin(); // 10:00am
result[0].getEnd(); // 10:30am

System Configuration

caltime version: 1.4.5 Node.js version: v12.10.0 NPM version:6.10.3 lodash version: 4.17.15 moment-timezone version: 0.5.26

kler commented 4 years ago

Actually the underlaying problem is that all calls to datespanCtor() in the README are wrong.

The functions is defined as:

function dateSpanFunc(inBegin,
                                      inEnd=null,
                                      inDurationMins=0,
                                      inDurationSecs=0,
                                      inDurationMs=0) {

But throughout the README the inEnd param is omitted.

I propose that documentation is updated, and that a check is provided, not allowing inEnd to be anything else than a Date or null (integer is not accepted).

I will provide a PR for this.