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

@hemingway_at/asset-url-toolkit

v1.0.3

Published

A toolkit for asset URL manipulation.

Downloads

3

Readme

Dynamic Asset URL Resolver and Favicon Setter

简体中文

This TypeScript module provides functionalities to resolve asset URLs based on dynamic configuration and to set the favicon of a page.

Config Interface

export interface Config {
  dynamicAssets?: {
    urlPrefix: string
    domainMap: { [key: string]: string }
    assetsMap: { [key: string]: string }
  } | null
}

The Config interface defines the structure of the dynamic asset configuration, including an optional dynamicAssets property. If present, it is an object with the following properties:

  • urlPrefix is the common prefix for all generated URLs.

  • domainMap is an object mapping different domains to their respective subpaths.

  • assetsMap is an object with asset names as keys and the corresponding asset paths under the specified domain as values.

getAssetUrl Function

export function getAssetUrl(
  assetName: string,
  config: Config
): string | undefined

This function takes an asset name and a configuration object as parameters and returns the URL of the asset based on the configuration. It returns undefined if it fails to obtain a valid URL. The function first validates the configuration, then looks up the corresponding domain and asset mapping based on the current hostname and asset name, and finally constructs and validates the URL.

isValidUrl Function

export function isValidUrl(urlString: string, base?: string): boolean

This function is used to validate whether a provided URL string is in a valid URL format. It achieves this by attempting to create a new URL object, returning true if successful and false otherwise.

setFavicon Function

export function setFavicon(url: string): void

This function takes the URL of a favicon and sets or updates the favicon of the current page. It first tries to update an existing favicon link element, and if it doesn't exist, it creates a new link element, sets the relevant properties, and appends it to the document head.

Summary

This module is primarily used for handling dynamic asset loading and favicon setting, providing a flexible object mapping mechanism based on configuration to accommodate varying requirements.