2021-07-14 23:49:18 +03:00
|
|
|
import path from 'path'
|
|
|
|
import { escapeRegExp } from 'lodash-es'
|
2020-12-09 22:20:24 +03:00
|
|
|
/* eslint-disable prefer-regex-literals */
|
|
|
|
|
|
|
|
// slash at the beginning of a filename
|
|
|
|
const leadingPathSeparator = new RegExp(`^${escapeRegExp(path.sep)}`)
|
|
|
|
const windowsLeadingPathSeparator = new RegExp('^/')
|
|
|
|
|
|
|
|
// all slashes in the filename. path.sep is OS agnostic (windows, mac, etc)
|
|
|
|
const pathSeparator = new RegExp(escapeRegExp(path.sep), 'g')
|
|
|
|
const windowsPathSeparator = new RegExp('/', 'g')
|
|
|
|
|
|
|
|
// handle MS Windows style double-backslashed filenames
|
|
|
|
const windowsDoubleSlashSeparator = new RegExp('\\\\', 'g')
|
|
|
|
|
|
|
|
// derive `foo.bar.baz` object key from `foo/bar/baz.yml` filename
|
2021-07-15 00:35:01 +03:00
|
|
|
export default function filenameToKey(filename) {
|
2020-12-09 22:20:24 +03:00
|
|
|
const extension = new RegExp(`${path.extname(filename)}$`)
|
|
|
|
const key = filename
|
|
|
|
.replace(extension, '')
|
|
|
|
.replace(leadingPathSeparator, '')
|
|
|
|
.replace(windowsLeadingPathSeparator, '')
|
|
|
|
.replace(pathSeparator, '.')
|
|
|
|
.replace(windowsPathSeparator, '.')
|
|
|
|
.replace(windowsDoubleSlashSeparator, '.')
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|