| 382 | 382 | } |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | | -function getImages(path) { |
| 385 | +/** |
| 386 | + * Get a list of images in a directory. |
| 387 | + * @param {string} directoryPath Path to the directory containing the images |
| 388 | + * @param {'name' | 'date'} sortBy Sort images by name or date |
| 389 | + * @returns {string[]} List of image file names |
| 390 | + */ |
| 391 | +function getImages(directoryPath, sortBy = 'name') { |
| 392 | + function getSortFunction() { |
| 393 | + switch (sortBy) { |
| 394 | + case 'name': |
| 395 | + return Intl.Collator().compare; |
| 396 | + case 'date': |
| 397 | + return (a, b) => fs.statSync(path.join(directoryPath, a)).mtimeMs - fs.statSync(path.join(directoryPath, b)).mtimeMs; |
| 398 | + default: |
| 399 | + return (_a, _b) => 0; |
| 400 | + } |
| 401 | + } |
| 402 | + |
| 386 | 403 | return fs |
| 387 | 404 | .readdirSync(pathdirectoryPath) |
| 388 | 405 | .filter(file => { |
| 389 | 406 | const type = mime.lookup(file); |
| 390 | 407 | return type && type.startsWith('image/'); |
| 391 | 408 | }) |
| 392 | 409 | .sort(Intl.CollatorgetSortFunction().compare); |
| 393 | 410 | } |
| 394 | 411 | |
| 395 | 412 | /** |