This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
ampbench_util.js
150 lines (128 loc) · 4.73 KB
/
ampbench_util.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
// Copyright 2015-2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
/* eslint no-unused-vars: 0 */
// use: import {get_bare_url, urls_are_similar} from 'ampbench_util'; !!!2018.09: NOT SUPPORTED BY NODE.JS YET!!!
const S = require('string');
const util = require('util');
function inspect_obj(obj) {
return util.inspect(obj, { showHidden: true, depth: null });
}
function unwrap_js_object(obj, maxDepth, prefix) {
let result = '';
if (!prefix) {prefix = '';}
for (let key in obj) {
if (typeof obj[key] === 'object') {
if (maxDepth !== undefined && maxDepth <= 1) {
result += (prefix + key + '=object [max depth reached]\n');
} else {
result += unwrap_js_object(obj[key], (maxDepth) ? maxDepth - 1 : maxDepth, prefix + key + '.');
}
} else {
result += (prefix + key + '=' + obj[key] + '\n');
}
}
return result;
}
function log_js_object(o, prefix) {
for (let key in o) {
if (o.hasOwnProperty(key)) {
console.log(prefix + key + ': ' + o[key]);
}
}
}
function ifdef(v) { // useful for outputting potentially undefined variable values
return v ? v : '';
}
// exports.last_element_of_list = function (list) {
function last_element_of_list(list) {
return list[list.length - 1];
}
function last_element_of_path(path) {
const path_str = path.toString();
return path_str.substr(path_str.lastIndexOf('/') + 1);
}
function str_rtrim_char(str, char) {
let str_ret = str;
if (str.charAt(str.length - 1) === char) {
str_ret = str.substr(0, str.length - 1);
}
return str_ret;
}
function format_dashes(dash_count) { // needs: const S = require('string');
return (S(('- ').repeat(dash_count)).s);
}
function print_dashes(dash_count) { // needs: const S = require('string');
console.log(format_dashes(dash_count));
}
function make_url_href(url, title) {
const pref = '<a href="' + url + '" ',
suff = 'target="_blank">' + title + '</a>';
return pref + suff;
}
function make_url_href_list(urls) {
const kBR = '<br>';
let __result = '';
if (urls) {
if (Array.isArray(urls)) {
urls.forEach((url) => {
__result += make_url_href(url, url) + kBR ;
});
}
}
return __result;
}
function multiline_to_html(multiline_str) { // convert os.EOL to HTML line-breaks
// console.log('=> multiline_str:\n' + multiline_str);
if (multiline_str) {
if (-1 !== multiline_str.indexOf(os.EOL)) { // eslint-disable-line no-undef
var h = multiline_str.split(os.EOL).join('<br/>'); // eslint-disable-line no-undef
// return '<div><span style="color:black;font-weight:bold">' + h + '</span></div>';
return ('<div><span style="font-family:Monospace;">' + h + '</span></div>');
}
return ('<div><span style="font-family:Monospace;">' + multiline_str + '</span></div>');
}
return '';
}
function get_bare_url(url) {
// note: https://ilikekillnerds.com/2016/05/removing-character-startend-string-javascript/
// test: 'xwp.co' === get_bare_url('https://xwp.co')
// test: 'xwp.co' === get_bare_url('https://xwp.co/')
let _url = url.trim();
if ('/' === _url.slice(-1)) { // check last character of the url
_url = _url.slice(0, -1); // remove last character of the url
}
if (-1 < _url.indexOf('http://')) {
_url = _url.substr(7);
}
if (-1 < _url.indexOf('https://')) {
_url = _url.substr(8);
}
return _url;
}
function urls_are_similar(url1, url2) {
// note: https://ilikekillnerds.com/2016/05/removing-character-startend-string-javascript/
// test: urls_are_similar('https://xwp.co', 'https://xwp.co/')
// test: urls_are_similar('https://xwp.co', 'http://xwp.co')
let _url1 = get_bare_url(url1),
_url2 = get_bare_url(url2);
return ( // compare what is left of the two urls
_url1 === _url2
);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// module exports
//
exports.get_bare_url = get_bare_url;
exports.urls_are_similar = urls_are_similar;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -