Just a simple lightweight angularjs string helper that maybe helps and also provides filters with each functions.
Install via npm
$ npm install angular-string-helper --save
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....
});
})();
var sampleString = "This is a sample string";
var slug = Str.toSlug(sampleString);
console.log(slug); //Outputs: this-is-a-sample-string
var sampleString = "This is a sample string";
var camelCase = Str.toCamelCase(sampleString);
console.log(camelCase); //Outputs: thisIsASampleString
var sampleString = "This is a sample string";
var snakeCase = Str.toSnakeCase(sampleString);
console.log(snakeCase); //Outputs: this_is_a_sample_string
var sampleString = "uppercase";
var str = Str.toUpperFirstChar(sampleString);
console.log(str); //Outputs: Uppercase
var sampleString = "Lowercase";
var str = Str.toLowerFirstChar(sampleString);
console.log(str); //Outputs: lowercase
var sampleString = "this-is-a-sample-string";
var str = Str.toCamelCaseFromDashed(sampleString);
console.log(str); //Outputs: thisIsASampleString
var sampleString = "thisIsASampleString";
var str = Str.toDashFromCamelCase(sampleString);
console.log(str); //Outputs: this-is-a-sample-string
var sampleString = "thisIsASampleString";
var str = Str.toSnakeCaseFromCamelCase(sampleString);
console.log(str); //Outputs: this_is_a_sample_string
//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
//returns true
if (Str.startsWith('sample', 's')) {
//Do something awesome here..
}
//returns true
if (Str.endsWith('sample', 'e')) {
//Do something awesome here..
}
//returns true
if (Str.contains('this is a sample string', 'sample')) {
//Do something awesome here..
}
var sampleString = "This is a sample string";
var str = Str.truncate(sampleString, 15);
console.log(str); //Outputs: This is a sampl...
We all know that AngularJs provides built-in filters, but I think these filters below are not included;
<!--Controller: $scope.title = "This is a sample string"-->
<p>Title:{{title | toSlug}}</p> <!-- Browser: this-is-a-sample-string -->
<!--Controller: $scope.title = "This is a sample string"-->
<p>Title:{{title | toCamelCase}}</p> <!-- Browser: thisIsASampleString -->
<!--Controller: $scope.title = "This is a sample string"-->
<p>Title:{{title | toSnakeCase}}</p> <!-- Browser: this_is_a_sample_string -->
<!--Controller: $scope.title = "uppercase"-->
<p>Title:{{title | toUpperFirstChar}}</p> <!-- Browser: Uppercase -->
<!--Controller: $scope.title = "Lowercase"-->
<p>Title:{{title | toLowerFirstChar}}</p> <!-- lowercase -->
<!--Controller: $scope.title = "this-is-a-sample-string"-->
<p>Title:{{title | toCamelCaseFromDashed}}</p> <!-- Browser: thisIsASampleString -->
<!--Controller: $scope.title = "thisIsASampleString"-->
<p>Slug:{{title | toDashFromCamelCase}}</p> <!-- Browser: this-is-a-sample-string -->
<!--Controller: $scope.title = "thisIsASampleStringg"-->
<p>Title:{{title | toSnakeCaseFromCamelCase}}</p> <!-- Browser: this_is_a_sample_string -->
<!--Controller: $scope.ordinalVal = "42"-->
<p>Title:{{ordinalVal | toOrdinal}}</p> <!-- Browser: 42nd -->
MIT © Rexon A. De los Reyes