TypeScript String Operations
The `typescript-string-operations` library (current stable v1.6.1) provides a collection of lightweight, utility functions for common string manipulations in TypeScript, inspired by C# string operations. It includes robust `formatString` with specifiers (e.g., `{0:L}` for lowercase, `{0:d}` for short date), `joinString` for concatenating arrays or objects with a delimiter, `isNullOrWhiteSpace` for comprehensive empty/null/whitespace checks, and an `emptyString` constant. A `StringBuilder` class is also available for efficient, mutable string construction, mirroring its C# counterpart. The library is designed to be lightweight, has no jQuery dependency, and is unit-tested, making it suitable for modern web applications, including those built with Angular. Its release cadence is feature-driven, with notable breaking changes in version 1.5.0, where uppercase-prefixed methods and the `String` class were deprecated in favor of named function imports to avoid polluting the global `String` object.
Warnings
- breaking Methods and properties starting with an uppercase letter (e.g., `String.Format`, `String.Join`) were marked as deprecated in v1.5.0 and are scheduled for removal in future versions.
- breaking The `String` class, which overrides the native JavaScript `String` object when imported, was deprecated in v1.5.0 and will be removed in the next major release.
- gotcha Version 1.5.0 updated to use the latest TypeScript version, which might cause compatibility issues or require environment checks for projects using older TypeScript versions.
- gotcha Prior to its deprecation, importing the `String` class (e.g., `import { String } from 'typescript-string-operations'`) would override the native `String` object, potentially leading to conflicts or unexpected behavior with other libraries or native string methods.
Install
-
npm install typescript-string-operations -
yarn add typescript-string-operations -
pnpm add typescript-string-operations
Imports
- StringBuilder
const StringBuilder = require('typescript-string-operations').StringBuilder;import { StringBuilder, formatString, isNullOrWhiteSpace, joinString, emptyString } from 'typescript-string-operations'; - String (deprecated class)
import { String } from 'typescript-string-operations'; - formatString
import { String } from 'typescript-string-operations'; String.format(...);import { formatString } from 'typescript-string-operations';
Quickstart
import { StringBuilder, emptyString, joinString, formatString, isNullOrWhiteSpace } from 'typescript-string-operations';
// 1. Using emptyString
console.log('Empty String:', emptyString); // Output: Empty String:
// 2. Using isNullOrWhiteSpace
const value1 = null;
const value2 = '';
const value3 = ' ';
const value4 = 'Hello';
console.log(`isNullOrWhiteSpace(${JSON.stringify(value1)}):`, isNullOrWhiteSpace(value1)); // Output: true
console.log(`isNullOrWhiteSpace(${JSON.stringify(value2)}):`, isNullOrWhiteSpace(value2)); // Output: true
console.log(`isNullOrWhiteSpace(${JSON.stringify(value3)}):`, isNullOrWhiteSpace(value3)); // Output: true
console.log(`isNullOrWhiteSpace(${JSON.stringify(value4)}):`, isNullOrWhiteSpace(value4)); // Output: false
// 3. Using formatString with indexed arguments
const userId = '2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5';
const formattedImageName = formatString("image_{0}.jpg", userId);
console.log('Formatted Image Name:', formattedImageName); // Output: image_2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5.jpg
// 4. Using formatString with specifiers and objects
interface Fruit {
type: string;
color: string;
shippingDate: Date;
amount: number;
}
const fruit: Fruit = {
type: "apple",
color: "RED",
shippingDate: new Date(2018, 1, 1),
amount: 10000
};
const fruitDescription = formatString("The {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit);
console.log('Fruit Description:', fruitDescription); // Output: The APPLE is red shipped on 2018-01-01T00:00:00 with an amount of 10.000
// 5. Using joinString
const joinedFruits = joinString("; ", "Apple", "Banana", "Orange");
console.log('Joined Fruits (args):', joinedFruits); // Output: Apple; Banana; Orange
const fruitArray = ['Mango', 'Pineapple'];
const joinedFruitArray = joinString(", ", fruitArray);
console.log('Joined Fruits (array):', joinedFruitArray); // Output: Mango, Pineapple
const objectToJoin = { Name: "Foo", Value: "Bar", ID: "123" };
const joinedObject = joinString('.', objectToJoin);
console.log('Joined Object:', joinedObject); // Output: Foo.Bar.123
// 6. Using StringBuilder
const sb = new StringBuilder();
sb.Append('Hello').Append(' ').AppendLine('World!');
sb.AppendFormat('The answer is {0}.', 42);
console.log('StringBuilder Output:\n', sb.ToString());
/* Expected Output:
Hello World!
The answer is 42.
*/