Skip to content

Commit 2789896

Browse files
authored
feat: create transform array strings (#41)
1 parent 7979819 commit 2789896

File tree

7 files changed

+1655
-2353
lines changed

7 files changed

+1655
-2353
lines changed

.github/workflows/github_actions_ci.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ on:
55
branches: [main]
66

77
jobs:
8-
test_lint:
9-
name: Test Lint
8+
test_prettier:
9+
name: Test Prettier
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v4
@@ -16,8 +16,8 @@ jobs:
1616
node-version: "lts/*"
1717
- name: Install Dependencies
1818
run: npm install
19-
- name: Run the lint tests
20-
run: npm run lint:fix
19+
- name: Run the prettier tests
20+
run: npm run prettier
2121
test_unit:
2222
name: Test Unit
2323
runs-on: ubuntu-latest

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ fi
66

77
npm run test:unit
88
npm run test:coverage
9-
npm run lint:fix
9+
npm run prettier

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- 🦧 **CobolCase** - Format string in `COBOL-CASE`.
2626
- 🐫 **LeetSpeak** - Make everything `1337 5p34k`.
2727
- 🦄 **ConvertWithCustomDelimiter** - Convert any string to `custom-delimiter where - is the new delimiter`.
28+
- 🛠️ **TransformArrayStrings** - Transform an array of strings into various cases.
2829

2930
---
3031

@@ -57,7 +58,8 @@ import {
5758
toPathCase,
5859
toCobolCase,
5960
toLeetSpeak,
60-
convertToCustomDelimiter
61+
convertToCustomDelimiter,
62+
transformArrayStrings
6163
} from 'casenator';
6264

6365
// Camel Case
@@ -104,6 +106,10 @@ console.log(toLeetSpeak('hello world')); // 'h3110 w021d'
104106

105107
// Convert With Custom Delimiter
106108
console.log(convertWithCustomDelimiter("hello-world", "-", ".")); // 'hello.world'
109+
110+
// Transform Array of Strings
111+
console.log(transformArrayStrings(['hello world', 'foo bar'], 'snake')); // ['hello_world', 'foo_bar']
112+
107113
```
108114

109115
---

__test__/index.spec.cjs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
toPathCase,
1616
toSnakeCase,
1717
toUpperCase,
18+
transformArrayStrings,
1819
} from "../src";
1920

2021
describe("toCamelCase", () => {
@@ -622,3 +623,155 @@ describe("convertWithCustomDelimiter", () => {
622623
expect(result).toBe("testString");
623624
});
624625
});
626+
627+
describe("transformArrayStrings - success cases", () => {
628+
test("camel case", () => {
629+
const input = ["hello_world", "this_is_a_test"];
630+
expect(transformArrayStrings(input, "camel")).toEqual(
631+
input.map(toCamelCase),
632+
);
633+
});
634+
635+
test("pascal case", () => {
636+
const input = ["hello world", "another example"];
637+
expect(transformArrayStrings(input, "pascal")).toEqual(
638+
input.map(toPascalCase),
639+
);
640+
});
641+
642+
test("kebab case", () => {
643+
const input = ["Hello World", "Another Test"];
644+
expect(transformArrayStrings(input, "kebab")).toEqual(
645+
input.map(toKebabCase),
646+
);
647+
});
648+
649+
test("UPPERCASE", () => {
650+
const input = ["hello", "world"];
651+
expect(transformArrayStrings(input, "upper")).toEqual(
652+
input.map(toUpperCase),
653+
);
654+
});
655+
656+
test("Capital Case", () => {
657+
const input = ["hello world", "nice day"];
658+
expect(transformArrayStrings(input, "capital")).toEqual(
659+
input.map(toCapitalCase),
660+
);
661+
});
662+
663+
test("CONSTANT_CASE", () => {
664+
const input = ["my value", "another one"];
665+
expect(transformArrayStrings(input, "constant")).toEqual(
666+
input.map(toConstantCase),
667+
);
668+
});
669+
670+
test("dot.case", () => {
671+
const input = ["Dot Case Here", "Another.Value"];
672+
expect(transformArrayStrings(input, "dot")).toEqual(input.map(toDotCase));
673+
});
674+
675+
test("no case", () => {
676+
const input = ["No-Case-Here", "Also.This"];
677+
expect(transformArrayStrings(input, "no")).toEqual(input.map(toNoCase));
678+
});
679+
680+
test("snake_case", () => {
681+
const input = ["snake case", "AnotherTest"];
682+
expect(transformArrayStrings(input, "snake")).toEqual(
683+
input.map(toSnakeCase),
684+
);
685+
});
686+
687+
test("path/case", () => {
688+
const input = ["path case", "Another Test"];
689+
expect(transformArrayStrings(input, "path")).toEqual(input.map(toPathCase));
690+
});
691+
692+
test("COBOL-CASE", () => {
693+
const input = ["Cobol Case", "Hard Core"];
694+
expect(transformArrayStrings(input, "cobol")).toEqual(
695+
input.map(toCobolCase),
696+
);
697+
});
698+
699+
test("leet speak", () => {
700+
const input = ["leet", "speak"];
701+
expect(transformArrayStrings(input, "leet")).toEqual(
702+
input.map(toLeetSpeak),
703+
);
704+
});
705+
706+
test("reverse", () => {
707+
const input = ["abc", "123"];
708+
expect(transformArrayStrings(input, "reverse")).toEqual(
709+
input.map(reverseString),
710+
);
711+
});
712+
713+
test("substring", () => {
714+
const input = ["hello world", "javascript"];
715+
const options = { start: 0, end: 4 };
716+
expect(transformArrayStrings(input, "substring", options)).toEqual(
717+
input.map((str) => substring(str, options.start, options.end)),
718+
);
719+
});
720+
721+
test("custom delimiter", () => {
722+
const input = ["one_two", "three_four"];
723+
const options = { fromDelimiter: "_", toDelimiter: "-" };
724+
expect(transformArrayStrings(input, "custom", options)).toEqual(
725+
input.map((str) => convertWithCustomDelimiter(str, "_", "-")),
726+
);
727+
});
728+
729+
test("empty array returns empty array", () => {
730+
expect(transformArrayStrings([], "camel")).toEqual([]);
731+
});
732+
733+
test("works with empty strings", () => {
734+
const input = ["", ""];
735+
expect(transformArrayStrings(input, "upper")).toEqual(["", ""]);
736+
});
737+
});
738+
739+
describe("transformArrayStrings - error handling", () => {
740+
test("throws on unsupported case type", () => {
741+
expect(() => transformArrayStrings(["test"], "invalid")).toThrow(
742+
/Unsupported case type/,
743+
);
744+
});
745+
746+
test("throws if input is not an array", () => {
747+
expect(() => transformArrayStrings("not-an-array", "camel")).toThrow(
748+
TypeError,
749+
);
750+
});
751+
752+
test("throws if array contains non-strings", () => {
753+
expect(() => transformArrayStrings(["valid", 123], "camel")).toThrow(
754+
TypeError,
755+
);
756+
});
757+
758+
test("throws if substring is missing options", () => {
759+
expect(() => transformArrayStrings(["hello"], "substring")).toThrow();
760+
});
761+
762+
test("throws if custom delimiter is missing options", () => {
763+
expect(() => transformArrayStrings(["x_y"], "custom")).toThrow();
764+
});
765+
766+
test("throws if custom delimiter is missing fromDelimiter", () => {
767+
expect(() =>
768+
transformArrayStrings(["x_y"], "custom", { toDelimiter: "-" }),
769+
).toThrow();
770+
});
771+
772+
test("throws if custom delimiter is missing toDelimiter", () => {
773+
expect(() =>
774+
transformArrayStrings(["x_y"], "custom", { fromDelimiter: "_" }),
775+
).toThrow();
776+
});
777+
});

0 commit comments

Comments
 (0)