c9-core/node_modules/c9/string.js

56 wiersze
1.4 KiB
JavaScript
Czysty Zwykły widok Historia

2015-02-10 19:41:24 +00:00
define(function(require, exports, module) {
"use strict";
/*
* Casts the first character in a string to uppercase.
*
* @param {String} str
* @type {String}
*/
exports.uCaseFirst = function(str) {
return str.substr(0, 1).toUpperCase() + str.substr(1);
};
/*
* Removes spaces and other space-like characters from the left and right ends
* of a string
*
* @param {String} str
* @type {String}
*/
exports.trim = function(str) {
return str.replace(/[\s\n\r]*$/, "").replace(/^[\s\n\r]*/, "");
};
/*
* Concatenate a string with itself n-times.
*
* @param {String} str
* @param {Number} times Number of times to repeat the String concatenation
* @type {String}
*/
exports.repeat = function(str, times) {
return new Array(times + 1).join(str);
};
/*
* Count the number of occurences of substring 'str' inside a string
*
* @param {String} str
* @param {String} substr
* @type {Number}
*/
exports.count = function(str, substr){
return str.split(substr).length - 1;
};
2015-10-20 10:47:14 +00:00
exports.endsWith = function(subjectString, searchString, position) {
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
2015-02-10 19:41:24 +00:00
});