JSPM

  • Created
  • Published
  • Downloads 125666
  • Score
    100M100P100Q167764F
  • License MIT

Simple lightweight string operation library for Typescript, works with Angular

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (typescript-string-operations) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    CircleCI Quality Gate Status npm npm

    Simple lightweight string operation library for Typescript.

    No jQuery required! Unit tested, works with Angular.

     import { StringBuilder, join, format, isNullOrWhiteSpace } from 'typescript-string-operations';

    When migrating from Version 1.4.1 or lower, you can also import the clas String, this feature will be removed in the next major release

     import { String } from 'typescript-string-operations';

    USAGE:

    String.empty

    var id = String.empty;

    String.isNullOrWhiteSpace():

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

    String.format():

    var id = image.GetId()
    String.format("image_{0}.jpg", id)
    output: "image_2db5da20-1c5d-4f1a-8fd4-b41e34c8c5b5.jpg";

    Specifier available!

    var value = String.format("{0:L}", "APPLE"); //output "apple"
    
    value = String.format("{0:U}", "apple"); // output "APPLE"
    
    value = String.format("{0:d}", "2017-01-23 00:00"); //output "23.01.2017"
    
    value = String.format("{0:s}", "21.03.2017 22:15:01") //output "2017-03-21T22:15:01"
    
    value = String.format("{0:n}", 1000000);
    //output "1.000.000"
    
    value = String.format("{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;
    
    String.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

    String.Join():

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

    OR

     let object = { Name: "Foo", Value: "Bar" };
     var value = String.join('.', object);
    //output: "Foo.Bar";
    
    var array = ['Apple', 'Banana']
    var value = String.join("; ", array);
    //output: "Apple; Banana";

    Methods

    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 Method Converts the value of objects to strings based on the formats specified and inserts them into another string. format, args
    join Method Combines arguments delimited by given seperator. delimiter,args
    join Method Combines 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

    Method Type Description Parameter
    Append Method appends a string. value
    AppendFormat Method see description for String.format() format, args
    AppendLine Method appends a string in a new line. format, args
    AppendLineFormat Method like String.format() in a new line format, args
    Clear Method clears the StringBuilder
    ToString Method creates the actual string.