Skip to content

Commit acc1143

Browse files
authored
Merge pull request #22 from slidoapp/refactor/esm
2 parents ca2a07e + 15dd4c6 commit acc1143

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+725
-633
lines changed

Diff for: bin/qrcode

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env node
2-
var yargs = require('yargs')
3-
var qr = require('../lib')
2+
import yargs from 'yargs/yargs'
3+
import * as qr from '../lib/server.js'
4+
5+
const yparser = yargs(process.argv.slice(2))
46

57
function save (file, text, options) {
68
qr.toFile(file, text, options, function (err, data) {
@@ -45,7 +47,7 @@ function parseOptions (args) {
4547

4648
function processInputs (text, opts) {
4749
if (!text.length) {
48-
yargs.showHelp()
50+
yparser.showHelp()
4951
process.exit(1)
5052
}
5153

@@ -56,7 +58,7 @@ function processInputs (text, opts) {
5658
}
5759
}
5860

59-
var argv = yargs
61+
const argv = yparser
6062
.detectLocale(false)
6163
.usage('Usage: $0 [options] <input string>')
6264
.option('v', {

Diff for: helper/to-sjis-browser.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/* global QRCode */
2-
QRCode.toSJIS = require('./to-sjis')
2+
3+
import { toSJIS } from './to-sjis.js'
4+
5+
QRCode.toSJIS = toSJIS

Diff for: helper/to-sjis.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const SJIS_UTF8 = [
9191
[0xea80, '黴黶黷黹黻黼黽鼇鼈皷鼕鼡鼬鼾齊齒齔齣齟齠齡齦齧齬齪齷齲齶龕龜龠堯槇遙瑤凜熙']
9292
]
9393

94-
module.exports = function toSJIS (utf8Char) {
94+
export function toSJIS (utf8Char) {
9595
if (!utf8Char || utf8Char === '') return
9696

9797
for (let i = 0; i < SJIS_UTF8.length; i++) {

Diff for: lib/browser.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const canPromise = require('./can-promise')
1+
import canPromise from './can-promise.js'
22

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

77
function renderCanvas (renderFunc, canvas, text, opts, cb) {
88
const args = [].slice.call(arguments, 1)
@@ -65,11 +65,10 @@ function renderCanvas (renderFunc, canvas, text, opts, cb) {
6565
}
6666
}
6767

68-
exports.create = QRCode.create
69-
exports.toCanvas = renderCanvas.bind(null, CanvasRenderer.render)
70-
exports.toDataURL = renderCanvas.bind(null, CanvasRenderer.renderToDataURL)
68+
export const create = QRCode.create
69+
export const toCanvas = renderCanvas.bind(null, CanvasRenderer.render)
70+
export const toDataURL = renderCanvas.bind(null, CanvasRenderer.renderToDataURL)
7171

72-
// only svg for now.
73-
exports.toString = renderCanvas.bind(null, function (data, _, opts) {
72+
export const toString = renderCanvas.bind(null, function (data, _, opts) {
7473
return SvgRenderer.render(data, opts)
7574
})

Diff for: lib/can-promise.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// standard global objects
33
// https://github.com/soldair/node-qrcode/issues/157
44

5-
module.exports = function () {
5+
export default function canPromise () {
66
return typeof Promise === 'function' && Promise.prototype && Promise.prototype.then
77
}

Diff for: lib/core/alignment-pattern.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Copyright 2016 Vincenzo Greco
2+
// Copyright 2024 Cisco Systems, Inc.
3+
// Licensed under MIT-style license (see LICENSE.txt file).
4+
5+
import * as Utils from './utils.js'
6+
17
/**
28
* Alignment pattern are fixed reference pattern in defined positions
39
* in a matrix symbology, which enables the decode software to re-synchronise
@@ -8,7 +14,7 @@
814
* and their number depends on the symbol version.
915
*/
1016

11-
const getSymbolSize = require('./utils').getSymbolSize
17+
const getSymbolSize = Utils.getSymbolSize
1218

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

3036
const posCount = Math.floor(version / 7) + 2
@@ -61,9 +67,9 @@ exports.getRowColCoords = function getRowColCoords (version) {
6167
* @param {Number} version QR Code version
6268
* @return {Array} Array of coordinates
6369
*/
64-
exports.getPositions = function getPositions (version) {
70+
export function getPositions (version) {
6571
const coords = []
66-
const pos = exports.getRowColCoords(version)
72+
const pos = getRowColCoords(version)
6773
const posLength = pos.length
6874

6975
for (let i = 0; i < posLength; i++) {

Diff for: lib/core/alphanumeric-data.js

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const Mode = require('./mode')
1+
// Copyright 2017 Vincenzo Greco
2+
// Copyright 2024 Cisco Systems, Inc.
3+
// Licensed under MIT-style license (see LICENSE.txt file).
4+
5+
import * as Mode from './mode.js'
26

37
/**
48
* Array of characters available in alphanumeric mode
@@ -16,44 +20,44 @@ const ALPHA_NUM_CHARS = [
1620
' ', '$', '%', '*', '+', '-', '.', '/', ':'
1721
]
1822

19-
function AlphanumericData (data) {
20-
this.mode = Mode.ALPHANUMERIC
21-
this.data = data
22-
}
23+
export default class AlphanumericData {
24+
constructor (data) {
25+
this.mode = Mode.ALPHANUMERIC
26+
this.data = data
27+
}
2328

24-
AlphanumericData.getBitsLength = function getBitsLength (length) {
25-
return 11 * Math.floor(length / 2) + 6 * (length % 2)
26-
}
29+
static getBitsLength (length) {
30+
return 11 * Math.floor(length / 2) + 6 * (length % 2)
31+
}
2732

28-
AlphanumericData.prototype.getLength = function getLength () {
29-
return this.data.length
30-
}
33+
getLength () {
34+
return this.data.length
35+
}
3136

32-
AlphanumericData.prototype.getBitsLength = function getBitsLength () {
33-
return AlphanumericData.getBitsLength(this.data.length)
34-
}
37+
getBitsLength () {
38+
return AlphanumericData.getBitsLength(this.data.length)
39+
}
3540

36-
AlphanumericData.prototype.write = function write (bitBuffer) {
37-
let i
41+
write (bitBuffer) {
42+
let i
3843

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

45-
// The character value of the second digit is added to the product
46-
value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1])
50+
// The character value of the second digit is added to the product
51+
value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1])
4752

48-
// The sum is then stored as 11-bit binary number
49-
bitBuffer.put(value, 11)
50-
}
53+
// The sum is then stored as 11-bit binary number
54+
bitBuffer.put(value, 11)
55+
}
5156

52-
// If the number of input data characters is not a multiple of two,
53-
// the character value of the final character is encoded as a 6-bit binary number.
54-
if (this.data.length % 2) {
55-
bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6)
57+
// If the number of input data characters is not a multiple of two,
58+
// the character value of the final character is encoded as a 6-bit binary number.
59+
if (this.data.length % 2) {
60+
bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6)
61+
}
5662
}
5763
}
58-
59-
module.exports = AlphanumericData

Diff for: lib/core/bit-buffer.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
function BitBuffer () {
2-
this.buffer = []
3-
this.length = 0
4-
}
5-
6-
BitBuffer.prototype = {
1+
// Copyright 2017 Vincenzo Greco
2+
// Copyright 2024 Cisco Systems, Inc.
3+
// Licensed under MIT-style license (see LICENSE.txt file).
4+
5+
export default class BitBuffer {
6+
constructor () {
7+
this.buffer = []
8+
this.length = 0
9+
}
710

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

13-
put: function (num, length) {
16+
put (num, length) {
1417
for (let i = 0; i < length; i++) {
1518
this.putBit(((num >>> (length - i - 1)) & 1) === 1)
1619
}
17-
},
20+
}
1821

19-
getLengthInBits: function () {
22+
getLengthInBits () {
2023
return this.length
21-
},
24+
}
2225

23-
putBit: function (bit) {
26+
putBit (bit) {
2427
const bufIndex = Math.floor(this.length / 8)
2528
if (this.buffer.length <= bufIndex) {
2629
this.buffer.push(0)
@@ -33,5 +36,3 @@ BitBuffer.prototype = {
3336
this.length++
3437
}
3538
}
36-
37-
module.exports = BitBuffer

Diff for: lib/core/bit-matrix.js

+61-56
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,70 @@
1+
// Copyright 2017 Vincenzo Greco
2+
// Copyright 2024 Cisco Systems, Inc.
3+
// Licensed under MIT-style license (see LICENSE.txt file).
4+
15
/**
26
* Helper class to handle QR Code symbol modules
3-
*
4-
* @param {Number} size Symbol size
57
*/
6-
function BitMatrix (size) {
7-
if (!size || size < 1) {
8-
throw new Error('BitMatrix size must be defined and greater than 0')
9-
}
8+
export default class BitMatrix {
9+
/**
10+
* @param {Number} size Symbol size
11+
*/
12+
constructor (size) {
13+
if (!size || size < 1) {
14+
throw new Error('BitMatrix size must be defined and greater than 0')
15+
}
1016

11-
this.size = size
12-
this.data = new Uint8Array(size * size)
13-
this.reservedBit = new Uint8Array(size * size)
14-
}
17+
this.size = size
18+
this.data = new Uint8Array(size * size)
19+
this.reservedBit = new Uint8Array(size * size)
20+
}
1521

16-
/**
17-
* Set bit value at specified location
18-
* If reserved flag is set, this bit will be ignored during masking process
19-
*
20-
* @param {Number} row
21-
* @param {Number} col
22-
* @param {Boolean} value
23-
* @param {Boolean} reserved
24-
*/
25-
BitMatrix.prototype.set = function (row, col, value, reserved) {
26-
const index = row * this.size + col
27-
this.data[index] = value
28-
if (reserved) this.reservedBit[index] = true
29-
}
22+
/**
23+
* Set bit value at specified location
24+
* If reserved flag is set, this bit will be ignored during masking process
25+
*
26+
* @param {Number} row
27+
* @param {Number} col
28+
* @param {Boolean} value
29+
* @param {Boolean} reserved
30+
*/
31+
set (row, col, value, reserved) {
32+
const index = row * this.size + col
33+
this.data[index] = value
34+
if (reserved) this.reservedBit[index] = true
35+
}
3036

31-
/**
32-
* Returns bit value at specified location
33-
*
34-
* @param {Number} row
35-
* @param {Number} col
36-
* @return {Boolean}
37-
*/
38-
BitMatrix.prototype.get = function (row, col) {
39-
return this.data[row * this.size + col]
40-
}
37+
/**
38+
* Returns bit value at specified location
39+
*
40+
* @param {Number} row
41+
* @param {Number} col
42+
* @return {Boolean}
43+
*/
44+
get (row, col) {
45+
return this.data[row * this.size + col]
46+
}
4147

42-
/**
43-
* Applies xor operator at specified location
44-
* (used during masking process)
45-
*
46-
* @param {Number} row
47-
* @param {Number} col
48-
* @param {Boolean} value
49-
*/
50-
BitMatrix.prototype.xor = function (row, col, value) {
51-
this.data[row * this.size + col] ^= value
52-
}
48+
/**
49+
* Applies xor operator at specified location
50+
* (used during masking process)
51+
*
52+
* @param {Number} row
53+
* @param {Number} col
54+
* @param {Boolean} value
55+
*/
56+
xor (row, col, value) {
57+
this.data[row * this.size + col] ^= value
58+
}
5359

54-
/**
55-
* Check if bit at specified location is reserved
56-
*
57-
* @param {Number} row
58-
* @param {Number} col
59-
* @return {Boolean}
60-
*/
61-
BitMatrix.prototype.isReserved = function (row, col) {
62-
return this.reservedBit[row * this.size + col]
60+
/**
61+
* Check if bit at specified location is reserved
62+
*
63+
* @param {Number} row
64+
* @param {Number} col
65+
* @return {Boolean}
66+
*/
67+
isReserved (row, col) {
68+
return this.reservedBit[row * this.size + col]
69+
}
6370
}
64-
65-
module.exports = BitMatrix

0 commit comments

Comments
 (0)