Skip to content

Commit

Permalink
fix: handle top level arrays (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendrucker committed Apr 9, 2024
1 parent c8faea9 commit 3dd91f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ const map = require('map-obj')
const { snakeCase } = require('snake-case')

module.exports = function (obj, options) {
if (Array.isArray(obj) && obj.some(item => item.constructor !== Object)) { throw new Error('obj must be array of plain objects') }
if (obj.constructor !== Object) throw new Error('obj must be an plain object')
if (Array.isArray(obj)) {
if (obj.some(item => item.constructor !== Object)) {
throw new Error('obj must be array of plain objects')
}
} else {
if (obj.constructor !== Object) {
throw new Error('obj must be an plain object')
}
}

options = Object.assign({ deep: true, exclude: [], parsingOptions: {} }, options)

Expand Down
9 changes: 8 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ test('shallow conversion with {deep: false}', function (t) {
t.end()
})

test('arrays', function (t) {
test('array of objects', function (t) {
const result = Snake([{ fooBar: 'baz' }])
t.deepEqual(result, [{ foo_bar: 'baz' }])
t.ok(Array.isArray(result))
t.end()
})

test('nested arrays', function (t) {
const result = Snake({ foo: [0, 1, 2] })
t.deepEqual(result, { foo: [0, 1, 2] })
t.ok(Array.isArray(result.foo))
Expand Down

0 comments on commit 3dd91f8

Please sign in to comment.