All files / src/fsa-to-node util.ts

100% Statements 21/21
100% Branches 3/3
100% Functions 2/2
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28        3x 3102x 3102x 3102x 3102x 1870x 1870x 1870x     3x 2x 2x 2x 1x   1x   2x 2x        
import { IFileSystemDirectoryHandle } from '../fsa/types';
import { FsaToNodeConstants } from './constants';
import type { FsLocation } from './types';
 
export const pathToLocation = (path: string): FsLocation => {
  if (path[0] === FsaToNodeConstants.Separator) path = path.slice(1);
  if (path[path.length - 1] === FsaToNodeConstants.Separator) path = path.slice(0, -1);
  const lastSlashIndex = path.lastIndexOf(FsaToNodeConstants.Separator);
  if (lastSlashIndex === -1) return [[], path];
  const file = path.slice(lastSlashIndex + 1);
  const folder = path.slice(0, lastSlashIndex).split(FsaToNodeConstants.Separator);
  return [folder, file];
};
 
export const testDirectoryIsWritable = async (dir: IFileSystemDirectoryHandle): Promise<boolean> => {
  const testFileName = '__memfs_writable_test_file_' + Math.random().toString(36).slice(2) + Date.now();
  try {
    await dir.getFileHandle(testFileName, { create: true });
    return true;
  } catch {
    return false;
  } finally {
    try {
      await dir.removeEntry(testFileName);
    } catch (e) {}
  }
};