-
Notifications
You must be signed in to change notification settings - Fork 36
/
common.js
77 lines (68 loc) · 1.85 KB
/
common.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const url = require('url');
function isValidURL(val) {
try {
return url.parse(val).hostname !== null;
} catch (e) {
return false;
}
}
/*
* Based on https://github.com/jonschlinkert/get-value/blob/2.0.6/index.js with some enhancements.
*
* - Performs "hasOwnProperty" checks for safety.
* - Now accepts Handlebars.SafeString paths.
*/
function getValue(object, path, globals) {
let parts;
// for backwards compatibility
if (!path) {
return object;
}
// unwrap Handlebars.SafeString for compatibility with `concat` etc.
if (globals && globals.handlebars) {
path = unwrapIfSafeString(globals.handlebars, path);
}
// accept array or string for backwards compatibility
if (typeof path === 'string') {
parts = path.split('.');
} else if (Array.isArray(path)) {
parts = path;
} else {
let key = String(path);
return Object.keys(object).indexOf(key) !== -1 ? object[key] : undefined;
}
let result = object;
let prefix = '';
for (let key of parts) {
if (result === undefined || result === null) {
return undefined;
}
// preserve handling of trailing backslashes for backwards compatibility
if (key.slice(-1) === '\\') {
prefix = prefix + key.slice(0, -1) + '.';
continue;
}
key = prefix + key;
if (Object.keys(result).indexOf(key) !== -1) {
result = result[key];
prefix = '';
} else {
return;
}
}
return result;
}
function unwrapIfSafeString(handlebars, val) {
if (val instanceof handlebars.SafeString) {
val = val.toString();
}
return val;
}
const maximumPixelSize = 5120;
module.exports = {
isValidURL,
getValue,
unwrapIfSafeString,
maximumPixelSize
};