Skip to content

Commit

Permalink
🔥 chore(consolji.ts): remove unused time functions
Browse files Browse the repository at this point in the history
The time functions were not being used and were removed to improve code readability and maintainability.

🔥 refactor(types.ts): remove unused time-related functions from ConsoljiOptions interface
The time, timeLog, and timeEnd functions were not being used in the codebase and were therefore removed to improve code readability and maintainability.
  • Loading branch information
nyxb committed May 12, 2023
1 parent d13b063 commit 9c0e98d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 101 deletions.
File renamed without changes
176 changes: 89 additions & 87 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
<p align="center">
<img src="https://github.com/nyxb/consolji/blob/main/assets/bg_black.png#gh-light-mode-only">
<img src="https://github.com/nyxb/consolji/blob/main/assets/bg_light.png#gh-dark-mode-only">
</p>
[![cover][cover-src]][cover-href]
[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![bundle][bundle-src]][bundle-href]
[![License][license-src]][license-href]

<a href="https://www.npmjs.com/package/@nyxb/eslint-config"><img src="https://img.shields.io/npm/v/@nyxb/eslint-config.svg?style=flat&colorA=18181B&colorB=14F195" alt="Version"></a>
<a href="https://www.npmjs.com/package/consolji"><img src="https://img.shields.io/bundlephobia/min/consolji?style=flat&colorA=18181B&colorB=14F195">
> 🧙‍♂️ Magical Console Wrapper with Conventional💙Commits
---
## 🤔 Why Consolji?

> Magical Console Wrapper with Conventional💙Commits
## Why Consolji?

Consolji's Enchanting Features:
🌟 Consolji's Enchanting Features:

&nbsp; Effortless to use</br>
🎩&nbsp; Captivating output with graceful fallbacks</br>
Expand All @@ -29,34 +25,26 @@ Consolji's Enchanting Features:



