Skip to content

Commit

Permalink
Merge pull request #5 from zvakanaka/3-0-5
Browse files Browse the repository at this point in the history
Get Types Working
  • Loading branch information
zvakanaka committed Feb 28, 2023
2 parents 54ce26a + 9d837b2 commit 2342e61
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions color-json.d.ts → index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export type Colors = {
* @returns {string} Stringified JSON colored with ANSI escape characters.
*/
export default function colorJson(
json: Object | string,
json: (Object | string),
colors?: Colors,
colorMap?: ColorMap,
spacing?: number
spacing?: number,
): string
11 changes: 10 additions & 1 deletion color-json.js → index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ export const defaultColors = {
key: 'white'
};

/**
* Given an object, it returns its JSON representation colored using
* ANSI escape characters.
* @param {(Object | string)} json - JSON object to highlighter.
* @param {Colors} [colors] - A map with the ANSI characters for each supported color.
* @param {ColorMap} [colorMap] - An object to configure the coloring.
* @param {number} [spacing=2] - The indentation spaces.
* @returns {string} Stringified JSON colored with ANSI escape characters.
*/
export default function colorJson(json, colors = defaultColors, colorMap = defaultColorMap, spacing = 2) {
if (supportsColor()) {
if (typeof json != 'string') json = JSON.stringify(json, undefined, spacing);
else json = JSON.stringify(JSON.parse(json), undefined, spacing);
return colorMap[colors.separator] + json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
return colorMap[colors.separator] + json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
let colorCode = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) colorCode = 'key';
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
{
"name": "color-json",
"version": "3.0.4",
"version": "3.0.5",
"description": "Color JSON in the console with no dependencies",
"type": "module",
"directories": {
"test": "tests"
},
"exports": {
"types": "./color-json.d.ts",
"default": "./color-json.js"
},
"exports": "./index.js",
"scripts": {
"test": "node tests/test.js; TMP_FORCE_NO_COLOR=$FORCE_NO_COLOR; export FORCE_NO_COLOR=true; echo '=====================\nTesting without color\n====================='; node tests/test.js; export FORCE_NO_COLOR=$TMP_FORCE_NO_COLOR; unset TMP_FORCE_NO_COLOR"
},
Expand Down
2 changes: 1 addition & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import colorJson from '../color-json.js';
import colorJson from '../index.js';

console.log('default usage:');
const topPlanets = [
Expand Down

0 comments on commit 2342e61

Please sign in to comment.