Skip to content

Commit

Permalink
refactor(ts): stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Oct 23, 2024
1 parent 4a3aced commit 17cb260
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/scripts/components/loglike-meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type MeterScope = IScope & {

angular.module("korpApp").component("loglikeMeter", {
template: html`<div>
<div class="background p-1" ng-style="{ width: barWidth }">{{display}}</div>
<div class="background p-1" ng-style="{ width: barWidth }" ng-bind-html="display | trust"></div>
<div class="abs badge absolute right-1 top-2 px-1.5" uib-tooltip-html="tooltipHtml | trust">{{abs}}</div>
</div>`,
bindings: {
Expand Down
7 changes: 3 additions & 4 deletions app/scripts/components/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from "lodash"
import "../../styles/sidebar.scss"
import statemachine from "../statemachine"
import settings from "@/settings"
import { stringify } from "@/stringify.js"
import { getStringifier } from "@/stringify"
import { html, regescape, splitLemgram, safeApply } from "@/util"
import { loc, locAttribute } from "@/i18n"
import "@/services/utils"
Expand Down Expand Up @@ -294,8 +294,7 @@ angular.module("korpApp").component("sidebar", {
const lis = []
for (let x of itr) {
if (x.length) {
const stringifyKey = attrs.stringify
val = stringify(stringifyKey, x)
val = getStringifier(attrs.stringify)(x)

if (attrs.translation != null) {
val = locAttribute(attrs.translation, val, $ctrl.lang)
Expand Down Expand Up @@ -328,7 +327,7 @@ angular.module("korpApp").component("sidebar", {

let str_value = value
if (attrs.stringify) {
str_value = stringify(attrs.stringify, value)
str_value = getStringifier(attrs.stringify)(value)
} else if (attrs.translation) {
str_value = locAttribute(attrs.translation, value, $ctrl.lang)
}
Expand Down
12 changes: 6 additions & 6 deletions app/scripts/controllers/comparison_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import _ from "lodash"
import angular from "angular"
import settings from "@/settings"
import { stringifyFunc } from "@/stringify.js"
import { getStringifier } from "@/stringify"
import { locAttribute } from "@/i18n"
import { CompareTab, RootScope } from "@/root-scope.types"
import { SavedSearch } from "@/local-storage"
Expand Down Expand Up @@ -57,11 +57,11 @@ angular.module("korpApp").directive("compareCtrl", () => ({
// currently we only support one attribute to reduce/group by, so simplify by only checking first item
const reduceAttrName = _.trimStart(reduce[0], "_.")
if (attributes[reduceAttrName]) {
if (attributes[reduceAttrName].stringify) {
stringify = stringifyFunc(reduceAttrName)
} else if (attributes[reduceAttrName].translation) {
stringify = (value) =>
locAttribute(attributes[reduceAttrName].translation!, value, $rootScope.lang)
const attribute = attributes[reduceAttrName]
if (attribute.stringify) {
stringify = getStringifier(attribute.stringify)
} else if (attribute.translation) {
stringify = (value) => locAttribute(attribute.translation!, value, $rootScope.lang)
}
}
s.stringify = stringify
Expand Down
18 changes: 0 additions & 18 deletions app/scripts/stringify.js

This file was deleted.

16 changes: 16 additions & 0 deletions app/scripts/stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @format */

export type Stringifier = <T = any>(input: T) => string

const stringifiers: Record<string, Stringifier> = {}

try {
const custom = require("custom/stringify.js").default
Object.assign(stringifiers, custom)
} catch (error) {
console.log("No module for stringify functions available")
}

export const getStringifier = (key?: string): Stringifier {
return key && stringifiers[key] ? stringifiers[key] : String
}

0 comments on commit 17cb260

Please sign in to comment.