npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

morph

v0.2.0

Published

Lightweight string transformations

Downloads

1,606

Readme

Morph

Morph is a lightweight collection of string transformations useful for converting between variable naming styles. It can operate on a single string or convert all of an object's keys to the new style.

Installation

Install via npm:

$ npm install morph

Usage

var morph = require('morph')

morph.toSnake('loremIpsumDolor')         // => 'lorem_ipsum_dolor'
morph.toSnakeCaps('loremIpsumDolor')     // => 'LOREM_IPSUM_DOLOR'
morph.toCamel('lorem_ipsum_dolor')       // => 'loremIpsumDolor'
morph.toUpperCamel('lorem_ipsum_dolor')  // => 'LoremIpsumDolor'
morph.toDashed('lorem_ipsum_dolor')      // => 'lorem-ipsum-dolor'
morph.toHuman('lorem_ipsum_dolor')       // => 'Lorem ipsum dolor'
morph.toTitle('lorem_ipsum_dolor')       // => 'Lorem Ipsum Dolor'

// Convert an object's keys
var obj = {loremIpsum: 'test', dolorSit: 'test'}
morph.toSnake(obj)                       // => {lorem_ipsum: 'test', dolor_sit: 'test'}

Snake case

toSnake(input, [capFirst])

Converts the input string or hash to snake case. Set capFirst to true to capitalize the first letter of the output string.

morph.toSnake('loremIpsumDolor')        // => 'lorem_ipsum_dolor'
morph.toSnake('LOREM_IPSUM_DOLOR')      // => 'lorem_ipsum_dolor'
morph.toSnake('lorem-Ipsum.Dolor')      // => 'lorem_ipsum_dolor'

Caps snake case

toSnakeCaps(input)

Converts the input string or hash to caps snake case.

morph.toSnakeCaps('loremIpsumDolor')        // => 'LOREM_IPSUM_DOLOR'
morph.toSnakeCaps('lorem_ipsum_dolor')      // => 'LOREM_IPSUM_DOLOR'
morph.toSnakeCaps('lorem-Ipsum.Dolor')      // => 'LOREM_IPSUM_DOLOR'

Camel case

toCamel(input, [capFirst])

toUpperCamel(input, [capFirst])

Converts the input string or hash to caps snake case. Set capFirst to true to capitalize the first letter of the string, or use the aliased toUpperCamel function.

morph.toCamel('lorem_ipsum_dolor')          // => 'loremIpsumDolor'
morph.toCamel('lorem-Ipsum.dolor')          // => 'loremIpsumDolor'
morph.toUpperCamel('LOREM_IPSUM_DOLOR')     // => 'LoremIpsumDolor'

Dashed

toDashed(input, [capFirst])

Converts the input string or hash to human readable format. Set capFirst to true to capitalize the first letter of the output string.

morph.toDashed('loremIpsumDolor')        // => 'lorem-ipsum-dolor'
morph.toDashed('LOREM_IPSUM_DOLOR')      // => 'lorem-ipsum-dolor'
morph.toDashed('lorem-Ipsum.Dolor')      // => 'lorem-ipsum-dolor'

Human format

toHuman(input, [capFirst])

Converts the input string or hash to human readable format. Set capFirst to false to leave the first letter of the string lowercase.

morph.toHuman('lorem_ipsum_dolor')         // => 'Lorem ipsum dolor'
morph.toHuman('loremIpsumDolor')           // => 'Lorem ipsum dolor'
morph.toHuman('LOREM_IPSUM_DOLOR', false)  // => 'lorem ipsum dolor'

Title format

toTitle(input)

Converts the input string or hash to title format.

morph.toTitle('lorem_ipsum_dolor')         // => 'Lorem Ipsum Dolor'
morph.toTitle('loremIpsumDolor')           // => 'Lorem Ipsum Dolor'
morph.toTitle('LOREM_IPSUM_DOLOR')         // => 'Lorem Ipsum Dolor'

Converting objects

To convert an object's keys to a new style, pass in an object as input. Note that all keys will be converted.

var obj = {some_key: 123, another_key: 456}
morph.toCamel(obj)                         // => {someKey: 123, anotherKey: 456}

Running the tests

To run the test suite, invoke the following commands in the repository:

$ npm install
$ npm test