Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

filename-to-key.js 1.0 KB

You have to be logged in to leave a comment. Sign In
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
  1. /* eslint-disable prefer-regex-literals */
  2. const path = require('path')
  3. const { escapeRegExp } = require('lodash')
  4. // slash at the beginning of a filename
  5. const leadingPathSeparator = new RegExp(`^${escapeRegExp(path.sep)}`)
  6. const windowsLeadingPathSeparator = new RegExp('^/')
  7. // all slashes in the filename. path.sep is OS agnostic (windows, mac, etc)
  8. const pathSeparator = new RegExp(escapeRegExp(path.sep), 'g')
  9. const windowsPathSeparator = new RegExp('/', 'g')
  10. // handle MS Windows style double-backslashed filenames
  11. const windowsDoubleSlashSeparator = new RegExp('\\\\', 'g')
  12. // derive `foo.bar.baz` object key from `foo/bar/baz.yml` filename
  13. module.exports = function filenameToKey (filename) {
  14. const extension = new RegExp(`${path.extname(filename)}$`)
  15. const key = filename
  16. .replace(extension, '')
  17. .replace(leadingPathSeparator, '')
  18. .replace(windowsLeadingPathSeparator, '')
  19. .replace(pathSeparator, '.')
  20. .replace(windowsPathSeparator, '.')
  21. .replace(windowsDoubleSlashSeparator, '.')
  22. return key
  23. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...