@prelude/string
A TypeScript library for string manipulation with functional programming principles.
Core Functions
isBlank: (value: string) => booleanReturns
trueif provided string is blank,falseotherwise.ifBlank: <T>(value: string, thenThunk: () => T) => string | TMaps blank string to arbitrary value. Returns value as is if not blank.
dedent: (value: string) => stringReturns string lines without shared indentation.
firstIndent: (value: string) => stringReturns first line's indent.
indentTail: (value: string, indent?: string) => stringReturns string with all lines but the first one indented.
indent: (value: string, indent_?: string) => stringReturns string lines prefixed with provided indentation.
String Transformations
upperFirst: (value: string) => stringReturns the provided string with the first character as upper case.
truncate: (str: string, length: number, suffix?: string) => stringTruncates a string to the specified length and adds a suffix if truncated.
searchReplace: (source: string, search: string, replacement: string) => stringFuzzy search and replace functionality.
Case Conversions
camelCase: (str: string) => stringConverts a string to camelCase.
kebabCase: (str: string) => stringConverts a string to kebab-case.
snakeCase: (str: string) => stringConverts a string to snake_case.
startCase: (str: string) => stringConverts a string to Start Case.
String Distance
distance: (a: string, b: string, method?: 'levenshtein') => numberCalculate the distance between two strings using the specified method.
Lines Module
The library exports a Lines namespace with functions for working with string lines:
Lines.of: (value: string) => string[]Splits a string into an array of lines, handling different line endings.
Lines.similarIndexOf: (sourceLines: string[], searchLines: string[]) => numberFinds most similar index in source lines for search lines, useful for fuzzy text search.
Lines.distanceAt: (sourceLines: string[], at: number, queryLines: string[], distanceOf?) => numberCalculate the similarity score between query and source lines at a given index.
Lines.indent: (mutableLines: string[], indentation?: string) => voidMutably indents each line in the array with the specified indentation.
Lines.toIndent: (lines: string[], indentation?: string) => string[]Returns a new array of lines with the specified indentation.
Lines.Diff - Text Diffing Utilities
The Lines module includes powerful diffing capabilities:
Lines.Diff.myers: (sourceLines: string[], targetLines: string[]) => Op[]Calculates diff between two sets of lines using the Myers algorithm.
Lines.Diff.histogram: (sourceLines: string[], targetLines: string[]) => Op[]Performs histogram-based diff optimized for performance on large inputs.
Lines.Diff.Op- Operations API for working with diff results:type Op.T- Diff operation type:{ type: 'unchanged' | 'removed' | 'added', lines: string[] }Op.append- Appends operations at the end of diff opsOp.prepend- Prepends operations at the beginning of diff opsOp.verify- Verifies that ops applied to the source produce targetOp.readable- Maps ops to readable strings with prefixesOp.dump- Produces a full debug dump of source, target, and operations
Usage
npm i -E @prelude/string
import * as S from '@prelude/string'
License
This package is dedicated to the public domain under CC0 1.0.