Name the refusal rule, and refuse a period the encoder cannot spell (#99)
* Refuse a relative validity period the SMPP time format cannot hold * Export unencodableText(), so a caller can word a refusal as the library does * Name the refusal rule in the README, and correct what it claims * Word the period refusal by what the encoder spells, and keep unencodableText() internal * Correct the decision bullet premise about what the relative format holds * Close the stability nits on the time guard and its tests
This commit is contained in:
+18
-8
@@ -132,6 +132,9 @@ export function smppDate(date: Date): string {
|
||||
+ pad(date.getUTCMinutes(), 2);
|
||||
}
|
||||
|
||||
/** A second count is spelled in days and below: no fixed number of seconds is a month or a year. */
|
||||
const maxRelativeSeconds = 99 * 86400 + 86399;
|
||||
|
||||
const absoluteTime = /^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d)(\d\d)([+-])$/;
|
||||
const relativeTime = /^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)000R$/;
|
||||
|
||||
@@ -139,9 +142,8 @@ const relativeTime = /^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)000R$/;
|
||||
export const smppTime = {
|
||||
/**
|
||||
* A Date becomes an absolute UTC time; a number is a relative period in seconds, expressed in
|
||||
* days and below, so anything past the 99d 23:59:59 the format holds is clamped to it; a string
|
||||
* is a stamp the caller formatted itself and is passed through. A value naming no instant or
|
||||
* period is refused.
|
||||
* days and below; a string is a stamp the caller formatted itself and is passed through. A value
|
||||
* naming no instant or period, and a second count no day field reaches, are refused.
|
||||
*/
|
||||
encode(value: Date | number | string): Result<{ text: string }> {
|
||||
if (typeof value === 'string') return { text: value };
|
||||
@@ -149,14 +151,22 @@ export const smppTime = {
|
||||
if (typeof value === 'number') {
|
||||
if (!Number.isFinite(value)) return { err: new Error(`Not an SMPP time: ${String(value)}`) };
|
||||
|
||||
const capped = Math.min(Math.max(0, Math.floor(value)), 99 * 86400 + 86399);
|
||||
const seconds = Math.floor(value);
|
||||
|
||||
if (seconds < 0) {
|
||||
return { err: new Error(`A relative period cannot be negative, got ${String(value)}`) };
|
||||
}
|
||||
|
||||
if (seconds > maxRelativeSeconds) {
|
||||
return { err: new Error(`A period in seconds is spelled in days and below, so ${String(maxRelativeSeconds)} (99d 23:59:59) is the most, got ${String(value)}; pass a Date for an instant further out`) };
|
||||
}
|
||||
|
||||
return {
|
||||
text: '0000'
|
||||
+ pad(Math.floor(capped / 86400), 2)
|
||||
+ pad(Math.floor(capped / 3600) % 24, 2)
|
||||
+ pad(Math.floor(capped / 60) % 60, 2)
|
||||
+ pad(capped % 60, 2)
|
||||
+ pad(Math.floor(seconds / 86400), 2)
|
||||
+ pad(Math.floor(seconds / 3600) % 24, 2)
|
||||
+ pad(Math.floor(seconds / 60) % 60, 2)
|
||||
+ pad(seconds % 60, 2)
|
||||
+ '000R',
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user