tickle/modules/parseFileList.mjs

41 lines
979 B
JavaScript

//@ts-check
import { isNotNull } from "./isNotNull.mjs";
/**
* parses a filelist string. That's a string that looks like
* ```md
* name dd-mm-yyyy link name
* name dd-mm-yyyy
* name linkname
* name
* ```
*
* @param {string} lines
*/
export const parseFileList = (lines) =>
// @ts-ignore
lines
.trim()
.split(`\n`)
.map((line, index) => {
const today = new Date().toISOString().split("T")[0];
const result = line.match(
/(?<name>.+)\.(?<ext>\w{2,3})(?:\s+(?<date>[\d-]+)?(?<title>.+))?/
);
if (!result) {
console.error(`could not parse line number ${index}`);
return null;
}
const {
// @ts-ignore
groups: { name, ext, date = today, title = name },
} = result;
const href = `/${name}.${ext}`;
const date_unix = new Date(date).getTime();
return { name, href, date, title, date_unix, index };
})
.filter(isNotNull);
export default parseFileList;