commit e38d7dfb94f169481acce3a66a78ce2101ace893
parent 01a2ecce542a8f1ff86003cf55cac0b00de3b0de
Author: Demonstrandum <moi@knutsen.co>
Date: Fri, 15 May 2020 22:56:35 +0100
Reject google results >10 and don't use metric extensions.
Diffstat:
4 files changed, 36 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
@@ -31,7 +31,7 @@ with GitHub rendering).
## Getting Up & Running
-Make sure you have `node` (`v10.x`) and `yarn` installed
+Make sure you have `node` (`v13.11.x`) and `yarn` installed
(`npm` also possible).
- Clean up from previous build/install:
diff --git a/lib/api/google.ts b/lib/api/google.ts
@@ -38,10 +38,16 @@ const web_search = (param : CSE) => new Promise((resolve, reject) => {
delete CACHE[cache_keys[2]];
}
- const num_match = param.query.trim().match(/[ ]+(\d+)$/);
+ let num_match = param.query.trim().match(/[ ]+(\d+)$/);
if (num_match)
query = query.slice(0, -num_match[1].length).trim();
- const result_index = Math.abs(num_match ? Number(num_match[1]) - 1 : 0) % 10;
+
+ const result_num : number = Number(num_match[1]) || 1;
+
+ if (result_num > 10)
+ return reject("Can only query up to 10th result (API restriction).");
+
+ const result_index = Math.abs(result_num - 1);
const cs = google.customsearch('v1');
cs.cse.list({
@@ -57,7 +63,7 @@ const web_search = (param : CSE) => new Promise((resolve, reject) => {
return reject('No such results found.');
const item = res.data.items[result_index];
- const answer = `Search for ‘${param.query}’: ${item.link}\n>>> ${item.title}`;
+ const answer = `Search for ‘${param.query}’ (result no. ${result_num}) ${item.link}\n>>> ${item.title}`;
// Cache this query (DO NOT CACHE NSFW)
if (!param.nsfw)
CACHE[param.query] = answer;
diff --git a/lib/api/yt_scrape.ts b/lib/api/yt_scrape.ts
@@ -18,7 +18,7 @@ const yt_search = (params: YTSearch) => new Promise((resolve, reject) => {
upload_date, link: url } = res[0];
return resolve(`Searh for ‘${params.query}’: ${url}\n> ${title} | \
- ${views.to_metric()} views | uploaded ${upload_date} | \
+ ${views.to_abbrev()} views | uploaded ${upload_date} | \
by: ${by}.`.squeeze());
});
});
diff --git a/lib/extensions.ts b/lib/extensions.ts
@@ -113,7 +113,9 @@ declare global {
interface Number {
round_to(dp: number): number;
+ to_extension(figures, ext): string;
to_metric(figures): string;
+ to_abbrev(figures): string;
truncate(): number;
}
}
@@ -231,16 +233,34 @@ const SI_EXTENSIONS = [
{ value: 1E18, symbol: "E" }
];
-Number.prototype.to_metric = function (figures) {
- let i = SI_EXTENSIONS.length - 1;
+const NORMIE_EXTENSIONS = [
+ { value: 1, symbol: "" },
+ { value: 1E3, symbol: "K" },
+ { value: 1E6, symbol: "M" },
+ { value: 1E9, symbol: "B" },
+ { value: 1E12, symbol: "t" },
+ { value: 1E15, symbol: "q" },
+ { value: 1E18, symbol: "Q" }
+];
+
+Number.prototype.to_extension = function (figures, ext) {
+ let i = ext.length - 1;
for (; i > 0; --i)
- if (this >= SI_EXTENSIONS[i].value)
+ if (this >= ext[i].value)
break;
- return (this.valueOf() / SI_EXTENSIONS[i].value)
+ return (this.valueOf() / ext[i].value)
.toFixed(figures)
.replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1")
- + SI_EXTENSIONS[i].symbol;
+ + ext[i].symbol;
+};
+
+Number.prototype.to_metric = function (figures) {
+ return this.to_extension(figures, SI_EXTENSIONS);
+};
+
+Number.prototype.to_abbrev = function (figures) {
+ return this.to_extension(figures, NORMIE_EXTENSIONS);
};
Number.prototype.truncate = function() {