This repository was archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (60 loc) · 1.87 KB
/
index.js
File metadata and controls
77 lines (60 loc) · 1.87 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
var ints = ['w', 'h', 'blur'];
var bools = ['cover', 'bw', 'sepia'];
var limits = { w: 1500, h: 1500, blur: 25 };
module.exports.getCanonical = function (tpl, params) {
var opts = this.parseOptions(params.options);
var vars = ints.concat(bools).sort();
var optsArr = [];
var hasSize = false;
var opt;
for (var i = 0; i < vars.length; i++) {
opt = vars[i];
if (opt in opts) {
if (typeof opts[opt] === 'boolean') {
optsArr.push(opt);
} else {
// Check if width or height exist, change status
if (opt === 'w' || opt === 'h') {
hasSize = true;
}
// Limit values
if (opts[opt] > limits[opt]) {
opts[opt] = limits[opt];
}
optsArr.push(opt + opts[opt]);
}
}
}
// Remove cover if neither "w" or "h" are present
var coverIdx = optsArr.indexOf('cover');
if (coverIdx !== -1 && !hasSize) {
optsArr.splice(coverIdx, 1);
}
// Replace input options
params.options = optsArr.join(',')
// Replace :vars
for (var param in params) {
tpl = tpl.replace(':'+ param, params[param]);
}
// Remove trailing slash
return tpl.replace(/\/$/, '');
};
module.exports.parseOptions = function (opts) {
var out = {};
opts = (opts || '').split(',');
for (var i = 0; i < opts.length; i++) {
var opt = opts[i].toLowerCase();
if (/^[a-z]+\d+$/.test(opt)) {
var name = opt.replace(/\d+/, '');
if (ints.indexOf(name) !== -1) {
out[name] = parseInt(opt.replace(name, ''));
}
} else {
if (bools.indexOf(opt) !== -1) {
out[opt] = true;
}
}
}
return out;
};