From a85151d6a37983b8d66f3bf0d04b5a0484f0e5d2 Mon Sep 17 00:00:00 2001 From: Arild Matsson Date: Thu, 7 Nov 2024 15:55:37 +0100 Subject: [PATCH] refactor(ts): statistics_config --- ...tistics_config.js => statistics_config.ts} | 41 ++++++++++--------- app/scripts/components/statistics.js | 2 +- app/scripts/statistics.ts | 2 +- 3 files changed, 24 insertions(+), 21 deletions(-) rename app/config/{statistics_config.js => statistics_config.ts} (80%) diff --git a/app/config/statistics_config.js b/app/config/statistics_config.ts similarity index 80% rename from app/config/statistics_config.js rename to app/config/statistics_config.ts index 6e71bcbd7..b1e1a180d 100644 --- a/app/config/statistics_config.js +++ b/app/config/statistics_config.ts @@ -3,8 +3,13 @@ import _ from "lodash" import settings from "@/settings" import { lemgramToHtml, regescape, saldoToHtml } from "@/util" import { locAttribute } from "@/i18n" +import { Token } from "@/backend/kwic-proxy" +import { Attribute } from "@/settings/config.types" +import { JQueryStaticExtended } from "@/jquery.types" -let customFunctions = {} +type Stringifier = (tokens: string[], ignoreCase?: boolean) => string + +let customFunctions: Record = {} try { customFunctions = require("custom/statistics.js").default @@ -12,14 +17,14 @@ try { console.log("No module for statistics functions available") } -export function getCqp(hitValues, ignoreCase) { +export function getCqp(hitValues: Token[], ignoreCase: boolean): string { const positionalAttributes = ["word", ...Object.keys(settings.corpusListing.getCurrentAttributes())] let hasPositionalAttributes = false - var tokens = [] + var tokens: string[] = [] for (var i = 0; i < hitValues.length; i++) { var token = hitValues[i] - var andExpr = [] + var andExpr: string[] = [] for (var attribute in token) { if (token.hasOwnProperty(attribute)) { var values = token[attribute] @@ -39,11 +44,11 @@ export function getCqp(hitValues, ignoreCase) { return ` ${tokens.join(" ")} ` } -function reduceCqp(type, tokens, ignoreCase) { +function reduceCqp(type: string, tokens: string[], ignoreCase: boolean): string { let attrs = settings.corpusListing.getCurrentAttributes() if (attrs[type] && attrs[type].stats_cqp) { // A stats_cqp function should call regescape for the value as appropriate - return customFunctions[attrs[type].stats_cqp](tokens, ignoreCase) + return customFunctions[attrs[type].stats_cqp!](tokens, ignoreCase) } tokens = _.map(tokens, (val) => regescape(val)) switch (type) { @@ -55,11 +60,11 @@ function reduceCqp(type, tokens, ignoreCase) { case "sense": case "transformer-neighbour": if (tokens[0] === "") return "ambiguity(" + type + ") = 0" - else var res + let res: string if (tokens.length > 1) { var key = tokens[0].split(":")[0] - var variants = [] + const variants: string[][] = [] _.map(tokens, function (val) { const parts = val.split(":") if (variants.length == 0) { @@ -68,15 +73,12 @@ function reduceCqp(type, tokens, ignoreCase) { for (var idx = 1; idx < parts.length; idx++) variants[idx - 1].push(parts[idx]) }) - variants = _.map(variants, function (variant) { - return ":(" + variant.join("|") + ")" - }) - - res = key + variants.join("") + const variantsJoined = variants.map((variant) => ":(" + variant.join("|") + ")") + res = key + variantsJoined.join("") } else { res = tokens[0] } - return type + " contains '" + res + "'" + return `${type} contains '${res}'` case "word": let s = 'word="' + tokens[0] + '"' if (ignoreCase) s = s + " %c" @@ -101,11 +103,11 @@ function reduceCqp(type, tokens, ignoreCase) { } // Get the html (no linking) representation of the result for the statistics table -export function reduceStringify(type, values, structAttributes) { +export function reduceStringify(type: string, values: string[], structAttributes: Attribute): string { let attrs = settings.corpusListing.getCurrentAttributes() if (attrs[type] && attrs[type].stats_stringify) { - return customFunctions[attrs[type].stats_stringify](values) + return customFunctions[attrs[type].stats_stringify!](values) } switch (type) { @@ -123,15 +125,16 @@ export function reduceStringify(type, values, structAttributes) { case "lex": case "lemma": case "sense": + let stringify: (value: string, appendIndex?: boolean) => string if (type == "saldo" || type == "sense") { - var stringify = saldoToHtml + stringify = saldoToHtml } else if (type == "lemma") { stringify = (lemma) => lemma.replace(/_/g, " ") } else { stringify = lemgramToHtml } - var html = _.map(values, function (token) { + const html = _.map(values, function (token) { if (token === "") return "–" return stringify(token.replace(/:.*/g, ""), true) }) @@ -148,7 +151,7 @@ export function reduceStringify(type, values, structAttributes) { return output case "msd_orig": // TODO: OMG this is corpus specific, move out to config ASAP (ASU corpus) var output = _.map(values, function (token) { - return $("").text(token).outerHTML() + return ($("").text(token) as any).outerHTML() }).join(" ") return output default: diff --git a/app/scripts/components/statistics.js b/app/scripts/components/statistics.js index 4dc1d87c8..6ceb87ef8 100644 --- a/app/scripts/components/statistics.js +++ b/app/scripts/components/statistics.js @@ -5,7 +5,7 @@ import CSV from "comma-separated-values/csv" import settings from "@/settings" import { html } from "@/util" import { loc, locObj } from "@/i18n" -import { getCqp } from "../../config/statistics_config.js" +import { getCqp } from "../../config/statistics_config" import { expandOperators } from "@/cqp_parser/cqp" import { requestMapData } from "@/backend/backend" import "@/backend/backend" diff --git a/app/scripts/statistics.ts b/app/scripts/statistics.ts index 993312bd4..83a935181 100644 --- a/app/scripts/statistics.ts +++ b/app/scripts/statistics.ts @@ -33,7 +33,7 @@ const createStatisticsService = function () { sortable: true, formatter(row, cell, value, columnDef, dataContext) { if (dataContext["rowId"] !== 0) { - const formattedValue = reduceStringify(reduceVal, dataContext[reduceVal!], attrObj[reduceVal!]) + const formattedValue = reduceStringify(reduceVal!, dataContext[reduceVal!], attrObj[reduceVal!]) dataContext["formattedValue"][reduceVal] = formattedValue return `${formattedValue}` } else {