Skip to content

Commit ec1ba90

Browse files
committed
chore: add test 0010 example
1 parent 126d62d commit ec1ba90

File tree

4 files changed

+191
-11
lines changed

4 files changed

+191
-11
lines changed

build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import fs from 'node:fs/promises'
12
import { log } from '@stacksjs/cli'
23
import { dts } from 'bun-plugin-dtsx'
3-
import fs from 'node:fs/promises'
44

55
log.info('Building...')
66

fixtures/input/example/0010.ts

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
export type DelimiterType = string
3+
export const DefaultPhoneDelimiter: DelimiterType = '-'
4+
export const DefaultPhonePattern: number[] = [3, 3, 4]
5+
6+
export interface FormatPhoneOptions {
7+
delimiter?: string
8+
pattern?: number[]
9+
region?: string
10+
includeCountryCode?: boolean
11+
format?: 'national' | 'international'
12+
}
13+
14+
export const DefaultPhoneRegion = 'US'
15+
16+
const PHONE_PATTERNS: Record<string, number[]> = {
17+
US: [3, 3, 4],
18+
GB: [4, 3, 3],
19+
FR: [2, 2, 2, 2, 2],
20+
DE: [3, 2, 2, 2],
21+
JP: [3, 4, 4],
22+
CN: [3, 4, 4],
23+
IN: [4, 3, 3],
24+
BR: [2, 4, 4],
25+
AU: [4, 3, 3],
26+
CA: [3, 3, 4],
27+
} as const
28+
29+
const COUNTRY_CODES: Record<string, string> = {
30+
US: '+1',
31+
GB: '+44',
32+
FR: '+33',
33+
DE: '+49',
34+
JP: '+81',
35+
CN: '+86',
36+
IN: '+91',
37+
BR: '+55',
38+
AU: '+61',
39+
CA: '+1',
40+
}
41+
42+
function handleFormat({
43+
value,
44+
delimiter,
45+
pattern,
46+
region,
47+
includeCountryCode,
48+
format,
49+
}: {
50+
value: string
51+
delimiter?: string
52+
pattern?: number[]
53+
region?: string
54+
includeCountryCode?: boolean
55+
format?: 'national' | 'international'
56+
}): string {
57+
// Remove all non-digit characters
58+
const digits = value.replace(/\D/g, '')
59+
60+
// Apply pattern
61+
let result = ''
62+
let digitIndex = 0
63+
64+
for (let i = 0; i < pattern.length; i++) {
65+
const groupSize = pattern[i]
66+
const group = digits.slice(digitIndex, digitIndex + groupSize)
67+
68+
if (group) {
69+
if (result) {
70+
result += delimiter
71+
}
72+
result += group
73+
digitIndex += groupSize
74+
}
75+
}
76+
77+
// Add country code if needed
78+
if (includeCountryCode && format === 'international' && COUNTRY_CODES[region]) {
79+
result = `${COUNTRY_CODES[region]} ${result}`
80+
}
81+
82+
return result
83+
}
84+
85+
export function formatPhone(value: string, options?: FormatPhoneOptions): string {
86+
const {
87+
delimiter = DefaultPhoneDelimiter,
88+
pattern,
89+
region = DefaultPhoneRegion,
90+
includeCountryCode = false,
91+
format = 'national',
92+
} = options ?? {}
93+
94+
// Use region pattern if no custom pattern provided
95+
const selectedPattern = pattern ?? PHONE_PATTERNS[region] ?? DefaultPhonePattern
96+
97+
return handleFormat({
98+
value,
99+
delimiter,
100+
pattern: selectedPattern,
101+
region,
102+
includeCountryCode,
103+
format,
104+
})
105+
}
106+
107+
export function unformatPhone(value: string): string {
108+
return value.replace(/\D/g, '')
109+
}

