1 | const IMPORT_REGEX = /^import\s/
|
2 | const EXPORT_REGEX = /^export\s/
|
3 | const EXPORT_DEFAULT_REGEX = /^export default\s/
|
4 | const STARTS_WITH_CAPITAL_LETTER_REGEX = /^[A-Z]/
|
5 | const EMPTY_NEWLINE = '\n\n'
|
6 | const COMMENT_OPEN = '<!--'
|
7 | const COMMENT_CLOSE = '-->'
|
8 |
|
9 | const isImport = text => IMPORT_REGEX.test(text)
|
10 | const isExport = text => EXPORT_REGEX.test(text)
|
11 | const isExportDefault = text => EXPORT_DEFAULT_REGEX.test(text)
|
12 | const isImportOrExport = text => isImport(text) || isExport(text)
|
13 |
|
14 | const isComment = str =>
|
15 | str.startsWith(COMMENT_OPEN) && str.endsWith(COMMENT_CLOSE)
|
16 |
|
17 | const getCommentContents = str =>
|
18 | str.slice(COMMENT_OPEN.length, -COMMENT_CLOSE.length)
|
19 |
|
20 | const startsWithCapitalLetter = str =>
|
21 | STARTS_WITH_CAPITAL_LETTER_REGEX.test(str)
|
22 |
|
23 | const paramCase = string =>
|
24 | string
|
25 | .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
26 | .replace(/([a-z])([0-9])/g, '$1-$2')
|
27 | .toLowerCase()
|
28 |
|
29 | const toTemplateLiteral = text => {
|
30 | const escaped = text
|
31 | .replace(/\\(?!\$)/g, '\\\\')
|
32 |
|
33 |
|
34 | .replace(/`/g, '\\`')
|
35 | .replace(/(\\\$)/g, '\\$1')
|
36 | .replace(/(\\\$)(\{)/g, '\\$1\\$2')
|
37 | .replace(/\$\{/g, '\\${')
|
38 |
|
39 | return '{`' + escaped + '`}'
|
40 | }
|
41 |
|
42 | module.exports.EMPTY_NEWLINE = EMPTY_NEWLINE
|
43 | module.exports.isImport = isImport
|
44 | module.exports.isExport = isExport
|
45 | module.exports.isExportDefault = isExportDefault
|
46 | module.exports.isImportOrExport = isImportOrExport
|
47 | module.exports.startsWithCapitalLetter = startsWithCapitalLetter
|
48 | module.exports.isComment = isComment
|
49 | module.exports.getCommentContents = getCommentContents
|
50 | module.exports.paramCase = paramCase
|
51 | module.exports.toTemplateLiteral = toTemplateLiteral
|