JavaScript is like a canvas for developers, and when it comes to string formatting, keeping things consistent can make your code a joy to read. Enter the is.kebabCase
and is.not_kebabCase
methods from the 'thiis' package, your companions on the road to cleaner and more readable code. In this article, we'll embark on a delightful journey exploring these methods through various examples, including some exciting combinations with other methods.
The Charm of Kebab Case in JavaScript
Before we set off on our adventure, let's talk about kebab case. In JavaScript, kebab case is a convention where words are separated by hyphens (-
). It's often used in variable names, function names, and even URLs. is.kebabCase
helps you ensure that a given string adheres to this convention.
Meet is.kebabCase
- Your String Stylist
Imagine you have a string, and you want to check if it follows the elegant kebab case convention. The is.kebabCase
method is like a stylist for your strings, making sure they are formatted correctly. Let's see it in action:
import { is } from 'thiis';
const myString = 'hello-world';
const result = is.kebabCase(myString);
console.log(result); // true
In this example, we use is.kebabCase
to confirm that myString
is indeed in kebab case. As expected, it returns true
because the string follows the convention.
The Journey of Examples
Now, let's embark on a delightful journey exploring the capabilities of is.kebabCase
and its playful partner, is.not_kebabCase
. We'll delve into six unique scenarios, including a couple of exciting combinations.
1. Checking for Kebab Case
The primary purpose of is.kebabCase
is to verify if a string follows the kebab case convention. It's perfect for ensuring consistent naming in your code:
import { is } from 'thiis';
const variableName = 'my-variable-name';
const isVariableKebab = is.kebabCase(variableName);
if (isVariableKebab) {
// Proceed confidently with your kebab case variable.
} else {
// Handle cases where the variable name doesn't adhere to kebab case.
}
2. Guarding Against Non-Kebab Case
On the flip side, is.not_kebabCase
acts as your guardian against strings that don't follow the kebab case convention. Use it to catch non-kebab case strings before they sneak into your code:
import { is } from 'thiis';
const functionName = 'myFunctionName';
const isFunctionNotKebab = is.not_kebabCase(functionName);
if (isFunctionNotKebab) {
// Handle scenarios where the function name isn't in kebab case.
} else {
// Proceed with confidence.
}
3. Combining with Other Methods
Now, let's have some fun with combining methods. You can use logical OR (||
) to check for multiple conventions, like kebab case or camel case:
import { is } from 'thiis';
const mixedCaseString = 'mixedCaseString';
const isMixedCase = is.kebabCase(mixedCaseString) || is.camelCase(mixedCaseString);
if (isMixedCase) {
// Handle scenarios where the string is either kebab case or camel case.
} else {
// Explore other possibilities.
}
4. Stream of Kebabs with is.kebabCase
Let's bring in a stream of strings and filter out those that are not in kebab case. This time, we're introducing RxJS and the filter
operator:
import { is } from 'thiis';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';
const stream$ = from(['hello-world', 'notKebabCase', 'another-one', 'yet-another']);
stream$
.pipe(
filter(is.kebabCase)
)
.subscribe(kebabString => {
console.log(kebabString); // Only kebab case strings will make it to the stream!
});
In this example, the filter(is.kebabCase)
ensures that only kebab case strings get processed by the stream.
5. Array Exploration with is.not_kebabCase
Arrays provide yet another playground for is.not_kebabCase
. Check if any element in an array violates the kebab case convention:
import { is } from 'thiis';
const nonKebabArray = ['hello-world', 'notKebabCase', 'another-one', 'yet-another'];
const hasNonKebabElement = nonKebabArray.some(is.not_kebabCase); // true
console.log(hasNonKebabElement);
Here, hasNonKebabElement
checks if at least one element in the array is not in kebab case.
6. Creative URL Validation
In the world of web development, ensuring kebab case in URLs is common. Use is.kebabCase
for creative URL validation:
import { is } from 'thiis';
function validateURL(url) {
if (is.kebabCase(url)) {
return 'URL format is perfect!';
} else {
return 'Invalid URL format. Please use kebab case.';
}
}
7. Additional examples
is.kebabCase('hello'); // true
is.kebabCase('hello123'); // true
is.kebabCase('undefined'); // true
is.kebabCase('null'); // true
is.kebabCase('a1-b2-c3-d4-e5-f'); // true
is.kebabCase('g1-h2-i3-j4-k5-l6'); // true
is.kebabCase('in-kebab-case'); // true
is.kebabCase('helloWorld'); // false
is.kebabCase('a1B2c3d4e5f6g'); // false
is.kebabCase('a1B2c3d4e5f6'); // false
is.kebabCase('inCamelCase'); // false
is.kebabCase(BigInt(1)) // false
is.kebabCase(0) // false
is.kebabCase(1) // false
is.kebabCase(-1) // false
is.kebabCase(Symbol()) // false
is.kebabCase(null) // false
is.kebabCase(true) // false
is.kebabCase([]) // false
is.kebabCase(false) // false
is.kebabCase("") // false
is.kebabCase('') // false
is.kebabCase(``) // false
is.kebabCase({}) // false
is.kebabCase(undefined) // false
is.kebabCase(Function) // false
is.kebabCase(() => {}) // false
is.kebabCase(BigInt) // false
is.kebabCase(Symbol) // false
is.kebabCase(NaN) // false
is.kebabCase(Infinity) // false
is.kebabCase(-Infinity) // false
is.kebabCase(Number.POSITIVE_INFINITY) // false
is.kebabCase(Number.NEGATIVE_INFINITY) // false
// And all other known types will return false
is.not_kebabCase('hello'); // false
is.not_kebabCase('hello123'); // false
is.not_kebabCase('undefined'); // false
is.not_kebabCase('null'); // false
is.not_kebabCase('a1-b2-c3-d4-e5-f'); // false
is.not_kebabCase('g1-h2-i3-j4-k5-l6'); // false
is.not_kebabCase('in-kebab-case'); // false
is.not_kebabCase('helloWorld'); // true
is.not_kebabCase('a1B2c3d4e5f6g'); // true
is.not_kebabCase('a1B2c3d4e5f6'); // true
is.not_kebabCase('inCamelCase'); // true
is.not_kebabCase(BigInt(1)) // true
is.not_kebabCase(0) // true
is.not_kebabCase(1) // true
is.not_kebabCase(-1) // true
is.not_kebabCase(Symbol()) // true
is.not_kebabCase(null) // true
is.not_kebabCase(true) // true
is.not_kebabCase([]) // true
is.not_kebabCase(false) // true
is.not_kebabCase("") // true
is.not_kebabCase('') // true
is.not_kebabCase(``) // true
is.not_kebabCase({}) // true
is.not_kebabCase(undefined) // true
is.not_kebabCase(Function) // true
is.not_kebabCase(() => {}) // true
is.not_kebabCase(BigInt) // true
is.not_kebabCase(Symbol) // true
is.not_kebabCase(NaN) // true
is.not_kebabCase(Infinity) // true
is.not_kebabCase(-Infinity) // true
is.not_kebabCase(Number.POSITIVE_INFINITY) // true
is.not_kebabCase(Number.NEGATIVE_INFINITY) // true
// And all other known types will return true
8. Combination example
is.kebabCase_or_upperCase(...)
is.snakeCase_or_kebabCase(...)
is.snakeCase_or_kebabCase_or_pascalCase(...)
The Adventure Continues
The is.kebabCase
and is.not_kebabCase
methods from the 'thiis' package are your delightful companions on your JavaScript journey. They make string formatting playful and precise, ensuring your code adheres to the kebab case convention. By adding the 'thiis' package to your JavaScript toolkit and exploring its documentation for more tips and examples, you can elevate the readability of your code while having a touch of fun.
So, keep formatting those strings, and may your code be as elegant as a well-formatted kebab!
π ChatGPT & DALLΒ·E 3
Top comments (0)