Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not deep convert buffers #126

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Buffer} from 'node:buffer';
import mapObject from 'map-obj';
import camelCase from 'camelcase';
import QuickLru from 'quick-lru';
Expand All @@ -20,7 +21,8 @@ const isObject = value =>
&& value !== null
&& !(value instanceof RegExp)
&& !(value instanceof Error)
&& !(value instanceof Date);
&& !(value instanceof Date)
&& !(value instanceof Buffer);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, not just Buffer. It should detect any TypedArray

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus True. I was actually thinking if this lib should not convert any class instances but just arrays and plain objects. I've adjusted the implementation. If we want to check typed arrays, we could do ArrayBuffer.isView(), which is true for TypedArrays + DataViews (which we probably also want to exclude).


const transform = (input, options = {}) => {
if (!isObject(input)) {
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import process from 'node:process';
import {promisify} from 'node:util';
import {execFile} from 'node:child_process';
import {Buffer} from 'node:buffer';
import test from 'ava';
import camelcaseKeys from './index.js';

Expand Down Expand Up @@ -136,6 +137,11 @@ test('use locale independent camel-case transformation', async t => {
);
});

test('do not deep convert buffer', t => {
const input = {foo: Buffer.from('foo')};
t.true(camelcaseKeys(input, {deep: true}).foo instanceof Buffer);
});

/**
Executes the library with the given arguments and resolves with the parsed result.

Expand Down