nv-facutil-pretty-stringify-json
v1.0.1
Published
nv-facutil-pretty-stringify-json ================================ - pretty stringify json - its slow , for repl/cli using - 99% code is AI-generated, only some chinese comments is manual writed.
Readme
nv-facutil-pretty-stringify-json
- pretty stringify json
- its slow , for repl/cli using
- 99% code is AI-generated, only some chinese comments is manual writed.
install
- npm install nv-facutil-pretty-stringify-json
brief
- Customizable indentation - Control spacing for better readability
- Line item limits - Control how many items appear on each line
- Nested structure handling - Intelligently format nested objects and arrays
- Automatic key alignment - Keys in objects are aligned for better readability
- Syntax highlighting - Different colors for different parts of the JSON structure
- Configuration flexibility - Multiple ways to provide formatting options
1. Key Alignment
Objects with multiple properties have their keys aligned for better readability:
{
"x" : 1,
"yy" : 2,
"zzz" : 3,
"wwww": { ... }
}2. Line Item Control
Control how many items appear on each line with max_unit_cnt_each_line:
// With max_unit_cnt_each_line = 3
[
"item1", "item2", "item3",
"item4", "item5", "item6",
"item7", "item8", "item9"
]
// With max_unit_cnt_each_line = 1
[
"item1",
"item2",
"item3",
"item4"
]3. Depth Control
Control how deep nested structures are expanded with max_depth:
// With max_depth = 2
{
"nestedObject": {
"k": "v", "deeplyNested": {"deeper": {...}}
}
}
// With max_depth = 3
{
"nestedObject": {
"k": "v",
"deeplyNested": {
"deeper": {"evenDeeper": {...}}
}
}
}4. Color Highlighting
Elements are color-coded by their type:
- Object keys: Cyan
- Strings: Green
- Numbers: Yellow
- Boolean
true: Purple - Boolean
false: Red - Null: White
- Brackets and colons: Gray
5. Smart Layout
The library makes intelligent decisions about formatting:
- Single-line objects stay on one line
- Leading items in a line are properly aligned
- Complex nested structures are handled recursively
- Unicode characters and special symbols are properly handled
usage
const x = require("nv-facutil-pretty-stringify-json");
// Basic usage with default parameters
console.log(x(myObject));
// Customized usage
console.log(x(myObject, 3, 2)); // 3 items per line, max depth 2arguments
x(
jsonObject,
max_unit_cnt_each_line = 8, // Items per line (default: 8)
max_depth = 2, // Maximum nesting depth (default: 2)
enable_color = true, // Enable ANSI colors (default: true)
indent_unit = 4 // Number of spaces for indentation (default: 4)
)
x(jsonObject, {
max_unit_cnt_each_line: 4,
max_depth: 3,
enable_color: true,
indent_unit: 2
})example
基本测试 (默认参数):
调用参数: prettyJSON(obj, )
{
"a" : 1, "longKeyName": "这是一个很长的键对应的字符串值", "superExtraVeryLongKeyNameForTesting": true, "number": 12345.6789, "boolean": false, "nullValue": null, "stringWithQuotes": "包含\"引号\"的字符串", "nestedObject": {"k": "v", "deeplyNested": {"deeper": {"evenDeeper": {"deepestLevel": "这是最深的嵌套级别"}}}},
"arrayWithObjects" : [
{"id": 1, "name": "第一项"}, {"id": 2, "name": "第二项", "tags": ["tag1", "tag2", "longTagName"]}, {"id": 3, "name": "第三项", "metadata": {"created": "2023-01-01", "status": "active"}}
],
"objectWithArrays" : {
"simpleArray": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "mixedArray": [1, "string", true, null, {"a": 1}, [1, 2]], "matrixLike": [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
},
"complexNesting" : {
"level1": [{"level2": {"array": [{"level3": {"value": "深度嵌套值"}}, [{"level3Alt": [1, 2, {"level4": "最深层"}]}]]}}]
},
"specialChars" : {
"key-with-dash": "值带有破折号", "key_with_underscore": "值带有下划线", "key with spaces": "值带有空格", "🔑": "这是Unicode键", "emoji": "😀 😎 🚀"
},
"largeArray" : [
"item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8",
"item9", "item10", {"complexItem1": "值1"}, {"complexItem2": "值2"}, [1, 2, 3], [4, 5, 6], ["a", "b", "c"]
],
"mixedNesting" : [
1, [2, 3, [4, 5]], {"a": {"b": {"c": [6, 7, {"d": 8}]}}}, [[[9]]], {"outer": {"middle": {"inner": "center"}}}
],
"alignmentTest" : {
"x": 1, "yy": 2, "zzz": 3, "wwww": {"a": 1, "bb": 2, "ccc": 3}
}
}
行内元素限制为3,深度为2:
调用参数: prettyJSON(obj, 3, 2)
{
"a" : 1, "longKeyName": "这是一个很长的键对应的字符串值", "superExtraVeryLongKeyNameForTesting": true,
"number" : 12345.6789, "boolean": false, "nullValue": null,
"stringWithQuotes" : "包含\"引号\"的字符串", "nestedObject": {"k": "v", "deeplyNested": {"deeper": {"evenDeeper": {"deepestLevel": "这是最深的嵌套级别"}}}}, "arrayWithObjects": [{"id": 1, "name": "第一项"}, {"id": 2, "name": "第二项", "tags": ["tag1", "tag2", "longTagName"]}, {"id": 3, "name": "第三项", "metadata": {"created": "2023-01-01", "status": "active"}}],
"objectWithArrays" : {
"simpleArray": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "mixedArray": [1, "string", true, null, {"a": 1}, [1, 2]], "matrixLike": [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
},
"complexNesting" : {
"level1": [{"level2": {"array": [{"level3": {"value": "深度嵌套值"}}, [{"level3Alt": [1, 2, {"level4": "最深层"}]}]]}}]
},
"specialChars" : {
"key-with-dash" : "值带有破折号", "key_with_underscore": "值带有下划线", "key with spaces": "值带有空格",
"🔑" : "这是Unicode键", "emoji": "😀 😎 🚀"
},
"largeArray" : [
"item1", "item2", "item3",
"item4", "item5", "item6",
"item7", "item8", "item9",
"item10", {"complexItem1": "值1"}, {"complexItem2": "值2"},
[1, 2, 3], [4, 5, 6], ["a", "b", "c"]
],
"mixedNesting" : [
1, [2, 3, [4, 5]], {"a": {"b": {"c": [6, 7, {"d": 8}]}}},
[[[9]]], {"outer": {"middle": {"inner": "center"}}}
],
"alignmentTest" : {
"x" : 1, "yy": 2, "zzz": 3,
"wwww": {"a": 1, "bb": 2, "ccc": 3}
}
}
行内元素限制为1,深度为3:
调用参数: prettyJSON(obj, 1, 3)
{
"a" : 1,
"longKeyName" : "这是一个很长的键对应的字符串值",
"superExtraVeryLongKeyNameForTesting": true,
"number" : 12345.6789,
"boolean" : false,
"nullValue" : null,
"stringWithQuotes" : "包含\"引号\"的字符串",
"nestedObject" : {
"k" : "v",
"deeplyNested": {
"deeper": {"evenDeeper": {"deepestLevel": "这是最深的嵌套级别"}}
}
},
"arrayWithObjects" : [
{
"id" : 1,
"name": "第一项"
},
{
"id" : 2,
"name": "第二项",
"tags": ["tag1", "tag2", "longTagName"]
},
{
"id" : 3,
"name" : "第三项",
"metadata": {"created": "2023-01-01", "status": "active"}
}
],
"objectWithArrays" : {
"simpleArray": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
],
"mixedArray" : [
1,
"string",
true,
null,
{"a": 1},
[1, 2]
],
"matrixLike" : [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
},
"complexNesting" : {
"level1": [
{"level2": {"array": [{"level3": {"value": "深度嵌套值"}}, [{"level3Alt": [1, 2, {"level4": "最深层"}]}]]}}
]
},
"specialChars" : {
"key-with-dash" : "值带有破折号",
"key_with_underscore": "值带有下划线",
"key with spaces" : "值带有空格",
"🔑" : "这是Unicode键",
"emoji" : "😀 😎 🚀"
},
"largeArray" : [
"item1",
"item2",
"item3",
"item4",
"item5",
"item6",
"item7",
"item8",
"item9",
"item10",
{
"complexItem1": "值1"
},
{
"complexItem2": "值2"
},
[
1,
2,
3
],
[
4,
5,
6
],
[
"a",
"b",
"c"
]
],
"mixedNesting" : [
1,
[
2,
3,
[4, 5]
],
{
"a": {"b": {"c": [6, 7, {"d": 8}]}}
},
[
[[9]]
],
{
"outer": {"middle": {"inner": "center"}}
}
],
"alignmentTest" : {
"x" : 1,
"yy" : 2,
"zzz" : 3,
"wwww": {
"a" : 1,
"bb" : 2,
"ccc": 3
}
}
}
行内元素限制为5,深度为1:
调用参数: prettyJSON(obj, 5, 1)
{
"a" : 1, "longKeyName": "这是一个很长的键对应的字符串值", "superExtraVeryLongKeyNameForTesting": true, "number": 12345.6789, "boolean": false,
"nullValue" : null, "stringWithQuotes": "包含\"引号\"的字符串", "nestedObject": {"k": "v", "deeplyNested": {"deeper": {"evenDeeper": {"deepestLevel": "这是最深的嵌套级别"}}}}, "arrayWithObjects": [{"id": 1, "name": "第一项"}, {"id": 2, "name": "第二项", "tags": ["tag1", "tag2", "longTagName"]}, {"id": 3, "name": "第三项", "metadata": {"created": "2023-01-01", "status": "active"}}], "objectWithArrays": {"simpleArray": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "mixedArray": [1, "string", true, null, {"a": 1}, [1, 2]], "matrixLike": [[1, 2, 3], [4, 5, 6], [7, 8, 9]]},
"complexNesting" : {"level1": [{"level2": {"array": [{"level3": {"value": "深度嵌套值"}}, [{"level3Alt": [1, 2, {"level4": "最深层"}]}]]}}]}, "specialChars": {"key-with-dash": "值带有破折号", "key_with_underscore": "值带有下划线", "key with spaces": "值带有空格", "🔑": "这是Unicode键", "emoji": "😀 😎 🚀"}, "largeArray": ["item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10", {"complexItem1": "值1"}, {"complexItem2": "值2"}, [1, 2, 3], [4, 5, 6], ["a", "b", "c"]], "mixedNesting": [1, [2, 3, [4, 5]], {"a": {"b": {"c": [6, 7, {"d": 8}]}}}, [[[9]]], {"outer": {"middle": {"inner": "center"}}}], "alignmentTest": {"x": 1, "yy": 2, "zzz": 3, "wwww": {"a": 1, "bb": 2, "ccc": 3}}
}
无颜色:
调用参数: prettyJSON(obj, 3, 2, false)
{
"a" : 1, "longKeyName": "这是一个很长的键对应的字符串值", "superExtraVeryLongKeyNameForTesting": true,
"number" : 12345.6789, "boolean": false, "nullValue": null,
"stringWithQuotes" : "包含\"引号\"的字符串", "nestedObject": {"k": "v", "deeplyNested": {"deeper": {"evenDeeper": {"deepestLevel": "这是最深的嵌套级别"}}}}, "arrayWithObjects": [{"id": 1, "name": "第一项"}, {"id": 2, "name": "第二项", "tags": ["tag1", "tag2", "longTagName"]}, {"id": 3, "name": "第三项", "metadata": {"created": "2023-01-01", "status": "active"}}],
"objectWithArrays" : {
"simpleArray": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "mixedArray": [1, "string", true, null, {"a": 1}, [1, 2]], "matrixLike": [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
},
"complexNesting" : {
"level1": [{"level2": {"array": [{"level3": {"value": "深度嵌套值"}}, [{"level3Alt": [1, 2, {"level4": "最深层"}]}]]}}]
},
"specialChars" : {
"key-with-dash" : "值带有破折号", "key_with_underscore": "值带有下划线", "key with spaces": "值带有空格",
"🔑" : "这是Unicode键", "emoji": "😀 😎 🚀"
},
"largeArray" : [
"item1", "item2", "item3",
"item4", "item5", "item6",
"item7", "item8", "item9",
"item10", {"complexItem1": "值1"}, {"complexItem2": "值2"},
[1, 2, 3], [4, 5, 6], ["a", "b", "c"]
],
"mixedNesting" : [
1, [2, 3, [4, 5]], {"a": {"b": {"c": [6, 7, {"d": 8}]}}},
[[[9]]], {"outer": {"middle": {"inner": "center"}}}
],
"alignmentTest" : {
"x" : 1, "yy": 2, "zzz": 3,
"wwww": {"a": 1, "bb": 2, "ccc": 3}
}
}
无缩进:
调用参数: prettyJSON(obj, null, null, true, 0)
{"a": 1, "longKeyName": "这是一个很长的键对应的字符串值", "superExtraVeryLongKeyNameForTesting": true, "number": 12345.6789, "boolean": false, "nullValue": null, "stringWithQuotes": "包含\"引号\"的字符串", "nestedObject": {"k": "v", "deeplyNested": {"deeper": {"evenDeeper": {"deepestLevel": "这是最深的嵌套级别"}}}}, "arrayWithObjects": [{"id": 1, "name": "第一项"}, {"id": 2, "name": "第二项", "tags": ["tag1", "tag2", "longTagName"]}, {"id": 3, "name": "第三项", "metadata": {"created": "2023-01-01", "status": "active"}}], "objectWithArrays": {"simpleArray": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "mixedArray": [1, "string", true, null, {"a": 1}, [1, 2]], "matrixLike": [[1, 2, 3], [4, 5, 6], [7, 8, 9]]}, "complexNesting": {"level1": [{"level2": {"array": [{"level3": {"value": "深度嵌套值"}}, [{"level3Alt": [1, 2, {"level4": "最深层"}]}]]}}]}, "specialChars": {"key-with-dash": "值带有破折号", "key_with_underscore": "值带有下划线", "key with spaces": "值带有空格", "🔑": "这是Unicode键", "emoji": "😀 😎 🚀"}, "largeArray": ["item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10", {"complexItem1": "值1"}, {"complexItem2": "值2"}, [1, 2, 3], [4, 5, 6], ["a", "b", "c"]], "mixedNesting": [1, [2, 3, [4, 5]], {"a": {"b": {"c": [6, 7, {"d": 8}]}}}, [[[9]]], {"outer": {"middle": {"inner": "center"}}}], "alignmentTest": {"x": 1, "yy": 2, "zzz": 3, "wwww": {"a": 1, "bb": 2, "ccc": 3}}}
使用对象参数:
调用参数: prettyJSON(obj, {"max_unit_cnt_each_line":4,"max_depth":3,"enable_color":true,"indent_unit":2})
{
"a" : 1, "longKeyName": "这是一个很长的键对应的字符串值", "superExtraVeryLongKeyNameForTesting": true, "number": 12345.6789,
"boolean" : false, "nullValue": null, "stringWithQuotes": "包含\"引号\"的字符串", "nestedObject": {"k": "v", "deeplyNested": {"deeper": {"evenDeeper": {"deepestLevel": "这是最深的嵌套级别"}}}},
"arrayWithObjects" : [
{
"id": 1, "name": "第一项"
},
{
"id": 2, "name": "第二项", "tags": ["tag1", "tag2", "longTagName"]
},
{
"id": 3, "name": "第三项", "metadata": {"created": "2023-01-01", "status": "active"}
}
],
"objectWithArrays" : {
"simpleArray": [
1, 2, 3, 4,
5, 6, 7, 8,
9, 10
],
"mixedArray" : [
1, "string", true, null,
{"a": 1}, [1, 2]
],
"matrixLike" : [
[1, 2, 3], [4, 5, 6], [7, 8, 9]
]
},
"complexNesting" : {
"level1": [
{"level2": {"array": [{"level3": {"value": "深度嵌套值"}}, [{"level3Alt": [1, 2, {"level4": "最深层"}]}]]}}
]
},
"specialChars" : {
"key-with-dash" : "值带有破折号", "key_with_underscore": "值带有下划线", "key with spaces": "值带有空格", "🔑": "这是Unicode键",
"emoji" : "😀 😎 🚀"
},
"largeArray" : [
"item1", "item2", "item3", "item4",
"item5", "item6", "item7", "item8",
"item9", "item10", {"complexItem1": "值1"}, {"complexItem2": "值2"},
[
1, 2, 3
],
[
4, 5, 6
],
[
"a", "b", "c"
]
],
"mixedNesting" : [
1, [2, 3, [4, 5]], {"a": {"b": {"c": [6, 7, {"d": 8}]}}}, [[[9]]],
{
"outer": {"middle": {"inner": "center"}}
}
],
"alignmentTest" : {
"x" : 1, "yy": 2, "zzz": 3, "wwww": {"a": 1, "bb": 2, "ccc": 3}
}
}
测试对象中的数组嵌套:
调用参数: prettyJSON(obj, 2, 3)
{
"simpleArray": [
1, 2,
3, 4,
5, 6,
7, 8,
9, 10
],
"mixedArray" : [
1, "string",
true, null,
{
"a": 1
},
[
1, 2
]
],
"matrixLike" : [
[
1, 2,
3
],
[
4, 5,
6
],
[
7, 8,
9
]
]
}
测试深层嵌套结构:
调用参数: prettyJSON(obj, 2, 5)
{
"level1": [
{
"level2": {
"array": [
{"level3": {"value": "深度嵌套值"}}, [{"level3Alt": [1, 2, {"level4": "最深层"}]}]
]
}
}
]
}
测试键对齐功能:
调用参数: prettyJSON(obj, 1, 2)
{
"x" : 1,
"yy" : 2,
"zzz" : 3,
"wwww": {
"a" : 1,
"bb" : 2,
"ccc": 3
}
}METHODS
APIS
LICENSE
- ISC
