Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code to use ESM modules #22

Merged
merged 54 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
1f6eeae
Change package type to module
jozefizso Oct 20, 2024
ca4a7d2
Refactor `AlphanumericData` function to a class
jozefizso Oct 20, 2024
89bc985
Refactor `BitBuffer` function to a class
jozefizso Oct 20, 2024
f4eb0d7
Refactor `BitMatrix` function to a class
jozefizso Oct 20, 2024
f8eed06
Refactor `ByteData` function to a class
jozefizso Oct 20, 2024
ab2a77c
Refactor `KanjiData` function to a class
jozefizso Oct 20, 2024
f0d7374
Refactor `NumericData` function to a class
jozefizso Oct 20, 2024
a85df33
Refactor `ReedSolomonEncoder` function to a class
jozefizso Oct 20, 2024
a5297d3
Refactor `WritableStream` function to a class
jozefizso Oct 20, 2024
5949b77
Migrate `index.js` to esm module
jozefizso Oct 20, 2024
acc0ae4
Migrate `can-promise.js` to esm module
jozefizso Oct 20, 2024
498ec19
Migrate `server.js` to esm module
jozefizso Oct 20, 2024
969e776
Migrate `browser.js` to esm module
jozefizso Oct 20, 2024
877face
Migrate `renderer/canvas.js` to esm module
jozefizso Oct 20, 2024
00c2d70
Migrate `renderer/png.js` to esm module
jozefizso Oct 20, 2024
a9259b4
Migrate `renderer/svg-tag.js` to esm module
jozefizso Oct 20, 2024
7e36154
Migrate `renderer/svg.js` to esm module
jozefizso Oct 20, 2024
90c26b4
Migrate `renderer/terminal.js` to esm module
jozefizso Oct 20, 2024
f077292
Migrate `renderer/utf8.js` to esm module
jozefizso Oct 20, 2024
8dfc430
Migrate `renderer/util.js` to esm module
jozefizso Oct 20, 2024
b3b99d9
Migrate `renderer/terminal/terminal-small.js` to esm module
jozefizso Oct 20, 2024
59c09fb
Migrate `renderer/terminal/terminal.js` to esm module
jozefizso Oct 20, 2024
3805e3c
Migrate `lib/core/alignment-pattern.js` to esm module
jozefizso Oct 20, 2024
eadb866
Migrate `lib/core/alphanumeric-data.js` to esm module
jozefizso Oct 20, 2024
29271ce
Migrate `lib/core/bit-buffer.js` to esm module
jozefizso Oct 20, 2024
80e63ce
Migrate `lib/core/bit-matrix.js` to esm module
jozefizso Oct 20, 2024
e469711
Migrate `lib/core/byte-data.js` to esm module
jozefizso Oct 20, 2024
fe25812
Migrate `lib/core/error-correction-code.js` to esm module
jozefizso Oct 20, 2024
44ca2ee
Migrate `lib/core/error-correction-level.js` to esm module
jozefizso Oct 20, 2024
b2a03a9
Migrate `lib/core/finder-pattern.js` to esm module
jozefizso Oct 20, 2024
489741f
Migrate `lib/core/format-info.js` to esm module
jozefizso Oct 20, 2024
f5ab178
Migrate `lib/core/galois-field.js` to esm module
jozefizso Oct 20, 2024
7dd2fc5
Migrate `lib/core/kanji-data.js` to esm module
jozefizso Oct 20, 2024
ad7a194
Migrate `lib/core/mask-pattern.js` to esm module
jozefizso Oct 20, 2024
07db9ab
Migrate `lib/core/mode.js` to esm module
jozefizso Oct 20, 2024
4303a6f
Migrate `lib/core/numeric-data.js` to esm module
jozefizso Oct 20, 2024
3e26449
Migrate `lib/core/polynomial.js` to esm module
jozefizso Oct 20, 2024
6431169
Migrate `lib/core/qrcode.js` to esm module
jozefizso Oct 20, 2024
25e529c
Migrate `lib/core/reed-solomon-encoder.js` to esm module
jozefizso Oct 20, 2024
081f909
Migrate `lib/core/regex.js` to esm module
jozefizso Oct 20, 2024
9026a6f
Migrate `lib/core/segments.js` to esm module
jozefizso Oct 20, 2024
aec7e34
Migrate `lib/core/utils.js` to esm module
jozefizso Oct 20, 2024
914e108
Migrate `lib/core/version-check.js` to esm module
jozefizso Oct 20, 2024
b2c9333
Migrate `lib/core/version.js` to esm module
jozefizso Oct 20, 2024
4db80f0
Migrate `bin/qrcode` to esm module
jozefizso Oct 20, 2024
8264f30
Migrate `bin/qrcode` to esm module
jozefizso Oct 22, 2024
02fe594
Migrate `helper/to-sjis.js` to esm module
jozefizso Oct 20, 2024
5b136d4
Migrate helper code in tests to esm module
jozefizso Oct 20, 2024
4f200c1
Migrate `tap` import
jozefizso Oct 20, 2024
c665298
Migrate unit tests in `test/unit/core` folder to esm module syntax
jozefizso Oct 20, 2024
244d4cf
Migrate unit tests in `test/unit/helper` folder to esm module syntax
jozefizso Oct 20, 2024
d9cbfef
Migrate unit tests in `test/unit/renderer` folder to esm module syntax
jozefizso Oct 20, 2024
aa96d42
Migrate unit tests in `test/e2e` folder to esm module syntax
jozefizso Oct 20, 2024
15dd4c6
Run `tap` tests directly as package script
jozefizso Oct 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions bin/qrcode
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
var yargs = require('yargs')
var qr = require('../lib')
import yargs from 'yargs/yargs'
import * as qr from '../lib/server.js'

