A set of helper classes for working with strings, objects, etc.
npm i @terrestris/base-util
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:
Finds the meta tag in the current document by the given name and returns it's content.
name
type Description
Returns type Description
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.
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.
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.
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.
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.
Helper Class for various calculations.
Converts radians to degrees.
rad
number The radian value to convert.
Converts degrees to radians.
deg
number The degree value to convert.
Returns the modulo for (negative) values.
n
number The number.
This class provides some static methods which might be helpful when working with objects.
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!
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''
)
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.
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
Helper Class for Strings
Replaces any occurence of a link-like text with tag.
text
string The string context to replace.
Returns string The urlified string.
This coerces the value of a string by casting it to the most plausible datatype, guessed by the value itself.
string
string The input string to coerce.
Returns any The coerced value.
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
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.
Returns the displayed text of an string with html text.
htmlString
string A string containing html.
Returns string The stripped Text.
Helper class for state/undo-redo.
Checks if at least one state is undoable or not.
state
Object The global state.
Returns boolean Wheather at least one state is undoable or not.
Checks if at least one state is redoable or not.
state
Object The global state.
Returns boolean Wheather at least one state is redoable or not.
Helper Class for the URL handling.
Returns an object representation of an URL.
url
string The URL to read in.
Returns URL The parsed URL object.
Returns a string representation of an URL object.
urlObj
URL The URL object to write out.
Returns string The stringified URL.
Returns the base path of an URL.
url
string The URL to obtain the base path from.
Returns string The base path.
Returns the query params of a given URL as object.
url
string The URL to get the query params from.
Returns Object The query params of the given URL.
Returns the value of the given query param of the provided URL. If not found, undefined will be returned.
Returns string The query param value.
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'};
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.
Checks if a given URL has the provided query parameter present.
Returns boolean Whether the parameter is present or not.
Creates a valid GetCapabilitiesRequest out of the given URL by checking if SERVICE, REQUEST and VERSION are set.
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.
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
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']
)
Transforms an object into a string containing requestParams (without leading questionmark).
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'
Checks if a given URL is valid. Implementation based on https://www.npmjs.com/package/validator.
url
string The URL to validate.opts
Object The validationvalidator
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.