Extracting Components
Relevant source files
The following files were used as context for generating this wiki page:
Extracting components is a core capability of the node-semver library, allowing developers to isolate specific parts of a Semantic Versioning string, such as the major, minor, or patch versions, as well as prerelease identifiers and build metadata. This process relies on a robust parsing engine that validates input strings against the SemVer 2.0.0 specification before decomposition.
The system utilizes specialized functions and a central SemVer class to provide both functional and object-oriented interfaces for data extraction. These operations are critical for version comparison logic, range satisfaction checks, and automated version bumping. Sources: semver.js:24, index.js:12-16
Core Extraction Functions
The library provides several top-level functions designed for quick extraction of specific version segments. Most of these functions serve as wrappers that instantiate a SemVer object and return the requested property.
Primary Extraction API
| Function | Extracted Component | Return Type | Description |
|---|---|---|---|
major(v, options) | Major Version | Number | Returns the first numeric identifier (X.0.0). |
minor(v, options) | Minor Version | Number | Returns the second numeric identifier (0.X.0). |
patch(v, options) | Patch Version | Number | Returns the third numeric identifier (0.0.X). |
prerelease(v, options) | Prerelease Tags | Array | Returns an array of dot-separated prerelease identifiers, or null. |
Sources: functions/major.js:5, functions/minor.js:5, functions/patch.js:5, functions/prerelease.js:12
Extraction Logic Flow
The following diagram illustrates the typical flow when calling an extraction function like major().
The SemVer Class Extraction Architecture
The SemVer class serves as the primary data structure for held version information. When a string is parsed into a SemVer instance, the constructor decomposes the string into several internal properties.
Component Properties
Upon successful instantiation, the following properties are made available:
- major: The major version number. Sources: classes/semver.js:51
- minor: The minor version number. Sources: classes/semver.js:52
- patch: The patch version number. Sources: classes/semver.js:53
- prerelease: An array of prerelease identifiers, with numeric segments converted to actual numbers for proper comparison. Sources: classes/semver.js:68-79
- build: An array of build metadata identifiers. Sources: classes/semver.js:82
- version: A formatted string containing
major.minor.patch(and prerelease if applicable). Sources: classes/semver.js:86-91
Data Extraction Sequence
This sequence diagram shows how the SemVer constructor extracts components using internal regex tokens.
Technical Implementation Details
Numeric Boundaries
During extraction, the library enforces safety limits defined in internal/constants.js. Components are validated against MAX_SAFE_INTEGER (9007199254740991). If an extracted component exceeds this value, the constructor throws a TypeError. Sources: internal/constants.js:7, classes/semver.js:55-65
Prerelease and Build Splitting
Prerelease and build identifiers are extracted by splitting the captured strings from the regular expression by the dot (.) character.
this.prerelease = m[4].split('.').map((id) => {
if (/^[0-9]+$/.test(id)) {
const num = +id
if (num >= 0 && num < MAX_SAFE_INTEGER) {
return num
}
}
return id
})
this.build = m[5] ? m[5].split('.') : []
Sources: classes/semver.js:69-82
Regex Capturing Groups
The extraction mechanism relies on specific capturing groups within the FULL and LOOSE regular expressions defined in internal/re.js:
- Group 1: Major version Sources: internal/re.js:74
- Group 2: Minor version Sources: internal/re.js:75
- Group 3: Patch version Sources: internal/re.js:76
- Group 4: Prerelease string (everything after the first
-) Sources: internal/re.js:101 - Group 5: Build string (everything after the
+) Sources: internal/re.js:111
Conclusion
Extracting components in node-semver is a multi-step process involving normalization, regex pattern matching, and type conversion. By centralizing this logic within the SemVer class, the library ensures that all derived information—whether a single integer via major() or a complex array via prerelease()—is consistent and compliant with the Semantic Versioning specification.