-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONFormatter.js
More file actions
39 lines (36 loc) · 1009 Bytes
/
JSONFormatter.js
File metadata and controls
39 lines (36 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export default class JSONFormatter {
static getTag(value) {
if (value == null) {
return value === undefined ? '[object Undefined]' : '[object Null]';
}
return toString.call(value);
}
static isObjectLike(value) {
return typeof value === 'object' && value !== null;
}
static isPlainObject(value) {
if (!JSONFormatter.isObjectLike(value) || JSONFormatter.getTag(value) !== '[object Object]') {
return false;
}
if (Object.getPrototypeOf(value) === null) {
return true;
}
let proto = value;
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}
return Object.getPrototypeOf(value) === proto;
}
// eslint-disable-next-line class-methods-use-this
format(timestamp, category, level, message, meta) {
return JSON.stringify(
{
level,
message,
timestamp,
category,
...(JSONFormatter.isPlainObject(meta) ? meta : { meta }),
},
);
}
}