mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 09:20:08 +00:00
feat(util): add camelToSnakeCase() & snakeToCamelCase() (#473)
* Update utils.ts * add tests * Update utils.test.ts * void unnecessary * Apply suggestions from code review Co-authored-by: Ayyan <ayyantee@gmail.com> * Update src/util/utils.ts Co-authored-by: Ayyan <ayyantee@gmail.com> * Update utils.ts * remove object test * some name changes Co-authored-by: Ayyan <ayyantee@gmail.com>
This commit is contained in:
@@ -60,3 +60,57 @@ export const formatImageURL = (
|
||||
return `${url}.${format ||
|
||||
(url.includes("/a_") ? "gif" : "jpg")}?size=${size}`;
|
||||
};
|
||||
|
||||
function camelToSnakeCase(text: string) {
|
||||
return text.replace(/ID|[A-Z]/g, ($1) => {
|
||||
if ($1 === "ID") return "_id";
|
||||
return `_${$1.toLowerCase()}`;
|
||||
});
|
||||
}
|
||||
|
||||
function snakeToCamelCase(text: string) {
|
||||
return text.replace(/_id|([-_][a-z])/ig, ($1) => {
|
||||
if ($1 === "_id") return "ID";
|
||||
return $1.toUpperCase().replace("_", "");
|
||||
});
|
||||
}
|
||||
|
||||
function isObject(obj: unknown) {
|
||||
return obj === Object(obj) && !Array.isArray(obj) &&
|
||||
typeof obj !== "function";
|
||||
}
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export function camelKeysToSnakeCase(obj: Record<string, any>) {
|
||||
if (isObject(obj)) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const convertedObject: Record<string, any> = {};
|
||||
Object.keys(obj)
|
||||
.forEach((key) => {
|
||||
convertedObject[camelToSnakeCase(key)] = camelKeysToSnakeCase(
|
||||
obj[key],
|
||||
);
|
||||
});
|
||||
return convertedObject;
|
||||
} else if (Array.isArray(obj)) {
|
||||
obj = obj.map((element) => camelKeysToSnakeCase(element));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export function snakeKeysToCamelCase(obj: Record<string, any>) {
|
||||
if (isObject(obj)) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const convertedObject: Record<string, any> = {};
|
||||
Object.keys(obj)
|
||||
.forEach((key) => {
|
||||
convertedObject[snakeToCamelCase(key)] = snakeKeysToCamelCase(
|
||||
obj[key],
|
||||
);
|
||||
});
|
||||
return convertedObject;
|
||||
} else if (Array.isArray(obj)) {
|
||||
obj = obj.map((element) => snakeKeysToCamelCase(element));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
75
test/util/utils.test.ts
Normal file
75
test/util/utils.test.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import {
|
||||
camelKeysToSnakeCase,
|
||||
snakeKeysToCamelCase,
|
||||
} from "../../src/util/utils.ts";
|
||||
import { assertEquals } from "../deps.ts";
|
||||
|
||||
const testSnakeObject = {
|
||||
// deno-lint-ignore camelcase
|
||||
hello_world: "hello_world",
|
||||
// deno-lint-ignore camelcase
|
||||
the_universe: {
|
||||
blue_planet: {
|
||||
water: "is_blue",
|
||||
dirt: "isDirty",
|
||||
},
|
||||
moon: {
|
||||
earth_moon: {
|
||||
is_round: true,
|
||||
},
|
||||
other_moon: {
|
||||
is_round: 0,
|
||||
},
|
||||
},
|
||||
arrays: ["one_two", { moo_cow: { boo: true } }],
|
||||
test_the_id: "123123123123",
|
||||
},
|
||||
};
|
||||
|
||||
const testCamelObject = {
|
||||
helloWorld: "hello_world",
|
||||
theUniverse: {
|
||||
bluePlanet: {
|
||||
water: "is_blue",
|
||||
dirt: "isDirty",
|
||||
},
|
||||
moon: {
|
||||
earthMoon: {
|
||||
isRound: true,
|
||||
},
|
||||
otherMoon: {
|
||||
isRound: 0,
|
||||
},
|
||||
},
|
||||
arrays: ["one_two", { mooCow: { boo: true } }],
|
||||
testTheID: "123123123123",
|
||||
},
|
||||
};
|
||||
|
||||
const someOther = {
|
||||
helloWorld: 1,
|
||||
};
|
||||
|
||||
const someElseOther = {
|
||||
hello_world: 1,
|
||||
};
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] snakeKeysToCamelCase: assert convertion",
|
||||
fn() {
|
||||
const result = snakeKeysToCamelCase(testSnakeObject);
|
||||
assertEquals(result, testCamelObject);
|
||||
const resultTwo = snakeKeysToCamelCase(someOther);
|
||||
assertEquals(resultTwo, someOther);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[utils] camelKeysToSnakeCase: assert convertion",
|
||||
fn() {
|
||||
const result = camelKeysToSnakeCase(testCamelObject);
|
||||
assertEquals(result, testSnakeObject);
|
||||
const resultTwo = camelKeysToSnakeCase(someElseOther);
|
||||
assertEquals(resultTwo, someElseOther);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user