-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtools.js
More file actions
63 lines (53 loc) · 1.5 KB
/
tools.js
File metadata and controls
63 lines (53 loc) · 1.5 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import rename from 'deep-rename-keys'
import { parseSync } from 'xml-reader'
export const parseInput = (input) => {
const parsed = parseSync(`<root>${input}</root>`, {
parentNodes: false,
})
const isValid =
parsed.children &&
parsed.children.length > 0 &&
parsed.children.every((node) => node.name === 'svg')
if (isValid) {
return parsed.children.length === 1 ? parsed.children[0] : parsed.children
} else {
throw Error('nothing to parse')
}
}
export const removeDoctype = (input) => {
return input.replace(/<[\/]{0,1}(\!?DOCTYPE|\??xml)[^><]*>/gi, '')
}
export const wrapInput = (input) => `<root>${input}</root>`
// export const removeAttrs = (obj) => omitDeep(obj, ['parent'])
export const addCustomAttrs = (attrs, node) => ({
...node,
...attrs,
})
export const camelize = (node) => {
return rename(node, (key) => {
if (!notCamelcase(key)) {
return toCamelCase(key)
}
return key
})
}
export const toCamelCase = (prop) =>
prop.replace(/[-|:]([a-z])/gi, (all, letter) => letter.toUpperCase())
const notCamelcase = (prop) => /^(data|aria)(-\w+)/.test(prop)
export const escapeText = (text) => {
if (text) {
const str = String(text)
return /[&<>]/.test(str)
? `<![CDATA[${str.replace(/]]>/, ']]]]><![CDATA[>')}]]>`
: str
}
return ''
}
export const escapeAttr = (attr) => {
return String(attr)
.replace(/&/g, '&')
.replace(/'/g, ''')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>')
}