CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-deep-eql

Improved deep equality testing for Node.js and the browser with support for complex types and circular references.

Overall
score

96%

Overview
Eval results
Files

task.mdevals/scenario-4/

Date Comparison Utility

Overview

Build a utility function that compares date-related data structures and determines if they represent the same point in time or contain equivalent date values. Your solution should handle various date representations including Date objects, objects containing dates, and arrays of dates.

Requirements

Implement a module that exports a single function areDatesEqual with the following behavior:

Function Signature

function areDatesEqual(dateValue1, dateValue2)

Parameters

  • dateValue1: Can be a Date object, an object containing Date properties, or an array of Dates
  • dateValue2: Can be a Date object, an object containing Date properties, or an array of Dates

Return Value

Returns true if the date values are equivalent, false otherwise.

Behavior

  1. Single Date Comparison: When both parameters are Date objects, return true if they represent the same timestamp
  2. Invalid Date Handling: Two invalid Date objects (e.g., new Date('invalid')) should be considered equal
  3. Object with Date Properties: When comparing objects that contain Date properties, all Date values must match
  4. Array of Dates: When comparing arrays of Date objects, all corresponding elements must represent the same timestamps
  5. Nested Structures: Support comparing nested objects and arrays containing Date values

Implementation Notes

  • Do not manually compare timestamps or use .getTime() - use appropriate comparison techniques
  • Handle edge cases like invalid dates, empty arrays, and null values appropriately
  • The solution should work for arbitrarily nested structures containing dates

Dependencies { .dependencies }

deep-eql { .dependency }

Provides deep equality comparison support.

Test Cases

Test 1: Basic Date Comparison @test

File: date-comparison.test.js

const { areDatesEqual } = require('./date-comparison');

// Test: Compare two identical dates
const date1 = new Date('2023-01-15T10:30:00Z');
const date2 = new Date('2023-01-15T10:30:00Z');
console.assert(areDatesEqual(date1, date2) === true, 'Identical dates should be equal');

// Test: Compare two different dates
const date3 = new Date('2023-01-15T10:30:00Z');
const date4 = new Date('2023-01-16T10:30:00Z');
console.assert(areDatesEqual(date3, date4) === false, 'Different dates should not be equal');

Test 2: Invalid Dates @test

File: date-comparison.test.js

const { areDatesEqual } = require('./date-comparison');

// Test: Two invalid dates should be equal
const invalidDate1 = new Date('invalid-string');
const invalidDate2 = new Date('another-invalid');
console.assert(areDatesEqual(invalidDate1, invalidDate2) === true, 'Invalid dates should be equal');

// Test: Invalid date vs valid date should not be equal
const invalidDate = new Date('invalid');
const validDate = new Date('2023-01-15T10:30:00Z');
console.assert(areDatesEqual(invalidDate, validDate) === false, 'Invalid and valid dates should not be equal');

Test 3: Arrays of Dates @test

File: date-comparison.test.js

const { areDatesEqual } = require('./date-comparison');

// Test: Compare arrays with identical dates
const array1 = [new Date('2023-01-15'), new Date('2023-02-20'), new Date('2023-03-25')];
const array2 = [new Date('2023-01-15'), new Date('2023-02-20'), new Date('2023-03-25')];
console.assert(areDatesEqual(array1, array2) === true, 'Arrays with identical dates should be equal');

// Test: Compare arrays with different dates
const array3 = [new Date('2023-01-15'), new Date('2023-02-20')];
const array4 = [new Date('2023-01-15'), new Date('2023-02-21')];
console.assert(areDatesEqual(array3, array4) === false, 'Arrays with different dates should not be equal');

Test 4: Objects with Date Properties @test

File: date-comparison.test.js

const { areDatesEqual } = require('./date-comparison');

// Test: Compare objects with matching date properties
const obj1 = {
  startDate: new Date('2023-01-15T08:00:00Z'),
  endDate: new Date('2023-01-20T17:00:00Z')
};
const obj2 = {
  startDate: new Date('2023-01-15T08:00:00Z'),
  endDate: new Date('2023-01-20T17:00:00Z')
};
console.assert(areDatesEqual(obj1, obj2) === true, 'Objects with matching dates should be equal');

// Test: Compare objects with different date properties
const obj3 = {
  startDate: new Date('2023-01-15T08:00:00Z')
};
const obj4 = {
  startDate: new Date('2023-01-16T08:00:00Z')
};
console.assert(areDatesEqual(obj3, obj4) === false, 'Objects with different dates should not be equal');

Deliverables

  • date-comparison.js: Main implementation file exporting the areDatesEqual function
  • date-comparison.test.js: Test file containing the provided test cases

Install with Tessl CLI

npx tessl i tessl/npm-deep-eql

tile.json