JavaScript: Find if the String Has a Particular Case

Helper methods to tell if the case of the string is camel, snake, kebab, upper, lower, or start.

Talha Awan I'm open to new opportunities! For a full-time role, contract position, or freelance work, reach out at talha@talhaawan.net or LinkedIn.

Instead of writing our own logic to determine if the given string is of a particular case or not, we can write a helper method that acts as a wrapper, in which we compare the original string with the one returned by lodash method, and return true or false. The wrapper is a good way to pass on the responsibility of correct logic to lodash, a thoroughly tested library.

Following are the example codes:

isCapitalized

const isCapitalized = (str) => capitalize(str) === str;

console.log(isCapitalized("united")); 
// => false

console.log(isCapitalized("United")); 
// => true

isCamelCase

const isCamelCase = (str) => camelCase(str) === str;

console.log(isCamelCase("Firstname")); 
// => false

console.log(isCamelCase("firstName")); 
// => true

isKebabCase

const isKebabCase = (str) => kebabCase(str) === str;

console.log(isKebabCase("dataID")); 
// => false

console.log(isKebabCase("data-id")); 
// => true

isSnakeCase

const isSnakeCase = (str) => snakeCase(str) === str;

console.log(isSnakeCase("_first_name_")); 
// => false

console.log(isSnakeCase("first_name")); 
// => true

isStartCase

const isStartCase = (str) => startCase(str) === str;

console.log(isStartCase("Last name")); 
// => false

console.log(isStartCase("Last Name")); 
// => true

isUpperCase

const isUpperCase = (str) => upperCase(str) === str;

console.log(isUpperCase("Middle Name")); 
// => false

console.log(isUpperCase("MIDDLE NAME")); 
// => true

isLowerCase


const isLowerCase = (str) => lowerCase(str) === str;

console.log(isLowerCase("SSN")); 
// => false

console.log(isLowerCase("ssn")); 
// => true

isUpperFirst


const isUpperFirst = (str) => upperFirst(str) === str;

console.log(isUpperFirst("first name")); 
// => false

console.log(isUpperFirst("First name")); 
// => true

console.log(isUpperFirst("First Name")); 
// => true

isLowerFirst

const isLowerFirst = (str) => lowerFirst(str) === str;

console.log(isLowerFirst("software engineer")); 
// => true

console.log(isLowerFirst("Software engineer")); 
// => false

console.log(isLowerFirst("Software Engineer")); 
// => false



See also

When you purchase through links on techighness.com, I may earn an affiliate commission.
We use cookies to enhance your experience. By continuing to visit this site you agree to our use of cookies. More info cookie script