Skip to content

Commit

Permalink
feat: add helper to camelCase the keys in an object
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 11, 2021
1 parent 79875ab commit b5e5c83
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"postpublish": "pinst --enable"
},
"dependencies": {
"camelcase": "^5.3.1",
"got": "^11.8.2"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions src/helper/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Data } from "./types";
import camelCase from "camelcase";

export function camelCaseKeys<T>(obj: Data): T {
const out: Data = {};
for (const key in obj) {
out[camelCase(key)] = obj[key];
}
return out as T;
}
17 changes: 17 additions & 0 deletions test/helper/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as util from "../../src/helper/util";

describe("camelCaseKeys()", () => {
it("should convert keys to camel case", () => {
const a = { foo_bar: "a", bar_foo: "b" };
const b = { fooBar: "a", barFoo: "b" };

expect(util.camelCaseKeys(a)).toStrictEqual(b);
});

it("should preserve camel cased keys", () => {
const a = { fooBar: "a", barFoo: "b" };
const b = { fooBar: "a", barFoo: "b" };

expect(util.camelCaseKeys(a)).toStrictEqual(b);
});
});

0 comments on commit b5e5c83

Please sign in to comment.