Skip to content

Commit

Permalink
Merge pull request #22 from slidoapp/refactor/esm
Browse files Browse the repository at this point in the history
  • Loading branch information
jozefizso authored Oct 23, 2024
2 parents ca2a07e + 15dd4c6 commit acc1143
Show file tree
Hide file tree
Showing 75 changed files with 725 additions and 633 deletions.
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

0 comments on commit acc1143

Please sign in to comment.