Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor with TypeScript #5

Merged
merged 1 commit into from
Dec 12, 2024
Merged

feat: refactor with TypeScript #5

merged 1 commit into from
Dec 12, 2024

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Dec 12, 2024

BREAKING CHANGE: drop Node.js < 14 support

eggjs/egg#5257

Summary by CodeRabbit

  • New Features

    • Introduced a new function ms for converting human-readable time formats into milliseconds.
    • Added multiple GitHub Actions workflows for CI, testing, and publishing automation.
    • Updated TypeScript configuration for improved type safety.
  • Bug Fixes

    • Removed outdated configuration files that could lead to linting errors or CI issues.
  • Documentation

    • Updated README.md with new CI/CD references, installation instructions, and contributor acknowledgments.
    • Added a license section with the MIT License.
  • Chores

    • Updated package.json with new dependencies and scripts for linting and testing.

BREAKING CHANGE: drop Node.js < 14 support

eggjs/egg#5257
Copy link

coderabbitai bot commented Dec 12, 2024

Walkthrough

The changes in this pull request involve the removal of several configuration and source files, along with the introduction of new GitHub Actions workflows and TypeScript configurations. The .editorconfig, .jshintignore, .jshintrc, .travis.yml, and Makefile have been deleted, eliminating various coding standards and CI configurations. A new ESLint configuration file and multiple GitHub Actions workflows have been added to manage CI processes for different Node.js versions. Additionally, TypeScript support has been introduced with a new configuration file and a new function in the source code.

Changes

File/Path Change Summary
.editorconfig Deleted; defined coding style guidelines for the project.
.eslintrc New file; extends existing ESLint configurations for TypeScript and Node.js module prefix rules.
.github/workflows/nodejs-14.yml New workflow; automates CI for Node.js version 14.
.github/workflows/nodejs.yml New workflow; targets multiple Node.js versions for CI.
.github/workflows/pkg.pr.new.yml New workflow; automates package publishing on code changes.
.github/workflows/release.yml New workflow; automates release processes using GitHub Actions.
.gitignore Modified; added entries to ignore specific files and directories.
.jshintignore Deleted; contained directories excluded from JSHint linting.
.jshintrc Deleted; contained JSHint configuration settings.
.travis.yml Deleted; contained Travis CI configuration for Node.js.
LICENSE Updated; introduced MIT License with copyright statements.
Makefile Deleted; contained targets for managing tests and code quality checks.
README.md Modified; updated CI badges, installation instructions, and examples.
index.js Deleted; contained the main function for humanizing time durations.
package.json Modified; updated main entry, added module entry, and revised dependencies and scripts.
src/index.ts New file; introduced ms function for converting human-readable time formats.
test/benchmark/ms.cjs New file; benchmarking suite for performance testing of the ms function.
test/benchmark/ms.js Deleted; contained a benchmarking suite for comparing performance of time duration parsing.
test/index.test.js Deleted; contained unit tests for the humanize-ms module.
test/index.test.ts New file; introduced unit tests for the ms function.
tsconfig.json New file; TypeScript configuration extending from @eggjs/tsconfig.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CI
    participant Node
    participant Repo

    User->>Repo: Push changes
    Repo->>CI: Trigger CI workflow
    CI->>Node: Setup Node.js
    CI->>Node: Install dependencies
    CI->>Node: Run tests
    CI->>Repo: Publish package
    CI->>Repo: Release new version
Loading

🐰 "In the burrow, changes abound,
New workflows and config found.
With TypeScript now in play,
Our coding style's on display!
Goodbye to the old, hello to the new,
Hopping forward, we’ll see it through!" 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/benchmark@2.1.4 eval +1 1.5 MB jdalton

🚮 Removed packages: npm/autod@3.1.2, npm/benchmark@1.0.0, npm/istanbul@0.4.5, npm/mocha@11.0.2, npm/should@13.2.3

View full report↗︎

Copy link

pkg-pr-new bot commented Dec 12, 2024

Open in Stackblitz

npm i https://pkg.pr.new/node-modules/humanize-ms@5

commit: 5b89f78

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (7)
src/index.ts (2)

6-6: Consider adding more specific type definitions

