2.3.4 • Published 8 years ago

nativescript-stringformat v2.3.4

Weekly downloads
320
License
MIT
Repository
github
Last release
8 years ago

npm npm

NativeScript StringFormat

A NativeScript module for handling strings.

Donate

NativeScript Toolbox

This module is part of nativescript-toolbox.

License

MIT 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

import StringFormat = require('nativescript-stringformat');

// "TM + MK"
var newStr1 = StringFormat.format("{0} + {1}",
                                  "TM",  // {0}
                                  "MK");  // {1}
                                 
// the alternative:
var newStr2 = StringFormat.formatArray("{0} + {1}",
                                       ["TM", "MK"]);

Custom order of arguments

import StringFormat = require('nativescript-stringformat');

// "Marcel Kloubert"
var newStr = StringFormat.format("{1} {0}",
                                 "Kloubert",   // {0}
                                 "Marcel");  // {1}

Functions as arguments

You can use functions that return the value that should be included into the target string:

import StringFormat = require('nativescript-stringformat');

// "23091979 + 5091979 = 28183958"
var newStr = StringFormat.format("{0} + {1} = {2}",
                                 23091979,  // {0}
                                 5091979,  // {1}
                                 function (index, args) {  // {2}
                                     return args[0] + args[1];  // 28183958
                                 });

The full signature of a function:

function (index, args, match, formatExpr, funcDepth) {
    return <THE-VALUE-TO-USE>;
}
NameDescription
indexThe index value of the argument. For {7} this will be 7
argsThe values that were passed to the underlying format() or formatArray() function.
matchThe complete (unhandled) expression of the argument.
formatExprThe optional format expression of the argument. For {0:lower} this will be lower.
funcDepthThis 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:

import StringFormat = require("nativescript-stringformat");

StringFormat.addFormatProvider(function(ctx) {    
    var toStringSafe = function() { 
        return ctx.value ? ctx.value.toString() : "";
    }
    
    if (ctx.expression === "upper") {
        // UPPER case
        ctx.handled = true;
        return toStringSafe().toUpperCase();
    }
    
    if (ctx.expression === "lower") {
        // LOWER case
        ctx.handled = true;
        return toStringSafe().toLowerCase();
    }
});

Now you can use the extended logic in your code:

import StringFormat = require('nativescript-stringformat');

// MARCEL kloubert
var newStr = StringFormat.format("{0:upper} {1:lower}",
                                 "Marcel", "KlOUBERT");

The ctx object of a provider callback has the following structure:

NameDescription
expressionThe expression right to :. In that example upper and lower.
handledDefines if value was handled or not. Is (false) by default.
valueThe 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

NameDescription
compareCompares two strings.
concatJoins elements of an array to one string.
isEmptyChecks if a string is undefined, (null) or empty.
isEmptyOrWhitespaceChecks if a string is undefined, (null), empty or contains whitespaces only.
isNullOrEmptyChecks if a string is (null) or empty.
isNullOrUndefinedChecks if a string is (null) or undefined.
isNullOrWhitespaceChecks if a string is (null), empty or contains whitespaces only.
isWhitespaceChecks if a string is empty or contains whitespaces only.
joinJoins elements of an array to one string by using a separator.
similarityCalculates the similarity between two strings.

Classes

StringBuilder
import StringFormat = require('nativescript-stringformat');

var builder = new StringFormat.StringBuilder();

for (var i = 0; i < 5; i++) {
    builder.appendFormat("Line #{1}: {0}", i, i + 1)
           .appendLine();
}

// "Line 1: 0
// Line 2: 1
// Line 3: 2
// Line 4: 3
// Line 5: 4"
var str = builder.toString();
2.3.4

8 years ago

2.3.3

8 years ago

2.3.2

8 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.4

8 years ago

2.1.3

8 years ago

2.1.2

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago