@kitql/helpers
Advanced tools
Comparing version 0.8.8-next.0 to 0.8.8-next.1
# @kitql/helpers | ||
## 0.8.8-next.1 | ||
### Patch Changes | ||
- [#574](https://github.com/jycouet/kitql/pull/574) | ||
[`d6b5a08`](https://github.com/jycouet/kitql/commit/d6b5a08f5a15fa9db54818ce69221817b1a7d2bf) | ||
Thanks [@jycouet](https://github.com/jycouet)! - enable spread args to log functions info, | ||
success, error. Add infoO and successO for options. | ||
## 0.8.8-next.0 | ||
@@ -4,0 +13,0 @@ |
@@ -118,26 +118,37 @@ "use strict"; | ||
}; | ||
const colorProcess = (str) => { | ||
const colorProcess = (...msgs) => { | ||
if (!import_constants.BROWSER) { | ||
return [str]; | ||
return [...msgs]; | ||
} | ||
const originalStr = str; | ||
const posToReplace = []; | ||
const tagsUsed = [...new Set(extractKitQLTags(str))]; | ||
for (const key of tagsUsed) { | ||
const indexesStarts = getAllIndexOf(originalStr, `${START1}${key}${START2}`); | ||
for (const index of indexesStarts) { | ||
posToReplace.push({ index, browser: getStyleBrowser(key) }); | ||
const arr = [...msgs]; | ||
const msgsTransformed = []; | ||
const colors = []; | ||
const additional = []; | ||
for (let i = 0; i < arr.length; i++) { | ||
let msg = arr[i]; | ||
if (typeof msg !== "string") { | ||
additional.push(msg); | ||
} else { | ||
const originalStr = msg; | ||
const posToReplace = []; | ||
const tagsUsed = [...new Set(extractKitQLTags(originalStr))]; | ||
for (const key of tagsUsed) { | ||
const indexesStarts = getAllIndexOf(originalStr, `${START1}${key}${START2}`); | ||
for (const index of indexesStarts) { | ||
posToReplace.push({ index, browser: getStyleBrowser(key) }); | ||
} | ||
msg = msg.replaceAll(`${START1}${key}${START2}`, "%c"); | ||
} | ||
const indexesEnd = getAllIndexOf(originalStr, END); | ||
for (const index of indexesEnd) { | ||
posToReplace.push({ index, browser: "" }); | ||
} | ||
msg = msg.replaceAll(END, "%c"); | ||
for (const c of posToReplace.sort((a, b) => a.index - b.index)) { | ||
colors.push(c.browser); | ||
} | ||
msgsTransformed.push(msg); | ||
} | ||
str = str.replaceAll(`${START1}${key}${START2}`, "%c"); | ||
} | ||
const indexesEnd = getAllIndexOf(originalStr, END); | ||
for (const index of indexesEnd) { | ||
posToReplace.push({ index, browser: "" }); | ||
} | ||
str = str.replaceAll(END, "%c"); | ||
const colors = []; | ||
for (const c of posToReplace.sort((a, b) => a.index - b.index)) { | ||
colors.push(c.browser); | ||
} | ||
return [str, ...colors]; | ||
return [msgsTransformed.join(" "), ...colors, ...additional]; | ||
}; | ||
@@ -144,0 +155,0 @@ const reset = (str) => { |
@@ -36,3 +36,3 @@ "use strict"; | ||
} | ||
buildStr(msg, withError, withSuccess, indent) { | ||
buildStr(withError, withSuccess, level, ...msgs) { | ||
const table = []; | ||
@@ -55,33 +55,59 @@ if (this.toolName) { | ||
if (withError) { | ||
table.push((0, import_colors.bold)((0, import_colors.redBright)(" \u2718 "))); | ||
table.push((0, import_colors.bold)((0, import_colors.redBright)(" \u2718"))); | ||
} else if (withSuccess) { | ||
table.push((0, import_colors.bold)((0, import_colors.greenBright)(" \u2714 "))); | ||
table.push((0, import_colors.bold)((0, import_colors.greenBright)(" \u2714"))); | ||
} else { | ||
table.push(String(" " + this.prefixEmoji)); | ||
table.push(String("" + this.prefixEmoji)); | ||
} | ||
table.push(indent); | ||
table.push(String(msg)); | ||
const str = table.join(""); | ||
return (0, import_colors.colorProcess)(str); | ||
} | ||
info(msg, conf) { | ||
const level = conf?.level ?? 0; | ||
const withSuccess = conf?.withSuccess ?? false; | ||
if (this.levelsToShow !== null && level <= this.levelsToShow) { | ||
if (level > 0) { | ||
const indent = " ".repeat(level); | ||
const built = this.buildStr(msg, false, withSuccess, indent); | ||
console.info(...built); | ||
return built; | ||
table.push(indent); | ||
} | ||
return null; | ||
if (table.length === 0 || table.length === 1 && table[0] === "") { | ||
return (0, import_colors.colorProcess)(...[...msgs.flatMap((c) => c)]); | ||
} | ||
return (0, import_colors.colorProcess)(...[table.join(""), ...msgs.flatMap((c) => c)]); | ||
} | ||
success(msg, conf) { | ||
const level = conf?.level ?? 0; | ||
return this.info(msg, { level, withSuccess: true }); | ||
/** | ||
* console.info with options | ||
* @param conf with level of indentation | ||
*/ | ||
infoO(conf, ...msgs) { | ||
const built = this.buildStr(false, false, conf.level, ...msgs); | ||
console.info(...built.flatMap((c) => c)); | ||
return built; | ||
} | ||
error(msg) { | ||
const built = this.buildStr(msg, true, false, ""); | ||
console.error(...built); | ||
/** | ||
* console.info | ||
*/ | ||
info(...msgs) { | ||
const built = this.buildStr(false, false, 0, ...msgs); | ||
console.info(...built.flatMap((c) => c)); | ||
return built; | ||
} | ||
/** | ||
* console.info with options and success icon | ||
* @param conf with level of indentation | ||
*/ | ||
successO(conf, ...msgs) { | ||
const built = this.buildStr(false, true, conf.level, msgs); | ||
console.info(...built.flatMap((c) => c)); | ||
return built; | ||
} | ||
/** | ||
* console.info with success icon | ||
*/ | ||
success(...msgs) { | ||
const built = this.buildStr(false, true, 0, msgs); | ||
console.info(...built.flatMap((c) => c)); | ||
return built; | ||
} | ||
/** | ||
* console.error with error icon | ||
*/ | ||
error(...msgs) { | ||
const built = this.buildStr(true, false, 0, msgs); | ||
console.error(...built.flatMap((c) => c)); | ||
return built; | ||
} | ||
} |
import type { Style } from './types.js'; | ||
export declare const color: (style: Style, str: string) => string; | ||
export declare const colorProcess: (str: string) => string[]; | ||
export declare const colorProcess: (...msgs: any[]) => any[]; | ||
export declare const reset: (str: string) => string; | ||
@@ -5,0 +5,0 @@ export declare const bold: (str: string) => string; |
@@ -45,29 +45,41 @@ import { BROWSER } from '../constants.js'; | ||
}; | ||
export const colorProcess = (str) => { | ||
export const colorProcess = (...msgs) => { | ||
if (!BROWSER) { | ||
return [str]; | ||
return [...msgs]; | ||
} | ||
const originalStr = str; | ||
const posToReplace = []; | ||
// we need to make it unique | ||
const tagsUsed = [...new Set(extractKitQLTags(str))]; | ||
for (const key of tagsUsed) { | ||
// check indexes | ||
const indexesStarts = getAllIndexOf(originalStr, `${START1}${key}${START2}`); | ||
for (const index of indexesStarts) { | ||
posToReplace.push({ index, browser: getStyleBrowser(key) }); | ||
const arr = [...msgs]; | ||
const msgsTransformed = []; | ||
const colors = []; | ||
const additional = []; | ||
for (let i = 0; i < arr.length; i++) { | ||
let msg = arr[i]; | ||
if (typeof msg !== 'string') { | ||
additional.push(msg); | ||
} | ||
// replace with %c in another str to make sure we don't change the order of indexes | ||
str = str.replaceAll(`${START1}${key}${START2}`, '%c'); | ||
else { | ||
const originalStr = msg; | ||
const posToReplace = []; | ||
// we need to make it unique | ||
const tagsUsed = [...new Set(extractKitQLTags(originalStr))]; | ||
for (const key of tagsUsed) { | ||
// check indexes | ||
const indexesStarts = getAllIndexOf(originalStr, `${START1}${key}${START2}`); | ||
for (const index of indexesStarts) { | ||
posToReplace.push({ index, browser: getStyleBrowser(key) }); | ||
} | ||
// replace with %c in another str to make sure we don't change the order of indexes | ||
msg = msg.replaceAll(`${START1}${key}${START2}`, '%c'); | ||
} | ||
const indexesEnd = getAllIndexOf(originalStr, END); | ||
for (const index of indexesEnd) { | ||
posToReplace.push({ index, browser: '' }); | ||
} | ||
msg = msg.replaceAll(END, '%c'); | ||
for (const c of posToReplace.sort((a, b) => a.index - b.index)) { | ||
colors.push(c.browser); | ||
} | ||
msgsTransformed.push(msg); | ||
} | ||
} | ||
const indexesEnd = getAllIndexOf(originalStr, END); | ||
for (const index of indexesEnd) { | ||
posToReplace.push({ index, browser: '' }); | ||
} | ||
str = str.replaceAll(END, '%c'); | ||
const colors = []; | ||
for (const c of posToReplace.sort((a, b) => a.index - b.index)) { | ||
colors.push(c.browser); | ||
} | ||
return [str, ...colors]; | ||
return [msgsTransformed.join(' '), ...colors, ...additional]; | ||
}; | ||
@@ -74,0 +86,0 @@ // |
@@ -12,10 +12,28 @@ export declare class Log { | ||
private buildStr; | ||
info(msg: string, conf?: { | ||
level?: number; | ||
withSuccess?: boolean; | ||
}): string[] | null; | ||
success(msg: string, conf?: { | ||
level?: number; | ||
}): string[] | null; | ||
error(msg: string): string[]; | ||
/** | ||
* console.info with options | ||
* @param conf with level of indentation | ||
*/ | ||
infoO(conf: { | ||
level: number; | ||
}, ...msgs: any[]): any[]; | ||
/** | ||
* console.info | ||
*/ | ||
info(...msgs: any[]): any[]; | ||
/** | ||
* console.info with options and success icon | ||
* @param conf with level of indentation | ||
*/ | ||
successO(conf: { | ||
level: number; | ||
}, ...msgs: any[]): any[]; | ||
/** | ||
* console.info with success icon | ||
*/ | ||
success(...msgs: any[]): any[]; | ||
/** | ||
* console.error with error icon | ||
*/ | ||
error(...msgs: any[]): any[]; | ||
} |
@@ -13,3 +13,3 @@ import { bold, colorProcess, greenBright, redBright, bgRedBright, bgGreen, bgBlueBright, } from './colors/index.js'; | ||
} | ||
buildStr(msg, withError, withSuccess, indent) { | ||
buildStr(withError, withSuccess, level, ...msgs) { | ||
const table = []; | ||
@@ -37,36 +37,62 @@ if (this.toolName) { | ||
if (withError) { | ||
table.push(bold(redBright(' ✘ '))); | ||
table.push(bold(redBright(' ✘'))); | ||
} | ||
else if (withSuccess) { | ||
table.push(bold(greenBright(' ✔ '))); | ||
table.push(bold(greenBright(' ✔'))); | ||
} | ||
else { | ||
table.push(String(' ' + this.prefixEmoji)); | ||
table.push(String('' + this.prefixEmoji)); | ||
} | ||
table.push(indent); | ||
table.push(String(msg)); | ||
const str = table.join(''); | ||
return colorProcess(str); | ||
} | ||
info(msg, conf) { | ||
const level = conf?.level ?? 0; | ||
const withSuccess = conf?.withSuccess ?? false; | ||
if (this.levelsToShow !== null && level <= this.levelsToShow) { | ||
if (level > 0) { | ||
const indent = ' '.repeat(level); | ||
const built = this.buildStr(msg, false, withSuccess, indent); | ||
console.info(...built); | ||
return built; | ||
table.push(indent); | ||
} | ||
return null; | ||
if (table.length === 0 || (table.length === 1 && table[0] === '')) { | ||
return colorProcess(...[...msgs.flatMap(c => c)]); | ||
} | ||
return colorProcess(...[table.join(''), ...msgs.flatMap(c => c)]); | ||
} | ||
success(msg, conf) { | ||
const level = conf?.level ?? 0; | ||
return this.info(msg, { level, withSuccess: true }); | ||
/** | ||
* console.info with options | ||
* @param conf with level of indentation | ||
*/ | ||
infoO(conf, ...msgs) { | ||
const built = this.buildStr(false, false, conf.level, ...msgs); | ||
console.info(...built.flatMap(c => c)); | ||
return built; | ||
} | ||
error(msg) { | ||
const built = this.buildStr(msg, true, false, ''); | ||
/** | ||
* console.info | ||
*/ | ||
info(...msgs) { | ||
const built = this.buildStr(false, false, 0, ...msgs); | ||
console.info(...built.flatMap(c => c)); | ||
return built; | ||
} | ||
/** | ||
* console.info with options and success icon | ||
* @param conf with level of indentation | ||
*/ | ||
successO(conf, ...msgs) { | ||
const built = this.buildStr(false, true, conf.level, msgs); | ||
console.info(...built.flatMap(c => c)); | ||
return built; | ||
} | ||
/** | ||
* console.info with success icon | ||
*/ | ||
success(...msgs) { | ||
const built = this.buildStr(false, true, 0, msgs); | ||
console.info(...built.flatMap(c => c)); | ||
return built; | ||
} | ||
/** | ||
* console.error with error icon | ||
*/ | ||
error(...msgs) { | ||
const built = this.buildStr(true, false, 0, msgs); | ||
// Keep error to have the stacktrace in the browser | ||
console.error(...built); | ||
console.error(...built.flatMap(c => c)); | ||
return built; | ||
} | ||
} |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "0.8.8-next.0", | ||
"version": "0.8.8-next.1", | ||
"license": "MIT", | ||
@@ -15,0 +15,0 @@ "type": "module", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107257
2587