Skip to content

Commit 0351816

Browse files
committed
🐛 Fix extension typing
1 parent da70091 commit 0351816

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

packages/system/src/FilePath.spec.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,53 @@ import { withTask } from '@w5s/task/dist/Testing.js';
44
import { FilePath } from './FilePath.js';
55

66
describe('FilePath', () => {
7-
const absolutePath = (...parts: string[]) => (FilePath.separator + parts.join(FilePath.separator)) as FilePath;
8-
const relativePath = (...parts: string[]) => parts.join(FilePath.separator) as FilePath;
7+
const { separator, dirname, concat, resolve, parse, format, relative, normalize, basename, extname, isAbsolute, isRelative, isParentOf } = FilePath;
8+
const absolutePath = (...parts: string[]) => (separator + parts.join(separator)) as FilePath;
9+
const relativePath = (...parts: string[]) => parts.join(separator) as FilePath;
910
const expectTask = withTask(expect);
1011

11-
describe('.dirname', () => {
12+
describe(dirname, () => {
1213
it('should return the dirname of parameter', () => {
1314
const path = absolutePath('one', 'two', 'three');
14-
expect(FilePath.dirname(path)).toBe(`${FilePath.separator}one${FilePath.separator}two`);
15+
expect(dirname(path)).toBe(`${separator}one${separator}two`);
1516
});
1617
});
1718

18-
describe('.concat', () => {
19+
describe(concat, () => {
1920
it('should return joined path using separator', () => {
2021
const first = absolutePath('hello', 'world');
2122
const second = relativePath('..', 'earth');
22-
expect(FilePath.concat([first, second])).toBe(absolutePath('hello', 'earth'));
23+
expect(concat([first, second])).toBe(absolutePath('hello', 'earth'));
2324
});
2425
});
2526

26-
describe('.resolve', () => {
27+
describe(resolve, () => {
2728
it('return a resolved path', async () => {
2829
expectTask(
29-
FilePath.resolve(
30+
resolve(
3031
[absolutePath(''), relativePath('bar', 'baz'), relativePath('..', 'baz2')],
3132
relativePath('foo'),
3233
),
3334
).toResolveSync(absolutePath('bar', 'baz2', 'foo'));
34-
expectTask(FilePath.resolve([absolutePath('foo', 'bar')], relativePath('./baz'))).toResolveSync(
35+
expectTask(resolve([absolutePath('foo', 'bar')], relativePath('./baz'))).toResolveSync(
3536
absolutePath('foo', 'bar', 'baz'),
3637
);
37-
expectTask(FilePath.resolve([absolutePath('foo', 'bar')], absolutePath('tmp', 'file', ''))).toResolveSync(
38+
expectTask(resolve([absolutePath('foo', 'bar')], absolutePath('tmp', 'file', ''))).toResolveSync(
3839
absolutePath('tmp', 'file'),
3940
);
4041
expectTask(
41-
FilePath.resolve(
42+
resolve(
4243
[relativePath('wwwroot'), relativePath('static_files', 'png')],
4344
relativePath('..', 'gif', 'image.gif'),
4445
),
4546
).toResolveSync(relativePath(process.cwd(), 'wwwroot', 'static_files', 'gif', 'image.gif'));
4647
});
4748
});
4849

49-
describe('.parse', () => {
50+
describe(parse, () => {
5051
it('should parse empty path', () => {
5152
const path = relativePath('');
52-
expect(FilePath.parse(path)).toStrictEqual({
53+
expect(parse(path)).toStrictEqual({
5354
root: Option.None,
5455
base: Option.None,
5556
dir: Option.None,
@@ -59,10 +60,10 @@ describe('FilePath', () => {
5960
});
6061
});
6162

62-
describe('.format', () => {
63+
describe(format, () => {
6364
it('should format empty path object', () => {
6465
expect(
65-
FilePath.format({
66+
format({
6667
root: Option.None,
6768
base: Option.None,
6869
dir: Option.None,
@@ -73,53 +74,55 @@ describe('FilePath', () => {
7374
});
7475
});
7576

76-
describe('.relative', () => {
77+
describe(relative, () => {
7778
it('should return a relative path', () => {
7879
const from = absolutePath('home', 'hello', 'world');
7980
const to = absolutePath('home', 'earth');
80-
expect(FilePath.relative(from, to)).toBe(relativePath('..', '..', 'earth'));
81+
expect(relative(from, to)).toBe(relativePath('..', '..', 'earth'));
8182
});
8283
});
8384

84-
describe('.normalize', () => {
85+
describe(normalize, () => {
8586
it('return a normalized path', () => {
8687
const path = relativePath('hello', 'world', '..', 'earth');
87-
expect(FilePath.normalize(path)).toBe(relativePath('hello', 'earth'));
88+
expect(normalize(path)).toBe(relativePath('hello', 'earth'));
8889
});
8990
});
9091

91-
describe('.basename', () => {
92+
describe(basename, () => {
9293
it('return the base name of file path', () => {
9394
const path = absolutePath('hello', 'world', 'file.txt');
9495

95-
expect(FilePath.basename(path)).toBe(`file.txt`);
96-
expect(FilePath.basename(path, `.txt`)).toBe(`file`);
96+
expect(basename(path)).toBe(`file.txt`);
97+
expect(basename(path, `.txt`)).toBe(`file`);
9798
});
9899
});
99-
describe('.extname', () => {
100+
describe(extname, () => {
100101
it('return the base name of file path', () => {
101102
const path = relativePath('world', 'file.log.txt');
102-
expect(FilePath.extname(path)).toBe(`.txt`);
103+
expect(extname(path)).toBe(`.txt`);
104+
const noExt = relativePath('world', 'file');
105+
expect(extname(noExt)).toBe(``);
103106
});
104107
});
105108

106-
describe('.isAbsolute', () => {
109+
describe(isAbsolute, () => {
107110
it('return absolute path', () => {
108111
const absolute = absolutePath('world', 'file.log.txt');
109-
expect(FilePath.isAbsolute(absolute)).toBe(true);
110-
const relative = relativePath('.', 'world', 'file.log.txt');
111-
expect(FilePath.isAbsolute(relative)).toBe(false);
112+
expect(isAbsolute(absolute)).toBe(true);
113+
const _relative = relativePath('.', 'world', 'file.log.txt');
114+
expect(isAbsolute(_relative)).toBe(false);
112115
});
113116
});
114-
describe('.isRelative', () => {
117+
describe(isRelative, () => {
115118
it('return absolute path', () => {
116119
const absolute = absolutePath('world', 'file.log.txt');
117-
expect(FilePath.isRelative(absolute)).toBe(false);
118-
const relative = relativePath('.', 'world', 'file.log.txt');
119-
expect(FilePath.isRelative(relative)).toBe(true);
120+
expect(isRelative(absolute)).toBe(false);
121+
const _relative = relativePath('.', 'world', 'file.log.txt');
122+
expect(isRelative(_relative)).toBe(true);
120123
});
121124
});
122-
describe('.isParentOf', () => {
125+
describe(isParentOf, () => {
123126
it.each([
124127
[{ parent: '', child: '' }, false],
125128
[{ parent: '/first/second', child: '/first' }, false],
@@ -132,7 +135,7 @@ describe('FilePath', () => {
132135
] as [{ parent: string; child: string }, boolean][])(
133136
'should return correct value for %s',
134137
({ parent, child }, expected) => {
135-
expect(FilePath.isParentOf(FilePath(parent), FilePath(child))).toBe(expected);
138+
expect(isParentOf(FilePath(parent), FilePath(child))).toBe(expected);
136139
},
137140
);
138141
});

packages/system/src/FilePath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export const FilePath = Object.assign(
207207
);
208208
export namespace FilePath {
209209
export type Delimiter = ':' | ';';
210-
export type Extension = `.${string}`;
210+
export type Extension = `.${string}` | '';
211211
export type Separator = '/' | '\\';
212212

213213
export interface Parsed {

0 commit comments

Comments
 (0)