Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ jobs:
strategy:
matrix:
node-version:
- 20
- 22
- 24
- 26
env:
# Override Node.js version over mise.toml
MISE_NODE_VERSION: ${{ matrix.node-version }}
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@phanect/utils",
"version": "3.0.0",
"version": "4.0.0",
"description": "@phanect's personal utility library for JavaScript & TypeScript",
"repository": {
"type": "git",
Expand All @@ -27,18 +27,17 @@
}
},
"devDependencies": {
"@phanect/configs": "latest",
"@phanect/configs": "2026.5.19",
"@phanect/lint": "latest",
"@types/node": "^24.7.1",
"eslint": "^9.21.0",
"eslint": "^10.4.0",
"jiti": "^2.4.2",
"tsdown": "0.21.7",
"typescript": "^5.3.3",
"tsdown": "0.22.0",
"typescript": "^6.0.3",
"vitest": "^4.0.4"
},
"engines": {
"node": "^20.11.0 || ^22.16.0 || >=24.0.0",
"pnpm": ">=10.x"
"node": "^22.16.0 || ^24.0.0 || ^26.0.0"
},
"packageManager": "pnpm@10.26.0+sha512.3b3f6c725ebe712506c0ab1ad4133cf86b1f4b687effce62a9b38b4d72e3954242e643190fc51fa1642949c735f403debd44f5cb0edd657abe63a8b6a7e1e402"
"packageManager": "pnpm@11.3.0+sha512.2c403d6594527287672b1f7056343a1f7c3634036a67ffabfcc2b3d7595d843768f8787148d1b57cf7956c90606bbd192857c363af19e96d2d0ec9ec5741d215"
}
5 changes: 2 additions & 3 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
onlyBuiltDependencies:
- esbuild
- unrs-resolver
allowBuilds:
unrs-resolver: true
1 change: 1 addition & 0 deletions src/nodejs.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { loadJSON } from "./nodejs/load-json.ts";
export { npmPackageExists } from "./nodejs/npm-package-exists.ts";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { trimLines } from "./trim-lines.ts";
import { trimLines } from "../universal/trim-lines.ts";

