Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Created January 24, 2023 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenSem/cf55220884ba28240743fba4bb51540b to your computer and use it in GitHub Desktop.
Save DarrenSem/cf55220884ba28240743fba4bb51540b to your computer and use it in GitHub Desktop.
camelCase convert to-kebab-case (or to_snake_case) -- camelToKebab(string, toSnakeNotKebab) -- also will recognize automatically when to call REVERSE function
// camelToKebab.js (string, toSnakeNotKebab) -- also will recognize automatically when to call REVERSE function
// 207 characters var camelToKebab=(a,b,c=/[_-]/.test(a))=>a&&a.link?a.replace(/\s/g,"").replace(/[A-Z_-]/g,c?" ":a=>" "+a).replace(/^\s+|\s+$/g,"").toLowerCase().replace(/ ([a-z]*)/g,c?a=>a.charAt(1).toUpperCase()+a.slice(2):b?"_$1":"-$1"):a
// /* // (to run JSCRIPT-compatible version, comment out arrow function)
var camelToKebab = (string, toSnakeNotKebab,
_reverse = /[_-]/.test(string)
) => (
string && string.link
? string.replace(/\s/g, "").replace(
/[A-Z_-]/g,
_reverse
? " "
: ( s =>
" " + s
)
).replace(/^\s+|\s+$/g, "")
.toLowerCase()
.replace(
/ ([a-z]*)/g,
_reverse
? ( s =>
s.charAt(1).toUpperCase() + s.slice(2)
)
: toSnakeNotKebab ? "_$1"
: "-$1"
)
: string
);
// */ // (to run JSCRIPT-compatible version, comment out arrow function)
// non-arrow version (so it works in CSCRIPT.EXE /nologo JScript.js)... to run, => arrow version above must be commented out
// 257 characters var camelToKebab=function(a,b,c){return c=/[_-]/.test(a),a&&a.link?a.replace(/\s/g,"").replace(/[A-Z_-]/g,c?" ":function(a){return" "+a}).replace(/^\s+|\s+$/g,"").toLowerCase().replace(/ ([a-z]*)/g,c?function(a){return a.charAt(1).toUpperCase()+a.slice(2)}:b?"_$1":"-$1"):a}
var camelToKebab_JSCRIPT = function(string, toSnakeNotKebab,
_reverse
) { return (
_reverse = /[_-]/.test(string),
string && string.link
? string.replace(/\s/g, "").replace(
/[A-Z_-]/g,
_reverse
? " "
: function(s) { return (
" " + s
); }
).replace(/^\s+|\s+$/g, "")
.toLowerCase()
.replace(
/ ([a-z]*)/g,
_reverse
? function(s) { return (
s.charAt(1).toUpperCase() + s.slice(2)
); }
: toSnakeNotKebab ? "_$1"
: "-$1"
)
: string
); };
/*@cc_on
@if(@_jscript)
camelToKebab = camelToKebab_JSCRIPT;
console={log:function(){var a=arguments;a.length&&WScript.Echo([].join.call(a," "))}};
console.clear=function(){console.log(Array(29).join("\n"))};
@end@*/
console.clear();
var EMPTY_OBJECT = {};
var EMPTY_ARRAY = [];
console.log(
camelToKebab() === undefined,
camelToKebab(null) === null,
camelToKebab(EMPTY_OBJECT) === EMPTY_OBJECT,
camelToKebab(EMPTY_ARRAY) === EMPTY_ARRAY,
camelToKebab(0) === 0,
camelToKebab(1) === 1,
camelToKebab("") === "",
camelToKebab(" a b ") === "ab",
camelToKebab(" 1 2 ") === "12"
);
console.log(
camelToKebab("- - _ _--from--__--kebab--__--case--__--") === "fromKebabCase", // _to: 'camel' = "fromKebabCase"
camelToKebab("_ _ - -__from__--__snake__--__case__--__") === "fromSnakeCase", // _to: 'camel' = "fromSnakeCase"
camelToKebab(" FromPascalCase ") === "from-pascal-case", // _to: 'kebab' = "from-pascal-case"
camelToKebab(" elseFromCamelCase ") === "else-from-camel-case" // _to: 'kebab' = "else-from-camel-case"
);
console.log(
camelToKebab(" PascalToKebab", true) === "pascal_to_kebab", // _to: 'snake' = "pascal_to_kebab"
camelToKebab(" elseCamelToKebab", true) === "else_camel_to_kebab", // _to: 'snake' = "else_camel_to_kebab"
camelToKebab("- - _ _--kebab--__--to--__--camel--__--", true) === "kebabToCamel", // _to: 'camel' = "kebabToCamel"
camelToKebab("_ _ - -__snake__--__to__--__camel__--__", true) === "snakeToCamel" // _to: 'camel' = "snakeToCamel"
);
console.log(
camelToKebab([,][0], true) === undefined,
camelToKebab(null, true) === null,
camelToKebab(EMPTY_OBJECT, true) === EMPTY_OBJECT,
camelToKebab(EMPTY_ARRAY, true) === EMPTY_ARRAY,
camelToKebab(0, true) === 0,
camelToKebab(1, true) === 1,
camelToKebab("", true) === "",
camelToKebab(" a b ", true) === "ab",
camelToKebab(" 1 2 ", true) === "12"
);
"DONE";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment