diff --git a/src/Prototype.ts b/src/Prototype.ts new file mode 100644 index 0000000..046468a --- /dev/null +++ b/src/Prototype.ts @@ -0,0 +1,42 @@ + +import { camelCase, pascalCase, capitalizeWords } from './transformations'; + +declare global { + interface String { + camelCase(): string; + pascalCase(): string; + capitalizeWords(): string; + } +} + +if (!String.prototype.camelCase) { + Object.defineProperty(String.prototype, 'camelCase', { + value: function () { + return camelCase(this.toString()); + }, + writable: true, + configurable: true, + }); +} + +if (!String.prototype.pascalCase) { + Object.defineProperty(String.prototype, 'pascalCase', { + value: function () { + return pascalCase(this.toString()); + }, + writable: true, + configurable: true, + }); +} + +if (!String.prototype.capitalizeWords) { + Object.defineProperty(String.prototype, 'capitalizeWords', { + value: function () { + return capitalizeWords(this.toString()); + }, + writable: true, + configurable: true, + }); +} + +// (Add more methods similarly) diff --git a/src/index.ts b/src/index.ts index c8295bf..3eedb4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,8 @@ export * from './analyzing'; export * from './formatting'; export * from './transformations'; export * from './validations'; +export * from './Prototype'; + import { analyzing } from './analyzing'; import { formatting } from './formatting'; diff --git a/src/tests/prototype.test.ts b/src/tests/prototype.test.ts new file mode 100644 index 0000000..9bd619a --- /dev/null +++ b/src/tests/prototype.test.ts @@ -0,0 +1,19 @@ +import test from 'node:test'; +import '../Prototype'; +import { camelCase, capitalizeWords } from '../transformations'; + +test('String.prototype chaining: camelCase -> capitalizeWords', () => { + expect('hello world'.camelCase().capitalizeWords()).toBe( + capitalizeWords(camelCase('hello world')) + ); +}); +function expect(actual: string) { + return { + toBe(expected: string) { + if (actual !== expected) { + throw new Error(`Expected '${actual}' to be '${expected}'`); + } + } + }; +} +