camel-to-kebab
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -8,2 +8,6 @@ // github.com/pineapplemachine/camel-to-kebab-js | ||
function camelToKebabCase(text){ | ||
// When the input was null or undefined, return that same value | ||
if(text === null || text === undefined){ | ||
return text; | ||
} | ||
// The output string, to be built in the loop | ||
@@ -20,16 +24,20 @@ var result = ""; | ||
var ch = string[i]; | ||
// If it's an upper-case letter: | ||
if(ch !== ch.toLowerCase()){ | ||
var chLower = ch.toLowerCase(); | ||
// If it's an upper-case letter (not preceded by ascii whitespace): | ||
if(ch !== chLower){ | ||
var prev = result[result.length - 1]; | ||
// Handle cases like "innerHTML" => "inner-html", | ||
// NOT "inner-h-t-m-l" | ||
if(upper > 1 && result.length > 1){ | ||
result = result.slice(0, result.length - 2) + result[result.length - 1]; | ||
result = result.slice(0, result.length - 2) + prev; | ||
} | ||
// Add a hyphen (but not at the beginning of the output, and not | ||
// two in a row) | ||
if(result.length && result[result.length - 1] !== "-"){ | ||
// two in a row, and not after whitespace) | ||
if(result.length && prev !== "-" && | ||
prev !== " " && prev !== "\t" && prev !== "\r" && prev !== "\n" | ||
){ | ||
result += "-"; | ||
} | ||
// Append the lower-case character | ||
result += ch.toLowerCase(); | ||
result += chLower; | ||
// Update upper-case and numeric state | ||
@@ -36,0 +44,0 @@ upper++; |
// github.com/pineapplemachine/camel-to-kebab-js | ||
// zlib/libpng license https://opensource.org/licenses/zlib-license.php | ||
// Copyright (c) 2018 Sophie Kirschner (sophiek@pineapplemachine.com) | ||
function camelToKebabCase(e){for(var n="",l=0,t=void 0,o=String(e),a=0;a<o.length;a++){var g=o[a];g!==g.toLowerCase()?(1<l&&1<n.length&&(n=n.slice(0,n.length-2)+n[n.length-1]),n.length&&"-"!==n[n.length-1]&&(n+="-"),n+=g.toLowerCase(),l++,t=!1):"0"===g||"1"===g||"2"===g||"3"===g||"4"===g||"5"===g||"6"===g||"7"===g||"8"===g||"9"===g?(!1===t&&"-"!==n[n.length-1]&&(n+="-"),n+=g,t=!(l=0)):(t&&"-"!==n[n.length-1]?n+="-"+g:n+=g,l=0,t=!1)}return 1<l&&1<n.length&&(n=n.slice(0,n.length-2)+n[n.length-1]),n}"undefined"!=typeof module&&(module.exports=camelToKebabCase),"undefined"!=typeof window&&(window.camelToKebabCase=camelToKebabCase); | ||
function camelToKebabCase(e){if(null==e)return e;for(var n="",l=0,t=void 0,a=String(e),o=0;o<a.length;o++){var r=a[o],g=r.toLowerCase();if(r!==g){var i=n[n.length-1];1<l&&1<n.length&&(n=n.slice(0,n.length-2)+i),n.length&&"-"!==i&&" "!==i&&"\t"!==i&&"\r"!==i&&"\n"!==i&&(n+="-"),n+=g,l++,t=!1}else"0"===r||"1"===r||"2"===r||"3"===r||"4"===r||"5"===r||"6"===r||"7"===r||"8"===r||"9"===r?(!1===t&&"-"!==n[n.length-1]&&(n+="-"),n+=r,t=!(l=0)):(t&&"-"!==n[n.length-1]?n+="-"+r:n+=r,l=0,t=!1)}return 1<l&&1<n.length&&(n=n.slice(0,n.length-2)+n[n.length-1]),n}"undefined"!=typeof module&&(module.exports=camelToKebabCase),"undefined"!=typeof window&&(window.camelToKebabCase=camelToKebabCase); |
{ | ||
"name": "camel-to-kebab", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Convert camelCase inputs to kebab-case outputs.", | ||
@@ -24,4 +24,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"canary-test": "~1.0.1", | ||
"uglify-js": "~3.3.23" | ||
"canary-test": "1.0.3", | ||
"uglify-js": "3.3.23" | ||
}, | ||
@@ -50,3 +50,2 @@ "main": "dist/camel-to-kebab.js", | ||
"uglify-test": "node test/uglify-test.js", | ||
"dist-test": "node test/dist-test.js", | ||
"es-test": "node --experimental-modules test/es-module-test.mjs", | ||
@@ -53,0 +52,0 @@ "test-all": "npm run test && npm run es-test && npm run dist-test && npm run uglify-test", |
@@ -8,3 +8,3 @@ # camel-to-kebab | ||
This library is very lightweight at 826 bytes minified and 440 bytes gzipped. It requires no external dependencies, and it's licensed according to the permissive | ||
This library is very lightweight at 892 bytes minified and 467 bytes gzipped. It requires no external dependencies, and it's licensed according to the permissive | ||
[zlib/libpng license](https://github.com/pineapplemachine/camel-to-kebab-js/blob/master/LICENSE). | ||
@@ -11,0 +11,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9726
6
94