Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ditch moment #5

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnZeros extends boolean = false>(
start: Date | string | 'now',
end: Date | string | 'now',
options?: Options<ReturnZeros>,
): ReturnZeros extends true ? ReturnObj : Partial<ReturnObj> {
): ReturnZeros extends true ? Units : Partial<Units> {
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<keyof ReturnObj, boolean>;
units: Record<keyof Units, boolean>;
returnZeros: boolean;
callback: ((result: ReturnObj | Partial<ReturnObj>) => unknown) | undefined;
callback: ((result: Units | Partial<Units>) => unknown) | undefined;
} = {
units: {
years: true,
Expand Down Expand Up @@ -101,18 +99,18 @@ function timediff<ReturnZeros extends boolean = false>(
}
}

const result: Partial<ReturnObj> = {};
const result: Partial<Units> = {};
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<ReturnObj>;
return result as ReturnZeros extends true ? Units : Partial<Units>;
}

export { timediff, ReturnObj, Options }
export { timediff, Units, Options }
4 changes: 2 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface ReturnObj {
export interface Units {
years: number;
months: number;
weeks: number;
Expand All @@ -10,7 +10,7 @@ export interface ReturnObj {
}

export interface OptionsObj<ReturnZeros extends boolean> {
units?: string | Partial<Record<keyof ReturnObj, boolean>>;
units?: string | Partial<Record<keyof Units, boolean>>;
returnZeros?: ReturnZeros;
}

Expand Down
23 changes: 18 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -27,7 +27,7 @@ describe('timediff', function () {
}).not.toThrow();
expect(function () {
// @ts-ignore
timediff(new Date(), moment());
timediff(new Date(), dayjs());
}).not.toThrow();
});

Expand Down
Loading