super-trailing-slash
Version:
Add or remove trailing slash
105 lines (94 loc) • 2.07 kB
JavaScript
;
/**
* @module super-trailing-slash
*/
/**
* @const
* @type {string}
*/
var SLASH = '\\';
/**
* @const
* @type {string}
*/
var BACKSLASH = '/';
/**
* Add trailing slash to string if necessary
* @param str {string}
* @returns {string}
* @example
* slash.add('http://www.google.it'); //=> http://www.google.it/
* slash.add('\\path\\to\\file'); //=> \\path\\to\\file\\
*/
function add(str) {
return remove(str) + detectType(str);
}
/**
* Remove trailing slash from string if necessary
* @param str {string}
* @returns {string}
* @example
* slash.remove('http://www.google.it/'); //=> http://www.google.it
* slash.remove('\\path\\to\\file\\'); //=> \\path\\to\\file
*/
function remove(str) {
return checkSlashes(str) ? remove(str.slice(0, -1)) : str;
}
/**
* Check if string has backslash
* @param str {string}
* @returns {boolean}
*/
function isBackslash(str) {
return str.endsWith(BACKSLASH);
}
/**
* Check if string has slash
* @param str {string}
* @returns {boolean}
*/
function isSlash(str) {
return str.endsWith(SLASH);
}
/**
* Check if string has slashes, both slash and backslash
* @param str {string}
* @returns {boolean}
*/
function checkSlashes(str) {
return isSlash(str) || isBackslash(str);
}
/**
* Count slash type occurrences
* @param str {string}
* @param slashType {string}
* @returns {number}
* @private
*/
function _countSlash(str, slashType) {
var count = -1;
for (var index = -2; index !== -1; index = str.indexOf(slashType, index + 1)) {
count++;
}
return count;
}
/**
* Detect slash type, slash or backslash
* @param str {string}
* @returns {string}
*/
function detectType(str) {
if (_countSlash(str, BACKSLASH) >= _countSlash(str, SLASH)) return BACKSLASH;else return SLASH;
}
// Expose module
module.exports = {
SLASH: SLASH,
BACKSLASH: BACKSLASH,
_countSlash: _countSlash,
detectType: detectType,
checkSlashes: checkSlashes,
isSlash: isSlash,
isBackslash: isBackslash,
remove: remove,
add: add
};