-
Notifications
You must be signed in to change notification settings - Fork 371
/
helpers.ts
39 lines (34 loc) · 984 Bytes
/
helpers.ts
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
import filenamify from 'filenamify'
import {
FOLDER_ID_PREFIX,
NOTE_ID_PREFIX
} from '../../../lib/consts'
export function getFolderId (path: string): string {
return `${FOLDER_ID_PREFIX}${path}`
}
export function getNoteId (id: string): string {
return `${NOTE_ID_PREFIX}${id}`
}
export function getParentFolderPath (path: string): string {
if (path === '/') throw new Error('The given path is root path.')
const splitted = path.split('/')
splitted.shift()
splitted.pop()
return '/' + splitted.join('/')
}
export function normalizeFolderPath (path: string): string {
return '/' + path
.split('/')
.reduce((sum, element) => {
const sanitized = filenamify(element, { replacement: '_' })
if (sanitized.length > 0) {
sum.push(sanitized)
}
return sum
}, [] as string[])
.join('/')
}
export function validateFolderPath (path: string): boolean {
if (path === normalizeFolderPath(path)) return false
return true
}