npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@podium/utils

v5.4.0

Published

Common generic utility methods shared by @podium modules.

Downloads

9,514

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.