snakecase-keys
Convert an object's keys to snake case
Last updated 9 months ago by bendrucker .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install snakecase-keys 
SYNC missed versions from official npm registry.

snakecase-keys tests

Convert an object's keys to snake case

Install

npm install snakecase-keys

Usage

import snakecaseKeys from 'snakecase-keys'

snakecaseKeys({ fooBar: 'baz' })
//=> { foo_bar: 'baz' }

snakecaseKeys({ 'foo-bar': true, nested: { fooBaz: 'bar' } })
//=> { foo_bar: true, nested: { foo_baz: 'bar' } }

API

snakecaseKeys(obj, options) -> object

obj

Required
Type: object | Array<object>

A plain object or array of plain objects to transform into snake case (keys only).

options

Optional
Type: object

deep

Type: boolean
Default: true

Enables snake-casing of keys in nested objects.

exclude

Type: Array<string | RegExp>
Default: []

An array of strings or regular expressions matching keys that will be excluded from snake-casing.

shouldRecurse(key, val) -> boolean

Optional
Type: function

A function that determines if val should be recursed.

Requires deep: true.

parsingOptions

Type: object
Default: {}

Options object passed to the built-in snakeCase function from change-case. Available options include:

  • split: Custom function to split strings into words
  • locale: Locale for case conversion
  • separateNumbers: Whether to separate numbers (deprecated, use splitSeparateNumbers)
  • delimiter: Custom delimiter between words
  • prefixCharacters: Characters to preserve at start
  • suffixCharacters: Characters to preserve at end

See change-case for full documentation.

snakeCase

Optional Type: (key: string) => string

Custom function to convert a key to snake case. Use this to fully override the default behavior of the library and convert keys according to your own conventions. When provided, the return type will be a generic Record<string, unknown>, since specific keys cannot be inferred from the custom function.

Related

License

MIT © Ben Drucker

Current Tags

  • 9.0.2                                ...           latest (9 months ago)

39 Versions

  • 9.0.2                                ...           9 months ago
  • 9.0.1                                ...           9 months ago
  • 9.0.0                                ...           9 months ago
  • 8.1.0                                ...           9 months ago
  • 8.0.1                                ...           2 years ago
  • 8.0.0                                ...           2 years ago
  • 7.0.1                                ...           2 years ago
  • 7.0.0                                ...           2 years ago
  • 6.0.0                                ...           2 years ago
  • 5.5.0                                ...           2 years ago
  • 5.4.7                                ...           3 years ago
  • 5.4.6                                ...           3 years ago
  • 5.4.5                                ...           3 years ago
  • 5.4.4                                ...           4 years ago
  • 5.4.3                                ...           4 years ago
  • 5.4.2                                ...           4 years ago
  • 5.4.1                                ...           4 years ago
  • 5.4.0                                ...           4 years ago
  • 5.3.0                                ...           4 years ago
  • 5.2.0                                ...           4 years ago
  • 5.1.2                                ...           4 years ago
  • 5.1.1                                ...           4 years ago
  • 5.1.0                                ...           5 years ago
  • 5.0.0                                ...           5 years ago
  • 4.0.2 [deprecated]           ...           5 years ago
  • 4.0.1                                ...           5 years ago
  • 4.0.0                                ...           5 years ago
  • 3.2.1                                ...           5 years ago
  • 3.2.0                                ...           6 years ago
  • 3.1.2                                ...           6 years ago
  • 3.1.1                                ...           6 years ago
  • 3.1.0                                ...           7 years ago
  • 3.0.0                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.1                                ...           8 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.0                                ...           10 years ago
Maintainers (1)
Downloads
Today 0
This Week 4
This Month 9
Last Day 1
Last Week 5
Last Month 0
Dependencies (3)
Dev Dependencies (3)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |