Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@podium/utils

podium-lib7.2kMIT5.5.0TypeScript support: included

Common generic utility methods shared by @podium modules.

micro services, micro frontend, components, podium

readme

Podium Utils v5

Common generic utility methods shared by @podium modules.

GitHub Actions status

Installation

npm install @podium/utils

API

This module has the following API:

.isString(str)

Checks if a value is a string.

The method takes the following arguments:

  • str - * - A value to check. Required.

Returns a boolean.

.isFunction(fn)

Checks if a value is a function.

The method takes the following arguments:

  • fn - * - A value to check. Required.

Returns a boolean.

.pathnameBuilder(pathname... [])

Constructs an pathname from all arguments. Returned pathname will always end without a / and if the first argument starts with a / it will be preserved.

import { pathnameBuilder } from '@podium/utils';
const foo = 'foo/a/';
const bar = '/bar/b/';
const xyz = '/xyz/';

const pathname = pathnameBuilder(foo, bar, xyz);
console.log(pathname); // outputs: foo/a/bar/b/xyz

.uriBuilder(input, base, extra)

Constructs an absolute URI out of a absolute manifest URI and a relative URI.

The method takes the following arguments:

  • input - String - Relative URI. Required.
  • base - String - Absolute manifest URI to append the input too. Required.
  • extra - String - Relative path to be appended at the end of the URI. Optional.

Returns a resolved URI.

import { uriBuilder } from '@podium/utils';
const manifest = 'http://foo.site.com/bar/manifest.json';
const content = '/here/is/content.html';

const url = uriBuilder(content, manifest);
console.log(url); // outputs: http://foo.site.com/bar/here/is/content.html

.uriIsRelative(uri)

Checks if a URI is relative

The method takes the following arguments:

  • uri - String - The URI to check. Required.

Returns a Boolean.

import { uriIsRelative } from '@podium/utils';

uriIsRelative('http://foo.site.com/bar/'); // false
uriIsRelative('/bar/'); // true

.uriRelativeToAbsolute(input, base, extra)

Check if a URI is absolute or relative and if relative compose an absolute URI out of a absolute mainfest URI.

The method takes the following arguments:

  • input - String - Relative or absolute URI. Required.
  • base - String - Absolute manifest URI to append the possible relative input too. Required.
  • extra - String - Relative path to be appended at the end of the URI. Optional.

Returns a resolved URI.

import { uriRelativeToAbsolute } from '@podium/utils';
const manifest = 'http://foo.site.com/bar/manifest.json';
const content = 'http://foo.site.com/here/is/content.html';

const url = uriRelativeToAbsolute(content, manifest);
console.log(url); // outputs: http://foo.site.com/here/is/content.html

.setAtLocalsPodium(response, property, value)

Set a value on a property on .locals.podium on a http response object. Ensures that .locals.podium exists on the http response object.

If property is not provided, .locals.podium will be created, if not already existing, on the response object.

The method takes the following arguments:

  • response - Object - A http response object.
  • property - String - Property for the value.
  • value - String - Value to store on the property.

The http response object.

import { setAtLocalsPodium } from '@podium/utils';
const obj = setAtLocalsPodium({}, 'foo', 'bar');

/*
obj is now:
{
    locals: {
        podium: {
            foo: 'bar',
        },
    },
}
*/

.getFromLocalsPodium(response, property)

Get the value from a property on .locals.podium on a http response object Ensures that .locals.podium exists on the http response object.

  • response - Object - A http response object
  • property - String - Property for the value

returns The property, or null if it does not exist

.duplicateOnLocalsPodium(response, fromProperty, toProperty)

Get the value from a property on .locals.podium on a http response object and sets its value on another key.

  • response - Object - A http response object
  • fromProperty - String - Property for the existent value
  • toProperty - String - Property for the duplicated value

@returns {Object} The http response object

.serializeContext(headers, context, arg)

Serialize a context object into a http header object.

The method takes the following arguments:

  • headers - Object - A http headers object the context will be copied into.
  • context - Object - A contect object to copy from
  • arg - * - An argument value passed on to the function if a context value is a function.

A http header object.

import { serializeContext } from '@podium/utils';
const context = {
    'podium-foo': 'bar',
    'podium-bar': 'foo',
};

let headers = {
    test: 'xyz',
};

headers = serializeContext(headers, context);

