Add zero-dependency SMS utilities - #3
Merged
Merged
Conversation
Adds pure helpers for SMS text and phone numbers, usable without an API key and with any provider: segment counting with GSM-7 vs UCS-2 detection, GSM-7 sanitization for pasted smart punctuation, and E.164 validation and normalization. Also refreshes the example numbers used across the README and tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a small set of pure SMS helpers to the SDK. No API key, no network calls, no new dependencies, and useful with any SMS provider rather than only textbee.
What is new
Segments and encoding. Carriers bill per segment, and one emoji or curly quote silently flips a message from GSM-7 (160 chars) to UCS-2 (70 chars).
countSmsSegmentsreports encoding, encoded length, segment count, and remaining room, following the standards-default split including the rule that a two-unit character never straddles a segment boundary.getSmsEncodingandfindNonGsm7Characterscover the "why is this suddenly 3 segments" case.GSM-7 sanitization.
sanitizeForGsm7swaps curly quotes, ellipses, exotic spaces, and dash variants for plain equivalents so pasted copy does not quietly triple in cost. Best effort by design: unmapped characters pass through, and an optionaltransliterateAccentsstrips accents GSM-7 does not carry while leaving the ones it does (é, ü, ñ, à) alone. Zero-width joiners are deliberately preserved so emoji sequences survive.Phone numbers.
isValidE164andnormalizePhoneNumberhandle format-level validation and cleanup, including a leading00and an optional default country code. Documented plainly as format-only helpers, not libphonenumber: bad input returnsnull, a baddefaultCountryCodethrows.Everything is exported from the existing entry point and lives in one
src/sms-utils.ts, so the surface stays tree-shakeable and the GSM table helpers stay module-private.Notes
Verification
pnpm run typecheck,pnpm test(64 passing), andpnpm run buildall pass. Every code sample in the new README section was executed against the built output and produces exactly the documented result.