npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nativescript-stringformat

v2.3.4

Published

NativeScript module for handling strings.

Downloads

22

Readme

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

| 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:

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:

| 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
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();