-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: simplify
split_sms
function (it splits now simply by \n
)
- Loading branch information
Showing
2 changed files
with
18 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,9 @@ | ||
import { count } from 'sms-length' | ||
|
||
export const split_sms = (sms: string) => { | ||
// CM.com allows max 10 "parts" per SMS, | ||
// so if it's less, we can send it that way. | ||
const { messages } = count(sms) | ||
if (messages <= 10) return [sms] | ||
|
||
// Otherwise, we need to split it. | ||
let split_sms = sms.includes('\n') ? sms.split('\n') : sms.split('.') | ||
split_sms = split_sms.map((part) => part.trim().replace('.', '. ')).filter((part) => part !== '') | ||
|
||
const sms_to_send: string[] = [] | ||
let part = '' | ||
let index = 0 | ||
let part_counter = 0 | ||
|
||
for (const el of split_sms) { | ||
const { messages } = count(el) | ||
part_counter += messages | ||
|
||
// If it's the last part of split sms, | ||
// then push it to final SMS. | ||
if (index === split_sms.length - 1) sms_to_send.push(el) | ||
|
||
// If part number is lower than 10, | ||
// continue to add part to former part... | ||
if (part_counter < 10) { | ||
part += el | ||
index++ | ||
} else { | ||
sms_to_send.push(part) | ||
part = '' | ||
part_counter = 0 | ||
index++ | ||
} | ||
} | ||
|
||
return sms_to_send | ||
} | ||
// Caution: | ||
// CM supports a maximum of 10 `parts` per SMS (1 SMS = 1 grey/green bubble in the iOS Messages app). | ||
// Currently, this function does not split or group by `parts`, nor does it verify SMS length`. | ||
// While this may NOT cause issues, be mindful if users say they receive "incomplete" response. | ||
export const split_sms = (sms: string) => | ||
sms | ||
.split('\n') | ||
.map((part) => part.replace(/\n\n$/g, '').trim()) | ||
.filter((part) => part !== '') |