|
| 1 | +# Introduction |
| 2 | + |
| 3 | +## Default value for optional argument |
| 4 | + |
| 5 | +```javascript |
| 6 | +export function createAppointment(days, now = undefined) { |
| 7 | + if (now === undefined) { |
| 8 | + now = Date.now(); |
| 9 | + } |
| 10 | + |
| 11 | + /* ... */ |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | +```javascript |
| 16 | +export function createAppointment(days, now = undefined) { |
| 17 | + now = now || Date.now(); |
| 18 | + |
| 19 | + /* ... */ |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +```javascript |
| 24 | +export function createAppointment(days, now = undefined) { |
| 25 | + now = now ?? Date.now(); |
| 26 | + |
| 27 | + /* ... */ |
| 28 | +} |
| 29 | +``` |
| 30 | +
|
| 31 | +```javascript |
| 32 | +export function createAppointment(days, now = Date.now()) { |
| 33 | + /* ... */ |
| 34 | +} |
| 35 | +``` |
| 36 | +
|
| 37 | +```javascript |
| 38 | +export function createAppointment(days, now = new Date()) { |
| 39 | + const date = new Date(now); |
| 40 | + /* ... */ |
| 41 | +} |
| 42 | +``` |
| 43 | +
|
| 44 | +## Adding the requested days until the appointment |
| 45 | +
|
| 46 | +```javascript |
| 47 | +date.setDate(date.getDate() + days); |
| 48 | +return date; |
| 49 | +``` |
| 50 | +
|
| 51 | +```javascript |
| 52 | +return new Date(now + days * 24 * 60 * 60 * 1000); |
| 53 | +``` |
| 54 | +
|
| 55 | +```javascript |
| 56 | +return new Date(now + days * 86_400_000); |
| 57 | +``` |
| 58 | +
|
| 59 | +## Generating a ISO8601 timestamp string |
| 60 | +
|
| 61 | +```javascript |
| 62 | +return appointmentDate.toISOString(); |
| 63 | +``` |
| 64 | +
|
| 65 | +```javascript |
| 66 | +const date_yyyy = String(appointmentDate.getUTCFullYear()).padStart(4, '0'); |
| 67 | +const date_mm = String(appointmentDate.getUTCMonth() + 1).padStart(2, '0'); |
| 68 | +const date_dd = String(appointmentDate.getUTCDate()).padStart(2, '0'); |
| 69 | + |
| 70 | +const hh = String(appointmentDate.getUTCHours()).padStart(2, '0'); |
| 71 | +const mm = String(appointmentDate.getUTCMinutes()).padStart(2, '0'); |
| 72 | +const ss = String(appointmentDate.getUTCSeconds()).padStart(2, '0'); |
| 73 | +const ms = String(appointmentDate.getUTCMilliseconds()).padStart(3, '0'); |
| 74 | + |
| 75 | +return `${date_yyyy}-${date_mm}-${date_dd}T${hh}:${mm}:${ss}.${ms}Z`; |
| 76 | +``` |
| 77 | +
|
| 78 | +## Extracting the appointment details |
| 79 | +
|
| 80 | +```javascript |
| 81 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_capturing_group#browser_compatibility |
| 82 | + |
| 83 | +const regex = [ |
| 84 | + // matching YYYY-, extracts as year without leading zeros |
| 85 | + '^(?:000(?<year>\\d{1})|00(?<year>\\d{2})|0(?<year>\\d{3})|(?<year>\\d{4}))-', |
| 86 | + |
| 87 | + // matching MM-, extracts as month without leading zeros |
| 88 | + '(?:0(?<month>\\d{1})|(?<month>\\d{2}))-', |
| 89 | + |
| 90 | + // matching DD, extracts as date without leading zeros |
| 91 | + '(?:0(?<date>\\d{1})|(?<date>\\d{2}))', |
| 92 | + |
| 93 | + // matching T, no extraction |
| 94 | + 'T', |
| 95 | + |
| 96 | + // matching HH:, extracts as hour without leading zeros |
| 97 | + '(?:0(?<hour>\\d{1})|(?<hour>\\d{2})):', |
| 98 | + |
| 99 | + // matching MM:, extracts as minute without leading zeros |
| 100 | + '(?:0(?<minute>\\d{1})|(?<minute>\\d{2})):', |
| 101 | + |
| 102 | + // matching SS:mssZ, no extraction |
| 103 | + '\\d{2}\\.\\d{3}Z?$', |
| 104 | +].join(''); |
| 105 | +``` |
| 106 | +
|
| 107 | +```javascript |
| 108 | +const regex = [ |
| 109 | + // matching YYYY-, extracts as year including leading zeros |
| 110 | + '^(?<year>\\d{4})-', |
| 111 | + |
| 112 | + // matching MM-, extracts as month without leading zeros |
| 113 | + '(?<month>\\d{2})-', |
| 114 | + |
| 115 | + // matching DD, extracts as date without leading zeros |
| 116 | + '(?<date>\\d{2})', |
| 117 | + |
| 118 | + // matching T, no extraction |
| 119 | + 'T', |
| 120 | + |
| 121 | + // matching HH:, extracts as hour without leading zeros |
| 122 | + '(?<hour>\\d{2}):', |
| 123 | + |
| 124 | + // matching MM:, extracts as minute without leading zeros |
| 125 | + '(?<minute>\\d{2}):', |
| 126 | + |
| 127 | + // matching SS:mssZ, no extraction |
| 128 | + '\\d{2}\\.\\d{3}Z?$', |
| 129 | +].join(''); |
| 130 | + |
| 131 | +const detailsMatcher = new RegExp(regex); |
| 132 | +const match = detailsMatcher.exec(getAppointmentTimestamp(appointmentDate)); |
| 133 | +if (!match) { |
| 134 | + throw new Error('Could not extract details'); |
| 135 | +} |
| 136 | + |
| 137 | +// TODO: roll-over minute under or overflow |
| 138 | +// TODO: roll-over hour under or overflow |
| 139 | +const details = { |
| 140 | + year: match.groups ? parseInt(match.groups['year'], 10) : 0, |
| 141 | + month: match.groups ? parseInt(match.groups['month'], 10) - 1 : 0, |
| 142 | + date: match.groups ? parseInt(match.groups['date'], 10) : 0, |
| 143 | + hour: match.groups |
| 144 | + ? parseInt(match.groups['hour'], 10) - |
| 145 | + appointmentDate.getTimezoneOffset() / 60 |
| 146 | + : 0, |
| 147 | + minute: match.groups |
| 148 | + ? parseInt(match.groups['minute'], 10) - |
| 149 | + (appointmentDate.getTimezoneOffset() % 60) |
| 150 | + : 0, |
| 151 | +}; |
| 152 | + |
| 153 | +return details; |
| 154 | +``; |
| 155 | +``` |
0 commit comments