|
2568 | 2568 |
|
2569 | 2569 | /* -------------------------------------------------- |
2570 | 2570 | * 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 |
2572 | 2572 | * |
2573 | 2573 | * Usage: |
2574 | 2574 | * message.phantom.strings.toUpperCase().trim().value() |
| 2575 | + * message.ps.toUpperCase().trim().value() |
2575 | 2576 | * String(message.phantom.strings.toUpperCase().trim()) |
2576 | 2577 | * |
2577 | 2578 | * Note: Always call .value() at the end or wrap in String() |
2578 | 2579 | * when passing to logger.info() or other functions |
2579 | 2580 | * -------------------------------------------------- */ |
2580 | 2581 |
|
| 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 | + |
2581 | 2640 | // Add phantom property to String prototype |
2582 | 2641 | if (typeof String !== "undefined" && String.prototype) { |
2583 | 2642 | Object.defineProperty(String.prototype, 'phantom', { |
2584 | 2643 | get: function() { |
2585 | 2644 | var self = this; |
2586 | 2645 | 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), |
2641 | 2647 | numbers: { |
2642 | 2648 | chain: function() { |
2643 | 2649 | return phantom.numbers.chain(self); |
|
2648 | 2654 | enumerable: false, |
2649 | 2655 | configurable: true |
2650 | 2656 | }); |
| 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 | + }); |
2651 | 2666 | } |
2652 | 2667 |
|
2653 | 2668 | // default init |
|
0 commit comments