Skip to content

Commit

Permalink
perf: removes date-fns dependency for bundle size reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stacy committed Sep 12, 2024
1 parent 289954c commit 771c5eb
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
6 changes: 4 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@vuepress/theme-default": "^2.0.0-rc.11",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"date-fns": "^3.6.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"happy-dom": "^15.7.3",
Expand All @@ -87,7 +88,6 @@
},
"dependencies": {
"chalk": "^5.3.0",
"date-fns": "^3.6.0",
"vuepress-plugin-search-pro": "^2.0.0-rc.52"
},
"size-limit": [
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatISO } from 'date-fns/formatISO';
import { formatISO } from '../functions';
import { LevelConfiguration, ModifierData } from '../_types';
import { Configuration } from '../configuration';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/json/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Formatter from '../formatter';
import { ModifierData } from '../../_types';
import { formatISO } from 'date-fns/formatISO';
import { formatISO } from '../../functions';
import { JsonLog, JsonLogOptionalFields, JsonLogRequiredFields } from './types';
import { hasRequiredFields } from './type-guards';
import { setup } from '../../functions';
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/standard/standard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Formatter from '../formatter';
import { ModifierData } from '../../_types';
import { formatISO } from 'date-fns/formatISO';
import { formatISO } from '../../functions';
import { isNumber, isObject, isString } from '../../functions';

/**
Expand Down
33 changes: 33 additions & 0 deletions src/functions/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,36 @@ export function getActiveLevel(cfg: Configuration): number {
if (isNumber(cfg.activeLevel)) return cfg.activeLevel;
return cfg.levels[cfg.activeLevel].level;
}

/**
* Adds a leading zero to a number if it is less than 10.
*/
export function leadingZero(num: number): string {
return (num < 10 ? '0' : '') + `${num}`;
}

/**
* Returns an ISO-8601 formatted string from the provided date.
*/
export function formatISO(date: Date) {
const tzo = -date.getTimezoneOffset();
const dif = tzo >= 0 ? '+' : '-';

return (
`${date.getFullYear()}` +
'-' +
leadingZero(date.getMonth() + 1) +
'-' +
leadingZero(date.getDate()) +
'T' +
leadingZero(date.getHours()) +
':' +
leadingZero(date.getMinutes()) +
':' +
leadingZero(date.getSeconds()) +
dif +
leadingZero(Math.floor(Math.abs(tzo) / 60)) +
':' +
leadingZero(Math.abs(tzo) % 60)
);
}
13 changes: 13 additions & 0 deletions test/modifiers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ describe('modifiers with pretty format in the browser', () => {
adze.silent.log('Test log.');
expect(console.log).not.toHaveBeenCalled();
});

test('timestamp is formatted as ISO-8601 by default', () => {
const fn = vi.fn();
console.log = fn;

adze.timestamp.log('Log with ISO-8601 timestamp.');

expect(console.log).toHaveBeenCalledTimes(1);

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const timestamp = fn.mock.calls[0][2];
expect(timestamp).toMatch(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d/);
});
});

0 comments on commit 771c5eb

Please sign in to comment.