Skip to content

Commit c9d2ca3

Browse files
committed
feat: Add .ps alias as shortcut for .phantom.strings
- Added .ps property to String.prototype as alias for .phantom.strings - Enables shorter syntax: message.ps.toUpperCase().trim().value() - Works with variable alias: var ps = phantom.strings; ps.chain(msg)... - Refactored string chain methods into reusable function - Added 5 test cases for .ps alias - All 321 tests passing
1 parent fe0110c commit c9d2ca3

2 files changed

Lines changed: 114 additions & 55 deletions

File tree

phantom.js

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,76 +2568,82 @@
25682568

25692569
/* --------------------------------------------------
25702570
* STRING PROTOTYPE EXTENSION
2571-
* Add .phantom property to strings for direct method access
2571+
* Add .phantom and .ps properties to strings for direct method access
25722572
*
25732573
* Usage:
25742574
* message.phantom.strings.toUpperCase().trim().value()
2575+
* message.ps.toUpperCase().trim().value()
25752576
* String(message.phantom.strings.toUpperCase().trim())
25762577
*
25772578
* Note: Always call .value() at the end or wrap in String()
25782579
* when passing to logger.info() or other functions
25792580
* -------------------------------------------------- */
25802581

2582+
// Helper function to create chainable string methods object
2583+
function createStringChainMethods(self) {
2584+
return {
2585+
// Direct access to chain API
2586+
chain: function() {
2587+
return phantom.strings.chain(self);
2588+
},
2589+
// Direct access to operations (returns chainable)
2590+
toUpperCase: function() {
2591+
return phantom.strings.chain(self).toUpperCase();
2592+
},
2593+
toLowerCase: function() {
2594+
return phantom.strings.chain(self).toLowerCase();
2595+
},
2596+
trim: function() {
2597+
return phantom.strings.chain(self).trim();
2598+
},
2599+
leftTrim: function() {
2600+
return phantom.strings.chain(self).leftTrim();
2601+
},
2602+
rightTrim: function() {
2603+
return phantom.strings.chain(self).rightTrim();
2604+
},
2605+
capitalize: function() {
2606+
return phantom.strings.chain(self).capitalize();
2607+
},
2608+
reverse: function() {
2609+
return phantom.strings.chain(self).reverse();
2610+
},
2611+
reverseWords: function() {
2612+
return phantom.strings.chain(self).reverseWords();
2613+
},
2614+
replace: function(search, replace) {
2615+
return phantom.strings.chain(self).replace(search, replace);
2616+
},
2617+
replaceAll: function(search, replace) {
2618+
return phantom.strings.chain(self).replaceAll(search, replace);
2619+
},
2620+
remove: function(str) {
2621+
return phantom.strings.chain(self).remove(str);
2622+
},
2623+
leftPad: function(padChar, count) {
2624+
return phantom.strings.chain(self).leftPad(padChar, count);
2625+
},
2626+
rightPad: function(padChar, count) {
2627+
return phantom.strings.chain(self).rightPad(padChar, count);
2628+
},
2629+
substring: function(start, end) {
2630+
return phantom.strings.chain(self).substring(start, end);
2631+
},
2632+
wordwrap: function(size, cut, everything) {
2633+
return phantom.strings.chain(self).wordwrap(size, cut, everything);
2634+
},
2635+
// Direct operation access (non-chainable, returns value)
2636+
operation: phantom.strings.operation
2637+
};
2638+
}
2639+
25812640
// Add phantom property to String prototype
25822641
if (typeof String !== "undefined" && String.prototype) {
25832642
Object.defineProperty(String.prototype, 'phantom', {
25842643
get: function() {
25852644
var self = this;
25862645
return {
2587-
strings: {
2588-
// Direct access to chain API
2589-
chain: function() {
2590-
return phantom.strings.chain(self);
2591-
},
2592-
// Direct access to operations (returns chainable)
2593-
toUpperCase: function() {
2594-
return phantom.strings.chain(self).toUpperCase();
2595-
},
2596-
toLowerCase: function() {
2597-
return phantom.strings.chain(self).toLowerCase();
2598-
},
2599-
trim: function() {
2600-
return phantom.strings.chain(self).trim();
2601-
},
2602-
leftTrim: function() {
2603-
return phantom.strings.chain(self).leftTrim();
2604-
},
2605-
rightTrim: function() {
2606-
return phantom.strings.chain(self).rightTrim();
2607-
},
2608-
capitalize: function() {
2609-
return phantom.strings.chain(self).capitalize();
2610-
},
2611-
reverse: function() {
2612-
return phantom.strings.chain(self).reverse();
2613-
},
2614-
reverseWords: function() {
2615-
return phantom.strings.chain(self).reverseWords();
2616-
},
2617-
replace: function(search, replace) {
2618-
return phantom.strings.chain(self).replace(search, replace);
2619-
},
2620-
replaceAll: function(search, replace) {
2621-
return phantom.strings.chain(self).replaceAll(search, replace);
2622-
},
2623-
remove: function(str) {
2624-
return phantom.strings.chain(self).remove(str);
2625-
},
2626-
leftPad: function(padChar, count) {
2627-
return phantom.strings.chain(self).leftPad(padChar, count);
2628-
},
2629-
rightPad: function(padChar, count) {
2630-
return phantom.strings.chain(self).rightPad(padChar, count);
2631-
},
2632-
substring: function(start, end) {
2633-
return phantom.strings.chain(self).substring(start, end);
2634-
},
2635-
wordwrap: function(size, cut, everything) {
2636-
return phantom.strings.chain(self).wordwrap(size, cut, everything);
2637-
},
2638-
// Direct operation access (non-chainable, returns value)
2639-
operation: phantom.strings.operation
2640-
},
2646+
strings: createStringChainMethods(self),
26412647
numbers: {
26422648
chain: function() {
26432649
return phantom.numbers.chain(self);
@@ -2648,6 +2654,15 @@
26482654
enumerable: false,
26492655
configurable: true
26502656
});
2657+
2658+
// Add .ps as alias for .phantom.strings (shorter syntax)
2659+
Object.defineProperty(String.prototype, 'ps', {
2660+
get: function() {
2661+
return createStringChainMethods(this);
2662+
},
2663+
enumerable: false,
2664+
configurable: true
2665+
});
26512666
}
26522667

26532668
// default init

phantom.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,50 @@ describe('phantom.strings', () => {
940940
expect(result).toBe("0005");
941941
});
942942
});
943+
944+
describe('String.prototype.ps - Short alias for .phantom.strings', () => {
945+
test('should access methods via .ps alias', () => {
946+
const message = " HELLO WORLD ";
947+
const result = message.ps.toUpperCase().trim().value();
948+
expect(result).toBe("HELLO WORLD");
949+
});
950+
951+
test('should chain multiple operations via .ps', () => {
952+
const message = " hello world ";
953+
const result = message.ps
954+
.trim()
955+
.toUpperCase()
956+
.replace("WORLD", "UNIVERSE")
957+
.value();
958+
expect(result).toBe("HELLO UNIVERSE");
959+
});
960+
961+
test('should work with variable alias', () => {
962+
const message = "test";
963+
const ps = phantom.strings;
964+
const result = ps.chain(message).toUpperCase().trim().value();
965+
expect(result).toBe("TEST");
966+
});
967+
968+
test('should work with toLowerCase and capitalize via .ps', () => {
969+
const message = " HELLO WORLD ";
970+
const result = message.ps
971+
.trim()
972+
.toLowerCase()
973+
.capitalize()
974+
.value();
975+
expect(result).toBe("Hello world");
976+
});
977+
978+
test('should work with replace operations via .ps', () => {
979+
const message = "hello hello hello";
980+
const result = message.ps
981+
.replaceAll("hello", "hi")
982+
.toUpperCase()
983+
.value();
984+
expect(result).toBe("HI HI HI");
985+
});
986+
});
943987
});
944988

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

0 commit comments

Comments
 (0)