
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
To handle strings. The main method is Str.from
.
StringInput
is any
value that can be converted to string.
Usage: Str.*
import { Str } from 'hpr'
Str.from(2000); // "2000"
Str.from([1, 2, 3, 4]); // "1,2,3,4"
Str.from({ variant: "outline" }); // "{\"variant\": \"outline\"}"
Convert the given input to a string.
export function from(input: StringInput): string
Return the remainder of a string after the first occurrence of a given value.
export function after(input: StringInput, search: StringInput): string
Return the remainder of a string after the last occurrence of a given value.
export function afterLast(input: StringInput, search: StringInput): string
Transliterate a UTF-8 value to ASCII.
export function ascii(input: StringInput): string
Get the portion of a string before the first occurrence of a given value.
export function before(input: StringInput, search: StringInput): string
Get the portion of a string before the last occurrence of a given value.
export function beforeLast(input: StringInput, search: StringInput): string
Get the portion of a string between the first occurrence of two given values.
export function between(input: StringInput, start: StringInput, end: StringInput): string
Get the portion of a string between the last occurrence of two given values.
export function betweenLast(input: StringInput, start: StringInput, end: StringInput): string
Convert a value to camel case.
export function camel(input: StringInput): string
Get the character at the specified index.
export function charAt(input: StringInput, index: number): string
Remove the given string(s) if it exists at the end of the haystack.
export function chopEnd(input: string, needle: StringInput | StringInput[]): string
Remove the given string(s) if it exists at the start of the haystack.
export function chopStart(input: string, needle: StringInput | StringInput[]): string
Determine if a given string contains a given substring.
export function contains(input: StringInput, needles: StringInput | StringInput[], ignoreCase = false): boolean
Determine if a given string contains all array values.
export function containsAll(input: StringInput, needles: StringInput[], ignoreCase = false): boolean
Replace consecutive instances of a given character with a single character in the given string.
export function deduplicate(input: StringInput, character = ' '): string
Determine if a given string doesn't contain a given substring.
export function doesntContain(input: StringInput, needles: StringInput | StringInput[], ignoreCase = false): boolean
Determine if a given string ends with a given substring.
export function endsWith(input: StringInput, needles: StringInput | StringInput[]): boolean
Escape special characters in a string to be safely used in a regular expression.
export function escapeRegExp(input: StringInput): string
Determine if a given string matches a given pattern.
export function is(input: StringInput, pattern: RegExp | RegExp[]): boolean
Determine if a given string is 7 bit ASCII.
export function isAscii(input: StringInput): boolean
Determine if a given value is valid JSON.
export function isJson(input: unknown): boolean
Check if the input is a string.
export function isString(input: StringInput): boolean
Determine if a given value is a valid ULID.
export function isUlid(input: StringInput): boolean
Determine if a given value is a valid URL.
export function isUrl(input: StringInput): boolean
Determine if a given value is a valid UUID.
export function isUuid(input: StringInput): boolean
Convert a string to kebab case.
export function kebab(input: StringInput): string
Return the length of the given string.
export function length(input: StringInput): number
Limit the number of characters in a string.
export function limit(input: StringInput, limit = 100, end = '...', preserveWords = false): string
Limit the number of words in a string.
export function limitWords(input: StringInput, words = 100, end = '...'): string
Convert the given string to lowercase.
export function lower(input: StringInput): string
Make a string's first character lowercase.
export function lowerFirst(input: StringInput): string
Make a string's first word lowercase.
export function lowerFirstWord(input: StringInput): string
Masks a portion of a string with a repeated character.
export function mask(input: StringInput, character: string, index: number, length?: number): string
Pad both sides of a string with another.
export function pad(input: StringInput, length: number, pad = ' '): string
Pad the beginning of a string with another.
export function padEnd(input: StringInput, length: number, pad = ' '): string
Pad the end of a string with another.
export function padStart(input: StringInput, length: number, pad = ' '): string
Generate a random, secure password.
export function password(length = 32, options: StringInput = {}): string
Get the plural form of an word.
export function plural(input: StringInput, count = 2, plural?: string): string
Find the multi-byte safe position of the first occurrence of a given substring in a string.
export function position(haystack: StringInput, needle: StringInput, offset = 0): number
Begin a string with a single instance of a given value.
export function prefix(input: StringInput, prefix: StringInput): string
Generate a more truly "random" alpha-numeric string.
export function random(length = 16): string
Remove StringInput occurrence of the given string in the subject.
export function remove(input: StringInput, search: StringInput | StringInput[], ignoreCase = false): string
Repeat the given string.
export function repeat(input: StringInput, times: number): string
Replace the given value in the given string.
export function replace(input: StringInput | StringInput[], search: StringInput | StringInput[], replace: StringInput | StringInput[], ignoreCase = false): string | string[]
Replace a given value in the string sequentially with an array.
export function replaceArray(input: StringInput, search: StringInput, replacements: StringInput | StringInput[]): string
Replace the last occurrence of a given value if it appears at the end of the string.
export function replaceEnd(input: StringInput, search: StringInput, replace: StringInput): string
Replace the first occurrence of a given value in the string.
export function replaceFirst(input: StringInput, search: StringInput, replace: StringInput): string
Replace the last occurrence of a given value in the string.
export function replaceLast(input: StringInput, search: StringInput, replace: StringInput): string
Replace the patterns matching the given regular expression.
export function replaceMatches(input: StringInput, pattern: RegExp, replacement: StringInput): string
Replace the first occurrence of the given value if it appears at the start of the string.
export function replaceStart(input: StringInput, search: StringInput, replace: StringInput): string
Reverse the given string.
export function reverse(input: StringInput): string
Generate a URL friendly "slug" from a given string.
export function slug(input: StringInput, separator = '-'): string
Convert a string to snake case.
export function snake(input: StringInput, separator = '_'): string
Remove all "extra" blank space from the given string.
export function squish(input: StringInput): string
Determine if a given string starts with a given substring.
export function startsWith(input: StringInput, target: StringInput): boolean
Returns the portion of the string specified by the start and length parameters.
export function substr(input: StringInput, start: number, length?: number): string
Returns the number of substring occurrences.
export function substrCount(haystack: StringInput, needle: StringInput, offset: number = 0, length?: number): number
Replace text within a portion of a string.
export function substrReplace(input: StringInput, start: number, length: number, replacement: StringInput): string
Cap a string with a single instance of a given value.
export function suffix(input: StringInput, cap: StringInput): string
Swap multiple keywords in a string with other keywords.
export function swap(input: StringInput, map: Record<string, string>): string
Take the first or last {$limit} characters of a string.
export function take(input: StringInput, limit: number): string
Convert the given string to proper case.
export function title(input: StringInput): string
Convert the given string to Base64 encoding.
export function toBase64(input: StringInput): string
Remove all whitespace from both ends of a string.
export function trim(input: StringInput, charlist: null | string = null): string
Remove all whitespace from the end of a string.
export function trimEnd(input: StringInput, charlist: null | string = null): string
Remove all whitespace from the beginning of a string.
export function trimStart(input: StringInput, charlist: null | string = null): string
Unwrap the string with the given strings.
export function unwrap(input: StringInput, before: StringInput, after: StringInput = before): string
Convert the given string to upper-case.
export function upper(input: StringInput): string
Make a string's first character uppercase.
export function upperFirst(input: StringInput): string
Make a string's first word uppercase.
export function upperFirstWord(input: StringInput): string
Split a string into pieces by uppercase characters.
export function upperSplit(input: StringInput): string[]
Generate a UUID (version 4).
export function uuid(): string
Generate a UUID (version 7).
export function uuid7(time: Date = new Date()): string
Get the number of words a string contains.
export function wordCount(input: StringInput): number
Wrap a string to a given number of characters.
export function wordWrap(input: StringInput, characters = 75, breakChar = '\n', cutLongWords = false): string
Wrap the string with the given strings.
export function wrap(input: StringInput, before: StringInput, after: StringInput): string
FAQs
A collection of utility functions to manipulate values and data.
The npm package hpr receives a total of 23 weekly downloads. As such, hpr popularity was classified as not popular.
We found that hpr demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.