-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.js
50 lines (41 loc) · 1.33 KB
/
utils.js
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
var util = require('util')
var Promise = require('bluebird')
var streamToArray = require('stream-to-array')
var streamToArrayAsync = Promise.promisify(streamToArray)
exports.isHtmlFile = function isHtmlFile(filePath) {
return filePath.match(/\.html$/)
}
exports.loadFileContent = function loadFileContent(stream) {
return streamToArrayAsync(stream).then(function (parts) {
var buffers = parts.map(function (part) {
return util.isBuffer(part) ? part : Buffer.from(part)
});
return Buffer.concat(buffers);
})
}
exports.getTypeName = function getTypeName(type) {
return type.toUpperCase()
}
exports.svgToDataUri = function (svg) {
return 'data:image/svg+xml,' + encodeURIComponent(collapseWhitespace(svg)).replace(/%[\dA-F]{2}/g, specialHexEncode)
}
function collapseWhitespace(svg) {
return svg.trim().replace(/\s+/g, ' ')
}
function specialHexEncode(match) {
switch (match) { // Browsers tolerate these characters, and they're frequent
case '%20': return ' '
case '%3D': return '='
case '%3A': return ':'
case '%2F': return '/'
default: return match
}
}
exports.base64Encode = function base64Encode(data) {
var buff = new Buffer(data)
return buff.toString('base64')
}
exports.base64Decode = function base64Decode(data) {
var buff = new Buffer(data, 'base64')
return buff.toString('utf-8')
}