Novelty Polygonal Mathematics

    @terrestris/base-util
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.1 • Public • Published

    base-util

    Dependabot badge Build Status Coverage Status

    A set of helper classes for working with strings, objects, etc.

    Installation

    npm i @terrestris/base-util

    API Documentation

    Table of Contents

    CsrfUtil

    CSRF Utility methods.

    Some methods to access the csrf-token information served by spring security.

    The methods herein assume a certain HTML structure, which is easiest achieved by including a markup like the following in your base HTML file:

    getContentFromMetaTagByName

    Finds the meta tag in the current document by the given name and returns it's content.

    Parameters
    • name type Description

    Returns type Description

    getCsrfValue

    Get the CSRF token value.

    In order for this method to produce reliable output, your base HTML page should contain a <meta>-tag in the <head> with name _csrf. The content attribute is best filled from Spring by using this variable: ${_csrf.token}.

    Returns string the key value, e.g. "741a3b1-221f-4d1d-..." or the empty string if the meta tag cannot be found.

    getCsrfHeaderName

    Get the CSRF token key. This can be used if you want to send CSRF tokens as header. If you want to send it using a form parameter, use the method #getParamName instead.

    In order for this method to produce reliable output, your base HTML page should contain a <meta>-tag in the <head> with name _csrf_header. The content attribute is best filled from Spring by using this variable: ${_csrf.headerName}.

    Returns string the key string, e.g. "X-CSRF-TOKEN" ort the empty string if the meta tag cannot be found.

    getCsrfParameterName

    Get the name of the parameter to send when you want to pass CSRF tokens via a form. Alternatively you can use #getKey to get the name of the header to send for CSRF-protection.

    In order for this method to produce reliable output, your base HTML page should contain a <meta>-tag in the <head> with name _csrf_parameter_name. The content attribute is best filled from Spring by using this variable: ${_csrf.parameterName}.

    Returns string The name of the parameter to send when sending CSRF tokens via forms, e.g. "_csrf" or the empty string if the meta tag cannot be found.

    getHeader

    Get the full CSRF token header object. Can directly be used in fetch, e.g. in the following way:

    let csrfHeader = CsrfUtil.getHeader();

    fetch(targetUrl, { method: 'POST', headers: csrfHeader })

    Returns Header header - the header containing the CSRF key and value or an empty object if any of the required meta fields cannot be found.

    getHeaderObject

    Returns a simple object containing CSRF header name as key and CSRF value as field value

    Returns Object Simple object containing the CSRF key and value or an empty object if any of the required meta fields cannot be found.

    MathUtil

    Helper Class for various calculations.

    radToDeg

    Converts radians to degrees.

    Parameters
    • rad number The radian value to convert.

    degToRad

    Converts degrees to radians.

    Parameters
    • deg number The degree value to convert.

    mod

    Returns the modulo for (negative) values.

    Parameters

    ObjectUtil

    This class provides some static methods which might be helpful when working with objects.

    getPathByKeyValue

    Returns the dot delimited path of a given object by the given key-value pair. Example:

    const obj = {
      level: 'first',
      nested: {
        level: 'second'
      }
    };
    const key = 'level';
    const value = 'second';
    
    ObjectUtil.getPathByKeyValue(obj, key, value); // 'nested.level'
    

    Note: It will return the first available match!

    Parameters
    • obj Object The object to obtain the path from.
    • key string The key to look for.
    • value (string | number | boolean) The value to look for.
    • currentPath string The currentPath (if called in a recursion) or the custom root path (default is to ''). (optional, default '')

    getValue

    Method may be used to return a value of a given input object by a provided query key. The query key can be used in two ways:

    • Single-value: Find the first matching key in the provided object (Use with caution as the object/array order may not be as expected and/or deterministic!).
    • Backslash ("/") separated value: Find the last (!) matching key in the provided object.
    Parameters
    • queryKey string The key to be searched.
    • queryObject Object The object to be searched on

    Returns any The target value or undefined if the given couldn't be found, or an object if a path was passed, from which we only could find a part, but not the most specific one. TODO Harmonize return values

    StringUtil

    Helper Class for Strings

    urlify

    Replaces any occurence of a link-like text with tag.

    Parameters
    • text string The string context to replace.

    Returns string The urlified string.

    coerce

    This coerces the value of a string by casting it to the most plausible datatype, guessed by the value itself.

    Parameters
    • string string The input string to coerce.

    Returns any The coerced value.

    stringDivider

    Returns a string that is wrapped: every ~width chars a space is replaced with the passed spaceReplacer.

    See https://stackoverflow.com/questions/14484787/wrap-text-in-javascript

    Parameters
    • str string The string to wrap.
    • width number The width of a line (number of characters).
    • spaceReplacer string The string to replace spaces with.

    Returns string The 'wrapped' string.

    stripHTMLTags

    Returns the displayed text of an string with html text.

    Parameters
    • htmlString string A string containing html.

    Returns string The stripped Text.

    UndoUtil

    Helper class for state/undo-redo.

    atLeastOneUndoable

    Checks if at least one state is undoable or not.

    Parameters
    • state Object The global state.

    Returns boolean Wheather at least one state is undoable or not.

    atLeastOneRedoable

    Checks if at least one state is redoable or not.

    Parameters
    • state Object The global state.

    Returns boolean Wheather at least one state is redoable or not.

    UrlUtil

    Helper Class for the URL handling.

    read

    Returns an object representation of an URL.

    Parameters
    • url string The URL to read in.

    Returns URL The parsed URL object.

    write

    Returns a string representation of an URL object.

    Parameters
    • urlObj URL The URL object to write out.

    Returns string The stringified URL.

    getBasePath

    Returns the base path of an URL.

    Parameters
    • url string The URL to obtain the base path from.

    Returns string The base path.

    getQueryParams

    Returns the query params of a given URL as object.

    Parameters
    • url string The URL to get the query params from.

    Returns Object The query params of the given URL.

    getQueryParam

    Returns the value of the given query param of the provided URL. If not found, undefined will be returned.

    Parameters
    • url string The URL to get the query params from.
    • key string The key to get the value from.

    Returns string The query param value.

    joinQueryParams

    Joins some query parameters (defined by keys) of two query objects and returns the joined query parameters.

    var params1 = {FOO: 'foo,bar', BAZ: 'baz', HUMPTY: '1'};
    var params2 = {FOO: 'pupe,pape', BAZ: 'baz', DUMPTY: '42'};
    var keys = ['FOO'];
    var joined = this.joinQueryParams(params1, params2, keys);
    // joined is now
    // {FOO: 'foo,bar,pupe,pape', BAZ: 'baz', HUMPTY: '1'};
    
    Parameters
    • params1 Object The first object with parameters, where certain keys might have values that are joined with ,.
    • params2 Object The second object with parameters, where certain keys might have values that are joined with ,.
    • keys Array The keys which we will consider for joining. Others will be taken from the first object with parameters.

    Returns Object The joined query parameters.

    hasQueryParam

    Checks if a given URL has the provided query parameter present.

    Parameters
    • url string The URL to check.
    • key string The query parameter to check.

    Returns boolean Whether the parameter is present or not.

    createValidGetCapabilitiesRequest

    Creates a valid GetCapabilitiesRequest out of the given URL by checking if SERVICE, REQUEST and VERSION are set.

    Parameters
    • url string The URL to validate.
    • service string The service to set. Default is to 'WMS'. (optional, default 'WMS')
    • version string The version to set. Default is to '1.3.0'. (optional, default '1.3.0')

    Returns string The validated URL.

    bundleOgcRequests

    This joins/bundles a given set of (typically WMS GetFeatureInfo) requests by the base URL. E.g. it merges the following two requests:

    https://maps.bvb.de?SERVICE=WMS&REQUEST=GetFeatureInfo&LAYERS=Shinji https://maps.bvb.de?SERVICE=WMS&REQUEST=GetFeatureInfo&LAYERS=Kagawa

    to

    https://maps.bvb.de?SERVICE=WMS&REQUEST=GetFeatureInfo&LAYERS=Shinji,Kagawa

    Parameters
    • featureInfoUrls Array An array of requests to bundle.
    • stringify boolean Whether to stringify the output or not. If set to false an object keyed by the base URL and valued by the combined requests params will be returned.
    • bundleParams Array An array of query params to bundle, default is to ['LAYERS', 'QUERY_LAYERS', 'STYLES']. (optional, default ['LAYERS','QUERY_LAYERS','STYLES'])

    objectToRequestString

    Transforms an object into a string containing requestParams (without leading questionmark).

    Parameters
    • object Object An object containing kvp for the request. e.g. {height:400, width:200}

    Returns string The kvps as a requestString. e.g. 'height=400&width=200'

    isValid

    Checks if a given URL is valid. Implementation based on https://www.npmjs.com/package/validator.

    Parameters
    • url string The URL to validate.
    • opts Object The validation validator options. (optional, default {protocols:['http','https','ftp'],require_tld:false,require_protocol:true,require_host:true,require_valid_protocol:true,allow_underscores:false,host_whitelist:false,host_blacklist:false,allow_trailing_dot:false,allow_protocol_relative_urls:false})

    Returns boolean Whether the URL is valid or not.

    Keywords

    none

    Install

    npm i @terrestris/base-util

    DownloadsWeekly Downloads

    949

    Version

    1.0.1

    License

    BSD-2-Clause

    Unpacked Size

    138 kB

    Total Files

    41

    Last publish

    Collaborators

    • unraveler
    • jweskamm
    • hblitza
    • simonseyock
    • jansule
    • annarieger
    • terrestris-info
    • marcjansen
    • kaivolland
    • dnlkoch
    • ahennr
    • buehner
    • hwbllmnn
    • lukaslohoff
    • mholthausen
    • sgoetsch
    • jorzekowsky