Skip to content

Commit

Permalink
Merge pull request #950 from salesh/947-var-let-const-replacing
Browse files Browse the repository at this point in the history
Replacing var with let/const, somewhere left var because scope, somew…
  • Loading branch information
jsumners authored Jan 16, 2021
2 parents 6768d4e + 1442626 commit c2de8e1
Show file tree
Hide file tree
Showing 53 changed files with 268 additions and 244 deletions.
9 changes: 8 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
{ "extends": "standard" }
{
"extends": [
"standard"
],
"rules": {
"no-var": "off"
}
}
10 changes: 5 additions & 5 deletions benchmarks/child-creation.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,35 @@ require('bole').output({

const run = bench([
function benchBunyanCreation (cb) {
var child = blog.child({ a: 'property' })
const child = blog.child({ a: 'property' })
for (var i = 0; i < max; i++) {
child.info({ hello: 'world' })
}
setImmediate(cb)
},
function benchBoleCreation (cb) {
var child = bole('child')
const child = bole('child')
for (var i = 0; i < max; i++) {
child.info({ hello: 'world' })
}
setImmediate(cb)
},
function benchPinoCreation (cb) {
var child = plogDest.child({ a: 'property' })
const child = plogDest.child({ a: 'property' })
for (var i = 0; i < max; i++) {
child.info({ hello: 'world' })
}
setImmediate(cb)
},
function benchPinoAsyncCreation (cb) {
var child = plogAsync.child({ a: 'property' })
const child = plogAsync.child({ a: 'property' })
for (var i = 0; i < max; i++) {
child.info({ hello: 'world' })
}
setImmediate(cb)
},
function benchPinoNodeStreamCreation (cb) {
var child = plogNodeStream.child({ a: 'property' })
const child = plogNodeStream.child({ a: 'property' })
for (var i = 0; i < max; i++) {
child.info({ hello: 'world' })
}
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/internal/just-pino.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ const run = bench([
setImmediate(cb)
},
function benchPinoChildCreation (cb) {
var child = plog.child({ a: 'property' })
const child = plog.child({ a: 'property' })
for (var i = 0; i < max; i++) {
child.info({ hello: 'world' })
}
setImmediate(cb)
},
function benchPinoDestChildCreation (cb) {
var child = plogDest.child({ a: 'property' })
const child = plogDest.child({ a: 'property' })
for (var i = 0; i < max; i++) {
child.info({ hello: 'world' })
}
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/long-string.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ require('bole').output({
stream: dest
}).setFastTime(true)

var chill = winston.createLogger({
const chill = winston.createLogger({
transports: [
new winston.transports.Stream({
stream: fs.createWriteStream('/dev/null')
})
]
})

var run = bench([
const run = bench([
function benchBunyan (cb) {
for (var i = 0; i < max; i++) {
blog.info(longStr)
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/utils/runbench.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function runBenchmark (name, done) {
}

function sum (arr) {
var result = 0
let result = 0
for (var i = 0; i < arr.length; i += 1) {
result += Number.parseFloat(arr[i].time)
}
Expand All @@ -105,8 +105,8 @@ function displayResults (results) {
const benchmark = results[benchNames[i]]
const loggers = Object.keys(benchmark)
for (var j = 0; j < loggers.length; j += 1) {
var logger = benchmark[loggers[j]]
var average = sum(logger) / logger.length
const logger = benchmark[loggers[j]]
const average = sum(logger) / logger.length
console.log(`${loggers[j]} average: ${average.toFixed(3)}ms`)
}
}
Expand Down
18 changes: 9 additions & 9 deletions benchmarks/utils/wrap-log-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ const code = readFileSync(
const { Console } = require('console')

function build (dest) {
var sandbox = {
const sandbox = {
module: {},
console: new Console(dest, dest)
}
var context = vm.createContext(sandbox)
const context = vm.createContext(sandbox)

var script = new vm.Script(code)
const script = new vm.Script(code)
script.runInContext(context)

var loglevel = sandbox.log
const loglevel = sandbox.log

var originalFactory = loglevel.methodFactory
const originalFactory = loglevel.methodFactory
loglevel.methodFactory = function (methodName, logLevel, loggerName) {
var rawMethod = originalFactory(methodName, logLevel, loggerName)
const rawMethod = originalFactory(methodName, logLevel, loggerName)

return function () {
var time = new Date()
var array
const time = new Date()
let array
if (typeof arguments[0] === 'string') {
arguments[0] = '[' + time.toISOString() + '] ' + arguments[0]
rawMethod.apply(null, arguments)
Expand All @@ -48,7 +48,7 @@ function build (dest) {
module.exports = build

if (require.main === module) {
var loglevel = build(process.stdout)
const loglevel = build(process.stdout)
loglevel.info('hello')
loglevel.info({ hello: 'world' })
loglevel.info('hello %j', { hello: 'world' })
Expand Down
77 changes: 39 additions & 38 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

var format = require('quick-format-unescaped')
const format = require('quick-format-unescaped')

module.exports = pino

var _console = pfGlobalThisOrFallback().console || {}
var stdSerializers = {
const _console = pfGlobalThisOrFallback().console || {}
const stdSerializers = {
mapHttpRequest: mock,
mapHttpResponse: mock,
wrapRequestSerializer: passthrough,
Expand All @@ -20,33 +20,33 @@ function pino (opts) {
opts = opts || {}
opts.browser = opts.browser || {}

var transmit = opts.browser.transmit
const transmit = opts.browser.transmit
if (transmit && typeof transmit.send !== 'function') { throw Error('pino: transmit option must have a send function') }

var proto = opts.browser.write || _console
const proto = opts.browser.write || _console
if (opts.browser.write) opts.browser.asObject = true
var serializers = opts.serializers || {}
var serialize = Array.isArray(opts.browser.serialize)
const serializers = opts.serializers || {}
const serialize = Array.isArray(opts.browser.serialize)
? opts.browser.serialize.filter(function (k) {
return k !== '!stdSerializers.err'
})
return k !== '!stdSerializers.err'
})
: opts.browser.serialize === true ? Object.keys(serializers) : false
var stdErrSerialize = opts.browser.serialize
let stdErrSerialize = opts.browser.serialize

if (
Array.isArray(opts.browser.serialize) &&
opts.browser.serialize.indexOf('!stdSerializers.err') > -1
) stdErrSerialize = false

var levels = ['error', 'fatal', 'warn', 'info', 'debug', 'trace']
const levels = ['error', 'fatal', 'warn', 'info', 'debug', 'trace']

if (typeof proto === 'function') {
proto.error = proto.fatal = proto.warn =
proto.info = proto.debug = proto.trace = proto
}
if (opts.enabled === false) opts.level = 'silent'
var level = opts.level || 'info'
var logger = Object.create(proto)
const level = opts.level || 'info'
const logger = Object.create(proto)
if (!logger.log) logger.log = noop

Object.defineProperty(logger, 'levelVal', {
Expand All @@ -57,7 +57,7 @@ function pino (opts) {
set: setLevel
})

var setOpts = {
const setOpts = {
transmit,
serialize,
asObject: opts.browser.asObject,
Expand Down Expand Up @@ -108,7 +108,7 @@ function pino (opts) {
if (!bindings) {
throw new Error('missing bindings for child Pino')
}
var bindingsSerializers = bindings.serializers
const bindingsSerializers = bindings.serializers
if (serialize && bindingsSerializers) {
var childSerializers = Object.assign({}, serializers, bindingsSerializers)
var childSerialize = opts.browser.serialize === true
Expand Down Expand Up @@ -164,8 +164,9 @@ pino.stdSerializers = stdSerializers
pino.stdTimeFunctions = Object.assign({}, { nullTime, epochTime, unixTime, isoTime })

function set (opts, logger, level, fallback) {
var proto = Object.getPrototypeOf(logger)
logger[level] = logger.levelVal > logger.levels.values[level] ? noop
const proto = Object.getPrototypeOf(logger)
logger[level] = logger.levelVal > logger.levels.values[level]
? noop
: (proto[level] ? proto[level] : (_console[level] || _console[fallback] || noop))

wrap(opts, logger, level)
Expand All @@ -176,9 +177,9 @@ function wrap (opts, logger, level) {

logger[level] = (function (write) {
return function LOG () {
var ts = opts.timestamp()
var args = new Array(arguments.length)
var proto = (Object.getPrototypeOf && Object.getPrototypeOf(this) === _console) ? _console : this
const ts = opts.timestamp()
const args = new Array(arguments.length)
const proto = (Object.getPrototypeOf && Object.getPrototypeOf(this) === _console) ? _console : this
for (var i = 0; i < args.length; i++) args[i] = arguments[i]

if (opts.serialize && !opts.asObject) {
Expand All @@ -188,9 +189,9 @@ function wrap (opts, logger, level) {
else write.apply(proto, args)

if (opts.transmit) {
var transmitLevel = opts.transmit.level || logger.level
var transmitValue = pino.levels.values[transmitLevel]
var methodValue = pino.levels.values[level]
const transmitLevel = opts.transmit.level || logger.level
const transmitValue = pino.levels.values[transmitLevel]
const methodValue = pino.levels.values[level]
if (methodValue < transmitValue) return
transmit(this, {
ts,
Expand All @@ -208,14 +209,14 @@ function wrap (opts, logger, level) {

function asObject (logger, level, args, ts) {
if (logger._serialize) applySerializers(args, logger._serialize, logger.serializers, logger._stdErrSerialize)
var argsCloned = args.slice()
var msg = argsCloned[0]
var o = {}
const argsCloned = args.slice()
let msg = argsCloned[0]
const o = {}
if (ts) {
o.time = ts
}
o.level = pino.levels.values[level]
var lvl = (logger._childLevel | 0) + 1
let lvl = (logger._childLevel | 0) + 1
if (lvl < 1) lvl = 1
// deliberate, catching objects, arrays
if (msg !== null && typeof msg === 'object') {
Expand All @@ -229,11 +230,11 @@ function asObject (logger, level, args, ts) {
}

function applySerializers (args, serialize, serializers, stdErrSerialize) {
for (var i in args) {
for (const i in args) {
if (stdErrSerialize && args[i] instanceof Error) {
args[i] = pino.stdSerializers.err(args[i])
} else if (typeof args[i] === 'object' && !Array.isArray(args[i])) {
for (var k in args[i]) {
for (const k in args[i]) {
if (serialize && serialize.indexOf(k) > -1 && k in serializers) {
args[i][k] = serializers[k](args[i][k])
}
Expand All @@ -244,7 +245,7 @@ function applySerializers (args, serialize, serializers, stdErrSerialize) {

function bind (parent, bindings, level) {
return function () {
var args = new Array(1 + arguments.length)
const args = new Array(1 + arguments.length)
args[0] = bindings
for (var i = 1; i < args.length; i++) {
args[i] = arguments[i - 1]
Expand All @@ -254,12 +255,12 @@ function bind (parent, bindings, level) {
}

function transmit (logger, opts, args) {
var send = opts.send
var ts = opts.ts
var methodLevel = opts.methodLevel
var methodValue = opts.methodValue
var val = opts.val
var bindings = logger._logEvent.bindings
const send = opts.send
const ts = opts.ts
const methodLevel = opts.methodLevel
const methodValue = opts.methodValue
const val = opts.val
const bindings = logger._logEvent.bindings

applySerializers(
args,
Expand Down Expand Up @@ -291,12 +292,12 @@ function createLogEventShape (bindings) {
}

function asErrValue (err) {
var obj = {
const obj = {
type: err.constructor.name,
msg: err.message,
stack: err.stack
}
for (var key in err) {
for (const key in err) {
if (obj[key] === undefined) {
obj[key] = err[key]
}
Expand Down
12 changes: 7 additions & 5 deletions lib/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function setLevel (level) {
const useOnlyCustomLevelsVal = this[useOnlyCustomLevelsSym]
const hook = this[hooksSym].logMethod

for (var key in values) {
for (const key in values) {
if (levelVal > values[key]) {
this[key] = noop
continue
Expand Down Expand Up @@ -123,10 +123,12 @@ function isLevelEnabled (logLevel) {
}

function mappings (customLevels = null, useOnlyCustomLevels = false) {
const customNums = customLevels ? Object.keys(customLevels).reduce((o, k) => {
o[customLevels[k]] = k
return o
}, {}) : null
const customNums = customLevels
? Object.keys(customLevels).reduce((o, k) => {
o[customLevels[k]] = k
return o
}, {})
: null

const labels = Object.assign(
Object.create(Object.prototype, { Infinity: { value: 'silent' } }),
Expand Down
Loading

0 comments on commit c2de8e1

Please sign in to comment.