|
| 1 | +import { |
| 2 | + NormalizedRubyGemsOwner, |
| 3 | + NormalizedRubyGemsPackage, |
| 4 | + NormalizedRubyGemsVersion, |
| 5 | + RubyGemsGemResponse, |
| 6 | + RubyGemsOwner, |
| 7 | + RubyGemsVersionItem, |
| 8 | +} from './types' |
| 9 | + |
| 10 | +function nonEmpty(value: string | null | undefined): string | null { |
| 11 | + if (!value) return null |
| 12 | + const trimmed = value.trim() |
| 13 | + return trimmed === '' ? null : trimmed |
| 14 | +} |
| 15 | + |
| 16 | +export function normalizeRubyGemsPackage(doc: RubyGemsGemResponse): NormalizedRubyGemsPackage { |
| 17 | + const licenses = doc.licenses && doc.licenses.length > 0 ? doc.licenses : null |
| 18 | + return { |
| 19 | + description: nonEmpty(doc.info), |
| 20 | + homepage: nonEmpty(doc.homepage_uri), |
| 21 | + declaredRepositoryUrl: nonEmpty(doc.source_code_uri), |
| 22 | + licenses, |
| 23 | + licensesRaw: licenses ? licenses.join(', ') : null, |
| 24 | + latestVersion: nonEmpty(doc.version), |
| 25 | + totalDownloads: doc.downloads ?? 0, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function parseCreatedAt(value: string | undefined): Date | null { |
| 30 | + if (!value) return null |
| 31 | + const date = new Date(value) |
| 32 | + return isNaN(date.getTime()) ? null : date |
| 33 | +} |
| 34 | + |
| 35 | +export function normalizeRubyGemsVersions( |
| 36 | + items: RubyGemsVersionItem[], |
| 37 | +): NormalizedRubyGemsVersion[] { |
| 38 | + return items.map((item) => ({ |
| 39 | + number: item.number, |
| 40 | + publishedAt: parseCreatedAt(item.created_at), |
| 41 | + isPrerelease: item.prerelease ?? false, |
| 42 | + licenses: item.licenses && item.licenses.length > 0 ? item.licenses : null, |
| 43 | + })) |
| 44 | +} |
| 45 | + |
| 46 | +export function pickLatestRubyGemsVersion( |
| 47 | + versions: NormalizedRubyGemsVersion[], |
| 48 | +): NormalizedRubyGemsVersion | null { |
| 49 | + if (versions.length === 0) return null |
| 50 | + const stable = versions.find((v) => !v.isPrerelease) |
| 51 | + return stable ?? versions[0] |
| 52 | +} |
| 53 | + |
| 54 | +export function normalizeRubyGemsOwners(owners: RubyGemsOwner[]): NormalizedRubyGemsOwner[] { |
| 55 | + return owners |
| 56 | + .filter((o): o is RubyGemsOwner & { handle: string } => !!o.handle && o.handle.trim() !== '') |
| 57 | + .map((o) => ({ username: o.handle, email: nonEmpty(o.email) })) |
| 58 | +} |
0 commit comments