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