fixtures/output/0010.d.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export declare type DelimiterType = string
2+
export const DefaultPhoneDelimiter: DelimiterType = '-'
3+
export const DefaultPhonePattern: number[] = [3, 3, 4]
4+
5+
export interface FormatPhoneOptions {
6+
delimiter?: string
7+
pattern?: number[]
8+
region?: string
9+
includeCountryCode?: boolean
10+
format?: 'national' | 'international'
11+
}
12+
13+
export const DefaultPhoneRegion = 'US'
14+
15+
const PHONE_PATTERNS: Record<string, number[]> = {
16+
US: [3, 3, 4],
17+
GB: [4, 3, 3],
18+
FR: [2, 2, 2, 2, 2],
19+
DE: [3, 2, 2, 2],
20+
JP: [3, 4, 4],
21+
CN: [3, 4, 4],
22+
IN: [4, 3, 3],
23+
BR: [2, 4, 4],
24+
AU: [4, 3, 3],
25+
CA: [3, 3, 4],
26+
} as const
27+
28+
const COUNTRY_CODES: Record<string, string> = {
29+
US: '+1',
30+
GB: '+44',
31+
FR: '+33',
32+
DE: '+49',
33+
JP: '+81',
34+
CN: '+86',
35+
IN: '+91',
36+
BR: '+55',
37+
AU: '+61',
38+
CA: '+1',
39+
}
40+
declare function handleFormat({ value, delimiter, pattern, region, includeCountryCode, format }: {
41+
value: string
42+
delimiter?: string
43+
pattern?: number[]
44+
region?: string
45+
includeCountryCode?: boolean
46+
format?: 'national' | 'international'
47+
}): string;
48+
export declare function formatPhone(value: string, options?: FormatPhoneOptions): string;
49+
export declare function unformatPhone(value: string): string;

test/dts.test.ts

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { DtsGenerationOption } from '../src/types'
22
import { afterEach, describe, expect, it } from 'bun:test'
3-
import { rm } from 'node:fs/promises'
43
import { join } from 'node:path'
54
import { generate } from '../src/generate'
5+
import { rm } from 'node:fs/promises'
66

77
describe('dts-generation', () => {
88
const testDir = join(__dirname, '../fixtures')
@@ -84,7 +84,7 @@ describe('dts-generation', () => {
8484
outdir: generatedDir,
8585
clean: false,
8686
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
87-
outputStructure: 'flat'
87+
outputStructure: 'flat',
8888
}
8989

9090
await generate(config)
@@ -106,7 +106,7 @@ describe('dts-generation', () => {
106106
outdir: generatedDir,
107107
clean: false,
108108
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
109-
outputStructure: 'flat'
109+
outputStructure: 'flat',
110110
}
111111

112112
await generate(config)
@@ -128,7 +128,7 @@ describe('dts-generation', () => {
128128
outdir: generatedDir,
129129
clean: false,
130130
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
131-
outputStructure: 'flat'
131+
outputStructure: 'flat',
132132
}
133133

134134
await generate(config)
@@ -150,7 +150,7 @@ describe('dts-generation', () => {
150150
outdir: generatedDir,
151151
clean: false,
152152
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
153-
outputStructure: 'flat'
153+
outputStructure: 'flat',
154154
}
155155

156156
await generate(config)
@@ -172,7 +172,7 @@ describe('dts-generation', () => {
172172
outdir: generatedDir,
173173
clean: false,
174174
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
175-
outputStructure: 'flat'
175+
outputStructure: 'flat',
176176
}
177177

178178
await generate(config)
@@ -194,7 +194,7 @@ describe('dts-generation', () => {
194194
outdir: generatedDir,
195195
clean: false,
196196
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
197-
outputStructure: 'flat'
197+
outputStructure: 'flat',
198198
}
199199

200200
await generate(config)
@@ -216,7 +216,7 @@ describe('dts-generation', () => {
216216
outdir: generatedDir,
217217
clean: false,
218218
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
219-
outputStructure: 'flat'
219+
outputStructure: 'flat',
220220
}
221221

222222
await generate(config)
@@ -238,7 +238,7 @@ describe('dts-generation', () => {
238238
outdir: generatedDir,
239239
clean: false,
240240
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
241-
outputStructure: 'flat'
241+
outputStructure: 'flat',
242242
}
243243

244244
await generate(config)
@@ -260,7 +260,29 @@ describe('dts-generation', () => {
260260
outdir: generatedDir,
261261
clean: false,
262262
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
263-
outputStructure: 'flat'
263+
outputStructure: 'flat',
264+
}
265+
266+
await generate(config)
267+
268+
const outputPath = join(outputDir, `${example}.d.ts`)
269+
const generatedPath = join(generatedDir, `${example}.d.ts`)
270+
271+
const expectedContent = await Bun.file(outputPath).text()
272+
const generatedContent = await Bun.file(generatedPath).text()
273+
274+
expect(generatedContent).toBe(expectedContent)
275+
})
276+
277+
it('should properly generate types for type example/0010', async () => {
278+
const example = '0010'
279+
280+
const config: DtsGenerationOption = {
281+
entrypoints: [join(inputDir, `example/${example}.ts`)],
282+
outdir: generatedDir,
283+
clean: false,
284+
tsconfigPath: join(__dirname, '..', 'tsconfig.json'),
285+
outputStructure: 'flat',
264286
}
265287

266288
await generate(config)

0 commit comments

Comments
 (0)