Skip to content

Commit

Permalink
Merge pull request #6 from konkerdotdev/basename
Browse files Browse the repository at this point in the history
Add basename to TInyFileSystem API
  • Loading branch information
konker committed Jan 31, 2024
2 parents b9e1dfa + 7f218e9 commit 5f5d144
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@konker.dev/tiny-filesystem-fp",
"version": "0.0.8",
"version": "0.0.9",
"author": "Konrad Markus <mail@konker.dev>",
"description": "A minimal filesystem abstraction for Node.js and AWS S3, based on Effect-TS",
"license": "ISC",
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ export type TinyFileSystem = {
*/
fileName: (filePath: string) => P.Effect.Effect<never, TinyFileSystemError, FileName>;

/**
* Extract the last part of a file or directory path
*
* @param fileOrDirPath - The full path of the file or directory
*/
basename: (fileOrDirPath: string) => Ref;

/**
* Join the given parts into a full path
*
Expand Down
10 changes: 10 additions & 0 deletions src/memfs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ describe('MemFsTinyFileSystem', () => {
});
});

describe('basename', () => {
it('should function correctly', () => {
expect(memFsTinyFileSystem.basename('foo/bar/baz.json')).toEqual('baz.json');
expect(memFsTinyFileSystem.basename('foo/bar')).toEqual('bar');
expect(memFsTinyFileSystem.basename('foo/bar/')).toEqual('bar');
expect(memFsTinyFileSystem.basename('foo/')).toEqual('foo');
expect(memFsTinyFileSystem.basename('foo')).toEqual('foo');
});
});

describe('extname', () => {
it('should return the file extension', () => {
const filePath = '/path/to/file.txt';
Expand Down
16 changes: 9 additions & 7 deletions src/memfs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,19 @@ function fileName(filePath: string): P.Effect.Effect<never, TinyFileSystemError,
return P.pipe(
getFileType(filePath),
P.Effect.flatMap((fileType) =>
P.pipe(
fileTypeIsFile(fileType),
(isFile) =>
isFile
? P.Effect.succeed(path.basename(filePath) as FileName)
: P.Effect.fail(toTinyFileSystemError('ERROR MESSAGE'))
// FIXME: error message
P.pipe(fileTypeIsFile(fileType), (isFile) =>
isFile
? P.Effect.succeed(path.basename(filePath) as FileName)
: P.Effect.fail(toTinyFileSystemError('Cannot get fileName of a directory'))
)
)
);
}

function basename(fileOrDirPath: string): Ref {
return path.basename(fileOrDirPath) as Ref;
}

function extname(filePath: string): string {
return path.extname(filePath);
}
Expand Down Expand Up @@ -185,6 +186,7 @@ export function MemFsTinyFileSystem(fsState: any = {}, cwd = '/'): TinyFileSyste
relative,
dirName,
fileName,
basename,
extname,
};
}
Expand Down
10 changes: 10 additions & 0 deletions src/node/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ describe('NodeTinyFileSystem', () => {
});
});

describe('basename', () => {
it('should function correctly', () => {
expect(unit.basename('foo/bar/baz.json')).toEqual('baz.json');
expect(unit.basename('foo/bar')).toEqual('bar');
expect(unit.basename('foo/bar/')).toEqual('bar');
expect(unit.basename('foo/')).toEqual('foo');
expect(unit.basename('foo')).toEqual('foo');
});
});

describe('extname', () => {
it('should return the file extension', () => {
const filePath = '/path/to/file.txt';
Expand Down
21 changes: 10 additions & 11 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,26 @@ function relative(from: string, to: string): Ref {
}

function dirName(filePath: string): P.Effect.Effect<never, TinyFileSystemError, Ref> {
return P.pipe(
getFileType(filePath),
P.Effect.map((_) => path.dirname(filePath) as DirectoryPath)
);
return P.Effect.succeed(path.dirname(filePath) as DirectoryPath);
}

function fileName(filePath: string): P.Effect.Effect<never, TinyFileSystemError, FileName> {
return P.pipe(
getFileType(filePath),
P.Effect.flatMap((fileType) =>
P.pipe(
fileTypeIsFile(fileType),
(isFile) =>
isFile
? P.Effect.succeed(path.basename(filePath) as FileName)
: P.Effect.fail(toTinyFileSystemError('ERROR MESSAGE'))
// FIXME: error message
P.pipe(fileTypeIsFile(fileType), (isFile) =>
isFile
? P.Effect.succeed(path.basename(filePath) as FileName)
: P.Effect.fail(toTinyFileSystemError('Cannot get file name of a directory'))
)
)
);
}

function basename(fileOrDirPath: string): Ref {
return path.basename(fileOrDirPath) as Ref;
}

function extname(filePath: string): string {
return path.extname(filePath);
}
Expand All @@ -178,5 +176,6 @@ export const NodeTinyFileSystem: TinyFileSystemWithGlob<TinyFileSystemAppendable
relative,
dirName,
fileName,
basename,
extname,
};
10 changes: 10 additions & 0 deletions src/s3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,16 @@ describe('S3TinyFileSystem', () => {
});
});

describe('basename', () => {
it('should function correctly', () => {
expect(s3TinyFileSystem.basename('s3://foo/bar/baz.json')).toEqual('baz.json');
expect(s3TinyFileSystem.basename('s3://foo/bar')).toEqual('bar');
expect(s3TinyFileSystem.basename('s3://foo/bar/')).toEqual('bar');
expect(s3TinyFileSystem.basename('s3://foo/')).toEqual('foo');
expect(s3TinyFileSystem.basename('s3://foo')).toEqual('foo');
});
});

describe('relative', () => {
it('should function correctly', async () => {
expect(s3TinyFileSystem.relative('s3://foo/bar/', 's3://foo/bar/baz/qux.json')).toBe('baz/qux.json');
Expand Down
5 changes: 5 additions & 0 deletions src/s3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ function fileName(filePath: string): P.Effect.Effect<never, TinyFileSystemError,
);
}

function basename(fileOrDirPath: string): Ref {
return path.posix.basename(fileOrDirPath) as Ref;
}

function extname(filePath: string): string {
return path.posix.extname(filePath);
}
Expand All @@ -331,6 +335,7 @@ export const S3TinyFileSystem = (config: S3ClientConfig): P.Effect.Effect<S3Fact
relative,
dirName,
fileName,
basename,
extname,

getFileReadStream: getFileReadStream(s3Client),
Expand Down

0 comments on commit 5f5d144

Please sign in to comment.