Skip to content

Commit

Permalink
Move pad2/pad4 to separate file for tree-shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Jul 15, 2024
1 parent 8a9b879 commit 90e7cbc
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 65 deletions.
104 changes: 90 additions & 14 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/hdate",
"version": "0.11.1",
"version": "0.11.2",
"description": "converts between Hebrew and Gregorian dates using Rata Die (R.D.) algorithm by Dershowitz and Reingold",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"contributors": [
Expand Down Expand Up @@ -47,12 +47,12 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "20.14.9",
"@types/node": "20.14.10",
"gts": "^5.3.1",
"jest": "^29.7.0",
"ts-jest": "^29.1.5",
"ts-jest": "^29.2.2",
"ttag-cli": "^1.10.12",
"typedoc": "^0.26.3",
"typedoc": "^0.26.4",
"typescript": "^5.5.3"
}
}
32 changes: 2 additions & 30 deletions src/dateFormat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {pad2, pad4} from './pad';

const _formatters = new Map();

/**
Expand Down Expand Up @@ -51,36 +53,6 @@ export function getTimezoneOffset(tzid: string, date: Date): number {
return Math.ceil(diffMs / 1000 / 60);
}

/**
* Formats a number with leading zeros so the resulting string is 4 digits long.
* Similar to `string.padStart(4, '0')` but will also format
* negative numbers similar to how the JavaScript date formats
* negative year numbers (e.g. `-37` is formatted as `-000037`).
*/
export function pad4(num: number): string {
if (num < 0) {
return '-00' + pad4(-num);
} else if (num < 10) {
return '000' + num;
} else if (num < 100) {
return '00' + num;
} else if (num < 1000) {
return '0' + num;
}
return String(num);
}

/**
* Formats a number with leading zeros so the resulting string is 2 digits long.
* Similar to `string.padStart(2, '0')`.
*/
export function pad2(num: number): string {
if (num >= 0 && num < 10) {
return '0' + num;
}
return String(num);
}

/**
* Returns YYYY-MM-DD in the local timezone
*/
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './anniversary';
export * from './gematriya';
export * from './omer';
export * from './molad';
export * from './pad';
export * from './dateFormat';
export * from './locale';
export {HDate} from './hdate';
29 changes: 29 additions & 0 deletions src/pad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Formats a number with leading zeros so the resulting string is 4 digits long.
* Similar to `string.padStart(4, '0')` but will also format
* negative numbers similar to how the JavaScript date formats
* negative year numbers (e.g. `-37` is formatted as `-000037`).
*/
export function pad4(num: number): string {
if (num < 0) {
return '-00' + pad4(-num);
} else if (num < 10) {
return '000' + num;
} else if (num < 100) {
return '00' + num;
} else if (num < 1000) {
return '0' + num;
}
return String(num);
}

/**
* Formats a number with leading zeros so the resulting string is 2 digits long.
* Similar to `string.padStart(2, '0')`.
*/
export function pad2(num: number): string {
if (num >= 0 && num < 10) {
return '0' + num;
}
return String(num);
}
18 changes: 1 addition & 17 deletions test/dateFormat.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPseudoISO, getTimezoneOffset, pad2, pad4 } from '../src/dateFormat';
import { getPseudoISO, getTimezoneOffset } from '../src/dateFormat';

test('getPseudoISO-2021', () => {
const dt = new Date(Date.UTC(2021, 0, 31, 7, 30, 50, 551));
Expand Down Expand Up @@ -41,19 +41,3 @@ test('getTimezoneOffset', () => {
expect(getTimezoneOffset('America/Phoenix', july)).toBe(420);
expect(getTimezoneOffset('Asia/Jerusalem', july)).toBe(-180);
});

test('pad2', () => {
expect(pad2(0)).toBe('00');
expect(pad2(5)).toBe('05');
expect(pad2(25)).toBe('25');
expect(pad2(-3)).toBe('-3');
});

test('pad4', () => {
expect(pad4(0)).toBe('0000');
expect(pad4(5)).toBe('0005');
expect(pad4(25)).toBe('0025');
expect(pad4(125)).toBe('0125');
expect(pad4(2025)).toBe('2025');
expect(pad4(-38)).toBe('-000038');
});
17 changes: 17 additions & 0 deletions test/pad.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { pad2, pad4 } from '../src/pad';

test('pad2', () => {
expect(pad2(0)).toBe('00');
expect(pad2(5)).toBe('05');
expect(pad2(25)).toBe('25');
expect(pad2(-3)).toBe('-3');
});

test('pad4', () => {
expect(pad4(0)).toBe('0000');
expect(pad4(5)).toBe('0005');
expect(pad4(25)).toBe('0025');
expect(pad4(125)).toBe('0125');
expect(pad4(2025)).toBe('2025');
expect(pad4(-38)).toBe('-000038');
});

0 comments on commit 90e7cbc

Please sign in to comment.