Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ describe("node-version", () => {
expect(typeof v.build).toBe("string");
});

test("object should have exactly 16 properties", () => {
expect(Object.keys(version)).toHaveLength(16);
expect(Object.keys(getVersion())).toHaveLength(16);
test("object should have exactly 17 properties (including toJSON)", () => {
expect(Object.keys(version)).toHaveLength(17);
expect(Object.keys(getVersion())).toHaveLength(17);
});

test("original property should start with v", () => {
Expand Down Expand Up @@ -139,6 +139,25 @@ describe("node-version", () => {
expect(String(version)).toBe(version.original);
});

test("toJSON should return pure object representation", () => {
const v = getVersion();
const json = v.toJSON();

expect(json).toHaveProperty("original");
expect(json).toHaveProperty("major");
// Ensure functions are stripped
expect(json).not.toHaveProperty("isAtLeast");
expect(json).not.toHaveProperty("toJSON");

// Verify round-trip via JSON.stringify
const str = JSON.stringify(v);
const parsed = JSON.parse(str);
expect(parsed.original).toBe(v.original);
expect(parsed.major).toBe(v.major);
// Functions should be missing in parsed object
expect(parsed.isAtLeast).toBeUndefined();
});

describe("robustness", () => {
test("should handle malformed version string", () => {
mockVersion.node = "10";
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,21 @@ export const getVersion = (): NodeVersion => {
return 0;
};

return {
const versionObject = {
original: `v${nodeVersion}`,
short: `${split[0] || "0"}.${split[1] || "0"}`,
long: nodeVersion,
major: major,
minor: split[1] || "0",
build: split[2] || "0",
isLTS: !!release.lts,
ltsName: String(release.lts || "") || undefined,
isEOL: checkEOL(major),
eolDate: eolString ? new Date(eolString) : undefined,
};

return {
...versionObject,
isAtLeast: (version: string): boolean => {
return compareTo(version) >= 0;
},
Expand All @@ -104,11 +112,8 @@ export const getVersion = (): NodeVersion => {
isAtMost: (version: string): boolean => {
return compareTo(version) <= 0;
},
isLTS: !!release.lts,
ltsName: String(release.lts || "") || undefined,
isEOL: checkEOL(major),
eolDate: eolString ? new Date(eolString) : undefined,
toString: () => `v${nodeVersion}`,
toJSON: () => versionObject,
};
};

Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ export interface NodeVersion {
* Returns the original version string.
*/
toString(): string;
/**
* Returns a JSON representation of the version object.
* This method allows `JSON.stringify(version)` to work correctly.
*/
toJSON(): Omit<NodeVersion, "isAtLeast" | "is" | "isAbove" | "isBelow" | "isAtMost" | "toString" | "toJSON">;
}