Skip to content

Commit a85151d

Browse files
committed
refactor(ts): statistics_config
1 parent 3dd8cc9 commit a85151d

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

app/config/statistics_config.js renamed to app/config/statistics_config.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ import _ from "lodash"
33
import settings from "@/settings"
44
import { lemgramToHtml, regescape, saldoToHtml } from "@/util"
55
import { locAttribute } from "@/i18n"
6+
import { Token } from "@/backend/kwic-proxy"
7+
import { Attribute } from "@/settings/config.types"
8+
import { JQueryStaticExtended } from "@/jquery.types"
69

7-
let customFunctions = {}
10+
type Stringifier = (tokens: string[], ignoreCase?: boolean) => string
11+
12+
let customFunctions: Record<string, Stringifier> = {}
813

914
try {
1015
customFunctions = require("custom/statistics.js").default
1116
} catch (error) {
1217
console.log("No module for statistics functions available")
1318
}
1419

15-
export function getCqp(hitValues, ignoreCase) {
20+
export function getCqp(hitValues: Token[], ignoreCase: boolean): string {
1621
const positionalAttributes = ["word", ...Object.keys(settings.corpusListing.getCurrentAttributes())]
1722
let hasPositionalAttributes = false
1823

19-
var tokens = []
24+
var tokens: string[] = []
2025
for (var i = 0; i < hitValues.length; i++) {
2126
var token = hitValues[i]
22-
var andExpr = []
27+
var andExpr: string[] = []
2328
for (var attribute in token) {
2429
if (token.hasOwnProperty(attribute)) {
2530
var values = token[attribute]
@@ -39,11 +44,11 @@ export function getCqp(hitValues, ignoreCase) {
3944
return `<match> ${tokens.join(" ")} </match>`
4045
}
4146

42-
function reduceCqp(type, tokens, ignoreCase) {
47+
function reduceCqp(type: string, tokens: string[], ignoreCase: boolean): string {
4348
let attrs = settings.corpusListing.getCurrentAttributes()
4449
if (attrs[type] && attrs[type].stats_cqp) {
4550
// A stats_cqp function should call regescape for the value as appropriate
46-
return customFunctions[attrs[type].stats_cqp](tokens, ignoreCase)
51+
return customFunctions[attrs[type].stats_cqp!](tokens, ignoreCase)
4752
}
4853
tokens = _.map(tokens, (val) => regescape(val))
4954
switch (type) {
@@ -55,11 +60,11 @@ function reduceCqp(type, tokens, ignoreCase) {
5560
case "sense":
5661
case "transformer-neighbour":
5762
if (tokens[0] === "") return "ambiguity(" + type + ") = 0"
58-
else var res
63+
let res: string
5964
if (tokens.length > 1) {
6065
var key = tokens[0].split(":")[0]
6166

62-
var variants = []
67+
const variants: string[][] = []
6368
_.map(tokens, function (val) {
6469
const parts = val.split(":")
6570
if (variants.length == 0) {
@@ -68,15 +73,12 @@ function reduceCqp(type, tokens, ignoreCase) {
6873
for (var idx = 1; idx < parts.length; idx++) variants[idx - 1].push(parts[idx])
6974
})
7075

71-
variants = _.map(variants, function (variant) {
72-
return ":(" + variant.join("|") + ")"
73-
})
74-
75-
res = key + variants.join("")
76+
const variantsJoined = variants.map((variant) => ":(" + variant.join("|") + ")")
77+
res = key + variantsJoined.join("")
7678
} else {
7779
res = tokens[0]
7880
}
79-
return type + " contains '" + res + "'"
81+
return `${type} contains '${res}'`
8082
case "word":
8183
let s = 'word="' + tokens[0] + '"'
8284
if (ignoreCase) s = s + " %c"
@@ -101,11 +103,11 @@ function reduceCqp(type, tokens, ignoreCase) {
101103
}
102104

103105
// Get the html (no linking) representation of the result for the statistics table
104-
export function reduceStringify(type, values, structAttributes) {
106+
export function reduceStringify(type: string, values: string[], structAttributes: Attribute): string {
105107
let attrs = settings.corpusListing.getCurrentAttributes()
106108

107109
if (attrs[type] && attrs[type].stats_stringify) {
108-
return customFunctions[attrs[type].stats_stringify](values)
110+
return customFunctions[attrs[type].stats_stringify!](values)
109111
}
110112

111113
switch (type) {
@@ -123,15 +125,16 @@ export function reduceStringify(type, values, structAttributes) {
123125
case "lex":
124126
case "lemma":
125127
case "sense":
128+
let stringify: (value: string, appendIndex?: boolean) => string
126129
if (type == "saldo" || type == "sense") {
127-
var stringify = saldoToHtml
130+
stringify = saldoToHtml
128131
} else if (type == "lemma") {
129132
stringify = (lemma) => lemma.replace(/_/g, " ")
130133
} else {
131134
stringify = lemgramToHtml
132135
}
133136

134-
var html = _.map(values, function (token) {
137+
const html = _.map(values, function (token) {
135138
if (token === "") return "–"
136139
return stringify(token.replace(/:.*/g, ""), true)
137140
})
@@ -148,7 +151,7 @@ export function reduceStringify(type, values, structAttributes) {
148151
return output
149152
case "msd_orig": // TODO: OMG this is corpus specific, move out to config ASAP (ASU corpus)
150153
var output = _.map(values, function (token) {
151-
return $("<span>").text(token).outerHTML()
154+
return ($("<span>").text(token) as any).outerHTML()
152155
}).join(" ")
153156
return output
154157
default:

app/scripts/components/statistics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import CSV from "comma-separated-values/csv"
55
import settings from "@/settings"
66
import { html } from "@/util"
77
import { loc, locObj } from "@/i18n"
8-
import { getCqp } from "../../config/statistics_config.js"
8+
import { getCqp } from "../../config/statistics_config"
99
import { expandOperators } from "@/cqp_parser/cqp"
1010
import { requestMapData } from "@/backend/backend"
1111
import "@/backend/backend"

app/scripts/statistics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const createStatisticsService = function () {
3333
sortable: true,
3434
formatter(row, cell, value, columnDef, dataContext) {
3535
if (dataContext["rowId"] !== 0) {
36-
const formattedValue = reduceStringify(reduceVal, dataContext[reduceVal!], attrObj[reduceVal!])
36+
const formattedValue = reduceStringify(reduceVal!, dataContext[reduceVal!], attrObj[reduceVal!])
3737
dataContext["formattedValue"][reduceVal] = formattedValue
3838
return `<span class="statistics-link" data-row=${dataContext["rowId"]}>${formattedValue}</span>`
3939
} else {

0 commit comments

Comments
 (0)