Skip to content

Commit 533d2a3

Browse files
committed
Added good and proper types for NPM package, we're under 850B again
1 parent bf2ec60 commit 533d2a3

13 files changed

Lines changed: 80 additions & 99 deletions

File tree

.github/workflows/publish.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
workflow_dispatch:
66
push:
77
tags:
8-
- 'v*'
8+
- 'value*'
99

1010
# Grant write permissions to the repository contents so we can push version updates
1111
permissions:
@@ -36,6 +36,9 @@ jobs:
3636
- name: Run tests
3737
run: bun test
3838

39+
- name: Generate types
40+
run: bun run types
41+
3942
- name: Publish to NPM
4043
run: bun publish --access public
4144
continue-on-error: true

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ template.render(new Date());
4646

4747
tinytime takes an approach similar to a compiler and generates an AST representing your template. This AST is generated when
4848
you call the main `tinytime` function. This lets you efficiently re-render your template without tinytime having to parse the
49-
template string again. That means its important that you aren't recreating the template object frequently.
49+
template string again. That means its important that you aren'type recreating the template object frequently.
5050

5151
Here's an example showing the right and wrong way to use tinytime with React.
5252

53-
Don't do this:
53+
Don'type do this:
5454

5555
```jsx
5656
function Time({ date }) {

biome.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"files": {
99
"ignoreUnknown": false,
10-
"ignore": []
10+
"ignore": ["dist", "node_modules"]
1111
},
1212
"formatter": {
1313
"enabled": true,

bun.lock

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://jsr.io/schema/config-file.v1.json",
33
"name": "@angius/tinytime",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"license": "MIT",
66
"exports": "./src/index.ts"
77
}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "@angius/tinytime",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"exports": ["./dist/tinytime.js", "./dist/tinytime.min.js"],
5+
"types": "./dist/tinytime.d.ts",
56
"license": "MIT",
67
"keywords": ["date", "time", "formatter"],
78
"type": "module",
@@ -12,12 +13,14 @@
1213
"scripts": {
1314
"build": "bun build ./src/index.ts --outdir dist",
1415
"test": "bun test",
15-
"prepublish": "bun run build",
16+
"types": "bunx tsc",
17+
"prepublish": "bun run build && bun run types",
1618
"fix": "bunx biome check --fix"
1719
},
1820
"devDependencies": {
1921
"@biomejs/biome": "1.9.4",
2022
"@types/bun": "latest",
23+
"dts-bundle-generator": "^9.5.1",
2124
"jsr": "^0.13.4"
2225
},
2326
"peerDependencies": {

src/compiler.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import {
1717
UserText,
1818
} from "./subs";
1919

20-
/**
21-
* These types help ensure we don't misspell them anywhere. They will be
22-
* removed during build.
23-
*/
2420
const months = [
2521
"January",
2622
"February",
@@ -35,7 +31,6 @@ const months = [
3531
"November",
3632
"December",
3733
] as const;
38-
type Months = (typeof months)[number];
3934

4035
const days = [
4136
"Sunday",
@@ -46,7 +41,6 @@ const days = [
4641
"Friday",
4742
"Saturday",
4843
] as const;
49-
type Days = (typeof days)[number];
5044

5145
/**
5246
* Takes an integer and returns a string left padded with
@@ -60,13 +54,15 @@ function padWithZeros(int: number): string {
6054
* Adds suffix to day, so 16 becomes 16th.
6155
*/
6256
function suffix(int: number): string {
63-
return int % 10 === 1 && int !== 11
64-
? `${int}st`
65-
: int % 10 === 2 && int !== 12
66-
? `${int}nd`
67-
: int % 10 === 3 && int !== 13
68-
? `${int}rd`
69-
: `${int}th`;
57+
const suf =
58+
int % 10 === 1 && int !== 11
59+
? "st"
60+
: int % 10 === 2 && int !== 12
61+
? "nd"
62+
: int % 10 === 3 && int !== 13
63+
? "rd"
64+
: "th";
65+
return `${int}${suf}`;
7066
}
7167

7268
/**
@@ -99,9 +95,9 @@ export default function compiler(
9995
break;
10096
}
10197

102-
switch (token.t) {
98+
switch (token[0]) {
10399
case UserText:
104-
compiled += token.v;
100+
compiled += token[1];
105101
break;
106102

107103
case Day:
@@ -130,7 +126,7 @@ export default function compiler(
130126
break;
131127

132128
case PartialYear:
133-
compiled += `${year}`.slice(2);
129+
compiled += `${year % 100}`;
134130
break;
135131

136132
case DayOfTheWeek:
@@ -143,7 +139,7 @@ export default function compiler(
143139

144140
case Hour:
145141
{
146-
const hour = hours === 0 || hours === 12 ? 12 : hours % 12;
142+
const hour = hours % 12 || 12;
147143
compiled += options.padHours ? padWithZeros(hour) : hour;
148144
}
149145
break;

src/diff.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
import compiler from "./compiler";
32
import parser from "./parser";
43

src/parser.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
// @flow
21
import { SubToTypeIdentifierMap, UserText } from "./subs";
32

4-
/**
5-
* t is type and v is value. Minified property
6-
* names are being used because the current minification
7-
* step does not mangle property names, and we want to
8-
* reduce bundle size as much as possible.
9-
*/
10-
export type Token = {
11-
t: string;
12-
v?: string;
13-
};
3+
export type Token = [type: string, value?: string];
144

155
/**
166
* Rather than using a bunch of potentially confusing regular
@@ -59,10 +49,7 @@ export default function parser(template: string): Token[] {
5949
if (char === "{") {
6050
// Push any `UserText` we've accumulated and reset the `text` variable.
6151
if (text) {
62-
tokens.push({
63-
t: UserText,
64-
v: text,
65-
});
52+
tokens.push([UserText, text]);
6653
}
6754
text = "";
6855
let sub = "";
@@ -75,9 +62,7 @@ export default function parser(template: string): Token[] {
7562
if (!identifier) {
7663
throw new Error(`Unknown substitution: ${sub}`);
7764
}
78-
tokens.push({
79-
t: identifier,
80-
});
65+
tokens.push([identifier]);
8166
}
8267
// Anything not inside brackets is just plain text.
8368
else {
@@ -89,10 +74,7 @@ export default function parser(template: string): Token[] {
8974
* the template ends with some `UserText`.
9075
*/
9176
if (text) {
92-
tokens.push({
93-
t: UserText,
94-
v: text,
95-
});
77+
tokens.push([UserText, text]);
9678
}
9779
return tokens;
9880
}

0 commit comments

Comments
 (0)