t-comm
Version:
专业、稳定、纯粹的工具库
30 lines (27 loc) • 657 B
JavaScript
import * as fs from 'fs';
/**
* 解析带注释的 json 文件
* @param content 原始文件内容
* @returns json数据
*/
function parseCommentJson(content) {
var newContent = content.replace(/(?:\s+|^)\/\/[^\n]*/g, '');
// console.log('[newContent]', newContent);
var json = {};
try {
json = JSON.parse(newContent);
} catch (err) {}
return json;
}
/**
* 获取带注释的 json 文件内容
* @param file 文件路径
* @returns json数据
*/
function readCommentJson(file) {
var content = fs.readFileSync(file, {
encoding: 'utf-8'
});
return parseCommentJson(content);
}
export { parseCommentJson, readCommentJson };