All files / src/fs path.ts

0% Statements 0/30
0% Branches 0/14
0% Functions 0/10
0% Lines 0/29

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                                                                                                                                         
import { NonEmptyPath } from './types'
 
export const splitParts = (path: string): string[] => {
  return path.split('/').filter(p => p.length > 0)
}
 
export const join = (parts: string[]): string => {
  return parts.join('/')
}
 
export const joinNoSuffix = (parts: string[]): string => {
  const joined = join(parts)
  return joined[joined.length -1] === '/'
          ? joined.slice(0, joined.length -1)
          : joined
}
 
export const parent = (path: string): string | null => {
  return takeTail(path).parentPath
}
 
type HeadParts = {
  head: string | null
  nextPath: string | null
}
 
export const takeHead = (path: string): HeadParts => {
  const parts = splitParts(path)
  const next = parts.slice(1)
  return {
    head: parts[0] || null,
    nextPath: next.length > 0 ? join(next) : null
  }
}
 
type TailParts = {
  tail: string | null
  parentPath: string | null
}
 
export const takeTail = (path: string): TailParts => {
  const parts = splitParts(path)
  const parent = parts.slice(0, parts.length - 1)
  return {
    tail: parts[parts.length - 1] || null,
    parentPath: parent.length > 0 ? join(parent) : null
  }
}
 
export const splitNonEmpty = (path: string): NonEmptyPath | null => {
  const parts = splitParts(path)
  if (parts.length < 1) {
    return null
  }
  return parts as NonEmptyPath
}
 
export const nextNonEmpty = (parts: NonEmptyPath): NonEmptyPath | null => {
  const next = parts.slice(1)
  if (next.length < 1) {
    return null
  }
  return next as NonEmptyPath
}
 
export const sameParent = (a: string, b: string): boolean => {
  return splitParts(a)[0] === splitParts(b)[0]
}