/*
headers is now:
{
    'podium-foo': 'bar',
    'podium-bar': 'foo',
    test: 'xyz',
}
*/

.deserializeContext(headers, prefix)

Deserialize a context object from a http header object

The method takes the following arguments:

  • headers - Object - A http headers object the context will be extracted from.
  • prefix - String - The prefix used to mark what properties are context properties

A object containing context properties and values

import { deserializeContext } from '@podium/utils';
const headers = {
    bar: 'foo',
    'podium-foo': 'bar podium',
};

const context = deserializeContext(headers);
// context is: { foo: 'bar podium' }

.template(data)

Shared template function for use in layout and podlet

This method takes a single argument:

  • data - Object - An object with template variables as key/value pairs

data can contain any of the following keys:

  • data.title - document title
  • data.locale - language tag/locale identifier defaults to en-US
  • data.encoding - defaults to utf-8
  • data.head - Any additional HTML markup that should be placed in the document <head>
  • data.js - JavaScript URL, will be used as a src value in a script tag
  • data.css - CSS URL, will be used as an href value in a link tag
  • data.body - HTML body markup to be rendered

.buildLinkElement(assetCss)

Build a HTML link element out of a AssetCss object.

The method takes the following arguments:

  • assetCss - Object - A CSS Asset object
import { AssetCss, buildLinkElement } from '@podium/utils';

const css = new AssetCss({
    value: 'https://cdn.foo.com/style.css',
});

const element = buildLinkElement(css);
// element is: <link href="" .....

returns A HTML link element as a String.

.buildScriptElement(assetJs)

Build a HTML script element out of a AssetJs object.

The method takes the following arguments:

  • assetJs - Object - A JS Asset object
import { AssetJs, buildScriptElement } from '@podium/utils';

const js = new utils.AssetJs({
    value: 'https://cdn.foo.com/script.js',
});

const element = utils.buildScriptElement(js);
// element is: <script src="" .....

returns A HTML script element as a String.

changelog

5.5.0 (2025-06-24)

