tiny-essentials 1.7.1 β 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ColorSafeStringify.js +235 -0
- package/dist/ColorSafeStringify.min.js +1 -0
- package/dist/TinyBasicsEs.js +15 -7
- package/dist/TinyBasicsEs.min.js +1 -1
- package/dist/TinyEssentials.js +213 -7
- package/dist/TinyEssentials.min.js +1 -1
- package/dist/v1/basics/objFilter.cjs +16 -7
- package/dist/v1/basics/objFilter.d.mts +7 -0
- package/dist/v1/basics/objFilter.mjs +14 -7
- package/dist/v1/build/ColorSafeStringify.cjs +7 -0
- package/dist/v1/build/ColorSafeStringify.d.mts +3 -0
- package/dist/v1/build/ColorSafeStringify.mjs +2 -0
- package/dist/v1/index.cjs +2 -0
- package/dist/v1/index.d.mts +2 -1
- package/dist/v1/index.mjs +2 -1
- package/dist/v1/libs/ColorSafeStringify.cjs +196 -0
- package/dist/v1/libs/ColorSafeStringify.d.mts +62 -0
- package/dist/v1/libs/ColorSafeStringify.mjs +168 -0
- package/docs/README.md +9 -8
- package/docs/basics/objFilter.md +6 -0
- package/docs/libs/ColorSafeStringify.md +145 -0
- package/package.json +9 -2
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {Record<string, string>} ColorsList
|
|
5
|
+
* Represents a mapping of color keys to ANSI escape codes.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
class ColorSafeStringify {
|
|
9
|
+
/**
|
|
10
|
+
* Currently active color configuration.
|
|
11
|
+
* @type {ColorsList}
|
|
12
|
+
*/
|
|
13
|
+
#colors;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Preset collections (internal and user-defined).
|
|
17
|
+
* @type {Record<string, ColorsList>}
|
|
18
|
+
* @static
|
|
19
|
+
*/
|
|
20
|
+
static #PRESETS = {
|
|
21
|
+
default: {
|
|
22
|
+
reset: '\x1b[0m',
|
|
23
|
+
key: '\x1b[36m', // Cyan (object keys)
|
|
24
|
+
string: '\x1b[32m', // Green (regular strings)
|
|
25
|
+
string_url: '\x1b[34m', // Blue (URLs)
|
|
26
|
+
string_bool: '\x1b[35m', // Magenta (boolean/null in string form)
|
|
27
|
+
string_number: '\x1b[33m', // Yellow (numbers in string form)
|
|
28
|
+
number: '\x1b[33m', // Yellow (raw numbers)
|
|
29
|
+
boolean: '\x1b[35m', // Magenta (true/false)
|
|
30
|
+
null: '\x1b[1;30m', // Gray (null)
|
|
31
|
+
special: '\x1b[31m', // Red (e.g., [Circular], [undefined])
|
|
32
|
+
func: '\x1b[90m', // Dim (function string representations)
|
|
33
|
+
},
|
|
34
|
+
solarized: {
|
|
35
|
+
reset: '\x1b[0m',
|
|
36
|
+
key: '\x1b[38;5;37m',
|
|
37
|
+
string: '\x1b[38;5;136m',
|
|
38
|
+
string_url: '\x1b[38;5;33m',
|
|
39
|
+
string_bool: '\x1b[38;5;166m',
|
|
40
|
+
string_number: '\x1b[38;5;136m',
|
|
41
|
+
number: '\x1b[38;5;136m',
|
|
42
|
+
boolean: '\x1b[38;5;166m',
|
|
43
|
+
null: '\x1b[38;5;241m',
|
|
44
|
+
special: '\x1b[38;5;160m',
|
|
45
|
+
func: '\x1b[38;5;244m',
|
|
46
|
+
},
|
|
47
|
+
monokai: {
|
|
48
|
+
reset: '\x1b[0m',
|
|
49
|
+
key: '\x1b[38;5;81m',
|
|
50
|
+
string: '\x1b[38;5;114m',
|
|
51
|
+
string_url: '\x1b[38;5;75m',
|
|
52
|
+
string_bool: '\x1b[38;5;204m',
|
|
53
|
+
string_number: '\x1b[38;5;221m',
|
|
54
|
+
number: '\x1b[38;5;221m',
|
|
55
|
+
boolean: '\x1b[38;5;204m',
|
|
56
|
+
null: '\x1b[38;5;241m',
|
|
57
|
+
special: '\x1b[38;5;160m',
|
|
58
|
+
func: '\x1b[38;5;102m',
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Constructs a new instance with an optional base preset or custom override.
|
|
64
|
+
* @param {ColorsList} [defaultColors] - Optional override for the default color scheme.
|
|
65
|
+
*/
|
|
66
|
+
constructor(defaultColors = {}) {
|
|
67
|
+
this.#colors = { ...ColorSafeStringify.#PRESETS.default, ...defaultColors };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Internal method to apply ANSI color codes to different parts of a JSON string.
|
|
72
|
+
* @param {string} str - Raw JSON string to be colorized.
|
|
73
|
+
* @param {ColorsList} colors - ANSI color mapping to be applied to each JSON element type.
|
|
74
|
+
* @returns {string} Colorized JSON string.
|
|
75
|
+
*/
|
|
76
|
+
#colorizeJSON(str, colors) {
|
|
77
|
+
/** @type {{ marker: string, key: string }[]} */
|
|
78
|
+
const keyMatches = [];
|
|
79
|
+
|
|
80
|
+
// Colorize numeric values
|
|
81
|
+
str = str.replace(
|
|
82
|
+
/(?<!")\b(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)\b(?!")/g,
|
|
83
|
+
`${colors.number}$1${colors.reset}`,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// Replace keys with temporary markers for later colorization
|
|
87
|
+
str = str.replace(/"([^"]+)":/g, (_, key) => {
|
|
88
|
+
const marker = `___KEY${keyMatches.length}___`;
|
|
89
|
+
keyMatches.push({ marker, key });
|
|
90
|
+
return `${marker}:`; // Keep the colon for valid syntax
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Replace strings and apply specific colors based on their content
|
|
94
|
+
str = str.replace(/"(?:\\.|[^"\\])*?"/g, (match) => {
|
|
95
|
+
const val = match.slice(1, -1); // Remove surrounding quotes
|
|
96
|
+
|
|
97
|
+
if (/^(https?|ftp):\/\/[^\s]+$/i.test(val)) {
|
|
98
|
+
return `${colors.string_url}${match}${colors.reset}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (/^(true|false|null)$/.test(val)) {
|
|
102
|
+
return `${colors.string_bool}${match}${colors.reset}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (/^-?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(val)) {
|
|
106
|
+
return `${colors.string_number}${match}${colors.reset}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return `${colors.string}${match}${colors.reset}`;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Replace markers with colorized keys
|
|
113
|
+
for (const { marker, key } of keyMatches) {
|
|
114
|
+
const regex = new RegExp(marker, 'g');
|
|
115
|
+
str = str.replace(regex, `${colors.key}"${key}"${colors.reset}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Colorize boolean values
|
|
119
|
+
str = str.replace(/(?<!")\b(true|false)\b(?!")/g, `${colors.boolean}$1${colors.reset}`);
|
|
120
|
+
|
|
121
|
+
// Colorize null values
|
|
122
|
+
str = str.replace(/(?<!")\bnull\b(?!")/g, `${colors.null}null${colors.reset}`);
|
|
123
|
+
|
|
124
|
+
// Highlight special placeholder values
|
|
125
|
+
str = str.replace(/\[Circular\]/g, `${colors.special}[Circular]${colors.reset}`);
|
|
126
|
+
str = str.replace(/\[undefined\]/g, `${colors.special}[undefined]${colors.reset}`);
|
|
127
|
+
|
|
128
|
+
// Colorize function string representations
|
|
129
|
+
str = str.replace(/"function.*?[^\\]"/gs, `${colors.func}$&${colors.reset}`);
|
|
130
|
+
return str;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Colorizes a JSON string using the active or optionally overridden color set.
|
|
135
|
+
* @param {string} json - The JSON string to format.
|
|
136
|
+
* @param {ColorsList} [customColors] - Optional temporary color override.
|
|
137
|
+
* @returns {string}
|
|
138
|
+
*/
|
|
139
|
+
colorize(json, customColors = {}) {
|
|
140
|
+
const colors = { ...this.#colors, ...customColors };
|
|
141
|
+
return this.#colorizeJSON(json, colors);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns the currently active color scheme.
|
|
146
|
+
* @returns {ColorsList}
|
|
147
|
+
*/
|
|
148
|
+
getColors() {
|
|
149
|
+
return { ...this.#colors };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Updates the current color scheme with a partial override.
|
|
154
|
+
* @param {Partial<ColorsList>} newColors
|
|
155
|
+
*/
|
|
156
|
+
updateColors(newColors) {
|
|
157
|
+
Object.assign(this.#colors, newColors);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Resets the current color scheme to the default preset.
|
|
162
|
+
*/
|
|
163
|
+
resetColors() {
|
|
164
|
+
this.#colors = { ...ColorSafeStringify.#PRESETS.default };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Loads a color preset by name.
|
|
169
|
+
* @param {string} presetName - Name of the preset to load.
|
|
170
|
+
* @throws Will throw if the preset doesn't exist.
|
|
171
|
+
*/
|
|
172
|
+
loadColorPreset(presetName) {
|
|
173
|
+
const preset = ColorSafeStringify.#PRESETS[presetName];
|
|
174
|
+
if (!preset) throw new Error(`Preset "${presetName}" not found.`);
|
|
175
|
+
this.#colors = { ...preset };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Saves a new custom color preset.
|
|
180
|
+
* @param {string} name - Name of the new preset.
|
|
181
|
+
* @param {ColorsList} colors - ANSI color map to save.
|
|
182
|
+
*/
|
|
183
|
+
saveColorPreset(name, colors) {
|
|
184
|
+
ColorSafeStringify.#PRESETS[name] = { ...colors };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Returns a list of all available color preset names.
|
|
189
|
+
* @returns {string[]}
|
|
190
|
+
*/
|
|
191
|
+
getAvailablePresets() {
|
|
192
|
+
return Object.keys(ColorSafeStringify.#PRESETS);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = ColorSafeStringify;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export default ColorSafeStringify;
|
|
2
|
+
/**
|
|
3
|
+
* Represents a mapping of color keys to ANSI escape codes.
|
|
4
|
+
*/
|
|
5
|
+
export type ColorsList = Record<string, string>;
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Record<string, string>} ColorsList
|
|
8
|
+
* Represents a mapping of color keys to ANSI escape codes.
|
|
9
|
+
*/
|
|
10
|
+
declare class ColorSafeStringify {
|
|
11
|
+
/**
|
|
12
|
+
* Preset collections (internal and user-defined).
|
|
13
|
+
* @type {Record<string, ColorsList>}
|
|
14
|
+
* @static
|
|
15
|
+
*/
|
|
16
|
+
static "__#1@#PRESETS": Record<string, ColorsList>;
|
|
17
|
+
/**
|
|
18
|
+
* Constructs a new instance with an optional base preset or custom override.
|
|
19
|
+
* @param {ColorsList} [defaultColors] - Optional override for the default color scheme.
|
|
20
|
+
*/
|
|
21
|
+
constructor(defaultColors?: ColorsList);
|
|
22
|
+
/**
|
|
23
|
+
* Colorizes a JSON string using the active or optionally overridden color set.
|
|
24
|
+
* @param {string} json - The JSON string to format.
|
|
25
|
+
* @param {ColorsList} [customColors] - Optional temporary color override.
|
|
26
|
+
* @returns {string}
|
|
27
|
+
*/
|
|
28
|
+
colorize(json: string, customColors?: ColorsList): string;
|
|
29
|
+
/**
|
|
30
|
+
* Returns the currently active color scheme.
|
|
31
|
+
* @returns {ColorsList}
|
|
32
|
+
*/
|
|
33
|
+
getColors(): ColorsList;
|
|
34
|
+
/**
|
|
35
|
+
* Updates the current color scheme with a partial override.
|
|
36
|
+
* @param {Partial<ColorsList>} newColors
|
|
37
|
+
*/
|
|
38
|
+
updateColors(newColors: Partial<ColorsList>): void;
|
|
39
|
+
/**
|
|
40
|
+
* Resets the current color scheme to the default preset.
|
|
41
|
+
*/
|
|
42
|
+
resetColors(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Loads a color preset by name.
|
|
45
|
+
* @param {string} presetName - Name of the preset to load.
|
|
46
|
+
* @throws Will throw if the preset doesn't exist.
|
|
47
|
+
*/
|
|
48
|
+
loadColorPreset(presetName: string): void;
|
|
49
|
+
/**
|
|
50
|
+
* Saves a new custom color preset.
|
|
51
|
+
* @param {string} name - Name of the new preset.
|
|
52
|
+
* @param {ColorsList} colors - ANSI color map to save.
|
|
53
|
+
*/
|
|
54
|
+
saveColorPreset(name: string, colors: ColorsList): void;
|
|
55
|
+
/**
|
|
56
|
+
* Returns a list of all available color preset names.
|
|
57
|
+
* @returns {string[]}
|
|
58
|
+
*/
|
|
59
|
+
getAvailablePresets(): string[];
|
|
60
|
+
#private;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=ColorSafeStringify.d.mts.map
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Record<string, string>} ColorsList
|
|
3
|
+
* Represents a mapping of color keys to ANSI escape codes.
|
|
4
|
+
*/
|
|
5
|
+
class ColorSafeStringify {
|
|
6
|
+
/**
|
|
7
|
+
* Currently active color configuration.
|
|
8
|
+
* @type {ColorsList}
|
|
9
|
+
*/
|
|
10
|
+
#colors;
|
|
11
|
+
/**
|
|
12
|
+
* Preset collections (internal and user-defined).
|
|
13
|
+
* @type {Record<string, ColorsList>}
|
|
14
|
+
* @static
|
|
15
|
+
*/
|
|
16
|
+
static #PRESETS = {
|
|
17
|
+
default: {
|
|
18
|
+
reset: '\x1b[0m',
|
|
19
|
+
key: '\x1b[36m', // Cyan (object keys)
|
|
20
|
+
string: '\x1b[32m', // Green (regular strings)
|
|
21
|
+
string_url: '\x1b[34m', // Blue (URLs)
|
|
22
|
+
string_bool: '\x1b[35m', // Magenta (boolean/null in string form)
|
|
23
|
+
string_number: '\x1b[33m', // Yellow (numbers in string form)
|
|
24
|
+
number: '\x1b[33m', // Yellow (raw numbers)
|
|
25
|
+
boolean: '\x1b[35m', // Magenta (true/false)
|
|
26
|
+
null: '\x1b[1;30m', // Gray (null)
|
|
27
|
+
special: '\x1b[31m', // Red (e.g., [Circular], [undefined])
|
|
28
|
+
func: '\x1b[90m', // Dim (function string representations)
|
|
29
|
+
},
|
|
30
|
+
solarized: {
|
|
31
|
+
reset: '\x1b[0m',
|
|
32
|
+
key: '\x1b[38;5;37m',
|
|
33
|
+
string: '\x1b[38;5;136m',
|
|
34
|
+
string_url: '\x1b[38;5;33m',
|
|
35
|
+
string_bool: '\x1b[38;5;166m',
|
|
36
|
+
string_number: '\x1b[38;5;136m',
|
|
37
|
+
number: '\x1b[38;5;136m',
|
|
38
|
+
boolean: '\x1b[38;5;166m',
|
|
39
|
+
null: '\x1b[38;5;241m',
|
|
40
|
+
special: '\x1b[38;5;160m',
|
|
41
|
+
func: '\x1b[38;5;244m',
|
|
42
|
+
},
|
|
43
|
+
monokai: {
|
|
44
|
+
reset: '\x1b[0m',
|
|
45
|
+
key: '\x1b[38;5;81m',
|
|
46
|
+
string: '\x1b[38;5;114m',
|
|
47
|
+
string_url: '\x1b[38;5;75m',
|
|
48
|
+
string_bool: '\x1b[38;5;204m',
|
|
49
|
+
string_number: '\x1b[38;5;221m',
|
|
50
|
+
number: '\x1b[38;5;221m',
|
|
51
|
+
boolean: '\x1b[38;5;204m',
|
|
52
|
+
null: '\x1b[38;5;241m',
|
|
53
|
+
special: '\x1b[38;5;160m',
|
|
54
|
+
func: '\x1b[38;5;102m',
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Constructs a new instance with an optional base preset or custom override.
|
|
59
|
+
* @param {ColorsList} [defaultColors] - Optional override for the default color scheme.
|
|
60
|
+
*/
|
|
61
|
+
constructor(defaultColors = {}) {
|
|
62
|
+
this.#colors = { ...ColorSafeStringify.#PRESETS.default, ...defaultColors };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Internal method to apply ANSI color codes to different parts of a JSON string.
|
|
66
|
+
* @param {string} str - Raw JSON string to be colorized.
|
|
67
|
+
* @param {ColorsList} colors - ANSI color mapping to be applied to each JSON element type.
|
|
68
|
+
* @returns {string} Colorized JSON string.
|
|
69
|
+
*/
|
|
70
|
+
#colorizeJSON(str, colors) {
|
|
71
|
+
/** @type {{ marker: string, key: string }[]} */
|
|
72
|
+
const keyMatches = [];
|
|
73
|
+
// Colorize numeric values
|
|
74
|
+
str = str.replace(/(?<!")\b(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)\b(?!")/g, `${colors.number}$1${colors.reset}`);
|
|
75
|
+
// Replace keys with temporary markers for later colorization
|
|
76
|
+
str = str.replace(/"([^"]+)":/g, (_, key) => {
|
|
77
|
+
const marker = `___KEY${keyMatches.length}___`;
|
|
78
|
+
keyMatches.push({ marker, key });
|
|
79
|
+
return `${marker}:`; // Keep the colon for valid syntax
|
|
80
|
+
});
|
|
81
|
+
// Replace strings and apply specific colors based on their content
|
|
82
|
+
str = str.replace(/"(?:\\.|[^"\\])*?"/g, (match) => {
|
|
83
|
+
const val = match.slice(1, -1); // Remove surrounding quotes
|
|
84
|
+
if (/^(https?|ftp):\/\/[^\s]+$/i.test(val)) {
|
|
85
|
+
return `${colors.string_url}${match}${colors.reset}`;
|
|
86
|
+
}
|
|
87
|
+
if (/^(true|false|null)$/.test(val)) {
|
|
88
|
+
return `${colors.string_bool}${match}${colors.reset}`;
|
|
89
|
+
}
|
|
90
|
+
if (/^-?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(val)) {
|
|
91
|
+
return `${colors.string_number}${match}${colors.reset}`;
|
|
92
|
+
}
|
|
93
|
+
return `${colors.string}${match}${colors.reset}`;
|
|
94
|
+
});
|
|
95
|
+
// Replace markers with colorized keys
|
|
96
|
+
for (const { marker, key } of keyMatches) {
|
|
97
|
+
const regex = new RegExp(marker, 'g');
|
|
98
|
+
str = str.replace(regex, `${colors.key}"${key}"${colors.reset}`);
|
|
99
|
+
}
|
|
100
|
+
// Colorize boolean values
|
|
101
|
+
str = str.replace(/(?<!")\b(true|false)\b(?!")/g, `${colors.boolean}$1${colors.reset}`);
|
|
102
|
+
// Colorize null values
|
|
103
|
+
str = str.replace(/(?<!")\bnull\b(?!")/g, `${colors.null}null${colors.reset}`);
|
|
104
|
+
// Highlight special placeholder values
|
|
105
|
+
str = str.replace(/\[Circular\]/g, `${colors.special}[Circular]${colors.reset}`);
|
|
106
|
+
str = str.replace(/\[undefined\]/g, `${colors.special}[undefined]${colors.reset}`);
|
|
107
|
+
// Colorize function string representations
|
|
108
|
+
str = str.replace(/"function.*?[^\\]"/gs, `${colors.func}$&${colors.reset}`);
|
|
109
|
+
return str;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Colorizes a JSON string using the active or optionally overridden color set.
|
|
113
|
+
* @param {string} json - The JSON string to format.
|
|
114
|
+
* @param {ColorsList} [customColors] - Optional temporary color override.
|
|
115
|
+
* @returns {string}
|
|
116
|
+
*/
|
|
117
|
+
colorize(json, customColors = {}) {
|
|
118
|
+
const colors = { ...this.#colors, ...customColors };
|
|
119
|
+
return this.#colorizeJSON(json, colors);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Returns the currently active color scheme.
|
|
123
|
+
* @returns {ColorsList}
|
|
124
|
+
*/
|
|
125
|
+
getColors() {
|
|
126
|
+
return { ...this.#colors };
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Updates the current color scheme with a partial override.
|
|
130
|
+
* @param {Partial<ColorsList>} newColors
|
|
131
|
+
*/
|
|
132
|
+
updateColors(newColors) {
|
|
133
|
+
Object.assign(this.#colors, newColors);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Resets the current color scheme to the default preset.
|
|
137
|
+
*/
|
|
138
|
+
resetColors() {
|
|
139
|
+
this.#colors = { ...ColorSafeStringify.#PRESETS.default };
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Loads a color preset by name.
|
|
143
|
+
* @param {string} presetName - Name of the preset to load.
|
|
144
|
+
* @throws Will throw if the preset doesn't exist.
|
|
145
|
+
*/
|
|
146
|
+
loadColorPreset(presetName) {
|
|
147
|
+
const preset = ColorSafeStringify.#PRESETS[presetName];
|
|
148
|
+
if (!preset)
|
|
149
|
+
throw new Error(`Preset "${presetName}" not found.`);
|
|
150
|
+
this.#colors = { ...preset };
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Saves a new custom color preset.
|
|
154
|
+
* @param {string} name - Name of the new preset.
|
|
155
|
+
* @param {ColorsList} colors - ANSI color map to save.
|
|
156
|
+
*/
|
|
157
|
+
saveColorPreset(name, colors) {
|
|
158
|
+
ColorSafeStringify.#PRESETS[name] = { ...colors };
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Returns a list of all available color preset names.
|
|
162
|
+
* @returns {string[]}
|
|
163
|
+
*/
|
|
164
|
+
getAvailablePresets() {
|
|
165
|
+
return Object.keys(ColorSafeStringify.#PRESETS);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
export default ColorSafeStringify;
|
package/docs/README.md
CHANGED
|
@@ -12,16 +12,17 @@ Here, you can navigate through different sections of the project. Below are the
|
|
|
12
12
|
|
|
13
13
|
This folder contains the core scripts we have worked on so far. Each file is a module focused on specific functionality.
|
|
14
14
|
|
|
15
|
-
- π¦ **[array
|
|
16
|
-
- β° **[clock
|
|
17
|
-
- π§ **[objFilter
|
|
18
|
-
- π’ **[simpleMath
|
|
19
|
-
- βοΈ **[text
|
|
20
|
-
- π **[asyncReplace
|
|
15
|
+
- π¦ **[array](./basics/array.md)** β A tiny utility for shuffling arrays using the FisherβYates algorithm.
|
|
16
|
+
- β° **[clock](./basics/clock.md)** β A versatile time utility module for calculating and formatting time durations.
|
|
17
|
+
- π§ **[objFilter](./basics/objFilter.md)** β Type detection, extension, and analysis made easy with simple and extensible type validation.
|
|
18
|
+
- π’ **[simpleMath](./basics/simpleMath.md)** β A collection of simple math utilities for calculations like the Rule of Three and percentages.
|
|
19
|
+
- βοΈ **[text](./basics/text.md)** β A utility for transforming text into title case formats, with multiple options for capitalization.
|
|
20
|
+
- π **[asyncReplace](./basics/asyncReplace.md)** β Asynchronously replaces matches in a string using a regex and an async function.
|
|
21
21
|
|
|
22
22
|
### 2. **`libs/`**
|
|
23
|
-
- ποΈ **[TinyPromiseQueue
|
|
24
|
-
- π
**[TinyLevelUp
|
|
23
|
+
- ποΈ **[TinyPromiseQueue](./libs/TinyPromiseQueue.md)** β A class that allows sequential execution of asynchronous tasks, supporting task delays, cancellation, and queue management.
|
|
24
|
+
- π
**[TinyLevelUp](./libs/TinyLevelUp.md)** β A class to manage user level-up logic based on experience points, providing methods for experience validation, addition, removal, and calculation.
|
|
25
|
+
- π¨ **[ColorSafeStringify](./libs/ColorSafeStringify.md)** β A utility for applying customizable ANSI colors to JSON strings in terminal outputs, supporting presets and fine-grained type-based highlighting.
|
|
25
26
|
|
|
26
27
|
---
|
|
27
28
|
|
package/docs/basics/objFilter.md
CHANGED
|
@@ -151,3 +151,9 @@ Hereβs a full list of supported type names (in their default order):
|
|
|
151
151
|
- `object`
|
|
152
152
|
|
|
153
153
|
You can change this order or insert your own types with `extendObjType`.
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### π οΈ `getCheckObj()`
|
|
158
|
+
|
|
159
|
+
This function creates a clone of the functions from the `typeValidator` object. It returns a new object where the keys are the same and the values are the cloned functions.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# π¨ Color Safe Stringify
|
|
2
|
+
|
|
3
|
+
Color Safe Stringify is a lightweight JavaScript class for transforming boring JSON strings into colorful, readable terminal output using ANSI escape codes. Whether you're debugging, logging, or just want your JSONs to sparkle a bit more, this tool is here to help β¨
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## π Features
|
|
8
|
+
|
|
9
|
+
- π― **Syntax-aware highlighting**: Keys, strings, numbers, booleans, nulls, functions, URLs, and special values each get their own color.
|
|
10
|
+
- π¨ **Customizable presets**: Built-in themes like `default`, `solarized`, and `monokai` β and you can even define your own!
|
|
11
|
+
- π§ **Dynamic updates**: Change only the parts of the color scheme you want.
|
|
12
|
+
- π§ **Smart detection**: Differentiates between strings that *look* like booleans/numbers/URLs and real primitives.
|
|
13
|
+
- π **Safe formatting**: No circular structure errors β you can prepare your JSON ahead and color it afterward.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## π¦ Installation
|
|
18
|
+
|
|
19
|
+
This library is designed to work in any Node.js environment:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install safe-stable-stringify
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Then import both the stable stringifier and `ColorSafeStringify`:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import stringify from 'safe-stable-stringify';
|
|
29
|
+
import { ColorSafeStringify } from 'tiny-essentials';
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## π§ͺ Usage Example
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const colorizer = new ColorSafeStringify();
|
|
38
|
+
|
|
39
|
+
const jsonObject = {
|
|
40
|
+
name: "Yasmin",
|
|
41
|
+
age: 27,
|
|
42
|
+
website: "https://equestria.social/@jasmindreasond",
|
|
43
|
+
active: true,
|
|
44
|
+
notes: null,
|
|
45
|
+
big: BigInt(1234567890),
|
|
46
|
+
greet: () => "Hi!",
|
|
47
|
+
nested: {
|
|
48
|
+
color: "rainbow π",
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Stringify safely first
|
|
53
|
+
const jsonStr = stringify(jsonObject, null, 2);
|
|
54
|
+
|
|
55
|
+
// Apply beautiful colors
|
|
56
|
+
console.log(colorizer.colorize(jsonStr));
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## π¨ Built-in Presets
|
|
62
|
+
|
|
63
|
+
- `default` - a balanced palette with good terminal compatibility
|
|
64
|
+
- `solarized` - warm, low-contrast colors inspired by Solarized theme
|
|
65
|
+
- `monokai` - a bright and modern syntax-style theme
|
|
66
|
+
|
|
67
|
+
Use them like so:
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
colorizer.loadColorPreset("monokai");
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## π§ Create Your Own Preset
|
|
76
|
+
|
|
77
|
+
You can save your own custom preset:
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
colorizer.saveColorPreset('candy', {
|
|
81
|
+
reset: '\x1b[0m',
|
|
82
|
+
key: '\x1b[95m',
|
|
83
|
+
string: '\x1b[91m',
|
|
84
|
+
string_url: '\x1b[35m',
|
|
85
|
+
string_bool: '\x1b[95m',
|
|
86
|
+
string_number: '\x1b[91m',
|
|
87
|
+
number: '\x1b[91m',
|
|
88
|
+
boolean: '\x1b[95m',
|
|
89
|
+
null: '\x1b[90m',
|
|
90
|
+
special: '\x1b[31m',
|
|
91
|
+
func: '\x1b[35m',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
colorizer.loadColorPreset('candy');
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## π§ API
|
|
100
|
+
|
|
101
|
+
### `new ColorSafeStringify([defaultColors])`
|
|
102
|
+
|
|
103
|
+
Creates a new colorizer with optional color overrides.
|
|
104
|
+
|
|
105
|
+
### `.colorize(jsonString, [customColors])`
|
|
106
|
+
|
|
107
|
+
Returns a colorized string using current or temporary color settings.
|
|
108
|
+
|
|
109
|
+
### `.getColors()`
|
|
110
|
+
|
|
111
|
+
Returns the current color scheme.
|
|
112
|
+
|
|
113
|
+
### `.updateColors(partialColors)`
|
|
114
|
+
|
|
115
|
+
Updates only specific color keys in the active scheme.
|
|
116
|
+
|
|
117
|
+
### `.resetColors()`
|
|
118
|
+
|
|
119
|
+
Resets to the default preset.
|
|
120
|
+
|
|
121
|
+
### `.loadColorPreset(name)`
|
|
122
|
+
|
|
123
|
+
Loads a preset by name. Throws an error if it doesn't exist.
|
|
124
|
+
|
|
125
|
+
### `.saveColorPreset(name, colors)`
|
|
126
|
+
|
|
127
|
+
Saves a new custom preset.
|
|
128
|
+
|
|
129
|
+
### `.getAvailablePresets()`
|
|
130
|
+
|
|
131
|
+
Returns an array of all available preset names.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## π€ Best Practices
|
|
136
|
+
|
|
137
|
+
- Use `safe-stable-stringify` or equivalent to avoid issues with functions, circular references, and BigInt.
|
|
138
|
+
- Always apply coloring *after* serialization.
|
|
139
|
+
- For advanced highlighting, use proper typing for values (avoid turning everything into strings!).
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## π§ Why?
|
|
144
|
+
|
|
145
|
+
Plain JSON is fineβ¦ but colored JSON? Thatβs β¨ **magical** β¨ β and way easier on the eyes when you're reading logs, inspecting complex structures, or debugging live servers.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"test:mjs": "node test/index.mjs",
|
|
10
10
|
"test:mjs:promisequeue": "node test/index.mjs promiseQueue",
|
|
11
11
|
"test:mjs:objtype": "node test/index.mjs objType",
|
|
12
|
+
"test:mjs:jsoncolor": "node test/index.mjs colorStringify",
|
|
12
13
|
"fix:prettier": "npm run fix:prettier:src && npm run fix:prettier:test && npm run fix:prettier:rollup.config && npm run fix:prettier:webpack.config",
|
|
13
14
|
"fix:prettier:src": "prettier --write ./src/*",
|
|
14
15
|
"fix:prettier:test": "prettier --write ./test/*",
|
|
@@ -48,7 +49,12 @@
|
|
|
48
49
|
"text",
|
|
49
50
|
"lib",
|
|
50
51
|
"time",
|
|
51
|
-
"shuffle-array"
|
|
52
|
+
"shuffle-array",
|
|
53
|
+
"json-color",
|
|
54
|
+
"terminal-color",
|
|
55
|
+
"cli-output",
|
|
56
|
+
"colorize-json",
|
|
57
|
+
"pretty-print-json"
|
|
52
58
|
],
|
|
53
59
|
"author": "Yasmin Seidel (Jasmin Dreasond)",
|
|
54
60
|
"license": "GPL-3.0-only",
|
|
@@ -82,6 +88,7 @@
|
|
|
82
88
|
"prettier": "3.5.3",
|
|
83
89
|
"rollup": "^4.40.0",
|
|
84
90
|
"rollup-preserve-directives": "^1.1.3",
|
|
91
|
+
"safe-stable-stringify": "^2.5.0",
|
|
85
92
|
"sass": "^1.87.0",
|
|
86
93
|
"tinycolor2": "^1.6.0",
|
|
87
94
|
"tslib": "^2.8.1",
|