stringify-with-whitelist
v1.0.1
Published
Translate a JavaScript object or array into a formatted string, only outputting the properties within the whitelist.
Downloads
4
Readme
stringifyWithWhitelist
- This npm package provides a method called stringifyWithWhitelist that allows you to convert a JavaScript object into a string, but only includes key paths in a whitelist. This method marks circular references with "[Circular]" when dealing with objects containing circular references.
Installation
- Install this package using npm:
npm install stringify-with-whitelist
Usage
- First, import the stringifyWithWhitelist method:
const { stringifyWithWhitelist } = require('stringify-with-whitelist');
Then, you can pass any JavaScript object to the stringifyWithWhitelist method and provide an array containing whitelist key paths so that only these paths are included when converting to a string.
Copy code
const obj = {
a: {
b: {
c: 1,
d: 2
},
e: 3
},
f: [4, 5, 6]
};
const whitelist = ['a.b.c', 'a.e', 'f.*'];
const result = stringifyWithWhitelist(obj, whitelist);
console.log(result);
Output
css
{
a: {
b: {
c: 1
},
e: 3
},
f: [
4,
5,
6
]
}
API
- stringifyWithWhitelist(obj, whitelist)
- obj (Object): The JavaScript object to be converted into a string.
- whitelist (Array): An array of strings containing the key paths to be included in the output. Use an asterisk () to represent array elements. Examples: 'a.b.c', 'f.'. Return value: A string representing the JavaScript object filtered through the whitelist.
Note: This method marks circular references with "[Circular]" when dealing with objects containing circular references.
Contributions
- If you would like to contribute to this project, please see the code repository on GitHub for more details.
License
- This software is released under the MIT License. For more information, please refer to the LICENSE file.
stringifyWithWhitelist
- 这个npm包提供了一个名为stringifyWithWhitelist的方法,它可以让您将一个JavaScript对象转换成一个字符串,但只包括在白名单中的键路径。这个方法在处理包含循环引用的对象时,会用"[Circular]"标记循环引用。
安装
- 使用npm安装此包:
npm install stringify-with-whitelist
使用
- 首先,导入stringifyWithWhitelist方法:
const { stringifyWithWhitelist } = require('stringify-with-whitelist');
然后,您可以将任意JavaScript对象传递给stringifyWithWhitelist方法,并提供一个包含白名单键路径的数组,以便在转换为字符串时仅包含这些路径。
Copy code
const obj = {
a: {
b: {
c: 1,
d: 2
},
e: 3
},
f: [4, 5, 6]
};
const whitelist = ['a.b.c', 'a.e', 'f.*'];
const result = stringifyWithWhitelist(obj, whitelist);
console.log(result);
输出:
css
Copy code
{
a: {
b: {
c: 1
},
e: 3
},
f: [
4,
5,
6
]
}
API
- stringifyWithWhitelist(obj, whitelist)
- obj (Object): 要转换为字符串的JavaScript对象。
- whitelist (Array): 字符串数组,包含要包含在输出中的键路径。使用星号 () 表示数组元素。例如:'a.b.c', 'f.'。 返回值:一个字符串,表示经过白名单过滤的JavaScript对象。
注意:此方法在处理包含循环引用的对象时,会用"[Circular]"标记循环引用。
贡献
- 如果您想为此项目做出贡献,请查看GitHub上的代码库以获取详细信息。
许可证
- 此软件在MIT许可证下发布。有关详细信息,请参阅LICENSE文件。