| | import os from 'node:os'; |
| |
|
| | import title from 'title'; |
| |
|
| | import { parseDate } from '@/utils/parse-date'; |
| |
|
| | |
| | const toTitleCase = (str: string) => title(str); |
| |
|
| | const rWhiteSpace = /\s+/; |
| | const rAllWhiteSpace = /\s+/g; |
| |
|
| | |
| | const collapseWhitespace = (str?: string | null) => { |
| | if (str && rWhiteSpace.test(str)) { |
| | return str.replaceAll(rAllWhiteSpace, ' ').trim(); |
| | } |
| | return str; |
| | }; |
| |
|
| | const convertDateToISO8601 = (date?: string | Date | number | null) => { |
| | if (!date) { |
| | return date; |
| | } |
| | if (typeof date !== 'object') { |
| | |
| | date = parseDate(date); |
| | } |
| | return date.toISOString(); |
| | }; |
| |
|
| | const getSubPath = (ctx) => { |
| | const subPath = ctx.req.path.replace(/\/[^/]*/, '') || '/'; |
| | return subPath; |
| | }; |
| |
|
| | const getLocalhostAddress = () => { |
| | const interfaces = os.networkInterfaces(); |
| | const address = Object.keys(interfaces) |
| | .flatMap((name) => interfaces[name] ?? []) |
| | .filter((iface) => iface?.family === 'IPv4' && !iface.internal) |
| | .map((iface) => iface?.address) |
| | .filter(Boolean); |
| | address.push('[::]'); |
| | return address; |
| | }; |
| |
|
| | export { collapseWhitespace, convertDateToISO8601, getLocalhostAddress, getSubPath, toTitleCase }; |
| |
|