Repository navigation
fix: warn on CRLF-inflated segment count for every line-break style - #74
Conversation
The prior fix only checked lineBreakStyle === 'LF', so messages that already contain real CRLF or a mix of CRLF/LF got no warning at all, even when the CRLF encoding was already costing extra segments. Move the check into SegmentedMessage._checkForWarnings() so it covers all three line-break styles and is available to every consumer of the library, not just the browser UI. Revert src/browser/segmenter.ts to its previous plain pass-through of segmentedMessage.warnings.
_checkForWarnings() is prefixed with an underscore and documented @Private, but TypeScript still emits it in the public .d.ts, and it previously took no arguments. Making the message argument required would break any direct caller (TS compile error, JS runtime crash on message.replace(...) with message undefined). Make the parameter optional and fall back to the original generic line-break warning when omitted, so the previous zero-argument shape keeps working exactly as before.
|
Thanks @bermudezmt for your work on this! I tested the PR locally and it works very well with the edge cases I tested. Few comments on my my side:
Having said that, I think that before merging this we should look at this changes more holistically. The warning for the linke breaks was added inside the library (and hence the npm package) but it's mainly used by the front end: I don't see users of the npm package using it, since it's using an undocumented property of the I believe we should decide whether the warning code really lives in the npm package or not. If it does, we should properly document it and possibly surface in a better way in the response. I personally believe it shouldn't be in the npm package, since the initial approach of this tool was to have it running in the same system that calls the Messaging API, so the calculation of the segments and the cost would always be accurate. What do you think? |
Per review feedback: revert SegmentedMessage.ts to its 1.3.0 state (no message parameter, no _segmentsCountFor, no new branches) so the npm library is untouched. The richer CRLF-vs-LF pricing comparison now lives entirely in src/browser/segmenter.ts, computed from the line break count and character total instead of building a second SegmentedMessage instance. Also move the warning banner above the segment tape so it's visible without expanding the "Encoding details" panel.
|
Thanks for the detailed feedback, and for the context on how this library is meant to be used, that changes things here. Addressed:
On whether this belongs in the npm package at all: agreed it doesn't. The new CRLF-vs-LF pricing check now lives entirely in I'll leave that pre-existing one for a separate conversation, since deprecating/removing a field that's been in the public |
|
@bermudezmt sorry for the delay in reviewing this. This looks great! With this code, a warning will be shown every time the message contains a line return:
The One thing before merging: please use a Why: this repo publishes to npm via semantic-release on every push to The |


Summary
Warn when a message's line-break style costs more SMS segments than the alternative would (CRLF counts as two characters per line break, LF as one). Covers all three line-break states: LF-only, CRLF-only, and mixed.
The check lives in
SegmentedMessage._checkForWarnings()in the core library, so the warning is consistent for both the browser UI and any direct consumer of the npm package.What changed
src/libs/SegmentedMessage.ts:_checkForWarnings()covers all threelineBreakStylestates (LF,CRLF,LF+CRLF), warning whenever the message's actual segment count exceeds what the cheaper line-break style would need. Added a small_segmentsCountFor()helper to compute a variant's segment count without re-running the full constructor.src/browser/segmenter.ts: passessegmentedMessage.warningsstraight through._checkForWarnings()still works when called with no argument, since it's emitted in the public.d.tsand previously took no arguments.dist/anddocs/scripts/per this repo's convention.Automated testing
npm run check- 678/678 tests pass, lint clean.lineBreakStylecombinations (none, LF, CRLF, mixed) againstdist/, both under and over the segment boundary - warnings fire exactly when the alternative line-break style would cost more segments._checkForWarnings()still works with zero arguments (back-compat check).Manual testing
Run:
Paste this exact four-line message into the calculator:
Expected SMS result:
1LF segment count: 1; CRLF may use 2 and affect pricing.Then test a non-boundary case:
Expected: 1 segment, no warning.
Also test text with no line breaks at all - expected: 1 segment, no warning.
Note: pasting into the textarea normalizes any
\r\nto\n, so this exercises thelineBreakStyle === 'LF'warning path. The already-CRLF/mixed-line-break path can't be triggered through the browser textarea (browsers strip\rfrom textarea values) - it's covered instead by theSegmentedMessage-level tests intests/index.test.js.Fixes #72.