From 3fbf01918f8dfe166c15032bdb782ee6a2d339d1 Mon Sep 17 00:00:00 2001 From: Emelia Smith Date: Mon, 22 Jan 2024 11:02:26 +0100 Subject: [PATCH] Streaming: Move more methods to the utils from the main file (#28825) --- streaming/index.js | 42 +---------------------------------------- streaming/utils.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/streaming/index.js b/streaming/index.js index aa75a08b7fb..78b049723f7 100644 --- a/streaming/index.js +++ b/streaming/index.js @@ -16,7 +16,7 @@ const WebSocket = require('ws'); const { logger, httpLogger, initializeLogLevel, attachWebsocketHttpLogger, createWebsocketLogger } = require('./logging'); const { setupMetrics } = require('./metrics'); -const { isTruthy } = require("./utils"); +const { isTruthy, normalizeHashtag, firstParam } = require("./utils"); const environment = process.env.NODE_ENV || 'development'; @@ -1110,34 +1110,6 @@ const startServer = async () => { return arr; }; - /** - * See app/lib/ascii_folder.rb for the canon definitions - * of these constants - */ - const NON_ASCII_CHARS = 'ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž'; - const EQUIVALENT_ASCII_CHARS = 'AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz'; - - /** - * @param {string} str - * @returns {string} - */ - const foldToASCII = str => { - const regex = new RegExp(NON_ASCII_CHARS.split('').join('|'), 'g'); - - return str.replace(regex, match => { - const index = NON_ASCII_CHARS.indexOf(match); - return EQUIVALENT_ASCII_CHARS[index]; - }); - }; - - /** - * @param {string} str - * @returns {string} - */ - const normalizeHashtag = str => { - return foldToASCII(str.normalize('NFKC').toLowerCase()).replace(/[^\p{L}\p{N}_\u00b7\u200c]/gu, ''); - }; - /** * @param {any} req * @param {string} name @@ -1380,18 +1352,6 @@ const startServer = async () => { connectedChannels.labels({ type: 'websocket', channel: 'system' }).inc(2); }; - /** - * @param {string|string[]} arrayOrString - * @returns {string} - */ - const firstParam = arrayOrString => { - if (Array.isArray(arrayOrString)) { - return arrayOrString[0]; - } else { - return arrayOrString; - } - }; - /** * @param {WebSocket & { isAlive: boolean }} ws * @param {http.IncomingMessage & ResolvedAccount} req diff --git a/streaming/utils.js b/streaming/utils.js index ad8dd4889f7..7b87a1d14c9 100644 --- a/streaming/utils.js +++ b/streaming/utils.js @@ -20,3 +20,50 @@ const isTruthy = value => value && !FALSE_VALUES.includes(value); exports.isTruthy = isTruthy; + + +/** + * See app/lib/ascii_folder.rb for the canon definitions + * of these constants + */ +const NON_ASCII_CHARS = 'ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž'; +const EQUIVALENT_ASCII_CHARS = 'AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz'; + +/** + * @param {string} str + * @returns {string} + */ +function foldToASCII(str) { + const regex = new RegExp(NON_ASCII_CHARS.split('').join('|'), 'g'); + + return str.replace(regex, function(match) { + const index = NON_ASCII_CHARS.indexOf(match); + return EQUIVALENT_ASCII_CHARS[index]; + }); +} + +exports.foldToASCII = foldToASCII; + +/** + * @param {string} str + * @returns {string} + */ +function normalizeHashtag(str) { + return foldToASCII(str.normalize('NFKC').toLowerCase()).replace(/[^\p{L}\p{N}_\u00b7\u200c]/gu, ''); +} + +exports.normalizeHashtag = normalizeHashtag; + +/** + * @param {string|string[]} arrayOrString + * @returns {string} + */ +function firstParam(arrayOrString) { + if (Array.isArray(arrayOrString)) { + return arrayOrString[0]; + } else { + return arrayOrString; + } +} + +exports.firstParam = firstParam;