export const npmPackageExists = async (pkgName: string): Promise<boolean> => {
const res = await fetch(`https://registry.npmjs.com/${ pkgName }`);
Expand Down
4 changes: 1 addition & 3 deletions src/universal.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export { deduplicate } from "./universal/deduplicate.ts";
export { trimLines } from "./universal/trim-lines.ts";
export { getLastElementOf } from "./universal/get-last-element-of.ts";
export { npmPackageExists } from "./universal/npm-package-exists.ts";
export { pseudoRandomInt } from "./universal/pseudo-random-int.ts";
export { pseudoRandomPositiveInt } from "./universal/pseudo-random-positive-int.ts";
export { pseudoRandomString } from "./universal/pseudo-random-string.ts";
export { sleep } from "./universal/sleep.ts";
export { sortObjects } from "./universal/sort-objects.ts";
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param max - max number of the generated random number
* @returns pseudo-random non-negative integer number
*/
export const pseudoRandomInt = (max: number): number => {
export const pseudoRandomPositiveInt = (max: number): number => {
if (!Number.isFinite(max) || max < 0) {
throw new RangeError("max must be a finite, non-negative number");
}
Expand Down
4 changes: 2 additions & 2 deletions src/universal/pseudo-random-string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pseudoRandomInt } from "./pseudo-random-int.ts";
import { pseudoRandomPositiveInt } from "./pseudo-random-positive-int.ts";
import { chars } from "./pseudo-random-string-chars.ts";

/**
Expand All @@ -13,7 +13,7 @@ export const pseudoRandomString = (length: number): string => {
}

return Array.from({ length }, () => {
const index = pseudoRandomInt(chars.length - 1);
const index = pseudoRandomPositiveInt(chars.length - 1);
return chars[index];
}).join("");
};
11 changes: 0 additions & 11 deletions src/universal/sleep.test.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/universal/sleep.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/nodejs/tsconfig.json → src/universal/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": [
"@phanect/configs/ts/nodejs",
"@phanect/configs/ts",
"@phanect/configs/ts/addons/npm"
],
"include": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "vitest";
import { deduplicate } from "../universal.ts";
import { deduplicate } from "../src/universal.ts";

test("deduplicate", () => {
const deduplicated = deduplicate([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "vitest";
import { getLastElementOf } from "../universal.ts";
import { getLastElementOf } from "../src/universal.ts";

test("getLastElementOf (without index)", () => {
const lastElement = getLastElementOf([ "a", "b", "c" ]);
Expand Down
4 changes: 2 additions & 2 deletions src/nodejs/load-json.test.ts → test/load-json.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { join } from "node:path";
import { expect, test } from "vitest";
import { loadJSON } from "../nodejs.ts";
import { loadJSON } from "../src/nodejs.ts";

test("loadJSON", async () => {
const obj = await loadJSON(
join(import.meta.dirname, "../../test/fixtures/load-json/fixture.json"),
join(import.meta.dirname, "fixtures/load-json/fixture.json"),
);

expect(obj).toStrictEqual({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { ok } from "node:assert";
import { describe, expect, test } from "vitest";
import { pseudoRandomInt } from "./pseudo-random-int.ts";
import { pseudoRandomPositiveInt } from "../src/universal/pseudo-random-positive-int.ts";

describe("pseudoRandomInt()", () => {
test("returns a number within valid range", () => {
const generatedNumber = pseudoRandomInt(100);
const generatedNumber = pseudoRandomPositiveInt(100);

expect(generatedNumber).toBeLessThanOrEqual(100);
expect(generatedNumber).toBeGreaterThanOrEqual(0);
expect(typeof generatedNumber).toBe("number");
});

test("returns an integer (no decimals)", () => {
const generatedNumber = pseudoRandomInt(100);
const generatedNumber = pseudoRandomPositiveInt(100);
ok(Number.isInteger(generatedNumber));
});

test("handles max value of 0 (returns 0)", () => {
const generatedNumber = pseudoRandomInt(0);
const generatedNumber = pseudoRandomPositiveInt(0);
expect(generatedNumber).toBe(0);
});

test("returns 0 or 1 for max value of 1", () => {
const generatedNumber = pseudoRandomInt(1);
const generatedNumber = pseudoRandomPositiveInt(1);
expect([ 0, 1 ]).toContain(generatedNumber);
});

Expand All @@ -32,7 +32,7 @@ describe("pseudoRandomInt()", () => {

// Generate multiple random numbers to test distribution
for (let i = 0; i < 100; i++) {
const generatedNumber = pseudoRandomInt(max);
const generatedNumber = pseudoRandomPositiveInt(max);
results.add(generatedNumber);
expect(generatedNumber).toBeGreaterThanOrEqual(0);
expect(generatedNumber).toBeLessThanOrEqual(max);
Expand All @@ -47,7 +47,7 @@ describe("pseudoRandomInt()", () => {
const max = 1000000;

for (let i = 0; i < 100; i++) {
const generatedNumber = pseudoRandomInt(max);
const generatedNumber = pseudoRandomPositiveInt(max);
expect(generatedNumber).toBeGreaterThanOrEqual(0);
expect(generatedNumber).toBeLessThanOrEqual(max);
expect(Number.isInteger(generatedNumber)).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { pseudoRandomInt } from "./pseudo-random-int.ts";
import { pseudoRandomString } from "./pseudo-random-string.ts";
import { pseudoRandomPositiveInt } from "../src/universal/pseudo-random-positive-int.ts";
import { pseudoRandomString } from "../src/universal/pseudo-random-string.ts";

describe("pseudoRandomString()", () => {
test("returns a string with the correct length", () => {
Expand Down Expand Up @@ -34,7 +34,7 @@ describe("pseudoRandomString()", () => {
});

test("works with different length values", () => {
for (const length of Array.from({ length: 5 }, () => pseudoRandomInt(500) + 1)) {
for (const length of Array.from({ length: 5 }, () => pseudoRandomPositiveInt(500) + 1)) {
const generatedString = pseudoRandomString(length);
expect(generatedString).toHaveLength(length);
expect(typeof generatedString).toBe("string");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "vitest";
import { sortObjects } from "../universal.ts";
import { sortObjects } from "../src/universal.ts";

test("sortObjects", () => {
const result = sortObjects([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "vitest";
import { trimLines } from "../universal.ts";
import { trimLines } from "../src/universal.ts";

test("deindent", () => {
const before = `
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": [
"@phanect/configs/ts",
"@phanect/configs/ts/nodejs",
"@phanect/configs/ts/addons/npm"
],
"include": [
Expand All @@ -12,6 +12,8 @@
],
"exclude": [
"dist",
"node_modules"
"node_modules",
// Use different tsconfig.json for universal code
"./src/universal/**"
]
}