-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.js
182 lines (154 loc) · 6.02 KB
/
utility.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
// S4S Discovery utilities
// File: utility.js
const version = '20191122';
// Required modules
const moment = require('moment-timezone');
const fs = require('fs');
const Handlebars = require('handlebars');
// Configuration
const config = require('./config');
// Add an 'ifarray' block helper to Handlebars
Handlebars.registerHelper('ifarray', function (context, options) {
var type = Object.prototype.toString.call(context);
if (type === '[object Function]') { context = context.call(this); }
if (!context || Handlebars.Utils.isEmpty(context) || type != '[object Array]') {
return options.inverse(this);
} else {
return options.fn(this);
}
});
// Handlebars template file for documentRestifyRoutes()
const documentTemplate = Handlebars.compile(fs.readFileSync(__dirname + '/template.html', 'utf8'));
// Create a list of '($1,$2,...,$len)' with no offset
// or a list of '($n,$n+1,...)' with offset=n
exports.placeHolderList = function (len, offset) {
var h = new Array(len);
var start = (offset == undefined) ? 0 : offset;
for (var i = 0; i < len; i++)
h[i] = '$' + (start+i+1);
return '(' + h.join(',') + ')';
};
// Determine the type of obj (by hacking apart '[object Array]' etc.)
exports.typeOf = function (obj) {
return Object.prototype.toString.call(obj).split(' ')[1].slice(0, -1);
};
// Add a format method to String: 'hello {1} {2}'.format('there', '.') --> 'hello there.'
String.prototype.format = function() {
var formatted = this;
for (var i = 0; i < arguments.length; i++) {
var regexp = new RegExp('\\{'+i+'\\}', 'gi');
formatted = formatted.replace(regexp, arguments[i]);
}
return formatted;
};
// Functions to determine whether a set of asynchronous components are all "ready"
// (this could be done with a counter, but then harder to observe)
// Set the tracking structure's element for this named component to "not ready"
exports.setNotReady = function (isReady, name) {
isReady[name] = false;
};
// Set the tracking structure's element for this named component to "ready"
// and return true if ALL elements are "ready"
exports.setReady = function (isReady, name) {
isReady[name] = true;
for (var key in isReady) {
if (isReady.hasOwnProperty(key) && !isReady[key]) {
// At least one element is not "ready"
return false;
}
}
// All elements are "ready"
return true;
};
// Convert 'from' strings to 'to' strings (change case, instantiate parameters, etc.)
// 'struct' is a string or structured data object (hash).
// 'mapArray' is an array of objects, each containing a 'from' and 'to' member.
exports.remap = function (struct, mapArray) {
var result = (exports.typeOf(struct) == 'String') ? struct : JSON.stringify(struct);
for (var i = 0; i < mapArray.length; i++) {
var map = mapArray[i];
var regExp = new RegExp(map.from, 'g');
result = result.replace(regExp, map.to);
}
return (exports.typeOf(struct) == 'String') ? result : JSON.parse(result);
};
// Create group structure
// 'array' is the set of elements to group
// 'predicate' is the function to select the item of each element to group on
exports.groupBy = function (array, predicate) {
var groups = {};
for (var i = 0; i < array.length; i++) {
var groupKey = predicate(array[i]);
if (groups[groupKey] == undefined) {
groups[groupKey] = [];
}
groups[groupKey].push(array[i]);
}
return groups;
};
// Order the array 'array' (MODIFIES THE ARRAY!)
// 'dir' is 'ascending' for ascending order, else descending order
// 'select' is the function to select the item of each element to order on
exports.orderBy = function (array, dir, select) {
var isAscending = (dir == 'ascending');
return array.sort(function (a, b) {
var predA = select(a);
var predB = select(b);
return predA < predB ? (isAscending ? -1 : 1) : predA > predB ? (isAscending ? 1 : -1) : 0;
})
};
// Send text response (with headers)
exports.sendText = function (req, res, code, val) {
var strVal = val.toString();
req.log.info({req:req}, strVal);
res.header('Access-Control-Allow-Headers', 'content-type, x-requested-with');
res.header('content-type', 'text/plain');
res.send(code, strVal);
};
// Send JSON response (with headers)
// TODO: Check for Accept-Encoding header & compress response
exports.sendJson = function (req, res, val) {
req.log.info({req: req}, val);
res.header('Access-Control-Allow-Headers', 'content-type, x-requested-with');
res.json(val);
};
// Document restify routes
exports.documentRestifyRoutes = function (server) {
// Collect documentation
var calls = {};
var callUrls = [];
for (var route in server.routes) {
if (server.routes.hasOwnProperty(route)) {
try {
// Get documentation from route's function (if defined)
let routeFnIndex = server.routes[route].length - 1; // Route's function is the last element
var doc = server.routes[route][routeFnIndex]();
// Add route info
var spec = server.router.mounts[route].spec;
var route = spec.method + ' ' + spec.path;
var id = makeId(route);
doc.url = {route:route, id:id};
// Construct 'calls' element (for Javascript create/edit)
var elt = {path: route.split(' ')[1]};
if (doc.params != undefined) {
elt.params = doc.params;
};
if (doc.post != undefined) {
elt.post = doc.post;
};
calls[id] = elt;
// Add doc to 'callUrls' (for page display)
callUrls.push(doc);
} catch (e) {}
}
}
// Construct date & time
var timestamp = moment().format('D MMMM YYYY h:mm:ss A ') + moment.tz(moment.tz.guess()).zoneAbbr();
// Populate the template
return documentTemplate({calls:JSON.stringify(calls), callUrls:callUrls, timestamp:timestamp, deployMode:config.deploy, serverName:server.name});
}
// "Squish" a route into an identifier (remove spaces and colons, replace forward slash with underscore)
function makeId(route) {
return route.replace(/ /g, '').replace(/:/g, '').replace(/\//g, '_');
}