The parameter type number | string could be more specific to better document the expected string format (e.g., '1s', '1m', '1h').

-export function ms(t: number | string) {
+type TimeString = `${number}${'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'y'}`;
+export function ms(t: number | TimeString) {

3-5: Enhance function documentation

The current JSDoc comment could be more descriptive with parameter types, return type, and examples.

 /**
- * transform humanize time to ms
+ * Transforms a human-readable time string or number to milliseconds
+ * @param t - Time in milliseconds or a string like '1s', '1m', '1h'
+ * @returns The time in milliseconds
+ * @throws {TypeError} When the input format is invalid
+ * @example
+ * ms('2h')  // returns 7200000
+ * ms(2000)  // returns 2000
  */
.github/workflows/nodejs-14.yml (1)

1-8: Consider adding test coverage and artifact upload

The workflow could benefit from additional steps to track test coverage and preserve test results.

 name: Node.js 14 CI
 
 on:
   push:
     branches: [ master ]
   pull_request:
     branches: [ master ]
+    paths:
+      - '**.ts'
+      - 'package*.json'
+      - '.github/workflows/nodejs-14.yml'

Consider adding these steps after running tests:

      - name: Upload test coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage/
test/index.test.ts (2)

7-8: Enhance type safety in test assertions

Consider using TypeScript's type assertions to make the tests more type-safe.

-      assert.equal(ms(1000), 1000);
+      assert.equal(ms(1000), 1000 as number);

19-23: Consider testing error handling more thoroughly

The invalid string test case could be expanded to cover more edge cases.

Consider adding more test cases:

it('should handle various invalid inputs', () => {
  assert.equal(ms(''), undefined);
  assert.equal(ms('invalid'), undefined);
  assert.equal(ms('1x'), undefined);
  assert.equal(ms('ms'), undefined);
});
README.md (1)

28-33: Consider documenting both import styles

While the TypeScript/ESM import is now the primary method, consider documenting the CommonJS require syntax for users who haven't migrated to ESM yet:

// ESM / TypeScript
import { ms } from 'humanize-ms';

// CommonJS
const { ms } = require('humanize-ms');
package.json (1)

47-66: LGTM: Proper dual package hazard handling

The export configuration correctly handles both ESM and CommonJS formats. Consider adding tests to verify both import methods work as expected:

// test/esm.test.ts
import { ms } from 'humanize-ms';

// test/commonjs.test.ts
const { ms } = require('humanize-ms');
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d9cdffe and 5b89f78.

📒 Files selected for processing (21)
  • .editorconfig (0 hunks)
  • .eslintrc (1 hunks)
  • .github/workflows/nodejs-14.yml (1 hunks)
  • .github/workflows/nodejs.yml (1 hunks)
  • .github/workflows/pkg.pr.new.yml (1 hunks)
  • .github/workflows/release.yml (1 hunks)
  • .gitignore (1 hunks)
  • .jshintignore (0 hunks)
  • .jshintrc (0 hunks)
  • .travis.yml (0 hunks)
  • LICENSE (1 hunks)
  • Makefile (0 hunks)
  • README.md (1 hunks)
  • index.js (0 hunks)
  • package.json (1 hunks)
  • src/index.ts (1 hunks)
  • test/benchmark/ms.cjs (1 hunks)
  • test/benchmark/ms.js (0 hunks)
  • test/index.test.js (0 hunks)
  • test/index.test.ts (1 hunks)
  • tsconfig.json (1 hunks)
💤 Files with no reviewable changes (8)
  • .travis.yml
  • .jshintignore
  • index.js
  • test/benchmark/ms.js
  • .editorconfig
  • test/index.test.js
  • Makefile
  • .jshintrc
✅ Files skipped from review due to trivial changes (5)
  • LICENSE
  • .eslintrc
  • .gitignore
  • .github/workflows/nodejs.yml
  • tsconfig.json
🧰 Additional context used
🪛 LanguageTool
README.md

[style] ~3-~3: Using many exclamation marks might seem excessive (in this case: 3 exclamation marks for a text that’s 446 characters long)
Context: ...s [![NPM version][npm-image]][npm-url] Node.js CI [![Test coverage][codecov-image]][codecov-url] [![Known Vulnerabilities][snyk-image]][sny...

(EN_EXCESSIVE_EXCLAMATION)

🪛 eslint
test/benchmark/ms.cjs

[error] 1-1: Require statement not part of import statement.

(@typescript-eslint/no-var-requires)


[error] 2-2: Require statement not part of import statement.

(@typescript-eslint/no-var-requires)


[error] 3-3: Require statement not part of import statement.

(@typescript-eslint/no-var-requires)


[error] 4-4: Require statement not part of import statement.

(@typescript-eslint/no-var-requires)

🔇 Additional comments (9)
.github/workflows/release.yml (1)

3-13: Verify branch protection and secret configuration

The workflow looks good but requires verification of:

  1. Branch protection rules for master to ensure proper release control
  2. Required secrets (NPM_TOKEN, GIT_TOKEN) are properly configured
.github/workflows/pkg.pr.new.yml (1)

12-16: LGTM: Node.js version aligns with PR objectives

The use of Node.js 20 is appropriate given the PR's objective to drop support for Node.js versions < 14.

test/benchmark/ms.cjs (1)

38-40: LGTM: Performance improvements verified

The benchmark results show that the TypeScript implementation maintains or improves performance compared to the original:

  • Similar performance for string parsing (ms() vs origin ms())
  • Significantly better performance for number handling (ms(1000))
README.md (3)

1-16: LGTM: CI/CD modernization

The badge updates appropriately reflect the migration to GitHub Actions and modern tooling.

🧰 Tools
🪛 LanguageTool

[style] ~3-~3: Using many exclamation marks might seem excessive (in this case: 3 exclamation marks for a text that’s 446 characters long)
Context: ...s [![NPM version][npm-image]][npm-url] Node.js CI [![Test coverage][codecov-image]][codecov-url] [![Known Vulnerabilities][snyk-image]][sny...

(EN_EXCESSIVE_EXCLAMATION)


22-24: LGTM: Improved installation instructions

The simplified installation command enhances readability.


37-43: LGTM: Enhanced project documentation

The addition of the contributors section and updated license reference improve the project documentation.

package.json (3)

40-46: LGTM: Comprehensive development workflow

The new scripts properly support TypeScript development workflow with linting, testing, and CI integration.


67-73: LGTM: Proper TypeScript package configuration

The package files configuration correctly includes all necessary files and properly sets up type definitions.


22-39: Verify ms package update implications

The update of ms from ^2.0.0 to ^2.1.3 should be verified for:

  1. Breaking changes in the API
  2. Security implications
✅ Verification successful

Let me check the usage of the ms package in the codebase to understand the impact of the update.


Update to ms@^2.1.3 is safe to proceed

The update from ^2.0.0 to ^2.1.3 is safe because:

  1. No security vulnerabilities affect versions >= 2.0.0 (latest advisory shows vulnerabilities only for < 2.0.0)
  2. The codebase usage aligns with ms's stable API:
    • Only using basic time string parsing (ms('1s')) and number passthrough (ms(1000))
    • All test cases pass with expected behavior
  3. It's a minor version bump (2.0.x → 2.1.x) following semver, indicating no breaking changes
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check ms package changelog and security advisories
gh api graphql -f query='
{
  securityVulnerabilities(first: 5, ecosystem: NPM, package: "ms") {
    nodes {
      advisory {
        summary
        severity
        publishedAt
      }
      vulnerableVersionRange
      firstPatchedVersion {
        identifier
      }
    }
  }
}'

Length of output: 771


Script:

#!/bin/bash
# Search for ms package usage
rg -l "require\(['\"](ms)['\"]" || rg -l "from ['\"](ms)['\"]"

# Check package-lock.json for current version
if [ -f "package-lock.json" ]; then
    cat package-lock.json | jq '.packages["node_modules/ms"]'
fi

# Search for direct ms() function calls or time string parsing
rg "ms\(" -A 2

Length of output: 1632

src/index.ts Show resolved Hide resolved
.github/workflows/nodejs-14.yml Show resolved Hide resolved
.github/workflows/pkg.pr.new.yml Show resolved Hide resolved
test/benchmark/ms.cjs Show resolved Hide resolved
package.json Show resolved Hide resolved
@fengmk2 fengmk2 merged commit 14964af into master Dec 12, 2024
13 of 14 checks passed
fengmk2 pushed a commit that referenced this pull request Dec 12, 2024
[skip ci]

## 1.0.0 (2024-12-12)

### ⚠ BREAKING CHANGES

* drop Node.js < 14 support

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a new function `ms` for converting human-readable time
formats into milliseconds.
- Added multiple GitHub Actions workflows for CI, testing, and
publishing automation.
	- Updated TypeScript configuration for improved type safety.

- **Bug Fixes**
- Removed outdated configuration files that could lead to linting errors
or CI issues.

- **Documentation**
- Updated `README.md` with new CI/CD references, installation
instructions, and contributor acknowledgments.
	- Added a license section with the MIT License.

- **Chores**
- Updated `package.json` with new dependencies and scripts for linting
and testing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

### Features

* **index.js:** warn when result is undefined ([b89dae5](b89dae5))
* refactor with TypeScript ([#5](#5)) ([14964af](14964af))

### Bug Fixes

* package.json to reduce vulnerabilities ([#3](#3)) ([3b6f1ba](3b6f1ba))
fengmk2 pushed a commit that referenced this pull request Dec 12, 2024
[skip ci]

* drop Node.js < 14 support

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

- **New Features**
- Introduced a new function `ms` for converting human-readable time
formats into milliseconds.
- Added multiple GitHub Actions workflows for CI, testing, and
publishing automation.
	- Updated TypeScript configuration for improved type safety.

- **Bug Fixes**
- Removed outdated configuration files that could lead to linting errors
or CI issues.

- **Documentation**
- Updated `README.md` with new CI/CD references, installation
instructions, and contributor acknowledgments.
	- Added a license section with the MIT License.

- **Chores**
- Updated `package.json` with new dependencies and scripts for linting
and testing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

* **index.js:** warn when result is undefined ([b89dae5](b89dae5))
* refactor with TypeScript ([#5](#5)) ([14964af](14964af))

* package.json to reduce vulnerabilities ([#3](#3)) ([3b6f1ba](3b6f1ba))
fengmk2 pushed a commit that referenced this pull request Dec 12, 2024
[skip ci]

* drop Node.js < 14 support

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

- **New Features**
- Introduced a new function `ms` for converting human-readable time
formats into milliseconds.
- Added multiple GitHub Actions workflows for CI, testing, and
publishing automation.
	- Updated TypeScript configuration for improved type safety.

- **Bug Fixes**
- Removed outdated configuration files that could lead to linting errors
or CI issues.

- **Documentation**
- Updated `README.md` with new CI/CD references, installation
instructions, and contributor acknowledgments.
	- Added a license section with the MIT License.

- **Chores**
- Updated `package.json` with new dependencies and scripts for linting
and testing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

* **index.js:** warn when result is undefined ([b89dae5](b89dae5))
* refactor with TypeScript ([#5](#5)) ([14964af](14964af))

* package.json to reduce vulnerabilities ([#3](#3)) ([3b6f1ba](3b6f1ba))
fengmk2 pushed a commit that referenced this pull request Dec 12, 2024
[skip ci]

## 1.0.0 (2024-12-12)

### ⚠ BREAKING CHANGES

* drop Node.js < 14 support

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a new function `ms` for converting human-readable time
formats into milliseconds.
- Added multiple GitHub Actions workflows for CI, testing, and
publishing automation.
	- Updated TypeScript configuration for improved type safety.

- **Bug Fixes**
- Removed outdated configuration files that could lead to linting errors
or CI issues.

- **Documentation**
- Updated `README.md` with new CI/CD references, installation
instructions, and contributor acknowledgments.
	- Added a license section with the MIT License.

- **Chores**
- Updated `package.json` with new dependencies and scripts for linting
and testing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

### Features

* **index.js:** warn when result is undefined ([b89dae5](b89dae5))
* refactor with TypeScript ([#5](#5)) ([14964af](14964af))

### Bug Fixes

* package.json to reduce vulnerabilities ([#3](#3)) ([3b6f1ba](3b6f1ba))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant