What is dasherize?
The dasherize npm package is used to convert strings into a dasherized (kebab-case) format. This can be particularly useful for creating URL-friendly strings, CSS class names, or other identifiers that require a consistent, lowercase, hyphen-separated format.
What are dasherize's main functionalities?
Basic Dasherization
Converts camelCase strings into dasherized format. This is useful for transforming variable names or other identifiers into a more readable and URL-friendly format.
const dasherize = require('dasherize');
console.log(dasherize('helloWorld')); // Output: 'hello-world'
Dasherize with Spaces
Converts strings with spaces into dasherized format. This is useful for transforming titles or phrases into a consistent, URL-friendly format.
const dasherize = require('dasherize');
console.log(dasherize('Hello World')); // Output: 'hello-world'
Dasherize with Underscores
Converts snake_case strings into dasherized format. This is useful for transforming database field names or other identifiers into a more readable and URL-friendly format.
const dasherize = require('dasherize');
console.log(dasherize('hello_world')); // Output: 'hello-world'
Other packages similar to dasherize
lodash.kebabcase
The lodash.kebabcase package is a utility function from the Lodash library that converts strings to kebab case. It offers similar functionality to dasherize but is part of the larger Lodash utility library, which provides a wide range of other utility functions.
change-case
The change-case package provides a variety of string transformation utilities, including kebab case conversion. It offers more flexibility and additional string transformation options compared to dasherize.
param-case
The param-case package is part of the 'no-case' family of packages and is specifically designed to convert strings to param-case (kebab-case). It offers similar functionality to dasherize but is part of a suite of packages that handle different case transformations.
dasherize
recursively transform key strings to dash-case
example
var dasherize = require('dasherize');
var obj = {
feeFieFoe: 'fum',
beepBoop: [
{ 'abcXyz': 'mno' },
{ 'fooBar': 'baz' }
]
};
var res = dasherize(obj);
console.log(JSON.stringify(res, null, 2));
output:
{
"fee-fie-foe": "fum",
"beep-boop": [
{
"abc-xyz": "mno"
},
{
"foo-bar": "baz"
}
]
}
methods
var dasherize = require('dasherize')
dasherize(obj)
Convert the key strings in obj
to dash-case recursively.
install
With npm do:
npm install dasherize
To use in the browser, use browserify.
license
derives directly from camelize
MIT