Skip to content

Commit

Permalink
chore(modifiers): Change modifiers api and make modifiers execute in …
Browse files Browse the repository at this point in the history
…every log
  • Loading branch information
angelnext committed Nov 20, 2023
1 parent a93a45f commit ba274c7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ or your own modifiers...
import { rubiks } from "rubiks";

function customModifier(self) {
self.format = `my content: ${self.format}`
return `my content: ${self.format}, and the current level is ${self.level}`
}

rubiks()
.use(customModifier)
.log("testing")
.log("more")
.error("more")
```


6 changes: 6 additions & 0 deletions lib/modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function nerdIcons(
if (options.info) {
self.prefixes.info = self.noColor ? " " : "\x1b[34m\x1b[0m ";
}

return self.format;
};
}

Expand All @@ -56,6 +58,8 @@ export function withDates(self) {
self.prefixes.all = `${self.prefixes.all || ""}${
self.noColor ? "" : "\x1b[90m"
}[${date.toLocaleDateString()} ${date.toLocaleTimeString()}]\x1b[0m `;

return self.format;
}

/**
Expand All @@ -64,4 +68,6 @@ export function withDates(self) {
*/
export function noColor(self) {
self.noColor = true;

return self.format;
}
23 changes: 22 additions & 1 deletion lib/rubiks.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export class Rubiks {
*/
noColor = false;

/**
* @type {import("./modifiers").Modifier[]}
*/
modifiers = [];

constructor() {
if (process) {
this.noColor = process?.env?.NO_COLOR !== undefined ? true : false;
Expand All @@ -53,7 +58,8 @@ export class Rubiks {
* @param {import("./modifiers").Modifier} modifier
*/
use(modifier) {
modifier(this);
this.format = modifier(this);
this.modifiers.push(modifier);
return this;
}

Expand All @@ -64,6 +70,9 @@ export class Rubiks {
* @returns {Rubiks} This instance.
*/
log(content, level = log) {
for (const modifier of this.modifiers) {
modifier(this);
}
level(this, this.format.replace("%s", content));
return this;
}
Expand All @@ -74,6 +83,9 @@ export class Rubiks {
* @returns {Rubiks} This instance.
*/
warn(content) {
for (const modifier of this.modifiers) {
modifier(this);
}
warn(this, this.format.replace("%s", content));
return this;
}
Expand All @@ -84,6 +96,9 @@ export class Rubiks {
* @returns {Rubiks} This instance.
*/
error(content) {
for (const modifier of this.modifiers) {
modifier(this);
}
error(this, this.format.replace("%s", content));
return this;
}
Expand All @@ -94,6 +109,9 @@ export class Rubiks {
* @returns {Rubiks} This instance.
*/
success(content) {
for (const modifier of this.modifiers) {
modifier(this);
}
success(this, this.format.replace("%s", content));
return this;
}
Expand All @@ -104,6 +122,9 @@ export class Rubiks {
* @returns {Rubiks} This instance.
*/
info(content) {
for (const modifier of this.modifiers) {
modifier(this);
}
info(this, this.format.replace("%s", content));
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "rubiks",
"version": "0.6.0",
"version": "0.7.0",
"type": "module",
"description": "Rubiks is a 0 dependency extendable logging library for modern applications.",
"main": "./lib/index.js",
Expand Down

0 comments on commit ba274c7

Please sign in to comment.