diff --git a/lib/index.ts b/lib/index.ts index c312207..ff9b698 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,29 +1,27 @@ -import * as moment from 'moment'; -import Diff = moment.unitOfTime.Diff; -import DurationConstructor = moment.unitOfTime.DurationConstructor; -import { Options } from './types'; -import { ReturnObj } from './types'; +import { Options, Units } from './types'; +import * as dayjs from 'dayjs'; +import { ManipulateType, UnitTypeLongPlural } from 'dayjs'; function timediff( start: Date | string | 'now', end: Date | string | 'now', options?: Options, -): ReturnZeros extends true ? ReturnObj : Partial { +): ReturnZeros extends true ? Units : Partial { const now = new Date(); - const sd = moment(start === 'now' ? now : start); + let sd = dayjs(start === 'now' ? now : start); if (!sd.isValid()) { throw 'invalid start date ' + sd; } - const ed = moment(end === 'now' ? now : end); + const ed = dayjs(end === 'now' ? now : end); if (!ed.isValid()) { throw 'invalid end date ' + ed; } const config: { - units: Record; + units: Record; returnZeros: boolean; - callback: ((result: ReturnObj | Partial) => unknown) | undefined; + callback: ((result: Units | Partial) => unknown) | undefined; } = { units: { years: true, @@ -101,18 +99,18 @@ function timediff( } } - const result: Partial = {}; + const result: Partial = {}; for (let unit in config.units) { - if (config.units[unit as keyof ReturnObj]) { - const value = ed.diff(sd, unit as Diff); - sd.add(value, unit as DurationConstructor); + if (config.units[unit as keyof Units]) { + const value = ed.diff(sd, unit as UnitTypeLongPlural); + sd = sd.add(value, unit as ManipulateType); if (config.returnZeros || value != 0) { - result[unit as keyof ReturnObj] = value; + result[unit as keyof Units] = value; } } } - return result as ReturnZeros extends true ? ReturnObj : Partial; + return result as ReturnZeros extends true ? Units : Partial; } -export { timediff, ReturnObj, Options } +export { timediff, Units, Options } diff --git a/lib/types.ts b/lib/types.ts index 8800005..5b804bc 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,4 +1,4 @@ -export interface ReturnObj { +export interface Units { years: number; months: number; weeks: number; @@ -10,7 +10,7 @@ export interface ReturnObj { } export interface OptionsObj { - units?: string | Partial>; + units?: string | Partial>; returnZeros?: ReturnZeros; } diff --git a/package-lock.json b/package-lock.json index b94d12c..67117bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,18 @@ { "name": "@marc-maniac/timediff", - "version": "0.3.8", + "version": "0.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@marc-maniac/timediff", - "version": "0.3.8", + "version": "0.4.0", "license": "MIT", "dependencies": { + "date-fns": "^3.6.0", + "dayjs": "^1.11.11", "moment": "^2.9.0" }, - "bin": { - "timediff": "cli.js" - }, "devDependencies": { "@types/mocha": "^10.0.6", "expect": "^29.7.0", @@ -500,6 +499,20 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "dev": true }, + "node_modules/date-fns": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-3.6.0.tgz", + "integrity": "sha512-fRHTG8g/Gif+kSh50gaGEdToemgfj74aRX3swtiouboip5JDLAyDE9F11nHMIcvOaXeOC6D7SpNhi7uFyB7Uww==", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/kossnocorp" + } + }, + "node_modules/dayjs": { + "version": "1.11.11", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.11.tgz", + "integrity": "sha512-okzr3f11N6WuqYtZSvm+F776mB41wRZMhKP+hc34YdW+KmtYYK9iqvHSwo2k9FEH3fhGXvOPV6yz2IcSrfRUDg==" + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/package.json b/package.json index a5bc2a4..561c44d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@marc-maniac/timediff", - "version": "0.3.8", + "version": "0.4.0", "description": "Calculate a time difference in several time units.", "license": "MIT", "main": "dist/index.js", @@ -30,7 +30,7 @@ "url": "git+https://github.com/marc-at-brightnight/timediff.git" }, "dependencies": { - "moment": "^2.9.0" + "dayjs": "^1.11.11" }, "devDependencies": { "@types/mocha": "^10.0.6", diff --git a/test/index.spec.ts b/test/index.spec.ts index 6fe59d3..5dc98ba 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import * as dayjs from "dayjs"; import { timediff } from '../lib'; import expect from 'expect'; import { Options } from '../lib'; @@ -27,7 +27,7 @@ describe('timediff', function () { }).not.toThrow(); expect(function () { // @ts-ignore - timediff(new Date(), moment()); + timediff(new Date(), dayjs()); }).not.toThrow(); });