Skip to content

Commit 90e7cbc

Browse files
committed
Move pad2/pad4 to separate file for tree-shaking
1 parent 8a9b879 commit 90e7cbc

7 files changed

+144
-65
lines changed

package-lock.json

+90-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hebcal/hdate",
3-
"version": "0.11.1",
3+
"version": "0.11.2",
44
"description": "converts between Hebrew and Gregorian dates using Rata Die (R.D.) algorithm by Dershowitz and Reingold",
55
"author": "Michael J. Radwin (https://github.com/mjradwin)",
66
"contributors": [
@@ -47,12 +47,12 @@
4747
},
4848
"devDependencies": {
4949
"@types/jest": "^29.5.12",
50-
"@types/node": "20.14.9",
50+
"@types/node": "20.14.10",
5151
"gts": "^5.3.1",
5252
"jest": "^29.7.0",
53-
"ts-jest": "^29.1.5",
53+
"ts-jest": "^29.2.2",
5454
"ttag-cli": "^1.10.12",
55-
"typedoc": "^0.26.3",
55+
"typedoc": "^0.26.4",
5656
"typescript": "^5.5.3"
5757
}
5858
}

src/dateFormat.ts

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {pad2, pad4} from './pad';
2+
13
const _formatters = new Map();
24

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

54-
/**
55-
* Formats a number with leading zeros so the resulting string is 4 digits long.
56-
* Similar to `string.padStart(4, '0')` but will also format
57-
* negative numbers similar to how the JavaScript date formats
58-
* negative year numbers (e.g. `-37` is formatted as `-000037`).
59-
*/
60-
export function pad4(num: number): string {
61-
if (num < 0) {
62-
return '-00' + pad4(-num);
63-
} else if (num < 10) {
64-
return '000' + num;
65-
} else if (num < 100) {
66-
return '00' + num;
67-
} else if (num < 1000) {
68-
return '0' + num;
69-
}
70-
return String(num);
71-
}
72-
73-
/**
74-
* Formats a number with leading zeros so the resulting string is 2 digits long.
75-
* Similar to `string.padStart(2, '0')`.
76-
*/
77-
export function pad2(num: number): string {
78-
if (num >= 0 && num < 10) {
79-
return '0' + num;
80-
}
81-
return String(num);
82-
}
83-
8456
/**
8557
* Returns YYYY-MM-DD in the local timezone
8658
*/

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './anniversary';
55
export * from './gematriya';
66
export * from './omer';
77
export * from './molad';
8+
export * from './pad';
89
export * from './dateFormat';
910
export * from './locale';
1011
export {HDate} from './hdate';

src/pad.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Formats a number with leading zeros so the resulting string is 4 digits long.
3+
* Similar to `string.padStart(4, '0')` but will also format
4+
* negative numbers similar to how the JavaScript date formats
5+
* negative year numbers (e.g. `-37` is formatted as `-000037`).
6+
*/
7+
export function pad4(num: number): string {
8+
if (num < 0) {
9+
return '-00' + pad4(-num);
10+
} else if (num < 10) {
11+
return '000' + num;
12+
} else if (num < 100) {
13+
return '00' + num;
14+
} else if (num < 1000) {
15+
return '0' + num;
16+
}
17+
return String(num);
18+
}
19+
20+
/**
21+
* Formats a number with leading zeros so the resulting string is 2 digits long.
22+
* Similar to `string.padStart(2, '0')`.
23+
*/
24+
export function pad2(num: number): string {
25+
if (num >= 0 && num < 10) {
26+
return '0' + num;
27+
}
28+
return String(num);
29+
}

test/dateFormat.spec.ts

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getPseudoISO, getTimezoneOffset, pad2, pad4 } from '../src/dateFormat';
1+
import { getPseudoISO, getTimezoneOffset } from '../src/dateFormat';
22

33
test('getPseudoISO-2021', () => {
44
const dt = new Date(Date.UTC(2021, 0, 31, 7, 30, 50, 551));
@@ -41,19 +41,3 @@ test('getTimezoneOffset', () => {
4141
expect(getTimezoneOffset('America/Phoenix', july)).toBe(420);
4242
expect(getTimezoneOffset('Asia/Jerusalem', july)).toBe(-180);
4343
});
44-
45-
test('pad2', () => {
46-
expect(pad2(0)).toBe('00');
47-
expect(pad2(5)).toBe('05');
48-
expect(pad2(25)).toBe('25');
49-
expect(pad2(-3)).toBe('-3');
50-
});
51-
52-
test('pad4', () => {
53-
expect(pad4(0)).toBe('0000');
54-
expect(pad4(5)).toBe('0005');
55-
expect(pad4(25)).toBe('0025');
56-
expect(pad4(125)).toBe('0125');
57-
expect(pad4(2025)).toBe('2025');
58-
expect(pad4(-38)).toBe('-000038');
59-
});

test/pad.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { pad2, pad4 } from '../src/pad';
2+
3+
test('pad2', () => {
4+
expect(pad2(0)).toBe('00');
5+
expect(pad2(5)).toBe('05');
6+
expect(pad2(25)).toBe('25');
7+
expect(pad2(-3)).toBe('-3');
8+
});
9+
10+
test('pad4', () => {
11+
expect(pad4(0)).toBe('0000');
12+
expect(pad4(5)).toBe('0005');
13+
expect(pad4(25)).toBe('0025');
14+
expect(pad4(125)).toBe('0125');
15+
expect(pad4(2025)).toBe('2025');
16+
expect(pad4(-38)).toBe('-000038');
17+
});

0 commit comments

Comments
 (0)