Skip to content

Commit

Permalink
feat: env vars to adjust warning behaviour (#2)
Browse files Browse the repository at this point in the history
NUXT_ENV_DEVALUE_LOG_LEVEL to configure log level e.g. to silence warnings
NUXT_ENV_DEVALUE_LOG_LIMIT to configure max number of warnings per devalue call
  • Loading branch information
aldarund committed Oct 15, 2018
1 parent 7d350dc commit a5c6834
Show file tree
Hide file tree
Showing 4 changed files with 445 additions and 150 deletions.
17 changes: 9 additions & 8 deletions package.json
Expand Up @@ -14,16 +14,17 @@
"consola": "^1.4.3"
},
"devDependencies": {
"@types/mocha": "^2.2.44",
"@types/node": "^8.0.53",
"glob": "^7.1.2",
"mocha": "^4.0.1",
"rollup": "^0.52.0",
"rollup-plugin-typescript": "^0.8.1",
"@types/mocha": "^5.2.5",
"@types/node": "^10.11.7",
"glob": "^7.1.3",
"mocha": "^5.2.0",
"rollup": "^0.66.6",
"rollup-plugin-typescript": "^1.0.0",
"rollup-plugin-virtual": "^1.0.1",
"sander": "^0.6.0",
"ts-node": "^3.3.0",
"typescript": "^2.6.2"
"ts-node": "^7.0.1",
"tslib": "^1.9.3",
"typescript": "^3.1.3"
},
"scripts": {
"build-declarations": "tsc -d && node scripts/move-type-declarations.js",
Expand Down
5 changes: 2 additions & 3 deletions rollup.config.js
Expand Up @@ -4,13 +4,12 @@ import pkg from './package.json';
export default {
input: 'src/index.ts',
output: [
{ file: pkg.main, format: 'umd' },
{ file: pkg.main, format: 'umd', name: 'devalue' },
{ file: pkg.module, format: 'es' }
],
name: 'devalue',
plugins: [
typescript({
typescript: require('typescript')
})
]
};
};
19 changes: 15 additions & 4 deletions src/index.ts
Expand Up @@ -4,12 +4,23 @@ const reserved = /^(?:do|if|in|for|int|let|new|try|var|byte|case|char|else|enum|
const unsafe = /[<>\/\u2028\u2029]/g;
const escaped: Record<string, string> = { '<': '\\u003C', '>' : '\\u003E', '/': '\\u002F', '\u2028': '\\u2028', '\u2029': '\\u2029' };
const objectProtoOwnPropertyNames = Object.getOwnPropertyNames(Object.prototype).sort().join('\0');
// workaround to disable warnings, see https://github.com/nuxt/nuxt.js/issues/4026 for details
const defaultLogLevel = process.env.NUXT_ENV_DEVALUE_LOG_LEVEL || 'warn';
const logLimit = parseInt(process.env.NUXT_ENV_DEVALUE_LOG_LIMIT) || 99;


export default function devalue(value: any, level = 'warn') {
export default function devalue(value: any, level = defaultLogLevel) {
const counts = new Map();

let n = 0;
let logNum = 0;

function log(message: string) {
if (logNum < logLimit) {
consola[level](message);
logNum+=1
}
}


function walk(thing: any) {
if (typeof thing === 'function') {
Expand Down Expand Up @@ -53,10 +64,10 @@ export default function devalue(value: any, level = 'warn') {
Object.getOwnPropertyNames(proto).sort().join('\0') !== objectProtoOwnPropertyNames
) {
if (typeof thing.toJSON !== "function") {
consola[level](`Cannot stringify arbitrary non-POJOs ${thing.constructor.name}`);
log(`Cannot stringify arbitrary non-POJOs ${thing.constructor.name}`);
}
} else if (Object.getOwnPropertySymbols(thing).length > 0) {
consola[level](`Cannot stringify POJOs with symbolic keys ${Object.getOwnPropertySymbols(thing).map(symbol => symbol.toString())}`);
log(`Cannot stringify POJOs with symbolic keys ${Object.getOwnPropertySymbols(thing).map(symbol => symbol.toString())}`);
} else {
Object.keys(thing).forEach(key => walk(thing[key]));
}
Expand Down

0 comments on commit a5c6834

Please sign in to comment.