Skip to content

stringifyDependencies

Writes dependency groups back as a field value, the inverse of parseDependencies; the type of groups is that of format.

'debian' — the canonical spacing of the Debian Policy Manual 7.1: , between entries, | between alternatives, a space before each (op version), [archs] and <profiles>. 'rpm' — , between entries, name op version, and (a or b) for alternatives. 'arch' — entries separated by a space, name>=version. 'alpine' — the same, with a leading ! for a conflict. 'pep508' — one requirement per line, name[extras]>=1,<2 ; marker. The formats without alternatives ('arch', 'alpine', 'pep508') throw a RangeError for a group of several.

parseDependencies(stringifyDependencies(groups, f), f) gives groups back, and stringifying a parsed field normalises its whitespace. An empty group writes nothing, so it does not survive a round trip.

Available since v3.1.10

import { stringifyDependencies } from '@helpers4/package';
// or, from the all-in-one package (same code, one install):
import { stringifyDependencies } from 'helpers4/package';
stringifyDependencies<Format extends keyof DependencyFormats>(groups: readonly DependencyGroup<DependencyOf<NoInfer<Format>>>[], format: Format): string
ParameterTypeDescription
groupsreadonly DependencyGroup<DependencyOf<NoInfer<Format>>>[]The groups of alternatives, as returned by `parseDependencies`
formatFormatHow to write the field

string — The field value; '' for []

Entries are joined with ”, ” and alternatives with ” | ”.

stringifyDependencies([
  [{ name: 'libc6', version: { operator: '>=', version: '2.34' } }],
  [{ name: 'python3', arch: 'any' }, { name: 'python3' }],
], 'debian')
// => 'libc6 (>= 2.34), python3:any | python3'

Parse, change the groups, write them back in canonical spacing.

const groups = parseDependencies('libc6 (>=2.34) ,\n  foo [ amd64 ]', 'debian');
groups.push([{ name: 'bar', profiles: [['!nocheck']] }]);
stringifyDependencies(groups, 'debian')
// => 'libc6 (>= 2.34), foo [amd64], bar <!nocheck>'

The same function writes RPM, Arch, Alpine and PEP 508 fields, each from its own dependency type.

stringifyDependencies([[{ name: 'musl', version: { operator: '>=', version: '1.2' } }], [{ name: 'openrc', conflict: true }]], 'alpine')
// => 'musl>=1.2 !openrc'

For each supported format, the shape of one dependency and the options its parser takes. This is what makes parseDependencies and stringifyDependencies type-safe per format.

interface DependencyFormats {
  alpine: object;
  arch: object;
  debian: object;
  pep508: object;
  rpm: object;
}

The alternatives (a | b) that satisfy one entry of a dependency field. A format without alternatives ('arch', 'alpine', 'pep508') always has a single one.

type DependencyGroup<Dependency> = Dependency[]

The dependency type of a format.

type DependencyOf<Format extends DependencyFormat> = DependencyFormats[Format]['dependency']

The formats of dependency fields: 'debian', 'rpm', 'arch', 'alpine' and 'pep508'.

type DependencyFormat = keyof DependencyFormats

View source on GitHub