Skip to content

Commit aa3fda1

Browse files
committed
feat: Add String.prototype.phantom for direct method access
- Added .phantom property to String prototype - Enables direct method access: message.phantom.strings.toUpperCase().trim() - All chainable methods available via message.phantom.strings.* - Direct operation access via message.phantom.strings.operation.* - Added 8 test cases for String.prototype.phantom - All 316 tests passing
1 parent 20b26e2 commit aa3fda1

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

phantom.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,6 +2541,84 @@
25412541
}
25422542
};
25432543

2544+
/* --------------------------------------------------
2545+
* STRING PROTOTYPE EXTENSION
2546+
* Add .phantom property to strings for direct method access
2547+
* Usage: message.phantom.strings.toUpperCase().trim().split()...
2548+
* -------------------------------------------------- */
2549+
2550+
// Add phantom property to String prototype
2551+
if (typeof String !== "undefined" && String.prototype) {
2552+
Object.defineProperty(String.prototype, 'phantom', {
2553+
get: function() {
2554+
var self = this;
2555+
return {
2556+
strings: {
2557+
// Direct access to chain API
2558+
chain: function() {
2559+
return phantom.strings.chain(self);
2560+
},
2561+
// Direct access to operations (returns chainable)
2562+
toUpperCase: function() {
2563+
return phantom.strings.chain(self).toUpperCase();
2564+
},
2565+
toLowerCase: function() {
2566+
return phantom.strings.chain(self).toLowerCase();
2567+
},
2568+
trim: function() {
2569+
return phantom.strings.chain(self).trim();
2570+
},
2571+
leftTrim: function() {
2572+
return phantom.strings.chain(self).leftTrim();
2573+
},
2574+
rightTrim: function() {
2575+
return phantom.strings.chain(self).rightTrim();
2576+
},
2577+
capitalize: function() {
2578+
return phantom.strings.chain(self).capitalize();
2579+
},
2580+
reverse: function() {
2581+
return phantom.strings.chain(self).reverse();
2582+
},
2583+
reverseWords: function() {
2584+
return phantom.strings.chain(self).reverseWords();
2585+
},
2586+
replace: function(search, replace) {
2587+
return phantom.strings.chain(self).replace(search, replace);
2588+
},
2589+
replaceAll: function(search, replace) {
2590+
return phantom.strings.chain(self).replaceAll(search, replace);
2591+
},
2592+
remove: function(str) {
2593+
return phantom.strings.chain(self).remove(str);
2594+
},
2595+
leftPad: function(padChar, count) {
2596+
return phantom.strings.chain(self).leftPad(padChar, count);
2597+
},
2598+
rightPad: function(padChar, count) {
2599+
return phantom.strings.chain(self).rightPad(padChar, count);
2600+
},
2601+
substring: function(start, end) {
2602+
return phantom.strings.chain(self).substring(start, end);
2603+
},
2604+
wordwrap: function(size, cut, everything) {
2605+
return phantom.strings.chain(self).wordwrap(size, cut, everything);
2606+
},
2607+
// Direct operation access (non-chainable, returns value)
2608+
operation: phantom.strings.operation
2609+
},
2610+
numbers: {
2611+
chain: function() {
2612+
return phantom.numbers.chain(self);
2613+
}
2614+
}
2615+
};
2616+
},
2617+
enumerable: false,
2618+
configurable: true
2619+
});
2620+
}
2621+
25442622
// default init
25452623
phantom.init({ silent: true });
25462624

phantom.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,76 @@ describe('phantom.strings', () => {
870870
expect(chain.value()).toBe("test");
871871
});
872872
});
873+
874+
describe('String.prototype.phantom - Direct method access', () => {
875+
test('should access phantom methods directly on string', () => {
876+
const message = " HELLO WORLD ";
877+
const result = message.phantom.strings.toUpperCase().trim().value();
878+
expect(result).toBe("HELLO WORLD");
879+
});
880+
881+
test('should chain multiple operations directly on string', () => {
882+
const message = " hello world ";
883+
const result = message.phantom.strings
884+
.trim()
885+
.toUpperCase()
886+
.replace("WORLD", "UNIVERSE")
887+
.value();
888+
expect(result).toBe("HELLO UNIVERSE");
889+
});
890+
891+
test('should work with toLowerCase and capitalize', () => {
892+
const message = " HELLO WORLD ";
893+
const result = message.phantom.strings
894+
.trim()
895+
.toLowerCase()
896+
.capitalize()
897+
.value();
898+
expect(result).toBe("Hello world");
899+
});
900+
901+
test('should work with substring operation', () => {
902+
const message = "hello world";
903+
const result = message.phantom.strings
904+
.substring(0, 5)
905+
.toUpperCase()
906+
.value();
907+
expect(result).toBe("HELLO");
908+
});
909+
910+
test('should access operation methods directly', () => {
911+
const message = "hello";
912+
const upper = message.phantom.strings.operation.toUpperCase(message);
913+
expect(upper).toBe("HELLO");
914+
});
915+
916+
test('should convert to number chain from string', () => {
917+
const message = "123.45";
918+
const result = message.phantom.strings
919+
.trim()
920+
.toNumberChain()
921+
.round(1)
922+
.value();
923+
expect(result).toBe(123.5);
924+
});
925+
926+
test('should work with replace operations', () => {
927+
const message = "hello hello hello";
928+
const result = message.phantom.strings
929+
.replaceAll("hello", "hi")
930+
.toUpperCase()
931+
.value();
932+
expect(result).toBe("HI HI HI");
933+
});
934+
935+
test('should work with padding operations', () => {
936+
const message = "5";
937+
const result = message.phantom.strings
938+
.leftPad("0", 3)
939+
.value();
940+
expect(result).toBe("0005");
941+
});
942+
});
873943
});
874944

875945
describe('phantom.numbers', () => {

0 commit comments

Comments
 (0)