Features

  • add html tagged template literal for podiumSend (#286) (03e994e)

5.4.0 (2024-11-13)

Bug Fixes

  • refactor hints to assets (fa85f8a)

Features

  • add waitForAssets method (c4aaaf2)

5.3.2 (2024-11-04)

Bug Fixes

  • update @podium/schemas to 5.1.0 (67d630b)

5.3.1 (2024-09-22)

Bug Fixes

  • receive and emit hinted assets (a07e0e3)

5.3.0 (2024-09-20)

Features

  • add early hint tracking to html-template (e9b5867)

5.2.1 (2024-09-20)

Bug Fixes

  • remove asset type header tagging (ff16547)

5.2.0 (2024-09-05)

Features

  • add asset-type link header attribute and set to either style or script (0a02da6)

5.1.0 (2024-08-18)

Features

  • add toEarlyHint methods to Asset classes (150b6e2)

5.0.7 (2024-05-15)

Bug Fixes

  • type context and view to Record instead of {} (#238) (ab68b88)

5.0.6 (2024-05-13)

Bug Fixes

5.0.5 (2024-05-07)

Bug Fixes

  • include the types folder in the distributed package (#236) (fcf16d0)

5.0.4 (2024-04-23)

Bug Fixes

5.0.3 (2024-04-11)

Bug Fixes

  • ensure html-template lang attribute works in both layout and podlet (2545ac8)

5.0.2 (2024-02-01)

Bug Fixes

  • validate inputs when passing both async and defer (#226) (dfb40d6)

5.0.1 (2023-12-07)

Bug Fixes

  • deps: update dependency camelcase to v8 (4bb0483)

5.0.0 (2023-11-28)

Bug Fixes

  • Improve ESM exports to account for dual module exports (#123) (42ebb4f)
  • Point require export to main.js (#122) (1438ee6)
  • Remove original url module (#185) (6c01c0f)
  • Simplify js and css value validation (#70) (05a1ffc)

Features

BREAKING CHANGES

  • Convert from CommonJS to ESM

  • feat: convert to ESM

  • fix: Remove outcommented code

  • ci: Add build step for backward compat to CJS

  • ci: Ignore linting dist directory

Co-authored-by: Trygve Lie trygve.lie@finn.no

  • Due to dropping node 10.x support we use ES private properties instead of Symbols.

We've been using Symbols to define private properties in classes up until now. This has the downside that they are not true private and in later versions of node.js one would see these Symbols when inspecting an object. What we want is proper private properties.

This PR does also add a pretty printer which outputs an object literal or the object so when debugging one can see the getters and setters of the object.

Example: printing a object with console.log() would previously print the following:

PodiumHttpIncoming {
  [Symbol(podium:httpincoming:development)]: false,
  [Symbol(podium:httpincoming:response)]: {},
  [Symbol(podium:httpincoming:request)]: {},
  [Symbol(podium:httpincoming:context)]: {},
  [Symbol(podium:httpincoming:params)]: {},
  [Symbol(podium:httpincoming:proxy)]: false,
  [Symbol(podium:httpincoming:name)]: '',
  [Symbol(podium:httpincoming:view)]: {},
  [Symbol(podium:httpincoming:url)]: {},
  [Symbol(podium:httpincoming:css)]: [],
  [Symbol(podium:httpincoming:js)]: []
}

Now the following will be printed:

{
  development: false,
  response: {},
  request: {},
  context: {},
  params: {},
  proxy: false,
  name: '',
  view: {},
  url: {},
  css: [],
  js: []
}

Co-authored-by: Trygve Lie trygve.lie@finn.no

  • Only support node 12 and 14.

Co-authored-by: Trygve Lie trygve.lie@finn.no

5.0.0-next.10 (2023-11-20)

Bug Fixes

  • add missing functions to type definition (b260c4f)
  • correct typescript definitions for assets (6b39222)
  • correctly build lazy load script tag html when asset strategy is lazy (b1b2646)
  • deps: update dependency @podium/schemas to v4.1.33 (8036aa7)
  • deps: update dependency @podium/schemas to v4.1.34 (a86e661)
  • deps: update dependency @podium/schemas to v4.2.0 (be92192)
  • make AssetCss and AssetJs constructable for TS (f3f7fee)
  • mark params as optional (51d84da)

Features

  • add strategy and scope fields to AssetCss and AssetJs classes (5a8ecb1)
  • update default document template to use asset strategy (0688b03)

4.5.1 (2023-11-19)

Bug Fixes

  • correctly build lazy load script tag html when asset strategy is lazy (b1b2646)

4.5.0 (2023-11-16)

Features

  • add strategy and scope fields to AssetCss and AssetJs classes (5a8ecb1)
  • update default document template to use asset strategy (0688b03)

5.0.0-next.9 (2022-10-09)

Bug Fixes

5.0.0-next.8 (2022-05-04)

Features

5.0.0-next.7 (2022-05-03)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.16 (c47f0bf)
  • deps: update dependency @podium/schemas to v4.1.17 (b45e56d)
  • deps: update dependency @podium/schemas to v4.1.18 (21ef50a)
  • deps: update dependency @podium/schemas to v4.1.19 (3a4f6a8)
  • deps: update dependency @podium/schemas to v4.1.20 (7d9955b)
  • deps: update dependency @podium/schemas to v4.1.21 (cebdc13)
  • deps: update dependency @podium/schemas to v4.1.22 (15d8ea9)
  • deps: update dependency @podium/schemas to v4.1.23 (c18698a)
  • deps: update dependency @podium/schemas to v4.1.24 (35c41c7)
  • deps: update dependency @podium/schemas to v4.1.25 (27b8507)
  • deps: update dependency @podium/schemas to v4.1.26 (961e0e0)
  • deps: update dependency @podium/schemas to v4.1.27 (72d4596)
  • deps: update dependency @podium/schemas to v4.1.28 (b3d9275)
  • deps: update dependency @podium/schemas to v4.1.29 (cc43020)
  • deps: update dependency @podium/schemas to v4.1.30 (3a390ce)
  • deps: update dependency @podium/schemas to v4.1.31 (2f67490)
  • deps: update dependency @podium/schemas to v4.1.32 (0322287)
  • deps: update dependency camelcase to v6.2.1 (2f9a3e3)
  • deps: update dependency camelcase to v6.3.0 (fee3e49)
  • Do not allow origin as a pathname value (#143) (ff9785d)

5.0.0-next.6 (2021-04-30)

Bug Fixes

  • Improve ESM exports to account for dual module exports (#123) (42ebb4f)

4.4.42 (2023-11-16)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.2.0 (be92192)

4.4.41 (2023-10-19)

Bug Fixes

  • add missing functions to type definition (b260c4f)

4.4.40 (2023-10-09)

Bug Fixes

  • make AssetCss and AssetJs constructable for TS (f3f7fee)
  • mark params as optional (51d84da)

4.4.39 (2023-01-04)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.34 (a86e661)

4.4.38 (2022-12-07)

Bug Fixes

  • correct typescript definitions for assets (6b39222)

4.4.37 (2022-11-14)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.33 (8036aa7)

4.4.36 (2022-03-23)

Bug Fixes

  • Improve ESM exports to account for dual module exports (#123) (42ebb4f)

5.0.0-next.5 (2021-04-27)

Bug Fixes

4.4.35 (2022-02-05)

Bug Fixes

4.4.34 (2022-01-15)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.30 (3a390ce)

5.0.0-next.4 (2021-04-27)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.0.4 (5ff49c0)
  • deps: update dependency @podium/schemas to v4.0.5 (9bf1a14)
  • deps: update dependency @podium/schemas to v4.1.0 (4ff5fe9)
  • deps: update dependency @podium/schemas to v4.1.1 (f2cc938)
  • deps: update dependency @podium/schemas to v4.1.10 (5a10235)
  • deps: update dependency @podium/schemas to v4.1.11 (20941c6)
  • deps: update dependency @podium/schemas to v4.1.13 (f677772)
  • deps: update dependency @podium/schemas to v4.1.14 (0deb0d0)
  • deps: update dependency @podium/schemas to v4.1.15 (446458c)
  • Update @podium/schema to version 4.1.9 to fix ajv error (#110) (08ee7df)
  • deps: update dependency @podium/schemas to v4.0.7 (cbded99)
  • deps: update dependency @podium/schemas to v4.1.2 (04c7b1b)
  • deps: update dependency @podium/schemas to v4.1.3 (e663829)
  • deps: update dependency @podium/schemas to v4.1.4 (4cb9bb6)
  • deps: update dependency @podium/schemas to v4.1.5 (d920dab)
  • deps: update dependency @podium/schemas to v4.1.6 (f92062c)
  • deps: update dependency @podium/schemas to v4.1.7 (9eac72d)
  • deps: update dependency @podium/schemas to v4.1.8 (c8fde78)
  • deps: update dependency camelcase to v6.1.0 (ef22149)
  • deps: update dependency camelcase to v6.2.0 (3e9dec9)

Features

  • add .buildReactLinkAttributes and .buildReactScriptAttributes methods (ffb0bff)
  • add .toReactAttrs() method to AssetJs and AssetCss classes and integration tests (7ab097f)
  • Convert to ESM (#119) (349c5b9)

BREAKING CHANGES

  • Convert from CommonJS to ESM

  • feat: convert to ESM

  • fix: Remove outcommented code

  • ci: Add build step for backward compat to CJS

  • ci: Ignore linting dist directory

Co-authored-by: Trygve Lie trygve.lie@finn.no

5.0.0-next.3 (2020-07-27)

Features

  • Use ES private properties instead of Symbols for privacy (#72) (4083fa1)

BREAKING CHANGES

  • Due to dropping node 10.x support we use ES private properties instead of Symbols.

We've been using Symbols to define private properties in classes up until now. This has the downside that they are not true private and in later versions of node.js one would see these Symbols when inspecting an object. What we want is proper private properties.

This PR does also add a pretty printer which outputs an object literal or the object so when debugging one can see the getters and setters of the object.

Example: printing a object with console.log() would previously print the following:

PodiumHttpIncoming {
  [Symbol(podium:httpincoming:development)]: false,
  [Symbol(podium:httpincoming:response)]: {},
  [Symbol(podium:httpincoming:request)]: {},
  [Symbol(podium:httpincoming:context)]: {},
  [Symbol(podium:httpincoming:params)]: {},
  [Symbol(podium:httpincoming:proxy)]: false,
  [Symbol(podium:httpincoming:name)]: '',
  [Symbol(podium:httpincoming:view)]: {},
  [Symbol(podium:httpincoming:url)]: {},
  [Symbol(podium:httpincoming:css)]: [],
  [Symbol(podium:httpincoming:js)]: []
}

Now the following will be printed:

{
  development: false,
  response: {},
  request: {},
  context: {},
  params: {},
  proxy: false,
  name: '',
  view: {},
  url: {},
  css: [],
  js: []
}

Co-authored-by: Trygve Lie trygve.lie@finn.no

5.0.0-next.2 (2020-07-15)

Bug Fixes

  • Simplify js and css value validation (#70) (05a1ffc)

4.4.33 (2022-01-01)

Bug Fixes

  • deps: update dependency camelcase to v6.3.0 (fee3e49)

4.4.32 (2021-11-22)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.29 (cc43020)

4.4.31 (2021-11-17)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.28 (b3d9275)

4.4.30 (2021-11-15)

Bug Fixes

  • deps: update dependency camelcase to v6.2.1 (2f9a3e3)

4.4.29 (2021-11-14)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.27 (72d4596)

4.4.28 (2021-11-09)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.26 (961e0e0)

4.4.27 (2021-10-26)

Bug Fixes

  • Do not allow origin as a pathname value (#143) (ff9785d)

4.4.26 (2021-09-13)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.25 (27b8507)

4.4.25 (2021-08-14)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.24 (35c41c7)

4.4.24 (2021-07-15)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.23 (c18698a)

4.4.23 (2021-07-04)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.22 (15d8ea9)

4.4.22 (2021-06-06)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.21 (cebdc13)

4.4.21 (2021-05-24)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.20 (7d9955b)

4.4.20 (2021-05-14)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.19 (3a4f6a8)

4.4.19 (2021-05-09)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.18 (21ef50a)

4.4.18 (2021-05-05)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.17 (b45e56d)

4.4.17 (2021-04-27)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.16 (c47f0bf)

4.4.16 (2021-04-11)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.15 (446458c)

4.4.15 (2021-04-02)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.14 (0deb0d0)

4.4.14 (2021-04-02)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.13 (f677772)

4.4.13 (2021-04-01)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.10 (5a10235)
  • deps: update dependency @podium/schemas to v4.1.11 (20941c6)

4.4.12 (2021-03-30)

Bug Fixes

  • Update @podium/schema to version 4.1.9 to fix ajv error (#110) (08ee7df)

4.4.11 (2021-03-27)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.8 (c8fde78)

4.4.10 (2021-03-26)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.7 (9eac72d)

4.4.9 (2021-03-20)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.6 (f92062c)

4.4.8 (2021-03-08)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.5 (d920dab)

4.4.7 (2021-03-07)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.4 (4cb9bb6)

4.4.6 (2021-02-17)

Bug Fixes

  • Simplify js and css value validation (#70) (05a1ffc)

5.0.0-next.1 (2020-07-12)

Features

BREAKING CHANGES

  • Only support node 12 and 14.

Co-authored-by: Trygve Lie trygve.lie@finn.no

4.4.5 (2021-02-11)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.2 (04c7b1b)

4.4.4 (2021-02-02)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.1 (f2cc938)

4.4.3 (2021-01-22)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.1.0 (4ff5fe9)

4.4.2 (2021-01-21)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.0.7 (cbded99)

4.4.1 (2020-10-28)

Bug Fixes

  • deps: update dependency camelcase to v6.2.0 (3e9dec9)

4.4.0 (2020-10-12)

Features

BREAKING CHANGES

  • Only support node 12 and 14.

Co-authored-by: Trygve Lie trygve.lie@finn.no

  • add .buildReactLinkAttributes and .buildReactScriptAttributes methods (ffb0bff)
  • add .toReactAttrs() method to AssetJs and AssetCss classes and integration tests (7ab097f)

4.3.3 (2020-10-10)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.0.5 (9bf1a14)

4.3.2 (2020-10-10)

Bug Fixes

  • deps: update dependency camelcase to v6.1.0 (ef22149)

4.3.1 (2020-09-13)

Bug Fixes

  • deps: update dependency @podium/schemas to v4.0.4 (5ff49c0)

4.3.0 (2020-06-26)

Features

  • support data attributes on javascript assets (#61) (9f418b2)

Changelog

Notable changes to this project.

The latest version of this document is always available in releases.

Unreleased

3.1.2 - 2019-03-21

  • Improved performance of HttpIncoming - #6

3.1.1 - 2019-02-07

  • Initial open source release.