Skip to content

Latest commit

 

History

History
145 lines (91 loc) · 4.32 KB

File metadata and controls

145 lines (91 loc) · 4.32 KB

String Utilities

Description of the string utilities.

Import

import { string } from '@webalternatif/js-core'

API

pad

Pads a string to a specified length with a specified string and padding type

Parameters

Returns string

rgb2hex

Converts RGB values to a hexadecimal color string.

Parameters

  • r (number | Array<number>) Either an array representing a RGB color (e.g. [255, 0, 0]) or the red component as a number (0-255).
  • g number? The green component (0-255). Required if r is a number.
  • b number? The blue component (0-255). Required if r is a number.

Examples

// Using separate RGB components
rgb2hex(255, 0, 0); // Returns 'FF0000'
// Using an array of RGB components
rgb2hex([255, 0, 0]); // Returns 'FF0000'

Returns string The hexadecimal color string (e.g., 'FF0000').

hex2rgb

Converts a hexadecimal color to RGB values.

Parameters

  • hex string The hexadecimal value (e.g #FF0000)

Returns Array<number> The RGB color array (e.g [255, 0, 0]).

parse_url

Parses a URL string into its components.

Parameters

  • str string The URL string to be parsed.

Properties

  • scheme string? The scheme (protocol) of the URL (e.g., http, https).
  • authority string? The authority part of the URL.
  • userInfo string? The user information (e.g., user:pass).
  • user string? The username from the user information.
  • pass string? The password from the user information.
  • host string? The host of the URL (e.g., example.com).
  • port string? The port of the URL (e.g., 8080).
  • relative string? The relative URL.
  • path string? The path of the URL (e.g., /path/to/resource).
  • directory string? The directory of the URL path.
  • file string? The file name from the path, if applicable.
  • query string? The query string (e.g., key=value&key2=value2).
  • fragment string? The fragment (hash) of the URL (e.g., #section).

Returns Object An object containing the parsed components of the URL.

addUrlParam

Adds or updates one or more query parameters to a given URL.

Parameters

  • url string The URL to which the parameters should be added.
  • param (string | Object) The key of the parameter to add, or an object containing multiple key-value pairs to add.
  • value (string | null) The value of the parameter to add. Ignored if param is an object. (optional, default null)

Examples

Add a single parameter to a URL without a query string

addUrlParam('https://example.com', 'key', 'value');
// Returns: 'https://example.com?key=value'

Add multiple parameters to a URL

addUrlParam('https://example.com', { key1: 'value1', key2: 'value2' });
// Returns: 'https://example.com?key1=value1&key2=value2'

Returns string The updated URL with the new query parameters.

escapeRegex

Parameters

  • str

toCssClassName

Parameters

  • str