Skip to content

Commit

Permalink
Use const/let instead of var
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Apr 16, 2020
1 parent a5a8563 commit 9cc0e3b
Show file tree
Hide file tree
Showing 71 changed files with 776 additions and 776 deletions.
2 changes: 1 addition & 1 deletion examples/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var QRCode = require('../lib')
const QRCode = require('../lib')

QRCode.toString('yo yo yo', function (error, data) {
if (error) {
Expand Down
104 changes: 52 additions & 52 deletions examples/clientsideserver.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var express = require('express')
var app = express()// .createServer()
var http = require('http')
var fs = require('fs')
var QRCode = require('../lib')
var canvasutil = require('canvasutil')
var { createCanvas, loadImage } = require('canvas')
const express = require('express')
const app = express()// .createServer()
const http = require('http')
const fs = require('fs')
const QRCode = require('../lib')
const canvasutil = require('canvasutil')
const { createCanvas, loadImage } = require('canvas')

var path = require('path')
const path = require('path')

// app.use(express.methodOverride())
// app.use(express.bodyParser())
Expand All @@ -29,12 +29,12 @@ app.get('/', function (req, res) {
})
})

var effectHandlers = {}
const effectHandlers = {}

app.get('/generate', function (req, res) {
var q = req.query || {}
const q = req.query || {}

var effect = q.effect || 'plain'
let effect = q.effect || 'plain'
if (!effectHandlers[effect]) {
effect = 'plain'
}
Expand All @@ -48,7 +48,7 @@ app.get('/generate', function (req, res) {
}
})
} else {
var msg = error.message + '\n' + error.stack
const msg = error.message + '\n' + error.stack
res.header('Content-Type', 'text/plain')
res.send(msg)
console.error(msg)
Expand All @@ -72,28 +72,28 @@ effectHandlers.bacon = function (args, cb) {
}

effectHandlers.rounded = function (args, cb) {
var canvas = createCanvas(200, 200)
const canvas = createCanvas(200, 200)
QRCode.toCanvas(canvas, args.text || '', function (err) {
if (err) {
cb(err, canvas)
return
}

var tpx = new canvasutil.PixelCore()
var luma709Only = canvasutil.conversionLib.luma709Only
var up = []
var down = []
var left = []
var right = []
var upPx
var downPx
var leftPx
var rightPx
var r
var t
var l
var d
var corner = 0
const tpx = new canvasutil.PixelCore()
const luma709Only = canvasutil.conversionLib.luma709Only
const up = []
const down = []
const left = []
const right = []
let upPx
let downPx
let leftPx
let rightPx
let r
let t
let l
let d
let corner = 0

tpx.threshold = 100

Expand Down Expand Up @@ -166,16 +166,16 @@ effectHandlers.rounded = function (args, cb) {
}

effectHandlers.remoteImage = function (args, cb) {
var src = args.src
var domain
var uri
let src = args.src
let domain
let uri

if (!src) {
cb(new Error('src required'), null)
} else {
if (src.indexof('://') !== -1) {
src = src.split('://').unshift()
var parts = src.split('/')
const parts = src.split('/')

domain = parts.shift()
uri = parts.join('/')
Expand All @@ -187,22 +187,22 @@ effectHandlers.remoteImage = function (args, cb) {
return
}

var options = {
const options = {
host: domain,
port: 80,
path: uri,
method: 'GET'
}

var req = http.request(options, function (res) {
const req = http.request(options, function (res) {
if (res.statusCode < 200 || res.statusCode > 299) {
cb(new Error('http ' + res.statusCode + ' response code'), null)
return
}

res.setEncoding('utf8')

var data = ''
let data = ''
res.on('data', function (chunk) {
data += chunk
console.log('BODY: ' + chunk)
Expand All @@ -223,27 +223,27 @@ effectHandlers.remoteImage = function (args, cb) {

effectHandlers.image = function (args, cb) {
loadImage(args.src || '').then((img) => {
var convert = canvasutil.conversionLib
var canvas = createCanvas(200, 200)
const convert = canvasutil.conversionLib
const canvas = createCanvas(200, 200)
QRCode.toCanvas(canvas, args.text || '', function (err) {
if (err) {
cb(err, false)
return
}

var codeCtx = canvas.getContext('2d')
var frame = codeCtx.getImageData(0, 0, canvas.width, canvas.width)
var tpx = new canvasutil.PixelCore()
var baconCanvas = createCanvas(canvas.width, canvas.width)
var ctx = baconCanvas.getContext('2d')
var topThreshold = args.darkThreshold || 25
var bottomThreshold = args.lightThreshold || 75
const codeCtx = canvas.getContext('2d')
const frame = codeCtx.getImageData(0, 0, canvas.width, canvas.width)
const tpx = new canvasutil.PixelCore()
const baconCanvas = createCanvas(canvas.width, canvas.width)
const ctx = baconCanvas.getContext('2d')
const topThreshold = args.darkThreshold || 25
const bottomThreshold = args.lightThreshold || 75

tpx.threshold = 50

// scale image
var w = canvas.width
var h = canvas.height
let w = canvas.width
let h = canvas.height

if (img.width > img.height) {
w = w * (canvas.height / h)
Expand All @@ -256,10 +256,10 @@ effectHandlers.image = function (args, cb) {

try {
tpx.iterate(baconCanvas, function (px, i, l, pixels, w, h, pixelCore) {
var luma = (0.2125 * px.r + 0.7154 * px.g + 0.0721 * px.b)
var codeLuma = convert.luma709Only(frame.data[i * 4], frame.data[i * 4 + 1], frame.data[i * 4 + 2])
var yuv
var rgb
const luma = (0.2125 * px.r + 0.7154 * px.g + 0.0721 * px.b)
const codeLuma = convert.luma709Only(frame.data[i * 4], frame.data[i * 4 + 1], frame.data[i * 4 + 2])
let yuv
let rgb

if (codeLuma > pixelCore.threshold) {
if (luma < bottomThreshold) {
Expand Down Expand Up @@ -296,8 +296,8 @@ effectHandlers.image = function (args, cb) {
}

effectHandlers.plain = function (args, cb) {
var canvas = createCanvas(200, 200)
var text = args.text || ''
const canvas = createCanvas(200, 200)
const text = args.text || ''
QRCode.toCanvas(canvas, text || '', function (err) {
cb(err, canvas)
})
Expand Down
4 changes: 2 additions & 2 deletions examples/save.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var QRCode = require('../lib')
const QRCode = require('../lib')

var path = './tmp.png'
const path = './tmp.png'
QRCode.toFile(path, 'life of the party bros', {
color: {
dark: '#00F', // Blue modules
Expand Down
6 changes: 3 additions & 3 deletions examples/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var QRCode = require('../lib')
var http = require('http')
const QRCode = require('../lib')
const http = require('http')

function testQRCode (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' })

var jungleBook = "The moonlight was blocked out of the mouth of the cave, for Shere Khan's\n" +
const jungleBook = "The moonlight was blocked out of the mouth of the cave, for Shere Khan's\n" +
'great square head and shoulders were thrust into the entrance. Tabaqui,\n' +
'behind him, was squeaking: "My lord, my lord, it went in here!"\n' +
'\n' +
Expand Down
8 changes: 4 additions & 4 deletions helper/to-sjis.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var SJIS_UTF8 = [
const SJIS_UTF8 = [
[0x8140, ' 、。,.・:;?!゛゜´`¨^ ̄_ヽヾゝゞ〃仝々〆〇ー―‐/\~∥|…‥‘’“”()〔〕[]{}〈〉《》「」『』【】+-±×'],
[0x8180, '÷=≠<>'],
[0x818f, '¥$¢£%#&*@§☆★'],
Expand Down Expand Up @@ -89,10 +89,10 @@ var SJIS_UTF8 = [
module.exports = function toSJIS (utf8Char) {
if (!utf8Char || utf8Char === '') return

for (var i = 0; i < SJIS_UTF8.length; i++) {
var kanji = SJIS_UTF8[i][1]
for (let i = 0; i < SJIS_UTF8.length; i++) {
const kanji = SJIS_UTF8[i][1]

var posIndex = kanji.indexOf(utf8Char)
const posIndex = kanji.indexOf(utf8Char)
if (posIndex >= 0) {
return SJIS_UTF8[i][0] + posIndex
}
Expand Down
18 changes: 9 additions & 9 deletions lib/browser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

var canPromise = require('./can-promise')
const canPromise = require('./can-promise')

var QRCode = require('./core/qrcode')
var CanvasRenderer = require('./renderer/canvas')
var SvgRenderer = require('./renderer/svg-tag.js')
const QRCode = require('./core/qrcode')
const CanvasRenderer = require('./renderer/canvas')
const SvgRenderer = require('./renderer/svg-tag.js')

function renderCanvas (renderFunc, canvas, text, opts, cb) {
var args = [].slice.call(arguments, 1)
var argsNum = args.length
var isLastArgCb = typeof args[argsNum - 1] === 'function'
const args = [].slice.call(arguments, 1)
const argsNum = args.length
const isLastArgCb = typeof args[argsNum - 1] === 'function'

if (!isLastArgCb && !canPromise()) {
throw new Error('Callback required as last argument')
Expand Down Expand Up @@ -50,7 +50,7 @@ function renderCanvas (renderFunc, canvas, text, opts, cb) {

return new Promise(function (resolve, reject) {
try {
var data = QRCode.create(text, opts)
const data = QRCode.create(text, opts)
resolve(renderFunc(data, canvas, opts))
} catch (e) {
reject(e)
Expand All @@ -59,7 +59,7 @@ function renderCanvas (renderFunc, canvas, text, opts, cb) {
}

try {
var data = QRCode.create(text, opts)
const data = QRCode.create(text, opts)
cb(null, renderFunc(data, canvas, opts))
} catch (e) {
cb(e)
Expand Down
24 changes: 12 additions & 12 deletions lib/core/alignment-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* and their number depends on the symbol version.
*/

var getSymbolSize = require('./utils').getSymbolSize
const getSymbolSize = require('./utils').getSymbolSize

/**
* Calculate the row/column coordinates of the center module of each alignment pattern
Expand All @@ -27,12 +27,12 @@ var getSymbolSize = require('./utils').getSymbolSize
exports.getRowColCoords = function getRowColCoords (version) {
if (version === 1) return []

var posCount = Math.floor(version / 7) + 2
var size = getSymbolSize(version)
var intervals = size === 145 ? 26 : Math.ceil((size - 13) / (2 * posCount - 2)) * 2
var positions = [size - 7] // Last coord is always (size - 7)
const posCount = Math.floor(version / 7) + 2
const size = getSymbolSize(version)
const intervals = size === 145 ? 26 : Math.ceil((size - 13) / (2 * posCount - 2)) * 2
const positions = [size - 7] // Last coord is always (size - 7)

for (var i = 1; i < posCount - 1; i++) {
for (let i = 1; i < posCount - 1; i++) {
positions[i] = positions[i - 1] - intervals
}

Expand All @@ -55,19 +55,19 @@ exports.getRowColCoords = function getRowColCoords (version) {
* Note that the coordinates (6,6), (6,38), (38,6) are occupied by finder patterns
* and are not therefore used for alignment patterns.
*
* var pos = getPositions(7)
* let pos = getPositions(7)
* // [[6,22], [22,6], [22,22], [22,38], [38,22], [38,38]]
*
* @param {Number} version QR Code version
* @return {Array} Array of coordinates
*/
exports.getPositions = function getPositions (version) {
var coords = []
var pos = exports.getRowColCoords(version)
var posLength = pos.length
const coords = []
const pos = exports.getRowColCoords(version)
const posLength = pos.length

for (var i = 0; i < posLength; i++) {
for (var j = 0; j < posLength; j++) {
for (let i = 0; i < posLength; i++) {
for (let j = 0; j < posLength; j++) {
// Skip if position is occupied by finder patterns
if ((i === 0 && j === 0) || // top-left
(i === 0 && j === posLength - 1) || // bottom-left
Expand Down
8 changes: 4 additions & 4 deletions lib/core/alphanumeric-data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Mode = require('./mode')
const Mode = require('./mode')

/**
* Array of characters available in alphanumeric mode
Expand All @@ -9,7 +9,7 @@ var Mode = require('./mode')
*
* @type {Array}
*/
var ALPHA_NUM_CHARS = [
const ALPHA_NUM_CHARS = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
Expand All @@ -34,13 +34,13 @@ AlphanumericData.prototype.getBitsLength = function getBitsLength () {
}

AlphanumericData.prototype.write = function write (bitBuffer) {
var i
let i

// Input data characters are divided into groups of two characters
// and encoded as 11-bit binary codes.
for (i = 0; i + 2 <= this.data.length; i += 2) {
// The character value of the first character is multiplied by 45
var value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45
let value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45

// The character value of the second digit is added to the product
value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1])
Expand Down
6 changes: 3 additions & 3 deletions lib/core/bit-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ function BitBuffer () {
BitBuffer.prototype = {

get: function (index) {
var bufIndex = Math.floor(index / 8)
const bufIndex = Math.floor(index / 8)
return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1) === 1
},

put: function (num, length) {
for (var i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
this.putBit(((num >>> (length - i - 1)) & 1) === 1)
}
},
Expand All @@ -21,7 +21,7 @@ BitBuffer.prototype = {
},

putBit: function (bit) {
var bufIndex = Math.floor(this.length / 8)
const bufIndex = Math.floor(this.length / 8)
if (this.buffer.length <= bufIndex) {
this.buffer.push(0)
}
Expand Down
Loading

0 comments on commit 9cc0e3b

Please sign in to comment.