AngularJs String Helper
Just a simple lightweight angularjs string helper that maybe helps and also provides filters with each functions.
Installation
Install via bower
$ bower install angular-string-helper --save
or via npm
Be sure to include angular-string-helper.js
or angular-string-helper.min.js
in your HTML document after including your angular.js file.
Add rex-string-helper
as a dependency in your AngularJS app.
angular;
Inject Str on your Controller or Service.
{ 'use strict'; angular ;};
API Usage
Str.toSlug();
var sampleString = "This is a sample string"; var slug = Str; console; //Outputs: this-is-a-sample-string
Str.toCamelCase();
var sampleString = "This is a sample string"; var camelCase = Str; console; //Outputs: thisIsASampleString
Str.toSnakeCase();
var sampleString = "This is a sample string"; var snakeCase = Str; console; //Outputs: this_is_a_sample_string
Str.toUpperFirstChar();
var sampleString = "uppercase"; var str = Str; console; //Outputs: Uppercase
Str.toLowerFirstChar();
var sampleString = "Lowercase"; var str = Str; console; //Outputs: lowercase
Str.toCamelCaseFromDashed();
var sampleString = "this-is-a-sample-string"; var str = Str; console; //Outputs: thisIsASampleString
Str.toDashFromCamelCase();
var sampleString = "thisIsASampleString"; var str = Str; console; //Outputs: this-is-a-sample-string
Str.toSnakeCaseFromCamelCase();
var sampleString = "thisIsASampleString"; var str = Str; console; //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; console; //Outputs: 42nd
Str.startsWith();
//returns true if Str //Do something awesome here..
Str.endsWith();
//returns true if Str //Do something awesome here..
Str.contains();
//returns true if Str //Do something awesome here..
Str.truncate();
var sampleString = "This is a sample string"; var str = Str; console; //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"-->Title:{{title | toSlug}} <!-- Browser: this-is-a-sample-string -->
toCamelCase
<!--Controller: $scope.title = "This is a sample string"-->Title:{{title | toCamelCase}} <!-- Browser: thisIsASampleString -->
toSnakeCase
<!--Controller: $scope.title = "This is a sample string"-->Title:{{title | toSnakeCase}} <!-- Browser: this_is_a_sample_string -->
toUpperFirstChar
<!--Controller: $scope.title = "uppercase"-->Title:{{title | toUpperFirstChar}} <!-- Browser: Uppercase -->
toLowerFirstChar
<!--Controller: $scope.title = "Lowercase"-->Title:{{title | toLowerFirstChar}} <!-- lowercase -->
toCamelCaseFromDashed
<!--Controller: $scope.title = "this-is-a-sample-string"-->Title:{{title | toCamelCaseFromDashed}} <!-- Browser: thisIsASampleString -->
toDashFromCamelCase
<!--Controller: $scope.title = "thisIsASampleString"-->Slug:{{title | toDashFromCamelCase}} <!-- Browser: this-is-a-sample-string -->
toSnakeCaseFromCamelCase
<!--Controller: $scope.title = "thisIsASampleStringg"-->Title:{{title | toSnakeCaseFromCamelCase}} <!-- Browser: this_is_a_sample_string -->
toOrdinal
<!--Controller: $scope.ordinalVal = "42"-->Title:{{ordinalVal | toOrdinal}} <!-- Browser: 42nd -->