summaryrefslogtreecommitdiff
path: root/lib/path.ts
blob: df12e01ab48e0f913f43de8bae5f6e4ca2fd9a4a (plain)
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
import { last } from './util'

export const encodePath = (path: string): string =>
  path
    .split('/')
    .filter((k: string) => k)
    .join('%2F') // encode the '/' char for the url

export const decodePath = (path: string): string => `/${path}`

/**
 * Split path using "/" as a delimiter
 */
export const splitPath = (path: string): string[] =>
  path.split('/').filter(k => k)

/**
 * Identify if a path is a root node
 */
export const isRootNode = (path: string): boolean => path === '/' || path === ''

/**
 * Check if path1 matches path2,
 * if not, check if its a subpath
 *
 * https://stackoverflow.com/questions/37521893/determine-if-a-path-is-subdirectory-of-another-in-node-js
 */
export const isChildOrMatch = (child: string, parent: string) => {
  if (child === parent || parent === '/') return true
  const parentTokens = parent.split('/').filter((i: string) => i.length)
  return parentTokens.every((t, i) => child.split('/')[i] === t)
}

export const getPathSuperSets = (path: string): string[] =>
  splitPath(path).reduce((acc: any[], path: string) => {
    if (!acc.length) return [`/${path}`]
    else {
      return [...acc, [last(acc), path].join('/')]
    }
  }, [])