Skip to content

Commit

Permalink
feat: 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
badosz0 committed Jun 23, 2024
1 parent 33f6859 commit 3ead123
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
],
"ignorePatterns": [
"dist/",
"node_modules/"
"node_modules/",
"__tests__"
],
"rules": {
"no-dupe-keys": "warn",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "holy-time",
"version": "4.3.0",
"version": "5.0.0",
"description": "Utility functions",
"repository": "Yet another (type-safe) date time library",
"main": "dist/index.js",
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,46 @@ describe('test HolyTime.format with timeZone', () => {
expect(date.format('YYYY-MM-DD HH:mm:ss', 'Europe/Berlin')).toBe('2023-04-01 02:00:00');
});
});


describe('test HolyDuration.format', () => {
it('should format 7s duration in short format', () => {
const duration = HolyTime.duration(7, 'seconds');
expect(duration.format('short')).toBe('7s');
})

it('should format 32ms duration in short format', () => {
const duration = HolyTime.duration(32);
expect(duration.format('short')).toBe('32ms');
})

it('should format 32y 3d 5ms duration in short format', () => {
const duration = HolyTime.duration(32, 'years').add(3, 'days').add(5);
expect(duration.format('short')).toBe('32y 3d 5ms');
})

it('should format 1y 3d duration in short format', () => {
const duration = HolyTime.duration(1, 'years').add(3, 'days');
expect(duration.format('short')).toBe('1y 3d');
})

it('should format 7s duration in long format', () => {
const duration = HolyTime.duration(7, 'seconds');
expect(duration.format('long')).toBe('7 seconds');
})

it('should format 32ms duration in long format', () => {
const duration = HolyTime.duration(32);
expect(duration.format('long')).toBe('32 milliseconds');
})

it('should format 32y 3d 5s duration in long format', () => {
const duration = HolyTime.duration(32, 'years').add(3, 'days').add(5, 'seconds');
expect(duration.format('long')).toBe('32 years, 3 days and 5 seconds');
})

it('should format 1y 3d duration in long format', () => {
const duration = HolyTime.duration(1, 'years').add(3, 'days');
expect(duration.format('long')).toBe('1 year and 3 days');
})
})
9 changes: 8 additions & 1 deletion src/__tests__/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('test HolyTime.since', () => {
it('should return time difference', () => {
const date = new HolyTime(new Date('2023-01-01T01:02:03.000Z'));

expect(HolyTime.since(date)).toBe(Date.now() - date.getTime());
expect(HolyTime.since(date).in('seconds')).toBe((Date.now() - date.getTime()) / 1000);
});
});

Expand All @@ -106,3 +106,10 @@ describe('test HolyTime.min', () => {
});
});

describe('test HolyTime.getTimezone', () => {
it('should return timezone', () => {

expect(HolyTime.getTimeZone()).toBe('UTC');
});
})

38 changes: 38 additions & 0 deletions src/duration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ValueOf } from 'type-fest';
import { HolyTime } from './time';
import { HumanUnit } from './types';
import { TimeUnits } from './constants';

const UNIT_KEYS = Object.keys(TimeUnits).map((key) => key.toLowerCase());
const UNIT_VALUES = Object.values(TimeUnits);
const listFormatter = new Intl.ListFormat('en-GB', { type: 'conjunction' });

export class HolyDuration {
private milliseconds: number;
Expand Down Expand Up @@ -31,5 +36,38 @@ export class HolyDuration {
public in(unit: HumanUnit): number {
return this.milliseconds / HolyDuration.getUnit(unit);
}

public format(type: | 'short' | 'long' = 'short', maxSegments: number = Infinity): string {
let segments: number[] = [];
let { milliseconds } = this;

for (const unit of UNIT_VALUES) {
if (milliseconds >= unit) {
segments.push(Math.floor(milliseconds / unit));
milliseconds %= unit;
} else {
segments.push(0);
}
}

segments = segments.slice(0, segments.findIndex(Boolean) + maxSegments);

switch (type) {
case 'short': {
return segments
.map((segment, i) => `${segment}${UNIT_KEYS[i][0]}${i === UNIT_KEYS.length - 1 ? 's' : ''}`)
.filter(segment => !segment.startsWith('0'))
.join(' ');
}

case 'long': {
return listFormatter.format(
segments
.map((segment, i) => `${segment} ${UNIT_KEYS[i]}${segment === 1 ? '' : 's'}`)
.filter(segment => !segment.startsWith('0')),
);
}
}
}
}

15 changes: 12 additions & 3 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ export class HolyTime {
return this.date.getTime() < HolyTime.resolveDate(time).getTime();
}

public static since(time: TimeResolvable): number {
return Date.now() - HolyTime.resolveDate(time).getTime();
public static since(time: TimeResolvable): HolyDuration {
return new HolyDuration(Date.now() - HolyTime.resolveDate(time).getTime());
}

public static max(...times: TimeResolvable[]): HolyTime {
Expand Down Expand Up @@ -210,9 +210,13 @@ export class HolyTime {
return HolyTime.endOf(unit, this, timeZone);
}

public static format(time: TimeResolvable, format: string, timeZone?: TimeZone): string {
public static format(time: TimeResolvable, format: string | ((time: HolyTime) => string), timeZone?: TimeZone): string {
const date = HolyTime.adjustToTimeZone(HolyTime.resolveDate(time), timeZone);

if (typeof format === 'function') {
return format(new HolyTime(date));
}

const values: Record<string, string> = {
YY: date.getFullYear().toString().slice(2, 4),
YYYY: date.getFullYear().toString(),
Expand Down Expand Up @@ -359,4 +363,9 @@ export class HolyTime {
public getRelativeFrom(time: TimeResolvable): string {
return HolyTime.relativeFromTo(time, this);
}

public static getTimeZone(): TimeZone {
return Intl.DateTimeFormat().resolvedOptions().timeZone as TimeZone;
}
}

0 comments on commit 3ead123

Please sign in to comment.