str-to-machinename

Convert an string into a machine name with selected separator: `Foo bar baz faz` → `foo_bar_baz_faz`

Usage no npm install needed!

<script type="module">
  import strToMachinename from 'https://cdn.skypack.dev/str-to-machinename';
</script>

README

StrToMachineNmae

Convert an string to machine name with selected separator: Foo bar baz fazfoo_bar_baz_faz

Cases you may need this for:

  • To generate clean url alias for your articles from the title for example
const urlAlias = strToMachinename('My awesome article title', '-');
//=> my-awesome-article-title
  • or to generate clean username from first name and last name of your user entity.
const username = strToMachinename('First name ' + 'Last Name', '_');
//=> first_name_last_name

Installation

$ npm install str-to-machinename

Usage

const strToMachinename = require('str-to-machinename');

strToMachinename("C'est une phrase française avec des accenté éàçàçéìù", '_');
//=> 'cest_une_phrase_francaise_avec_des_accente_eacaceiu'

strToMachinename("C'est une phrase française avec des accenté éàçàçéìù", '-');
//=> 'cest-une-phrase-francaise-avec-des-accente-eacaceiu'

strToMachinename("C'est une phrase française avec des accenté éàçàçéìù", '/');
//=> 'cest/une/phrase/francaise/avec/des/accente/eacaceiu'

Note: The following punctuations will be removed . , # ! $ % ^ & * ; : { } = ` ~ ( ) " '

API

strToMachinename(input, separator)

input

Type: string

String to convert.

separator

Type: '-' | '_' | '/'

Separator used for conversion.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/berramou/str-to-machinename

License

This npm package is available under the terms of the MIT License.