Skip to content

Commit

Permalink
Add class
Browse files Browse the repository at this point in the history
  • Loading branch information
spookyuser committed Dec 27, 2023
1 parent 1d8e735 commit 169b8c3
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 128 deletions.
10 changes: 0 additions & 10 deletions .editorconfig

This file was deleted.

1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

26 changes: 0 additions & 26 deletions .github/dependabot.yml

This file was deleted.

22 changes: 2 additions & 20 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,21 @@ name: Node.js

on:
push:
pull_request:
schedule:
# Check if it works with current dependencies
- cron: '42 2 * * 6' # weekly on Saturday 2:42 UTC

jobs:
test:
name: Node.js
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/setup-node@v4
with:
node-version: 'lts/*'
- uses: actions/checkout@v4
- run: npm install
- run: npm test
- run: npm pack

publish:
name: Publish on NPM
if: startsWith(github.ref, 'refs/tags/v')
runs-on: ubuntu-latest
needs: test
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 'lts/*'
registry-url: 'https://registry.npmjs.org'
node-version: "lts/*"
registry-url: "https://registry.npmjs.org"
- run: npm install
- run: npm publish
env:
Expand Down
13 changes: 13 additions & 0 deletions .prettierrc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @type {import('prettier').Options}
*/
export default {
printWidth: 80,
tabWidth: 2,
useTabs: false,
semi: false,
singleQuote: false,
trailingComma: "none",
bracketSpacing: true,
bracketSameLine: true,
};
4 changes: 1 addition & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
MIT License

Copyright (c) EdJoPaTo <typescript-node-module-boilerplate-npm-package@edjopato.de> (https://edjopato.de)
Copyright 2023 spookyuser

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
23 changes: 0 additions & 23 deletions copy-template-into-existing-project.sh

This file was deleted.

42 changes: 26 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
{
"name": "typescript-node-module-boilerplate",
"version": "0.0.0",
"description": "",
"name": "web-console-progress-bar",
"version": "1.0.0",
"description": "A progress bar for the browser console.",
"keywords": [
""
"progress",
"bar",
"console",
"browser",
"web",
"cli"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/EdJoPaTo/typescript-node-module-boilerplate.git"
"url": "https://github.com/spookyuser/web-console-progress-bar.git"
},
"author": {
"name": "EdJoPaTo",
"email": "typescript-node-module-boilerplate-npm-package@edjopato.de",
"url": "https://edjopato.de"
"name": "spookyuser",
"email": "sfcz1wps@duck.com",
"url": "https://github.com/spookyuser"
},
"scripts": {
"build": "del-cli dist && tsc",
"prepack": "npm run build",
"test": "tsc --sourceMap && xo && c8 --all node --test --enable-source-maps"
"test": "tsc --sourceMap && xo"
},
"type": "module",
"engines": {
"node": ">=14"
},
"dependencies": {},
"targets": {
"default": {
"engines": {
"browsers": ">= 50%"
}
}
},
"devDependencies": {
"@sindresorhus/tsconfig": "^5.0.0",
"@types/node": "^18.18.13",
"c8": "^8.0.1",
"del-cli": "^5.0.0",
"typescript": "^5.0.2",
"del-cli": "^5.1.0",
"typescript": "^5.3.3",
"xo": "^0.56.0"
},
"files": [
"dist",
"!*.test.*",
"!test.*"
],
"main": "./dist/index.js",
"browser": "./dist/index.js",
"types": "./dist/index.d.ts",
"xo": {
"rules": {
"@typescript-eslint/prefer-readonly-parameter-types": "warn"
},
"prettier": true,
"overrides": [
{
"files": [
Expand All @@ -56,4 +66,4 @@
}
]
}
}
}
66 changes: 60 additions & 6 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
export function moduleName(input: string | undefined): string {
if (typeof input !== 'string' && input !== undefined) {
throw new TypeError(`Expected a string, got ${typeof input}`);
}
/**
* Represents a console progress bar that tracks the progress of a task.
*/
export class ConsoleProgressBar {
current: number
startTime: number
updateInterval: number
lastUpdate: number

const sanetized = input?.trim() ?? 'Glitter';
return sanetized + ' & rainbows';
/**
* Creates a new instance of ConsoleProgressBar.
* @param total The total number of units to be processed.
* @param updateInterval The interval at which the progress bar should be updated, in milliseconds. Default is 2000ms.
*/
constructor(
public total: number,
updateInterval = 2000
) {
this.current = 0
this.startTime = performance.now()
this.updateInterval = updateInterval
this.lastUpdate = 0
}

/**
* Updates the progress bar with the current value.
* @param currentValue The current value of the progress.
*/
update(currentValue: number) {
const now = performance.now()
if (now - this.lastUpdate < this.updateInterval) {
return
}

this.current = currentValue
const percentage = (this.current / this.total) * 100
const elapsedTime = now - this.startTime
const estimatedTotalTime = (elapsedTime / this.current) * this.total
const eta = (estimatedTotalTime - elapsedTime) / 1000 // Seconds

console.clear()
console.log(
`Progress: [${this.renderBar(
percentage
)}] ${percentage}% ETA: ${eta.toFixed(2)}s`
)

this.lastUpdate = now
}

/**
* Renders the progress bar based on the given percentage.
* @param percentage The percentage of progress completed.
* @returns The rendered progress bar.
*/
private renderBar(percentage: number) {
const barLength = 20
const filledLength = Math.round((barLength * percentage) / 100)
const bar = "█".repeat(filledLength) + "░".repeat(barLength - filledLength)
return bar
}
}
14 changes: 0 additions & 14 deletions source/test.ts

This file was deleted.

16 changes: 7 additions & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"extends": "@sindresorhus/tsconfig/tsconfig.json",
"include": [
"source"
],
"compilerOptions": {
"lib": ["ES2020"],
"target": "ES2020", // Node.js 14
"outDir": "dist"
}
"extends": "@sindresorhus/tsconfig/tsconfig.json",
"include": ["source"],
"compilerOptions": {
"target": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"outDir": "dist"
}
}

0 comments on commit 169b8c3

Please sign in to comment.