Skip to content

Commit

Permalink
feat: 🎸 introduce FSA context
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 14, 2023
1 parent b8e8a4c commit b696e09
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
9 changes: 5 additions & 4 deletions src/node-to-fsa/NodeFileSystemDirectoryHandle.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import {NodeFileSystemHandle} from "./NodeFileSystemHandle";
import {basename} from "./util";
import type {FsaNodeFs} from "./types";
import {basename, ctx as createCtx} from "./util";
import type {NodeFsaContext, NodeFsaFs} from "./types";

/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle
*/
export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle {
constructor (
protected readonly fs: FsaNodeFs,
protected readonly fs: NodeFsaFs,
protected readonly path: string,
protected readonly ctx: Partial<NodeFsaContext> = createCtx(ctx),
) {
super('directory', basename(path));
super('directory', basename(path, ctx.separator!));
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/node-to-fsa/NodeFileSystemFileHandle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {IFileHandle} from "../promises";
import {NodeFileSystemHandle} from "./NodeFileSystemHandle";
import {NodeFileSystemSyncAccessHandle} from "./NodeFileSystemSyncAccessHandle";
import {basename, ctx as createCtx} from "./util";
import type {NodeFsaContext, NodeFsaFs} from "./types";

export class NodeFileSystemFileHandle extends NodeFileSystemHandle {
constructor (name: string, protected readonly handle: IFileHandle) {
super('file', name);
constructor (
protected readonly fs: NodeFsaFs,
protected readonly path: string,
protected readonly ctx: Partial<NodeFsaContext> = createCtx(ctx),
) {
super('directory', basename(path, ctx.separator!));
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/node-to-fsa/__tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import {basename} from '../util';

describe('basename()', () => {
test('handles empty string', () => {
expect(basename('')).toBe('');
expect(basename('', '/')).toBe('');
});

test('return the same string if there is no nesting', () => {
expect(basename('scary.exe')).toBe('scary.exe');
expect(basename('scary.exe', '/')).toBe('scary.exe');
});

test('returns last step in path', () => {
expect(basename('/gg/wp/hf/gl.txt')).toBe('gl.txt');
expect(basename('gg/wp/hf/gl.txt')).toBe('gl.txt');
expect(basename('/wp/hf/gl.txt')).toBe('gl.txt');
expect(basename('wp/hf/gl.txt')).toBe('gl.txt');
expect(basename('/hf/gl.txt')).toBe('gl.txt');
expect(basename('hf/gl.txt')).toBe('gl.txt');
expect(basename('/gl.txt')).toBe('gl.txt');
expect(basename('gl.txt')).toBe('gl.txt');
expect(basename('/gg/wp/hf/gl.txt', '/')).toBe('gl.txt');
expect(basename('gg/wp/hf/gl.txt', '/')).toBe('gl.txt');
expect(basename('/wp/hf/gl.txt', '/')).toBe('gl.txt');
expect(basename('wp/hf/gl.txt', '/')).toBe('gl.txt');
expect(basename('/hf/gl.txt', '/')).toBe('gl.txt');
expect(basename('hf/gl.txt', '/')).toBe('gl.txt');
expect(basename('/gl.txt', '/')).toBe('gl.txt');
expect(basename('gl.txt', '/')).toBe('gl.txt');
});

test('handles double slashes', () => {
expect(basename('/gg/wp/hf//gl.txt')).toBe('gl.txt');
expect(basename('//gl.txt')).toBe('gl.txt');
expect(basename('/gg/wp/hf//gl.txt', '/')).toBe('gl.txt');
expect(basename('//gl.txt', '/')).toBe('gl.txt');
});
});
6 changes: 5 additions & 1 deletion src/node-to-fsa/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/**
* Required Node.js `fs` module functions for File System Access API.
*/
export type FsaNodeFs = Pick<typeof import('fs'), 'promises'>;
export type NodeFsaFs = Pick<typeof import('fs'), 'promises'>;

export interface NodeFsaContext {
separator: '/' | '\\';
}

export interface NodeFileSystemHandlePermissionDescriptor {
mode: 'read' | 'readwrite';
Expand Down
16 changes: 14 additions & 2 deletions src/node-to-fsa/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
export const basename = (path: string) => {
const lastSlashIndex = path.lastIndexOf('/');
import type {NodeFsaContext} from "./types";

/**
* Creates a new {@link NodeFsaContext}.
*/
export const ctx = (partial: Partial<NodeFsaContext> = {}): NodeFsaContext => {
return {
...partial,
separator: '/',
};
};

export const basename = (path: string, separator: string) => {
const lastSlashIndex = path.lastIndexOf(separator);
return lastSlashIndex === -1 ? path : path.slice(lastSlashIndex + 1);
};

0 comments on commit b696e09

Please sign in to comment.