linx-simulator2/node_modules/camelcase/index.js

57 lines
1016 B
JavaScript
Raw Normal View History

2019-09-18 08:11:16 +00:00
'use strict';
2019-11-22 09:43:37 +00:00
function preserveCamelCase(str) {
var isLastCharLower = false;
2019-09-18 08:11:16 +00:00
2019-11-22 09:43:37 +00:00
for (var i = 0; i < str.length; i++) {
var c = str.charAt(i);
2019-09-18 08:11:16 +00:00
2019-11-22 09:43:37 +00:00
if (isLastCharLower && (/[a-zA-Z]/).test(c) && c.toUpperCase() === c) {
str = str.substr(0, i) + '-' + str.substr(i);
2019-09-18 08:11:16 +00:00
isLastCharLower = false;
i++;
} else {
2019-11-22 09:43:37 +00:00
isLastCharLower = (c.toLowerCase() === c);
2019-09-18 08:11:16 +00:00
}
}
2019-11-22 09:43:37 +00:00
return str;
}
2019-11-22 09:43:37 +00:00
module.exports = function () {
var str = [].map.call(arguments, function (str) {
return str.trim();
}).filter(function (str) {
return str.length;
}).join('-');
2019-09-18 08:11:16 +00:00
2019-11-22 09:43:37 +00:00
if (!str.length) {
return '';
2019-09-18 08:11:16 +00:00
}
2019-11-22 09:43:37 +00:00
if (str.length === 1) {
return str;
}
2019-09-18 08:11:16 +00:00
2019-11-22 09:43:37 +00:00
if (!(/[_.\- ]+/).test(str)) {
if (str === str.toUpperCase()) {
return str.toLowerCase();
}
2019-09-18 08:11:16 +00:00
2019-11-22 09:43:37 +00:00
if (str[0] !== str[0].toLowerCase()) {
return str[0].toLowerCase() + str.slice(1);
}
return str;
2019-09-18 08:11:16 +00:00
}
2019-11-22 09:43:37 +00:00
str = preserveCamelCase(str);
2019-09-18 08:11:16 +00:00
2019-11-22 09:43:37 +00:00
return str
.replace(/^[_.\- ]+/, '')
.toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, function (m, p1) {
return p1.toUpperCase();
});
2019-09-18 08:11:16 +00:00
};