const yparser = yargs(process.argv.slice(2))

function save (file, text, options) {
qr.toFile(file, text, options, function (err, data) {
Expand Down Expand Up @@ -45,7 +47,7 @@ function parseOptions (args) {

function processInputs (text, opts) {
if (!text.length) {
yargs.showHelp()
yparser.showHelp()
process.exit(1)
}

Expand All @@ -56,7 +58,7 @@ function processInputs (text, opts) {
}
}

var argv = yargs
const argv = yparser
.detectLocale(false)
.usage('Usage: $0 [options] <input string>')
.option('v', {
Expand Down
5 changes: 4 additions & 1 deletion helper/to-sjis-browser.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/* global QRCode */
QRCode.toSJIS = require('./to-sjis')

import { toSJIS } from './to-sjis.js'

QRCode.toSJIS = toSJIS
2 changes: 1 addition & 1 deletion helper/to-sjis.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const SJIS_UTF8 = [
[0xea80, '黴黶黷黹黻黼黽鼇鼈皷鼕鼡鼬鼾齊齒齔齣齟齠齡齦齧齬齪齷齲齶龕龜龠堯槇遙瑤凜熙']
]

module.exports = function toSJIS (utf8Char) {
export function toSJIS (utf8Char) {
if (!utf8Char || utf8Char === '') return

for (let i = 0; i < SJIS_UTF8.length; i++) {
Expand Down
17 changes: 8 additions & 9 deletions lib/browser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const canPromise = require('./can-promise')
import canPromise from './can-promise.js'

const QRCode = require('./core/qrcode')
const CanvasRenderer = require('./renderer/canvas')
const SvgRenderer = require('./renderer/svg-tag.js')
import * as QRCode from './core/qrcode.js'
import * as CanvasRenderer from './renderer/canvas.js'
import * as SvgRenderer from './renderer/svg-tag.js'

function renderCanvas (renderFunc, canvas, text, opts, cb) {
const args = [].slice.call(arguments, 1)
Expand Down Expand Up @@ -65,11 +65,10 @@ function renderCanvas (renderFunc, canvas, text, opts, cb) {
}
}

exports.create = QRCode.create
exports.toCanvas = renderCanvas.bind(null, CanvasRenderer.render)
exports.toDataURL = renderCanvas.bind(null, CanvasRenderer.renderToDataURL)
export const create = QRCode.create
export const toCanvas = renderCanvas.bind(null, CanvasRenderer.render)
export const toDataURL = renderCanvas.bind(null, CanvasRenderer.renderToDataURL)

// only svg for now.
exports.toString = renderCanvas.bind(null, function (data, _, opts) {
export const toString = renderCanvas.bind(null, function (data, _, opts) {
return SvgRenderer.render(data, opts)
})
2 changes: 1 addition & 1 deletion lib/can-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// standard global objects
// https://github.com/soldair/node-qrcode/issues/157

module.exports = function () {
export default function canPromise () {
return typeof Promise === 'function' && Promise.prototype && Promise.prototype.then
}
14 changes: 10 additions & 4 deletions lib/core/alignment-pattern.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2016 Vincenzo Greco
// Copyright 2024 Cisco Systems, Inc.
// Licensed under MIT-style license (see LICENSE.txt file).

import * as Utils from './utils.js'

/**
* Alignment pattern are fixed reference pattern in defined positions
* in a matrix symbology, which enables the decode software to re-synchronise
Expand All @@ -8,7 +14,7 @@
* and their number depends on the symbol version.
*/

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

/**
* Calculate the row/column coordinates of the center module of each alignment pattern
Expand All @@ -24,7 +30,7 @@ const getSymbolSize = require('./utils').getSymbolSize
* @param {Number} version QR Code version
* @return {Array} Array of coordinate
*/
exports.getRowColCoords = function getRowColCoords (version) {
export function getRowColCoords (version) {
if (version === 1) return []

const posCount = Math.floor(version / 7) + 2
Expand Down Expand Up @@ -61,9 +67,9 @@ exports.getRowColCoords = function getRowColCoords (version) {
* @param {Number} version QR Code version
* @return {Array} Array of coordinates
*/
exports.getPositions = function getPositions (version) {
export function getPositions (version) {
const coords = []
const pos = exports.getRowColCoords(version)
const pos = getRowColCoords(version)
const posLength = pos.length

for (let i = 0; i < posLength; i++) {
Expand Down
68 changes: 36 additions & 32 deletions lib/core/alphanumeric-data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const Mode = require('./mode')
// Copyright 2017 Vincenzo Greco
// Copyright 2024 Cisco Systems, Inc.
// Licensed under MIT-style license (see LICENSE.txt file).

import * as Mode from './mode.js'

/**
* Array of characters available in alphanumeric mode
Expand All @@ -16,44 +20,44 @@ const ALPHA_NUM_CHARS = [
' ', '$', '%', '*', '+', '-', '.', '/', ':'
]

function AlphanumericData (data) {
this.mode = Mode.ALPHANUMERIC
this.data = data
}
export default class AlphanumericData {
constructor (data) {
this.mode = Mode.ALPHANUMERIC
this.data = data
}

AlphanumericData.getBitsLength = function getBitsLength (length) {
return 11 * Math.floor(length / 2) + 6 * (length % 2)
}
static getBitsLength (length) {
return 11 * Math.floor(length / 2) + 6 * (length % 2)
}

AlphanumericData.prototype.getLength = function getLength () {
return this.data.length
}
getLength () {
return this.data.length
}

AlphanumericData.prototype.getBitsLength = function getBitsLength () {
return AlphanumericData.getBitsLength(this.data.length)
}
getBitsLength () {
return AlphanumericData.getBitsLength(this.data.length)
}

AlphanumericData.prototype.write = function write (bitBuffer) {
let i
write (bitBuffer) {
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
let value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45
// 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
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])
// The character value of the second digit is added to the product
value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1])

// The sum is then stored as 11-bit binary number
bitBuffer.put(value, 11)
}
// The sum is then stored as 11-bit binary number
bitBuffer.put(value, 11)
}

// If the number of input data characters is not a multiple of two,
// the character value of the final character is encoded as a 6-bit binary number.
if (this.data.length % 2) {
bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6)
// If the number of input data characters is not a multiple of two,
// the character value of the final character is encoded as a 6-bit binary number.
if (this.data.length % 2) {
bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6)
}
}
}

module.exports = AlphanumericData
31 changes: 16 additions & 15 deletions lib/core/bit-buffer.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
function BitBuffer () {
this.buffer = []
this.length = 0
}

BitBuffer.prototype = {
// Copyright 2017 Vincenzo Greco
// Copyright 2024 Cisco Systems, Inc.
// Licensed under MIT-style license (see LICENSE.txt file).

export default class BitBuffer {
constructor () {
this.buffer = []
this.length = 0
}

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

put: function (num, length) {
put (num, length) {
for (let i = 0; i < length; i++) {
this.putBit(((num >>> (length - i - 1)) & 1) === 1)
}
},
}

getLengthInBits: function () {
getLengthInBits () {
return this.length
},
}

putBit: function (bit) {
putBit (bit) {
const bufIndex = Math.floor(this.length / 8)
if (this.buffer.length <= bufIndex) {
this.buffer.push(0)
Expand All @@ -33,5 +36,3 @@ BitBuffer.prototype = {
this.length++
}
}

module.exports = BitBuffer
117 changes: 61 additions & 56 deletions lib/core/bit-matrix.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,70 @@
// Copyright 2017 Vincenzo Greco
// Copyright 2024 Cisco Systems, Inc.
// Licensed under MIT-style license (see LICENSE.txt file).

/**
* Helper class to handle QR Code symbol modules
*
* @param {Number} size Symbol size
*/
function BitMatrix (size) {
if (!size || size < 1) {
throw new Error('BitMatrix size must be defined and greater than 0')
}
export default class BitMatrix {
/**
* @param {Number} size Symbol size
*/
constructor (size) {
if (!size || size < 1) {
throw new Error('BitMatrix size must be defined and greater than 0')
}

this.size = size
this.data = new Uint8Array(size * size)
this.reservedBit = new Uint8Array(size * size)
}
this.size = size
this.data = new Uint8Array(size * size)
this.reservedBit = new Uint8Array(size * size)
}

/**
* Set bit value at specified location
* If reserved flag is set, this bit will be ignored during masking process
*
* @param {Number} row
* @param {Number} col
* @param {Boolean} value
* @param {Boolean} reserved
*/
BitMatrix.prototype.set = function (row, col, value, reserved) {
const index = row * this.size + col
this.data[index] = value
if (reserved) this.reservedBit[index] = true
}
/**
* Set bit value at specified location
* If reserved flag is set, this bit will be ignored during masking process
*
* @param {Number} row
* @param {Number} col
* @param {Boolean} value
* @param {Boolean} reserved
*/
set (row, col, value, reserved) {
const index = row * this.size + col
this.data[index] = value
if (reserved) this.reservedBit[index] = true
}

/**
* Returns bit value at specified location
*
* @param {Number} row
* @param {Number} col
* @return {Boolean}
*/
BitMatrix.prototype.get = function (row, col) {
return this.data[row * this.size + col]
}
/**
* Returns bit value at specified location
*
* @param {Number} row
* @param {Number} col
* @return {Boolean}
*/
get (row, col) {
return this.data[row * this.size + col]
}

/**
* Applies xor operator at specified location
* (used during masking process)
*
* @param {Number} row
* @param {Number} col
* @param {Boolean} value
*/
BitMatrix.prototype.xor = function (row, col, value) {
this.data[row * this.size + col] ^= value
}
/**
* Applies xor operator at specified location
* (used during masking process)
*
* @param {Number} row
* @param {Number} col
* @param {Boolean} value
*/
xor (row, col, value) {
this.data[row * this.size + col] ^= value
}

/**
* Check if bit at specified location is reserved
*
* @param {Number} row
* @param {Number} col
* @return {Boolean}
*/
BitMatrix.prototype.isReserved = function (row, col) {
return this.reservedBit[row * this.size + col]
/**
* Check if bit at specified location is reserved
*
* @param {Number} row
* @param {Number} col
* @return {Boolean}
*/
isReserved (row, col) {
return this.reservedBit[row * this.size + col]
}
}

module.exports = BitMatrix
Loading