Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
typescript-string-operations
Advanced tools
Simple lightweight string operation library for Typescript, works with Angular
The typescript-string-operations package provides a variety of string manipulation utilities for TypeScript. It includes functionalities for formatting, padding, trimming, and more, making it easier to handle common string operations.
String Formatting
This feature allows you to format strings by replacing placeholders with specified values. It is useful for creating dynamic strings based on variable content.
const { StringFormat } = require('typescript-string-operations');
const formattedString = StringFormat('Hello, {0}!', 'World');
console.log(formattedString); // Output: Hello, World!
Padding
Padding allows you to add characters to the left or right of a string until it reaches a specified length. This is useful for aligning text or ensuring consistent string lengths.
const { StringPadding } = require('typescript-string-operations');
const paddedString = StringPadding.padLeft('123', 5, '0');
console.log(paddedString); // Output: 00123
Trimming
Trimming removes whitespace from the beginning and end of a string. This is useful for cleaning up user input or formatting strings for display.
const { StringTrim } = require('typescript-string-operations');
const trimmedString = StringTrim.trim(' Hello World ');
console.log(trimmedString); // Output: Hello World
The 'string' package provides a comprehensive set of string manipulation utilities, including formatting, padding, trimming, and more. It is similar to typescript-string-operations but offers a more extensive API and additional functionalities.
Lodash is a utility library that includes a wide range of functions for manipulating arrays, objects, and strings. While it is not focused solely on string operations, it provides robust string manipulation capabilities similar to typescript-string-operations.
The 'sprintf-js' package offers string formatting capabilities similar to those found in C's sprintf function. It is focused on formatting and is comparable to the formatting features of typescript-string-operations.
npm install typescript-string-operations
import { StringBuilder, emptyString, joinString, formatString, isNullOrWhiteSpace } from 'typescript-string-operations';
When migrating from Version 1.4.1 or lower, you can also import the class String
. Using this String class would override the native String
object from JavaScript. We will remove this declaration with the next major release
import { String } from 'typescript-string-operations';
var id = emptyString; // or String.empty
// output: id = '';
var id = image.GetId();
if(isNullOrWhiteSpace(id)) // String.isNullOrWhiteSpace(id)
return image;
var id = image.GetId()
formatString("image_{0}.jpg", id) // or String.format()
output: "image_2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5.jpg";
Specifier available!
var value = formatString("{0:L}", "APPLE"); //output "apple"
value = formatString("{0:U}", "apple"); // output "APPLE"
value = formatString("{0:d}", "2017-01-23 00:00"); //output "23.01.2017"
value = formatString("{0:s}", "21.03.2017 22:15:01") //output "2017-03-21T22:15:01"
value = formatString("{0:n}", 1000000);
//output "1.000.000"
value = formatString("{0:00}", 1);
//output "01"
var fruit = new Fruit();
fruit.type = "apple";
fruit.color = "RED";
fruit.shippingDate = new Date(2018, 1, 1);
fruit.amount = 10000;
format("the {type:U} is {color:L} shipped on {shippingDate:s} with an amount of {amount:n}", fruit);
// output: the APPLE is red shipped on 2018-01-01 with an amount of 10.000
Specifier | Result |
---|---|
L | LowerCase |
U | UpperCase |
d | ShortDatePattern |
s | SortableDateTimePattern |
n | Thousand seperator |
00 | Padding numbers |
var value = joinString("; ", "Apple", "Banana"); // or String.join()
//output: "Apple; Banana";
let object = { Name: "Foo", Value: "Bar" };
var value = joinString('.', object);
//output: "Foo.Bar";
var array = ['Apple', 'Banana']
var value = joinString("; ", array);
//output: "Apple; Banana";
Method | Type | Description | Parameter |
---|---|---|---|
empty | Property | simply returns "" . | |
isNullOrWhiteSpace | Method | returns true value if given parameter is either null, empty or undefined. | format , ...args |
format /formatString | Method | Converts the value of objects to strings based on the formats specified and inserts them into another string. | format , ...args |
join /joinString | Method | Combines arguments delimited by given seperator. | delimiter ,...args |
join /joinString | Method | Combines arguments delimited by given seperator from array. | delimiter ,array |
Just like you know from C#,
var favoriteFruit: string = this.fruitService.getFavorite(); //Blueberries
var builder = new StringBuilder("My favorite fruits are: ");
builder.Append("Apples, ");
builder.Append("Bananas ");
// using String.format() internally
builder.AppendFormat("and especially {0:U}!", favoriteFruit);
builder.AppendFormat(" I eat {0} every day!", 10);
var fruits = builder.ToString();
//output: "My favorite fruits are: Apples, Bananas and especially BLUEBERRIES! I eat 10 every day!";
Method | Type | Description | Parameter |
---|---|---|---|
Append | Method | appends a string. | value |
AppendFormat | Method | see description for format() | format , args |
AppendLine | Method | appends a string in a new line. | format , args |
AppendLineFormat | Method | like format() in a new line | format , args |
Clear | Method | clears the StringBuilder | |
ToString | Method | creates the actual string. |
FAQs
Simple lightweight string operation library for Typescript, works with Angular
The npm package typescript-string-operations receives a total of 121,138 weekly downloads. As such, typescript-string-operations popularity was classified as popular.
We found that typescript-string-operations 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.