All files / owid-grapher/serverUtils serverUtil.tsx

79.63% Statements 43/54
100% Branches 0/0
0% Functions 0/5
79.63% Lines 43/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301x 1x 1x 1x 1x 1x 1x 1x           1x 1x         1x 1x 1x   1x 1x 1x   1x 1x 1x
import * as ReactDOMServer from "react-dom/server"
import * as lodash from "lodash"
import urljoin from "url-join"
import { ADMIN_BASE_URL } from "../settings/serverSettings"
import { JsonError } from "../clientUtils/owidTypes"
 
// Fail-fast integer conversion, for e.g. ids in url params
export const expectInt = (value: any): number => {
    const num = parseInt(value)
    if (isNaN(num))
        throw new JsonError(`Expected integer value, not '${value}'`, 400)
    return num
}
 
export const tryInt = (value: any, defaultNum: number): number => {
    const num = parseInt(value)
    if (isNaN(num)) return defaultNum
    return num
}
 
// Generate a static html page string from a given JSX element
export const renderToHtmlPage = (element: any) =>
    `<!doctype html>${ReactDOMServer.renderToStaticMarkup(element)}`
 
// Determine if input is suitable for use as a url slug
export const isValidSlug = (slug: any) =>
    lodash.isString(slug) && slug.length > 1 && slug.match(/^[\w-]+$/)
 
export const absoluteUrl = (path: string) => urljoin(ADMIN_BASE_URL, path)