From 58dce43ed09550bb38b15e4a4775faab82420637 Mon Sep 17 00:00:00 2001 From: Akshadha Saxena Date: Sun, 24 Aug 2025 00:31:19 +0530 Subject: [PATCH] feat: add String.prototype extensions (camelCase, capitalizeWords) --- src/extension.ts | 17 +++++++++++++++++ src/index.ts | 3 +++ src/tests/extension.test.ts | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/extension.ts create mode 100644 src/tests/extension.test.ts diff --git a/src/extension.ts b/src/extension.ts new file mode 100644 index 0000000..1f5c198 --- /dev/null +++ b/src/extension.ts @@ -0,0 +1,17 @@ +import { camelCase } from './transformations/camelCase'; +import { capitalizeWords } from './transformations/capitalizeWords'; + +declare global { + interface String { + camelCase(): string; // 👈 match the existing function name + capitalizeWords(): string; + } +} + +String.prototype.camelCase = function () { + return camelCase(this.toString()); +}; +String.prototype.capitalizeWords = function () { + return capitalizeWords(this.toString()); +}; +export {}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index c8295bf..1c44a10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,9 @@ import { formatting } from './formatting'; import { transformations } from './transformations'; import { validations } from './validations'; +//importing the extension to ensure String prototype is extended +import './extension'; + export default { analyzing, formatting, diff --git a/src/tests/extension.test.ts b/src/tests/extension.test.ts new file mode 100644 index 0000000..5ee62be --- /dev/null +++ b/src/tests/extension.test.ts @@ -0,0 +1,18 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import "../extension"; + +test("should convert to camelCase", () => { + const result = "hello world".camelCase(); + assert.equal(result, "helloWorld"); +}); + +test("should capitalize words", () => { + const result = "hello world".capitalizeWords(); + assert.equal(result, "Hello World"); +}); + +test("should chain methods (camelCase + capitalizeWords)", () => { + const result = "string example".camelCase().capitalizeWords(); + assert.equal(result, "StringExample"); +}); \ No newline at end of file