• Stars
    star
    1,400
  • Rank 33,355 (Top 0.7 %)
  • Language
    JavaScript
  • Created about 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🐫 Underscore-to-camelCase converter (and vice versa) for strings and object keys in JavaScript.

humps Build status

Underscore-to-camelCase converter (and vice versa) for strings and object keys in JavaScript.

When converting object keys, it will walk the structure, converting any nested objects (or arrays of nested objects) along the way. Handy for converting JSON between JavaScript and Ruby/Rails APIs.

Takes inspiration from Ember Data and copies some utility functions from Underscore.js.

Usage

Converting strings

humps.camelize('hello_world') // 'helloWorld'
humps.decamelize('fooBar') // 'foo_bar'
humps.decamelize('fooBarBaz', { separator: '-' }) // 'foo-bar-baz'

Converting object keys

var object = { attr_one: 'foo', attr_two: 'bar' }
humps.camelizeKeys(object); // { attrOne: 'foo', attrTwo: 'bar' }

Arrays of objects are also converted

var array = [{ attr_one: 'foo' }, { attr_one: 'bar' }]
humps.camelizeKeys(array); // [{ attrOne: 'foo' }, { attrOne: 'bar' }]

It also accepts a callback which can modify the conversion behavior. For example to prevent conversion of keys containing only uppercase letters or numbers:

humps.camelizeKeys(obj, function (key, convert) {
  return /^[A-Z0-9_]+$/.test(key) ? key : convert(key);
});
humps.decamelizeKeys(obj, function (key, convert, options) {
  return /^[A-Z0-9_]+$/.test(key) ? key : convert(key, options);
});

In order to use the callback with options use the process option:

humps.decamelizeKeys(obj, {
    separator: '-',
    process: function (key, convert, options) {
      return /^[A-Z0-9_]+$/.test(key) ? key : convert(key, options);
    }
});

API

humps.camelize(string)

Removes any hypens, underscores, and whitespace characters, and uppercases the first character that follows.

humps.camelize('hello_world-foo bar') // 'helloWorldFooBar'

humps.pascalize(string)

Similar to humps.camelize(string), but also ensures that the first character is uppercase.

humps.pascalize('hello_world-foo bar') // 'HelloWorldFooBar'

humps.decamelize(string, options)

Converts camelCased string to an underscore-separated string.

humps.decamelize('helloWorldFooBar') // 'hello_world_foo_bar'

The separator can be customized with the separator option.

humps.decamelize('helloWorldFooBar', { separator: '-' }) // 'hello-world-foo-bar'

By default, decamelize will only split words on capital letters (not numbers as in humps pre v1.0). To customize this behaviour, use the split option. This should be a regular expression which, when passed into String.prototype.split, produces an array of words (by default the regular expression is: /(?=[A-Z])/). For example, to treat numbers as uppercase:

humps.decamelize('helloWorld1', { split: /(?=[A-Z0-9])/ }) // 'hello_world_1'

humps.depascalize(string, options)

Same as humps.decamelize above.

humps.camelizeKeys(object, options)

Converts object keys to camelCase. It also converts arrays of objects.

humps.pascalizeKeys(object, options)

Converts object keys to PascalCase. It also converts arrays of objects.

humps.decamelizeKeys(object, options)

Separates camelCased object keys with an underscore. It also converts arrays of objects. See humps.decamelize for details of options.

humps.depascalizeKeys(object, options)

See humps.decamelizeKeys.

Licence

humps is copyright Β© 2012+ Dom Christie and released under the MIT license.

More Repositories

1

juration

A natural language duration parser/stringifier written in javascript
JavaScript
103
star
2

turn

πŸ“– Animate page transitions in Turbo Drive apps
JavaScript
99
star
3

webrtc-hotwire-rails

A video chat app demonstration using Hotwire and Ruby on Rails
JavaScript
61
star
4

photography-view-transitions-nextjs

JavaScript
38
star
5

needles

πŸ“Audio loudness metering in the browser
JavaScript
36
star
6

tailwindcss-jit-rails

An experiment with tailwindcss-jit and the asset pipeline
Ruby
32
star
7

ios-placeholder

A jQuery plugin that mimics the behaviour of iOS placeholder text in input fields
JavaScript
7
star
8

drawer

A starting point for building 2-column responsive layouts that implement a smooth (jank free) sliding sidebar
CSS
4
star
9

csscompress

Minify modern CSS with Ruby
Ruby
3
star
10

slide-r

πŸ‘† Minimalist web component for building horizontal sliding user interfaces
JavaScript
3
star
11

audio-recorder

JavaScript
3
star
12

tmbundle

Frontend snippets for textmate
3
star
13

eddie

JavaScript
3
star
14

tailwind-initial

Tailwind plugin that generates utilities at a low specificity
JavaScript
2
star
15

online-street-photography-course

Eric Kim’s Online Photography Course: http://erickimphotography.com/blog/2014/02/04/free-open-source-online-street-photography-course-all-the-worlds-a-stage-introduction-to-street-photography/
Ruby
2
star
16

anagram

JavaScript
1
star
17

get

JSONP API to retrieve machine readable info from websites and generate embed code for known url types
Ruby
1
star
18

domchristie.co.uk

Astro
1
star
19

hotwire_personalization_test

Ruby
1
star
20

turbolinks_page_invalid_scroll_test

Ruby
1
star
21

go-make-things

JavaScript
1
star
22

turbolinks_materialize_autofocus

Ruby
1
star
23

guide

Styleguides
1
star
24

sweetener

Add getters and setters to a function to connect it with an object
JavaScript
1
star
25

turbo_back_test

demonstration of bug when navigating back whilst a request is in progress
Ruby
1
star
26

photography

HTML
1
star
27

photography-view-transitions

CSS
1
star
28

nice_partials_test

Ruby
1
star
29

progressive-alliance-resources

A list of resources about a progressive electoral alliance in the UK
1
star