String Utils
import * as stringUtils from '@green-stack/utils/stringUtils'Collection of utility functions for string manipulation and formatting:
- stringUtils.ts
Case Conversion Utils
snakeToCamel()
Converts a snake_case string to camelCase.
const result = snakeToCamel('hello_world') // => 'helloWorld'snakeToDash()
Converts a snake_case string to kebab-case.
const result = snakeToDash('hello_world') // => 'hello-world'dashToCamel()
Converts a kebab-case string to camelCase.
const result = dashToCamel('hello-world') // => 'helloWorld'dashToSnake()
Converts a kebab-case string to snake_case.
const result = dashToSnake('hello-world') // => 'hello_world'camelToSnake()
Converts a camelCase string to snake_case.
const result = camelToSnake('helloWorld') // => 'hello_world'camelToDash()
Converts a camelCase string to kebab-case.
const result = camelToDash('helloWorld') // => 'hello-world'Character Case Utils
uppercaseFirstChar()
Uppercases the first character of a string.
const result = uppercaseFirstChar('hello') // => 'Hello'lowercaseFirstChar()
Lowercases the first character of a string.
const result = lowercaseFirstChar('Hello') // => 'hello'String Transformation Utils
slugify()
Converts a string to a URL-friendly slug.
const result = slugify('Hello World!') // => 'hello-world'replaceStringVars()
Replaces placeholders like {somevar} or [somevar] with values from injectables.
const result = replaceStringVars(
'Hello {name}, welcome to [place]!',
{ name: 'John', place: 'Paris' }
) // => 'Hello John, welcome to Paris!'replaceMany()
Replaces every string you pass as the targets with the replacement string.
// Removes 'Update' or 'Add' from the string
replaceMany('useUpdateDataForm()', ['Update', 'Add'], '')
// => 'useDataForm()'
replaceMany('useAddUserForm()', ['Update', 'Add'], '')
// => 'useUserForm()'String Analysis Utils
findTargetString()
Finds a $target$ string inside another string.
const folderWithIcons = findTargetString(
'some/path/to/specific-folder/icons/',
'some/path/to/$target$/icons/'
) // => 'specific-folder'includesAny()
Checks whether a given string includes any of the provided words (case-insensitive).
const result = includesAny('Hello World', ['world', 'universe']) // => trueextractPathParams()
Extracts an array of potential params from a URL path.
const params = extractPathParams('/api/user/[slug]/posts/[id]')
// => ['slug', 'id']guessNameFromEmail()
Guesses the (full?) name from a provided email address.
Handy for sending out emails / suggesting data for newly signed up users.
guessNameFromEmail('thorr@fullproduct.dev') // → 'Thorr'
guessNameFromEmail('john-doe@hotmail.com') // → 'John Doe'
guessNameFromEmail('john-s-doe-1@gmail.com') // → 'John Doe'
guessNameFromEmail('john.doe@gmail.com') // → 'John Doe'
guessNameFromEmail('johndoe+something-else@gmail.com') // → 'Johndoe'
guessNameFromEmail('notAnEmail') // → ''
guessNameFromEmail('') // → ''guessDisplayName()
Attempts to extract a user’s full or display name, based on partial user or userlike metadata objects.
Checks in each potential source (function args) in the following order:
→ fullName? → name? → firstName? → username? → Guess from email? → ''
Handy for sending out emails / suggesting data for newly signed up users.
// Each source arg should be an object that could have any one of these keys
type Userlike = {
name?: string | null,
username?: string | null,
fullName?: string | null,
firstName?: string | null,
lastName?: string | null,
email?: string | null,
}
guessDisplayName({ fullName: 'John Doe' }) // → 'John Doe'
guessDisplayName({ ... }, { name: 'John Doe' }) // → 'John Doe'
guessDisplayName({ firstName: 'John', lastName: 'Doe' }) // → 'John Doe'
guessDisplayName({ firstName: 'John' }) // → 'John'
guessDisplayName({ lastName: 'Doe' }) // → ''
guessDisplayName({ ... }, { email: 'john@fullproduct.dev' }) // → 'John'
guessDisplayName({ email: 'john.doe@fullproduct.dev' }) // → 'John Doe'
guessDisplayName({ email: 'john@doe.dev' }, { username: 'jondovi' }) // → 'jondovi'ANSI Terminal Formatting
We provide two main objects for terminal text formatting:
ansi - constants
Constants for ANSI escape codes, including:
- Text styles:
reset,bold,dim,underscore, etc. - Colors:
black,red,green,yellow, etc. - Backgrounds:
bgBlack,bgRed,bgGreen, etc.
You should finish each ANSI code with reset to clear formatting for what follows after it.
To automate this, you can use the a helper functions below.
a - terminal formatters
Ansi helper functions for applying ANSI formatting to strings:
// Utility
a.bold('...') // Makes text bold
a.underscore('...') // Underlines text
a.reset('...') // Resets all formatting for this string + after
a.italic('...') // Italicizes text
// Colors
a.muted('...') // Makes text muted
a.black('...') // Black text
a.red('...') // Red text
a.green('...') // Green text
a.yellow('...') // Yellow text
a.blue('...') // Blue text
a.magenta('...') // Magenta text
a.cyan('...') // Cyan text
a.white('...') // White text
// Backgrounds
a.bgBlack('...') // Black background
a.bgRed('...') // Red background
a.bgGreen('...') // Green background
a.bgYellow('...') // Yellow background
a.bgBlue('...') // Blue background
a.bgMagenta('...') // Magenta background
a.bgCyan('...') // Cyan background
a.bgWhite('...') // White backgroundEach formatting function accepts:
msg: The message to formatclear: Optional boolean to reset formatting before applying new style, default istrue.
Example:
console.log(a.bold(a.red('Error:')) + ' Something went wrong')