## Installation
## 💻 Installation:
Using [nyxi](https://github.com/nyxb/nyxi)

```bash
# nyxi
nyxi consolji
```

Using npm:
# pnpm
pnpm add consolji

```bash
# npm
npm i consolji
```

Using yarn:

```bash
# yarn
yarn add consolji
```

Using pnpm:

```bash
pnpm add consolji
```
## 🚀 Getting Started

## Getting Started

```js
```ts
// ESM
import { consolji, createConsolji } from 'consolji'

Expand All @@ -73,102 +61,102 @@ await consolji.prompt('Deploy to the production?', {
})
```

Will display in the terminal:
🖥️ Will display in the terminal:

<img width="760" alt="image" src="assets/example.png">
<img width="760" alt="image" src=".github/assets/example.png">

You can use smaller core builds without fancy reporter to save 80% of the bundle size:
📦 You can use smaller core builds without fancy reporter to save 80% of the bundle size:

```ts
import { consolji, createconsolji } from 'consolji/basic'
import { consolji, createconsolji } from 'consolji/browser'
import { createconsolji } from 'consolji/core'
```

## consolji Methods
## 📚 consolji Methods

#### `<type>(logObject)` `<type>(args...)`
#### 📝 `<type>(logObject)` 📝 `<type>(args...)`

Log to all reporters.

Example: `consolji.info('Message')`

#### `await prompt(message, { type })`
#### `await prompt(message, { type })`

Show an input prompt. Type can either of `text`, `confirm`, `select` or `multiselect`.
🔠 Show an input prompt. Type can be one of `text`, `confirm`, `select`, or `multiselect`.

See [examples/prompt.ts](./examples/prompt.ts) for usage examples.
See [📂 examples/prompt.ts](./examples/prompt.ts) for usage examples.

#### `addReporter(reporter)`
#### `addReporter(reporter)`

- Aliases: `add`
- Aliases: `add`

Register a custom reporter instance.

#### `removeReporter(reporter?)`
#### `removeReporter(reporter?)`

- Aliases: `remove`, `clear`
- Aliases: `remove`, `clear`

Remove a registered reporter.

If no arguments are passed all reporters will be removed.

#### `setReporters(reporter|reporter[])`
#### 🔄 `setReporters(reporter|reporter[])`

Replace all reporters.

#### `create(options)`
#### 🔧 `create(options)`

Create a new `consolji` instance and inherit all parent options for defaults.

#### `withDefaults(defaults)`
#### 🛠️ `withDefaults(defaults)`

Create a new `consolji` instance with provided defaults

#### `withTag(tag)`
#### 🏷️ `withTag(tag)`

- Aliases: `withScope`
- Aliases: 🏷️ `withScope`

Create a new `consolji` instance with that tag.

#### `wrapConsole()` `restoreConsole()`
#### 🔄 `wrapConsole()` `restoreConsole()`

Globally redirect all `console.log`, etc calls to consolji handlers.

#### `wrapStd()` `restoreStd()`
#### 🔄 `wrapStd()` `restoreStd()`

Globally redirect all stdout/stderr outputs to consolji.

#### `wrapAll()` `restoreAll()`
#### 🔄 `wrapAll()` `restoreAll()`

Wrap both, std and console.

console uses std in the underlying so calling `wrapStd` redirects console too.
Benefit of this function is that things like `console.info` will be correctly redirected to the corresponding type.

#### `pauseLogs()` `resumeLogs()`
#### ⏸️ `pauseLogs()` ▶️ `resumeLogs()`

- Aliases: `pause`/`resume`
- Aliases: ⏸️ `pause`/▶️ `resume`

**Globally** pause and resume logs.
**Globally** ⏸️ pause and ▶️ resume logs.

consolji will enqueue all logs when paused and then sends them to the reported when resumed.

#### `mockTypes`
#### 🃏 `mockTypes`

- Aliases: `mock`
- Aliases: 🃏 `mock`

Mock all types. Useful for using with tests.

The first argument passed to `mockTypes` should be a callback function accepting `(typeName, type)` and returning the mocked value:

```js
```ts
consolji.mockTypes((typeName, type) => jest.fn())
```

Please note that with the example above, everything is mocked independently for each type. If you need one mocked fn create it outside:

```js
```ts
const fn = jest.fn()
consolji.mockTypes(() => fn)
```
Expand All @@ -177,20 +165,19 @@ If callback function returns a _falsy_ value, that type won't be mocked.

For example if you just need to mock `consolji.fatal`:

```js
```ts
consolji.mockTypes(typeName => typeName === 'fatal' && jest.fn())
```

**NOTE:** Any instance of consolji that inherits the mocked instance, will apply provided callback again.
This way, mocking works for `withTag` scoped loggers without need to extra efforts.
**NOTE:** Any instance of 🃏 consolji that inherits the mocked instance will apply the provided callback again. This way, mocking works for 🏷️ `withTag` scoped loggers without the need for extra efforts.

## Custom Reporters
## 📝 Custom Reporters

consolji ships with 3 built-in reporters out of the box. A fancy colored reporter by default and fallsback to a basic reporter if running in a testing or CI environment detected using [unjs/std-env](https://github.com/unjs/std-env) and a basic browser reporter.
😁 consolji ships with 3 built-in reporters out of the box. A fancy colored reporter by default and fallsback to a basic reporter if running in a testing or CI environment detected using [nyxblabs/envizor](https://github.com/nyxblabs/envizor) and a basic browser reporter.

You can create a new reporter object that implements `{ log(logObject): () => { } }` interface.

**Example:** Simple JSON reporter
**Example:** Simple JSON reporter 📝

```ts
import { createconsolji } from 'consolji'
Expand All @@ -209,39 +196,39 @@ const consolji = createconsolji({
consolji.log('foo bar')
```

## Log Level
## 📊 Log Level

consolji only shows logs with configured log level or below. (Default is `3`)
😁 consolji only shows logs with configured log level or below. (Default is `3`)

Available log levels:
📊 Available log levels:

- `0`: Fatal and Error
- `1`: Warnings
- `2`: Normal logs
- `3`: Informational logs, success, fail, ready, start, ...
- `4`: Debug logs
- `5`: Trace logs
- `-999`: Silent
- `+999`: Verbose logs
- `0`: ❗️ Fatal and Error
- `1`: ⚠️ Warnings
- `2`: ℹ️ Normal logs
- `3`: Informational logs, success, fail, ready, start, ...
- `4`: 🐞 Debug logs
- `5`: 🕵️ Trace logs
- `-999`: 🔇 Silent
- `+999`: 🔊 Verbose logs

You can set the log level by either:
📊 You can set the log level by either:

- Passing `level` option to `createconsolji`
- Setting `consolji.level` on instance
- Using the `consolji_LEVEL` environment variable (not supported for browser and core builds).
- 🛠️ Passing `level` option to `createconsolji`
- 🔄 Setting `consolji.level` on instance
- 🌐 Using the `consolji_LEVEL` environment variable (not supported for browser and core builds).

## Log Types
## 📝 Log Types

Log types are exposed as `consolji.[type](...)` and each is a preset of styles and log level.

A list of all available built-in types is [available here](./src/constants.ts).

## Creating a new instance
## 🧪 Creating a new instance

consolji has a global instance and is recommended to use everywhere.
😁 consolji has a global instance and is recommended to use everywhere.
In case more control is needed, create a new instance.

```js
```ts
import { createconsolji } from 'consolji'

const logger = createconsolji({
Expand All @@ -256,11 +243,11 @@ const logger = createconsolji({
})
```

## Integrations
## 🛠️ Integrations

### With jest or vitest
### With 🃏 jest or 🌱 vitest

```js
```ts
describe('your-consolji-mock-test', () => {
beforeAll(() => {
// Redirect std and console to consolji too
Expand All @@ -285,14 +272,29 @@ describe('your-consolji-mock-test', () => {
})
```

### With jsdom
### With 🌐 jsdom

```js
```ts
{
virtualConsole: new jsdom.VirtualConsole().sendTo(consolji)
}
```

## License
## 📜 License

[MIT](./LICENSE) - Made with 💞

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/consolji?style=flat&colorA=18181B&colorB=14F195
[npm-version-href]: https://npmjs.com/package/consolji
[npm-downloads-src]: https://img.shields.io/npm/dm/consolji?style=flat&colorA=18181B&colorB=14F195
[npm-downloads-href]: https://npmjs.com/package/consolji
[bundle-src]: https://img.shields.io/bundlephobia/minzip/consolji?style=flat&colorA=18181B&colorB=14F195
[bundle-href]: https://bundlephobia.com/result?p=consolji
[license-src]: https://img.shields.io/github/license/nyxblabs/consolji.svg?style=flat&colorA=18181B&colorB=14F195
[license-href]: https://github.com/nyxblabs/consolji/blob/main/LICENSE

MIT
<!-- Cover -->
[cover-src]: https://raw.githubusercontent.com/nyxblabs/consolji/main/.github/assets/cover-github-consolji.png
[cover-href]: https://💻nyxb.ws
Binary file removed assets/bg_black.png
Binary file not shown.
Binary file removed assets/bg_light.png
Binary file not shown.
12 changes: 1 addition & 11 deletions src/consolji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ export class Consolji {

// Track of last log
this._lastLog = {}

// Insert the Time functions here
this.options.time = (label: string) => {
console.time(label)
}
this.options.timeLog = (label: string, ...data: any[]) => {
console.timeLog(label, ...data)
}
this.options.timeEnd = (label: string) => {
console.timeEnd(label)
}
}

get level() {
Expand Down Expand Up @@ -390,6 +379,7 @@ export interface LogFn {
(message: InputLogObject | any, ...args: any[]): void
raw: (...args: any[]) => void
}

export type ConsoljiInstance = Consolji & Record<LogType, LogFn>

// Legacy support
Expand Down
3 changes: 0 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ export interface ConsoljiOptions {
mockFn?: (type: LogType, defaults: InputLogObject) => (...args: any) => void
prompt?: typeof import('./prompt').prompt | undefined
formatOptions: FormatOptions
time: (label: string) => void
timeLog: (label: string, ...data: any[]) => void
timeEnd: (label: string) => void
}

/**
Expand Down

0 comments on commit 9c0e98d

Please sign in to comment.