Design & Download
Your JS Library
in Seconds!!!

Snippet: Case Checking

Written by Christopher West (cwest) on August 09, 2016.
Determine if a string is lower cased, upper cased, mix cased, or has no casing.
'Mix---Lower-Upper-No'.replace(/\w+/g, function(name, i) {
  YourJS['is' + name + 'Case'] = Function('s', 'n', 's=0 in arguments?s+"":"";return(s=(s==s.toLowerCase()?6:0)+(s==s.toUpperCase()?12:0))==' + i + '||(!!n&&s>12);');
});

isLowerCase(...) API Documentation

Description

Determines if a string is lower cased.

Parameters

  1. str {*}:
    The value to be turned into a string and checked.
  2. opt_allowNoCasing {*}:
    Optional. Defaults to false. If true the function will additionally return true when str has no cased characters.

Returns

Returns true if str has lower cased characters or if opt_allowNoCasing is true and str only has characters without casing. Otherwise false is returned.

Example

console.log(YourJS.isLowerCase(''));  // -> false
console.log(YourJS.isLowerCase('', true));  // -> true
console.log(YourJS.isLowerCase('8'));  // -> false
console.log(YourJS.isLowerCase('8', true));  // -> true
console.log(YourJS.isLowerCase('COOL'));  // -> false
console.log(YourJS.isLowerCase('Cool'));  // -> false
console.log(YourJS.isLowerCase('cool'));  // -> true
console.log(YourJS.isLowerCase('cool!'));  // -> true

isUpperCase(...) API Documentation

Description

Determines if a string is upper cased.

Parameters

  1. str {*}:
    The value to be turned into a string and checked.
  2. opt_allowNoCasing {*}:
    Optional. Defaults to false. If true the function will additionally return true when str has no cased characters.

Returns

Returns true if str has upper cased characters or if opt_allowNoCasing is true and str only has characters without casing. Otherwise false is returned.

Example

console.log(YourJS.isUpperCase(''));  // -> false
console.log(YourJS.isUpperCase('', true));  // -> true
console.log(YourJS.isUpperCase('8'));  // -> false
console.log(YourJS.isUpperCase('8', true));  // -> true
console.log(YourJS.isUpperCase('cool'));  // -> false
console.log(YourJS.isUpperCase('Cool'));  // -> false
console.log(YourJS.isUpperCase('COOL'));  // -> true
console.log(YourJS.isUpperCase('COOL!'));  // -> true

isMixCase(...) API Documentation

Description

Determines if a string has both upper cased and lower cased characters.

Parameters

  1. str {*}:
    The value to be turned into a string and checked.
  2. opt_allowNoCasing {*}:
    Optional. Defaults to false. If true the function will additionally return true when str has no cased characters.

Returns

Returns true if str has both upper cased and lower cased characters or if opt_allowNoCasing is true and str only has characters without casing. Otherwise false is returned.

Example

console.log(YourJS.isMixCase(''));  // -> false
console.log(YourJS.isMixCase('', true));  // -> true
console.log(YourJS.isMixCase('8'));  // -> false
console.log(YourJS.isMixCase('8', true));  // -> true
console.log(YourJS.isMixCase('cool'));  // -> false
console.log(YourJS.isMixCase('COOL'));  // -> false
console.log(YourJS.isMixCase('Cool'));  // -> true
console.log(YourJS.isMixCase('Cool!'));  // -> true

isNoCase(...) API Documentation

Description

Determines if a string has no characters that can be cased.

Parameters

  1. str {*}:
    The value to be turned into a string and checked.

Returns

Returns true if str has no characters that can be cased. Otherwise false is returned.

Example

console.log(YourJS.isNoCase(''));  // -> true
console.log(YourJS.isNoCase('8 + 5 = 13'));  // -> true
console.log(YourJS.isNoCase('cool'));  // -> false
console.log(YourJS.isNoCase('COOL'));  // -> false
console.log(YourJS.isNoCase('Cool'));  // -> false
console.log(YourJS.isNoCase('Cool!'));  // -> false