/angular-string-helper

Lightweight angularJS string helper that maybe helps with filters.

Primary LanguageJavaScriptMIT LicenseMIT

AngularJs String Helper

Build Status npm version Downloads

Just a simple lightweight angularjs string helper that maybe helps and also provides filters with each functions.

Installation

Install via npm

$ npm install angular-string-helper --save

NPM

or via bower

$ bower install angular-string-helper --save

Be sure to include angular-string-helper.js or angular-string-helper.min.js in your HTML document after including your angular.js file.

<script src="js/angular-string-helper.js"></script>

Add rex-string-helper as a dependency in your AngularJS app.

angular.module("youApp", ["rex-string-helper", "Other-Dependencies"]);

Inject Str on your Controller or Service.

(function () {
    'use strict';
    
    angular
        .module('appName')
        .controller('appController', function ($scope, Str) {
            //code here....
    
        });
})();

API Usage

Str.toSlug();

        var sampleString = "This is a sample string";
        var slug = Str.toSlug(sampleString);
        console.log(slug); //Outputs: this-is-a-sample-string

Str.toCamelCase();

        var sampleString = "This is a sample string";
        var camelCase = Str.toCamelCase(sampleString);
        console.log(camelCase); //Outputs: thisIsASampleString

Str.toSnakeCase();

        var sampleString = "This is a sample string";
        var snakeCase = Str.toSnakeCase(sampleString);
        console.log(snakeCase); //Outputs: this_is_a_sample_string

Str.toUpperFirstChar();

        var sampleString = "uppercase";
        var str = Str.toUpperFirstChar(sampleString);
        console.log(str); //Outputs: Uppercase

Str.toLowerFirstChar();

        var sampleString = "Lowercase";
        var str = Str.toLowerFirstChar(sampleString);
        console.log(str); //Outputs: lowercase

Str.toCamelCaseFromDashed();

        var sampleString = "this-is-a-sample-string";
        var str = Str.toCamelCaseFromDashed(sampleString);
        console.log(str); //Outputs: thisIsASampleString

Str.toDashFromCamelCase();

        var sampleString = "thisIsASampleString";
        var str = Str.toDashFromCamelCase(sampleString);
        console.log(str); //Outputs: this-is-a-sample-string

Str.toSnakeCaseFromCamelCase();

        var sampleString = "thisIsASampleString";
        var str = Str.toSnakeCaseFromCamelCase(sampleString);
        console.log(str); //Outputs: this_is_a_sample_string

Str.toOrdinal();

        //accept number and string type number ex. 19 = number, '19' = string
        var alphaNum = 42; or var alphaNum = '42';
        var str = Str.toOrdinal(alphaNum);
        console.log(str); //Outputs: 42nd

Str.startsWith();

        //returns true
        if (Str.startsWith('sample', 's')) {
            //Do something awesome here..
        }

Str.endsWith();

        //returns true
        if (Str.endsWith('sample', 'e')) {
            //Do something awesome here..
        }

Str.contains();

        //returns true
        if (Str.contains('this is a sample string', 'sample')) {
            //Do something awesome here..
        }

Str.truncate();

        var sampleString = "This is a sample string";
        var str = Str.truncate(sampleString, 15);
        console.log(str); //Outputs: This is a sampl...

Filter

We all know that AngularJs provides built-in filters, but I think these filters below are not included;

toSlug

<!--Controller: $scope.title = "This is a sample string"-->
<p>Title:{{title | toSlug}}</p> <!-- Browser: this-is-a-sample-string -->

toCamelCase

<!--Controller: $scope.title = "This is a sample string"-->
<p>Title:{{title | toCamelCase}}</p> <!-- Browser: thisIsASampleString -->

toSnakeCase

<!--Controller: $scope.title = "This is a sample string"-->
<p>Title:{{title | toSnakeCase}}</p> <!-- Browser: this_is_a_sample_string -->

toUpperFirstChar

<!--Controller: $scope.title = "uppercase"-->
<p>Title:{{title | toUpperFirstChar}}</p> <!-- Browser: Uppercase -->

toLowerFirstChar

<!--Controller: $scope.title = "Lowercase"-->
<p>Title:{{title | toLowerFirstChar}}</p> <!-- lowercase -->

toCamelCaseFromDashed

<!--Controller: $scope.title = "this-is-a-sample-string"-->
<p>Title:{{title | toCamelCaseFromDashed}}</p> <!-- Browser: thisIsASampleString -->

toDashFromCamelCase

<!--Controller: $scope.title = "thisIsASampleString"-->
<p>Slug:{{title | toDashFromCamelCase}}</p> <!-- Browser: this-is-a-sample-string -->

toSnakeCaseFromCamelCase

<!--Controller: $scope.title = "thisIsASampleStringg"-->
<p>Title:{{title | toSnakeCaseFromCamelCase}}</p> <!-- Browser: this_is_a_sample_string -->

toOrdinal

<!--Controller: $scope.ordinalVal = "42"-->
<p>Title:{{ordinalVal | toOrdinal}}</p> <!-- Browser: 42nd -->

License

MIT © Rexon A. De los Reyes

Thanks and enjoy. Godspeed!