NativeScript StringFormat
A NativeScript module for handling strings.
NativeScript Toolbox
This module is part of nativescript-toolbox.
License
Platforms
- Android
- iOS
Installation
Run
tns plugin add nativescript-stringformat
inside your app project to install the module.
Demo
For quick start have a look at the demo/app/main-view-model.js file of the demo app to learn how it works.
Otherwise ...
Documentation
The full documentation can be found on readme.io.
Examples
Simple example
; // "TM + MK"; // {1} // the alternative:;
Custom order of arguments
; // "Marcel Kloubert"; // {1}
Functions as arguments
You can use functions that return the value that should be included into the target string:
; // "23091979 + 5091979 = 28183958";
The full signature of a function:
Name | Description |
---|---|
index | The index value of the argument. For {7} this will be 7 |
args | The values that were passed to the underlying format() or formatArray() function. |
match | The complete (unhandled) expression of the argument. |
formatExpr | The optional format expression of the argument. For {0:lower} this will be lower . |
funcDepth | This value is 0 at the beginning. If you return a function in that function again, this will increase until you stop to return a function. |
Format providers
Separated by a :
an argument can contain a format expression at the right side.
So you can define custom logic to convert an argument value.
Lets say we want to define a format provider that converts values to upper and lower case strings:
; StringFormat.addFormatProvider;
Now you can use the extended logic in your code:
; // MARCEL kloubert;
The ctx
object of a provider callback has the following structure:
Name | Description |
---|---|
expression | The expression right to : . In that example upper and lower . |
handled | Defines if value was handled or not. Is (false) by default. |
value | The value that should be parsed. In that example Marcel and KlOUBERT . |
The parsed value has to be returned and ctx.handled
has to be set to (true)
.
All upcoming format providers will be skipped if the value has been marked as "handled".
Helper functions and classes
Functions
Name | Description |
---|---|
compare | Compares two strings. |
concat | Joins elements of an array to one string. |
isEmpty | Checks if a string is undefined, (null) or empty. |
isEmptyOrWhitespace | Checks if a string is undefined, (null), empty or contains whitespaces only. |
isNullOrEmpty | Checks if a string is (null) or empty. |
isNullOrUndefined | Checks if a string is (null) or undefined. |
isNullOrWhitespace | Checks if a string is (null), empty or contains whitespaces only. |
isWhitespace | Checks if a string is empty or contains whitespaces only. |
join | Joins elements of an array to one string by using a separator. |
similarity | Calculates the similarity between two strings. |
Classes
StringBuilder
; ; for ; i < 5; i++ // "Line 1: 0// Line 2: 1// Line 3: 2// Line 4: 3// Line 5: 4";