1
0
mirror of synced 2025-12-22 11:26:57 -05:00
Files
docs/lib/filename-to-key.js
Kevin Heis 1b424dfdc4 Bring in data-directory, let's go async file reads (#16782)
* Bring in data-directory, let's go async file reads

* Lint fixes

* Update glossary.js
2020-12-09 11:20:24 -08:00

29 lines
1.0 KiB
JavaScript

/* eslint-disable prefer-regex-literals */
const path = require('path')
const { escapeRegExp } = require('lodash')
// 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
module.exports = function filenameToKey (filename) {
const extension = new RegExp(`${path.extname(filename)}$`)
const key = filename
.replace(extension, '')
.replace(leadingPathSeparator, '')
.replace(windowsLeadingPathSeparator, '')
.replace(pathSeparator, '.')
.replace(windowsPathSeparator, '.')
.replace(windowsDoubleSlashSeparator, '.')
return key
}