1.5.1 • Published 2 months ago

typescript-string-operations v1.5.1

Weekly downloads
9,534
License
MIT
Repository
github
Last release
2 months ago

CircleCI Quality Gate Status npm npm

Simple lightweight string operation library for Typescript.

Installation

npm install typescript-string-operations

No jQuery required! Unit tested, works with Angular.

 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';

USAGE:

String.empty

var id = emptyString; // or String.empty
// output: id = '';

String.isNullOrWhiteSpace():

var id = image.GetId();
if(isNullOrWhiteSpace(id)) // String.isNullOrWhiteSpace(id)
	return image;

String.format():

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"

UPDATE

String.format for Objects including specifiers

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
SpecifierResult
LLowerCase
UUpperCase
dShortDatePattern
sSortableDateTimePattern
nThousand seperator
00Padding numbers

String.join():

var value = joinString("; ", "Apple", "Banana"); // or String.join()
//output: "Apple; Banana";

OR

 let object = { Name: "Foo", Value: "Bar" };
 var value = joinString('.', object);
//output: "Foo.Bar";

var array = ['Apple', 'Banana']
var value = joinString("; ", array);
//output: "Apple; Banana";

Methods

MethodTypeDescriptionParameter
emptyPropertysimply returns "".
isNullOrWhiteSpaceMethodreturns true value if given parameter is either null, empty or undefined.format, ...args
format/formatStringMethodConverts the value of objects to strings based on the formats specified and inserts them into another string.format, ...args
join/joinStringMethodCombines arguments delimited by given seperator.delimiter,...args
join/joinStringMethodCombines arguments delimited by given seperator from array.delimiter,array

StringBuilder

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!";

Methods

MethodTypeDescriptionParameter
AppendMethodappends a string.value
AppendFormatMethodsee description for format()format, args
AppendLineMethodappends a string in a new line.format, args
AppendLineFormatMethodlike format() in a new lineformat, args
ClearMethodclears the StringBuilder
ToStringMethodcreates the actual string.
1.5.1

2 months ago

1.5.1-beta

2 months ago

1.6.0-rc.1

1 year ago

1.6.0-rc.0

1 year ago

1.5.0-beta.rc11

2 years ago

1.5.0-rc.14

2 years ago

1.5.0-rc.13

2 years ago

1.5.0-rc.15

2 years ago

1.5.0-rc.10

2 years ago

1.5.0-rc.12

2 years ago

1.5.0-rc.11

2 years ago

1.5.0-rc

2 years ago

1.5.0-rc.0

2 years ago

1.5.0-rc.1

2 years ago

1.5.0-rc.2

2 years ago

1.5.0-rc.3

2 years ago

1.5.0-rc.4

2 years ago

1.5.0-rc.5

2 years ago

1.5.0-rc.6

2 years ago

1.5.0-rc.7

2 years ago

1.5.0-rc.8

2 years ago

1.5.0-rc.9

2 years ago

1.5.0

2 years ago

1.5.0-beta.0

2 years ago

1.5.0-beta.2

2 years ago

1.5.0-beta.1

2 years ago

1.5.0-beta.4

2 years ago

1.5.0-beta.3

2 years ago

1.5.0-beta.5

2 years ago

1.5.0-beta

2 years ago

1.4.1

3 years ago

1.4.0

4 years ago

1.4.0-beta.2

4 years ago

1.4.0-beta.1

4 years ago

1.4.0-beta.0

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.5

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago