@akamfoad/qr
Advanced tools
Comparing version 0.1.4 to 0.2.0
{ | ||
"version": "0.1.4", | ||
"name": "@akamfoad/qr", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"description": "Fully typed QRCode encoding implementation in JavaScript with no dependencies!", | ||
"homepage": "https://akamfoad.github.io/qr/", | ||
"bugs": "https://github.com/akamfoad/qr/issues", | ||
"repository": "github:akamfoad/qr", | ||
"keywords": [ | ||
"qrcode", | ||
"qr-code", | ||
"qr", | ||
"qrcode-generator" | ||
], | ||
"author": "akamfoad", | ||
"files": [ | ||
@@ -10,2 +20,14 @@ "dist", | ||
], | ||
"main": "./dist/cjs/qr.cjs", | ||
"module": "./dist/qr.mjs", | ||
"typings": "dist/qr.d.mts", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"types": "./dist/qr.d.mts", | ||
"import": "./dist/qr.mjs", | ||
"default": "./dist/cjs/qr.cjs" | ||
} | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
@@ -15,25 +37,11 @@ "node": ">=16" | ||
"scripts": { | ||
"start": "tsdx watch", | ||
"build": "tsdx build", | ||
"test": "tsdx test", | ||
"lint": "tsdx lint", | ||
"prepare": "tsdx build", | ||
"start": "tsup --watch", | ||
"build": "tsup", | ||
"test": "vitest", | ||
"lint": "eslint", | ||
"prepare": "tsup && husky install", | ||
"size": "size-limit", | ||
"analyze": "size-limit --why" | ||
"analyze": "size-limit --why", | ||
"docs": "typedoc --out typedocs src/index.ts" | ||
}, | ||
"peerDependencies": {}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
} | ||
}, | ||
"prettier": { | ||
"printWidth": 80, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
}, | ||
"name": "@akamfoad/qr", | ||
"author": "akamfoad", | ||
"module": "dist/qr.esm.js", | ||
"size-limit": [ | ||
@@ -51,8 +59,15 @@ { | ||
"@size-limit/preset-small-lib": "^9.0.0", | ||
"husky": "^8.0.3", | ||
"@typescript-eslint/eslint-plugin": "^6.7.2", | ||
"@typescript-eslint/parser": "^6.7.2", | ||
"eslint": "^8.49.0", | ||
"eslint-plugin-prettier": "^5.0.0", | ||
"husky": "^8.0.0", | ||
"lint-staged": "^14.0.1", | ||
"prettier": "^3.0.3", | ||
"size-limit": "^9.0.0", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.6.2", | ||
"typescript": "^5.2.2" | ||
"tsup": "^7.2.0", | ||
"typedoc": "^0.25.1", | ||
"typescript": "^5.2.2", | ||
"vitest": "^0.34.4" | ||
} | ||
} |
105
README.md
@@ -1,103 +0,24 @@ | ||
# TSDX User Guide | ||
# @akamfoad/qr | ||
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it. | ||
Fully typed **QRCode encoding** implementation in JavaScript with no dependencies! | ||
> This TSDX setup is meant for developing libraries (not apps!) that can be published to NPM. If you’re looking to build a Node app, you could use `ts-node-dev`, plain `ts-node`, or simple `tsc`. | ||
```js | ||
import { qrcode as qr } from '@akamfoad/qr'; | ||
> If you’re new to TypeScript, checkout [this handy cheatsheet](https://devhints.io/typescript) | ||
const qrcode = qr('foo bar baz'); | ||
## Commands | ||
const cells = qrcode.modules; | ||
TSDX scaffolds your new library inside `/src`. | ||
To run TSDX, use: | ||
```bash | ||
npm start # or yarn start | ||
// cell[R] -> row | ||
// if cell[R][C] is true, that cell is "on" | ||
``` | ||
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`. | ||
[interactive example](https://js-2xc6qu.stackblitz.io/) | ||
To do a one-off build, use `npm run build` or `yarn build`. | ||
## type docs | ||
To run tests, use `npm test` or `yarn test`. | ||
<https://akamfoad.github.io/qr/> | ||
## Configuration | ||
## credit | ||
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly. | ||
### Jest | ||
Jest tests are set up to run with `npm test` or `yarn test`. | ||
### Bundle Analysis | ||
[`size-limit`](https://github.com/ai/size-limit) is set up to calculate the real cost of your library with `npm run size` and visualize the bundle with `npm run analyze`. | ||
#### Setup Files | ||
This is the folder structure we set up for you: | ||
```txt | ||
/src | ||
index.tsx # EDIT THIS | ||
/test | ||
blah.test.tsx # EDIT THIS | ||
.gitignore | ||
package.json | ||
README.md # EDIT THIS | ||
tsconfig.json | ||
``` | ||
### Rollup | ||
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details. | ||
### TypeScript | ||
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs. | ||
## Continuous Integration | ||
### GitHub Actions | ||
Two actions are added by default: | ||
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix | ||
- `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit) | ||
## Optimizations | ||
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations: | ||
```js | ||
// ./types/index.d.ts | ||
declare var __DEV__: boolean; | ||
// inside your code... | ||
if (__DEV__) { | ||
console.log('foo'); | ||
} | ||
``` | ||
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions. | ||
## Module Formats | ||
CJS, ESModules, and UMD module formats are supported. | ||
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found. | ||
## Named Exports | ||
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library. | ||
## Including Styles | ||
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like. | ||
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader. | ||
## Publishing to NPM | ||
We recommend using [np](https://github.com/sindresorhus/np). | ||
This library is repackaged work from the qrcode library here <http://www.d-project.com/> which is also licensed under the MIT license. |
import QRCode from './lib/QRCode'; | ||
import { ErrorCorrectLevel } from './lib/ErrorCorrectLevel'; | ||
export const qrcode = function( | ||
export const qrcode = ( | ||
data: string, | ||
opt?: { typeNumber?: number; errorCorrectLevel?: number } | ||
) { | ||
opt?: { typeNumber?: number; errorCorrectLevel?: number }, | ||
) => { | ||
opt = opt || {}; | ||
var qr = new QRCode( | ||
const qr = new QRCode( | ||
opt.typeNumber || -1, | ||
opt.errorCorrectLevel || ErrorCorrectLevel.H | ||
opt.errorCorrectLevel || ErrorCorrectLevel.H, | ||
); | ||
@@ -13,0 +13,0 @@ qr.addData(data); |
@@ -17,3 +17,3 @@ import mode from './mode'; | ||
write(buffer: QRBitBuffer) { | ||
for (var i = 0; i < this.data.length; i++) { | ||
for (let i = 0; i < this.data.length; i++) { | ||
// not JIS ... | ||
@@ -20,0 +20,0 @@ buffer.put(this.data.charCodeAt(i), 8); |
@@ -5,3 +5,3 @@ export default class QRBitBuffer { | ||
constructor() { | ||
this.buffer = new Array(); | ||
this.buffer = []; | ||
this.length = 0; | ||
@@ -11,3 +11,3 @@ } | ||
get(index: number) { | ||
var bufIndex = Math.floor(index / 8); | ||
const bufIndex = Math.floor(index / 8); | ||
return ((this.buffer[bufIndex] >>> (7 - (index % 8))) & 1) == 1; | ||
@@ -17,3 +17,3 @@ } | ||
put(num: number, length: number) { | ||
for (var i = 0; i < length; i++) { | ||
for (let i = 0; i < length; i++) { | ||
this.putBit(((num >>> (length - i - 1)) & 1) == 1); | ||
@@ -28,3 +28,3 @@ } | ||
putBit(bit: boolean) { | ||
var bufIndex = Math.floor(this.length / 8); | ||
const bufIndex = Math.floor(this.length / 8); | ||
if (this.buffer.length <= bufIndex) { | ||
@@ -31,0 +31,0 @@ this.buffer.push(0); |
@@ -1,3 +0,3 @@ | ||
var QRMath = { | ||
glog: function(n: number) { | ||
const QRMath = { | ||
glog: function (n: number) { | ||
if (n < 1) { | ||
@@ -10,3 +10,3 @@ throw new Error('glog(' + n + ')'); | ||
gexp: function(n: number) { | ||
gexp: function (n: number) { | ||
while (n < 0) { | ||
@@ -28,6 +28,6 @@ n += 255; | ||
for (var i = 0; i < 8; i++) { | ||
for (let i = 0; i < 8; i++) { | ||
QRMath.EXP_TABLE[i] = 1 << i; | ||
} | ||
for (var i = 8; i < 256; i++) { | ||
for (let i = 8; i < 256; i++) { | ||
QRMath.EXP_TABLE[i] = | ||
@@ -39,3 +39,3 @@ QRMath.EXP_TABLE[i - 4] ^ | ||
} | ||
for (var i = 0; i < 255; i++) { | ||
for (let i = 0; i < 255; i++) { | ||
QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i; | ||
@@ -42,0 +42,0 @@ } |
export default { | ||
MODE_NUMBER : 1 << 0, | ||
MODE_ALPHA_NUM : 1 << 1, | ||
MODE_8BIT_BYTE : 1 << 2, | ||
MODE_KANJI : 1 << 3 | ||
MODE_NUMBER: 1 << 0, | ||
MODE_ALPHA_NUM: 1 << 1, | ||
MODE_8BIT_BYTE: 1 << 2, | ||
MODE_KANJI: 1 << 3, | ||
}; |
@@ -11,3 +11,3 @@ import math from './math'; | ||
var offset = 0; | ||
let offset = 0; | ||
@@ -19,3 +19,3 @@ while (offset < num.length && num[offset] == 0) { | ||
this.num = new Array(num.length - offset + shift); | ||
for (var i = 0; i < num.length - offset; i++) { | ||
for (let i = 0; i < num.length - offset; i++) { | ||
this.num[i] = num[i + offset]; | ||
@@ -34,6 +34,6 @@ } | ||
multiply(e: QRPolynomial): QRPolynomial { | ||
var num = new Array(this.getLength() + e.getLength() - 1); | ||
const num = new Array(this.getLength() + e.getLength() - 1); | ||
for (var i = 0; i < this.getLength(); i++) { | ||
for (var j = 0; j < e.getLength(); j++) { | ||
for (let i = 0; i < this.getLength(); i++) { | ||
for (let j = 0; j < e.getLength(); j++) { | ||
num[i + j] ^= math.gexp(math.glog(this.get(i)) + math.glog(e.get(j))); | ||
@@ -51,11 +51,11 @@ } | ||
var ratio = math.glog(this.get(0)) - math.glog(e.get(0)); | ||
const ratio = math.glog(this.get(0)) - math.glog(e.get(0)); | ||
var num = new Array(this.getLength()); | ||
const num = new Array(this.getLength()); | ||
for (var i = 0; i < this.getLength(); i++) { | ||
for (let i = 0; i < this.getLength(); i++) { | ||
num[i] = this.get(i); | ||
} | ||
for (var i = 0; i < e.getLength(); i++) { | ||
for (let i = 0; i < e.getLength(); i++) { | ||
num[i] ^= math.gexp(math.glog(e.get(i)) + ratio); | ||
@@ -62,0 +62,0 @@ } |
@@ -25,3 +25,3 @@ import BitByte from './8BitByte'; | ||
addData(data: string) { | ||
var newData = new BitByte(data); | ||
const newData = new BitByte(data); | ||
this.dataList.push(newData); | ||
@@ -55,18 +55,21 @@ this.dataCache = null; | ||
if (this.typeNumber < 1) { | ||
var typeNumber = 1; | ||
let typeNumber = 1; | ||
for (typeNumber = 1; typeNumber < 40; typeNumber++) { | ||
var rsBlocks = RSBlock.getRSBlocks(typeNumber, this.errorCorrectLevel); | ||
const rsBlocks = RSBlock.getRSBlocks( | ||
typeNumber, | ||
this.errorCorrectLevel, | ||
); | ||
var buffer = new BitBuffer(); | ||
var totalDataCount = 0; | ||
for (var i = 0; i < rsBlocks.length; i++) { | ||
const buffer = new BitBuffer(); | ||
let totalDataCount = 0; | ||
for (let i = 0; i < rsBlocks.length; i++) { | ||
totalDataCount += rsBlocks[i].dataCount; | ||
} | ||
for (var i = 0; i < this.dataList.length; i++) { | ||
var data = this.dataList[i]; | ||
for (let i = 0; i < this.dataList.length; i++) { | ||
const data = this.dataList[i]; | ||
buffer.put(data.mode, 4); | ||
buffer.put( | ||
data.getLength(), | ||
util.getLengthInBits(data.mode, typeNumber) | ||
util.getLengthInBits(data.mode, typeNumber), | ||
); | ||
@@ -86,6 +89,6 @@ data.write(buffer); | ||
for (var row = 0; row < this.moduleCount; row++) { | ||
for (let row = 0; row < this.moduleCount; row++) { | ||
this.modules[row] = new Array(this.moduleCount); | ||
for (var col = 0; col < this.moduleCount; col++) { | ||
for (let col = 0; col < this.moduleCount; col++) { | ||
this.modules[row][col] = null; //(col + row) % 3; | ||
@@ -110,3 +113,3 @@ } | ||
this.errorCorrectLevel, | ||
this.dataList | ||
this.dataList, | ||
); | ||
@@ -119,6 +122,6 @@ } | ||
setupPositionProbePattern(row: number, col: number) { | ||
for (var r = -1; r <= 7; r++) { | ||
for (let r = -1; r <= 7; r++) { | ||
if (row + r <= -1 || this.moduleCount <= row + r) continue; | ||
for (var c = -1; c <= 7; c++) { | ||
for (let c = -1; c <= 7; c++) { | ||
if (col + c <= -1 || this.moduleCount <= col + c) continue; | ||
@@ -146,9 +149,9 @@ | ||
getBestMaskPattern() { | ||
var minLostPoint = 0; | ||
var pattern = 0; | ||
let minLostPoint = 0; | ||
let pattern = 0; | ||
for (var i = 0; i < 8; i++) { | ||
for (let i = 0; i < 8; i++) { | ||
this.makeImpl(true, i); | ||
var lostPoint = util.getLostPoint(this); | ||
const lostPoint = util.getLostPoint(this); | ||
@@ -165,2 +168,3 @@ if (i == 0 || minLostPoint > lostPoint) { | ||
// FIXME where is this used? outside maybe? Try to find out | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
@@ -171,15 +175,14 @@ createMovieClip(target_mc, instance_name, depth) { | ||
} | ||
var qr_mc = target_mc.createEmptyMovieClip(instance_name, depth); | ||
var cs = 1; | ||
const qr_mc = target_mc.createEmptyMovieClip(instance_name, depth); | ||
const cs = 1; | ||
this.make(); | ||
for (let row = 0; row < this.modules.length; row++) { | ||
const y = row * cs; | ||
for (var row = 0; row < this.modules.length; row++) { | ||
var y = row * cs; | ||
for (let col = 0; col < this.modules[row].length; col++) { | ||
const x = col * cs; | ||
const dark = this.modules[row][col]; | ||
for (var col = 0; col < this.modules[row].length; col++) { | ||
var x = col * cs; | ||
var dark = this.modules[row][col]; | ||
if (dark) { | ||
@@ -204,3 +207,3 @@ qr_mc.beginFill(0, 100); | ||
for (var r = 8; r < this.moduleCount - 8; r++) { | ||
for (let r = 8; r < this.moduleCount - 8; r++) { | ||
if (this.modules[r][6] != null) { | ||
@@ -212,3 +215,3 @@ continue; | ||
for (var c = 8; c < this.moduleCount - 8; c++) { | ||
for (let c = 8; c < this.moduleCount - 8; c++) { | ||
if (this.modules[6][c] != null) { | ||
@@ -226,8 +229,8 @@ continue; | ||
var pos = util.getPatternPosition(this.typeNumber); | ||
const pos = util.getPatternPosition(this.typeNumber); | ||
for (var i = 0; i < pos.length; i++) { | ||
for (var j = 0; j < pos.length; j++) { | ||
var row = pos[i]; | ||
var col = pos[j]; | ||
for (let i = 0; i < pos.length; i++) { | ||
for (let j = 0; j < pos.length; j++) { | ||
const row = pos[i]; | ||
const col = pos[j]; | ||
@@ -238,4 +241,4 @@ if (this.modules[row][col] != null) { | ||
for (var r = -2; r <= 2; r++) { | ||
for (var c = -2; c <= 2; c++) { | ||
for (let r = -2; r <= 2; r++) { | ||
for (let c = -2; c <= 2; c++) { | ||
if (r == -2 || r == 2 || c == -2 || c == 2 || (r == 0 && c == 0)) { | ||
@@ -257,11 +260,11 @@ this.modules[row + r][col + c] = true; | ||
var bits = util.getBCHTypeNumber(this.typeNumber); | ||
const bits = util.getBCHTypeNumber(this.typeNumber); | ||
for (var i = 0; i < 18; i++) { | ||
var mod = !test && ((bits >> i) & 1) == 1; | ||
for (let i = 0; i < 18; i++) { | ||
const mod = !test && ((bits >> i) & 1) == 1; | ||
this.modules[Math.floor(i / 3)][(i % 3) + this.moduleCount - 8 - 3] = mod; | ||
} | ||
for (var i = 0; i < 18; i++) { | ||
var mod = !test && ((bits >> i) & 1) == 1; | ||
for (let i = 0; i < 18; i++) { | ||
const mod = !test && ((bits >> i) & 1) == 1; | ||
this.modules[(i % 3) + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod; | ||
@@ -276,8 +279,8 @@ } | ||
var data = (this.errorCorrectLevel << 3) | maskPattern; | ||
var bits = util.getBCHTypeInfo(data); | ||
const data = (this.errorCorrectLevel << 3) | maskPattern; | ||
const bits = util.getBCHTypeInfo(data); | ||
// vertical | ||
for (var i = 0; i < 15; i++) { | ||
var mod = !test && ((bits >> i) & 1) == 1; | ||
for (let i = 0; i < 15; i++) { | ||
const mod = !test && ((bits >> i) & 1) == 1; | ||
@@ -294,4 +297,4 @@ if (i < 6) { | ||
// horizontal | ||
for (var i = 0; i < 15; i++) { | ||
var mod = !test && ((bits >> i) & 1) == 1; | ||
for (let i = 0; i < 15; i++) { | ||
const mod = !test && ((bits >> i) & 1) == 1; | ||
@@ -316,14 +319,15 @@ if (i < 8) { | ||
var inc = -1; | ||
var row = this.moduleCount - 1; | ||
var bitIndex = 7; | ||
var byteIndex = 0; | ||
let inc = -1; | ||
let row = this.moduleCount - 1; | ||
let bitIndex = 7; | ||
let byteIndex = 0; | ||
for (var col = this.moduleCount - 1; col > 0; col -= 2) { | ||
for (let col = this.moduleCount - 1; col > 0; col -= 2) { | ||
if (col == 6) col--; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
for (var c = 0; c < 2; c++) { | ||
for (let c = 0; c < 2; c++) { | ||
if (this.modules[row][col - c] == null) { | ||
var dark = false; | ||
let dark = false; | ||
@@ -334,3 +338,3 @@ if (byteIndex < data.length) { | ||
var mask = util.getMask(maskPattern, row, col - c); | ||
const mask = util.getMask(maskPattern, row, col - c); | ||
@@ -368,10 +372,10 @@ if (mask) { | ||
errorCorrectLevel: number, | ||
dataList: BitByte[] | ||
dataList: BitByte[], | ||
) { | ||
var rsBlocks = RSBlock.getRSBlocks(typeNumber, errorCorrectLevel); | ||
const rsBlocks = RSBlock.getRSBlocks(typeNumber, errorCorrectLevel); | ||
var buffer = new BitBuffer(); | ||
const buffer = new BitBuffer(); | ||
for (var i = 0; i < dataList.length; i++) { | ||
var data = dataList[i]; | ||
for (let i = 0; i < dataList.length; i++) { | ||
const data = dataList[i]; | ||
buffer.put(data.mode, 4); | ||
@@ -383,4 +387,4 @@ buffer.put(data.getLength(), util.getLengthInBits(data.mode, typeNumber)); | ||
// calc num max data. | ||
var totalDataCount = 0; | ||
for (var i = 0; i < rsBlocks.length; i++) { | ||
let totalDataCount = 0; | ||
for (let i = 0; i < rsBlocks.length; i++) { | ||
totalDataCount += rsBlocks[i].dataCount; | ||
@@ -395,3 +399,3 @@ } | ||
totalDataCount * 8 + | ||
')' | ||
')', | ||
); | ||
@@ -411,2 +415,3 @@ } | ||
// padding | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
@@ -428,13 +433,13 @@ if (buffer.getLengthInBits() >= totalDataCount * 8) { | ||
static createBytes(buffer: BitBuffer, rsBlocks: RSBlock[]): number[] { | ||
var offset = 0; | ||
let offset = 0; | ||
var maxDcCount = 0; | ||
var maxEcCount = 0; | ||
let maxDcCount = 0; | ||
let maxEcCount = 0; | ||
var dcdata = new Array<number[]>(rsBlocks.length); | ||
var ecdata = new Array<number[]>(rsBlocks.length); | ||
const dcdata = new Array<number[]>(rsBlocks.length); | ||
const ecdata = new Array<number[]>(rsBlocks.length); | ||
for (var r = 0; r < rsBlocks.length; r++) { | ||
var dcCount = rsBlocks[r].dataCount; | ||
var ecCount = rsBlocks[r].totalCount - dcCount; | ||
for (let r = 0; r < rsBlocks.length; r++) { | ||
const dcCount = rsBlocks[r].dataCount; | ||
const ecCount = rsBlocks[r].totalCount - dcCount; | ||
@@ -446,3 +451,3 @@ maxDcCount = Math.max(maxDcCount, dcCount); | ||
for (var i = 0; i < dcdata[r].length; i++) { | ||
for (let i = 0; i < dcdata[r].length; i++) { | ||
dcdata[r][i] = 0xff & buffer.buffer[i + offset]; | ||
@@ -452,9 +457,9 @@ } | ||
var rsPoly = util.getErrorCorrectPolynomial(ecCount); | ||
var rawPoly = new Polynomial(dcdata[r], rsPoly.getLength() - 1); | ||
const rsPoly = util.getErrorCorrectPolynomial(ecCount); | ||
const rawPoly = new Polynomial(dcdata[r], rsPoly.getLength() - 1); | ||
var modPoly = rawPoly.mod(rsPoly); | ||
const modPoly = rawPoly.mod(rsPoly); | ||
ecdata[r] = new Array(rsPoly.getLength() - 1); | ||
for (var i = 0; i < ecdata[r].length; i++) { | ||
var modIndex = i + modPoly.getLength() - ecdata[r].length; | ||
for (let i = 0; i < ecdata[r].length; i++) { | ||
const modIndex = i + modPoly.getLength() - ecdata[r].length; | ||
ecdata[r][i] = modIndex >= 0 ? modPoly.get(modIndex) : 0; | ||
@@ -464,12 +469,12 @@ } | ||
var totalCodeCount = 0; | ||
for (var i = 0; i < rsBlocks.length; i++) { | ||
let totalCodeCount = 0; | ||
for (let i = 0; i < rsBlocks.length; i++) { | ||
totalCodeCount += rsBlocks[i].totalCount; | ||
} | ||
var data = new Array<number>(totalCodeCount); | ||
var index = 0; | ||
const data = new Array<number>(totalCodeCount); | ||
let index = 0; | ||
for (var i = 0; i < maxDcCount; i++) { | ||
for (var r = 0; r < rsBlocks.length; r++) { | ||
for (let i = 0; i < maxDcCount; i++) { | ||
for (let r = 0; r < rsBlocks.length; r++) { | ||
if (i < dcdata[r].length) { | ||
@@ -481,4 +486,4 @@ data[index++] = dcdata[r][i]; | ||
for (var i = 0; i < maxEcCount; i++) { | ||
for (var r = 0; r < rsBlocks.length; r++) { | ||
for (let i = 0; i < maxEcCount; i++) { | ||
for (let r = 0; r < rsBlocks.length; r++) { | ||
if (i < ecdata[r].length) { | ||
@@ -485,0 +490,0 @@ data[index++] = ecdata[r][i]; |
@@ -259,3 +259,3 @@ import { ErrorCorrectLevel } from './ErrorCorrectLevel'; | ||
static getRSBlocks(typeNumber: number, errorCorrectLevel: number) { | ||
var rsBlock = QRRSBlock.getRsBlockTable(typeNumber, errorCorrectLevel); | ||
const rsBlock = QRRSBlock.getRsBlockTable(typeNumber, errorCorrectLevel); | ||
@@ -267,16 +267,16 @@ if (rsBlock == undefined) { | ||
'/errorCorrectLevel:' + | ||
errorCorrectLevel | ||
errorCorrectLevel, | ||
); | ||
} | ||
var length = rsBlock.length / 3; | ||
const length = rsBlock.length / 3; | ||
var list = new Array(); | ||
const list = []; | ||
for (var i = 0; i < length; i++) { | ||
var count = rsBlock[i * 3 + 0]; | ||
var totalCount = rsBlock[i * 3 + 1]; | ||
var dataCount = rsBlock[i * 3 + 2]; | ||
for (let i = 0; i < length; i++) { | ||
const count = rsBlock[i * 3 + 0]; | ||
const totalCount = rsBlock[i * 3 + 1]; | ||
const dataCount = rsBlock[i * 3 + 2]; | ||
for (var j = 0; j < count; j++) { | ||
for (let j = 0; j < count; j++) { | ||
list.push(new QRRSBlock(totalCount, dataCount)); | ||
@@ -283,0 +283,0 @@ } |
@@ -1,280 +0,299 @@ | ||
import Mode from './mode' | ||
import Polynomial from './Polynomial' | ||
import math from './math' | ||
import Mode from './mode'; | ||
import Polynomial from './Polynomial'; | ||
import math from './math'; | ||
import type QRCode from './QRCode'; | ||
var QRMaskPattern = { | ||
PATTERN000 : 0, | ||
PATTERN001 : 1, | ||
PATTERN010 : 2, | ||
PATTERN011 : 3, | ||
PATTERN100 : 4, | ||
PATTERN101 : 5, | ||
PATTERN110 : 6, | ||
PATTERN111 : 7 | ||
const QRMaskPattern = { | ||
PATTERN000: 0, | ||
PATTERN001: 1, | ||
PATTERN010: 2, | ||
PATTERN011: 3, | ||
PATTERN100: 4, | ||
PATTERN101: 5, | ||
PATTERN110: 6, | ||
PATTERN111: 7, | ||
}; | ||
var QRUtil = { | ||
const QRUtil = { | ||
PATTERN_POSITION_TABLE: [ | ||
[], | ||
[6, 18], | ||
[6, 22], | ||
[6, 26], | ||
[6, 30], | ||
[6, 34], | ||
[6, 22, 38], | ||
[6, 24, 42], | ||
[6, 26, 46], | ||
[6, 28, 50], | ||
[6, 30, 54], | ||
[6, 32, 58], | ||
[6, 34, 62], | ||
[6, 26, 46, 66], | ||
[6, 26, 48, 70], | ||
[6, 26, 50, 74], | ||
[6, 30, 54, 78], | ||
[6, 30, 56, 82], | ||
[6, 30, 58, 86], | ||
[6, 34, 62, 90], | ||
[6, 28, 50, 72, 94], | ||
[6, 26, 50, 74, 98], | ||
[6, 30, 54, 78, 102], | ||
[6, 28, 54, 80, 106], | ||
[6, 32, 58, 84, 110], | ||
[6, 30, 58, 86, 114], | ||
[6, 34, 62, 90, 118], | ||
[6, 26, 50, 74, 98, 122], | ||
[6, 30, 54, 78, 102, 126], | ||
[6, 26, 52, 78, 104, 130], | ||
[6, 30, 56, 82, 108, 134], | ||
[6, 34, 60, 86, 112, 138], | ||
[6, 30, 58, 86, 114, 142], | ||
[6, 34, 62, 90, 118, 146], | ||
[6, 30, 54, 78, 102, 126, 150], | ||
[6, 24, 50, 76, 102, 128, 154], | ||
[6, 28, 54, 80, 106, 132, 158], | ||
[6, 32, 58, 84, 110, 136, 162], | ||
[6, 26, 54, 82, 110, 138, 166], | ||
[6, 30, 58, 86, 114, 142, 170], | ||
], | ||
PATTERN_POSITION_TABLE : [ | ||
[], | ||
[6, 18], | ||
[6, 22], | ||
[6, 26], | ||
[6, 30], | ||
[6, 34], | ||
[6, 22, 38], | ||
[6, 24, 42], | ||
[6, 26, 46], | ||
[6, 28, 50], | ||
[6, 30, 54], | ||
[6, 32, 58], | ||
[6, 34, 62], | ||
[6, 26, 46, 66], | ||
[6, 26, 48, 70], | ||
[6, 26, 50, 74], | ||
[6, 30, 54, 78], | ||
[6, 30, 56, 82], | ||
[6, 30, 58, 86], | ||
[6, 34, 62, 90], | ||
[6, 28, 50, 72, 94], | ||
[6, 26, 50, 74, 98], | ||
[6, 30, 54, 78, 102], | ||
[6, 28, 54, 80, 106], | ||
[6, 32, 58, 84, 110], | ||
[6, 30, 58, 86, 114], | ||
[6, 34, 62, 90, 118], | ||
[6, 26, 50, 74, 98, 122], | ||
[6, 30, 54, 78, 102, 126], | ||
[6, 26, 52, 78, 104, 130], | ||
[6, 30, 56, 82, 108, 134], | ||
[6, 34, 60, 86, 112, 138], | ||
[6, 30, 58, 86, 114, 142], | ||
[6, 34, 62, 90, 118, 146], | ||
[6, 30, 54, 78, 102, 126, 150], | ||
[6, 24, 50, 76, 102, 128, 154], | ||
[6, 28, 54, 80, 106, 132, 158], | ||
[6, 32, 58, 84, 110, 136, 162], | ||
[6, 26, 54, 82, 110, 138, 166], | ||
[6, 30, 58, 86, 114, 142, 170] | ||
], | ||
G15: | ||
(1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0), | ||
G18: | ||
(1 << 12) | | ||
(1 << 11) | | ||
(1 << 10) | | ||
(1 << 9) | | ||
(1 << 8) | | ||
(1 << 5) | | ||
(1 << 2) | | ||
(1 << 0), | ||
G15_MASK: (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1), | ||
G15 : (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0), | ||
G18 : (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0), | ||
G15_MASK : (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1), | ||
getBCHTypeInfo: function (data: number) { | ||
let d = data << 10; | ||
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) { | ||
d ^= | ||
QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15)); | ||
} | ||
return ((data << 10) | d) ^ QRUtil.G15_MASK; | ||
}, | ||
getBCHTypeInfo : function(data:number) { | ||
var d = data << 10; | ||
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) { | ||
d ^= (QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) ) ); | ||
} | ||
return ( (data << 10) | d) ^ QRUtil.G15_MASK; | ||
}, | ||
getBCHTypeNumber: function (data: number) { | ||
let d = data << 12; | ||
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) { | ||
d ^= | ||
QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18)); | ||
} | ||
return (data << 12) | d; | ||
}, | ||
getBCHTypeNumber : function(data:number) { | ||
var d = data << 12; | ||
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) { | ||
d ^= (QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) ) ); | ||
} | ||
return (data << 12) | d; | ||
}, | ||
getBCHDigit: function (data: number) { | ||
let digit = 0; | ||
getBCHDigit : function(data:number) { | ||
while (data != 0) { | ||
digit++; | ||
data >>>= 1; | ||
} | ||
var digit = 0; | ||
return digit; | ||
}, | ||
while (data != 0) { | ||
digit++; | ||
data >>>= 1; | ||
} | ||
getPatternPosition: function (typeNumber: number) { | ||
return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1]; | ||
}, | ||
return digit; | ||
}, | ||
getMask: function (maskPattern: number, i: number, j: number) { | ||
switch (maskPattern) { | ||
case QRMaskPattern.PATTERN000: | ||
return (i + j) % 2 == 0; | ||
case QRMaskPattern.PATTERN001: | ||
return i % 2 == 0; | ||
case QRMaskPattern.PATTERN010: | ||
return j % 3 == 0; | ||
case QRMaskPattern.PATTERN011: | ||
return (i + j) % 3 == 0; | ||
case QRMaskPattern.PATTERN100: | ||
return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0; | ||
case QRMaskPattern.PATTERN101: | ||
return ((i * j) % 2) + ((i * j) % 3) == 0; | ||
case QRMaskPattern.PATTERN110: | ||
return (((i * j) % 2) + ((i * j) % 3)) % 2 == 0; | ||
case QRMaskPattern.PATTERN111: | ||
return (((i * j) % 3) + ((i + j) % 2)) % 2 == 0; | ||
getPatternPosition : function(typeNumber:number) { | ||
return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1]; | ||
}, | ||
default: | ||
throw new Error('bad maskPattern:' + maskPattern); | ||
} | ||
}, | ||
getMask : function(maskPattern:number, i:number, j:number) { | ||
switch (maskPattern) { | ||
case QRMaskPattern.PATTERN000 : return (i + j) % 2 == 0; | ||
case QRMaskPattern.PATTERN001 : return i % 2 == 0; | ||
case QRMaskPattern.PATTERN010 : return j % 3 == 0; | ||
case QRMaskPattern.PATTERN011 : return (i + j) % 3 == 0; | ||
case QRMaskPattern.PATTERN100 : return (Math.floor(i / 2) + Math.floor(j / 3) ) % 2 == 0; | ||
case QRMaskPattern.PATTERN101 : return (i * j) % 2 + (i * j) % 3 == 0; | ||
case QRMaskPattern.PATTERN110 : return ( (i * j) % 2 + (i * j) % 3) % 2 == 0; | ||
case QRMaskPattern.PATTERN111 : return ( (i * j) % 3 + (i + j) % 2) % 2 == 0; | ||
getErrorCorrectPolynomial: function (errorCorrectLength: number) { | ||
let a = new Polynomial([1], 0); | ||
default : | ||
throw new Error("bad maskPattern:" + maskPattern); | ||
} | ||
}, | ||
for (let i = 0; i < errorCorrectLength; i++) { | ||
a = a.multiply(new Polynomial([1, math.gexp(i)], 0)); | ||
} | ||
getErrorCorrectPolynomial : function(errorCorrectLength:number) { | ||
return a; | ||
}, | ||
var a = new Polynomial([1], 0); | ||
getLengthInBits: function (mode: number, type: number) { | ||
if (1 <= type && type < 10) { | ||
// 1 - 9 | ||
for (var i = 0; i < errorCorrectLength; i++) { | ||
a = a.multiply(new Polynomial([1, math.gexp(i)], 0) ); | ||
} | ||
switch (mode) { | ||
case Mode.MODE_NUMBER: | ||
return 10; | ||
case Mode.MODE_ALPHA_NUM: | ||
return 9; | ||
case Mode.MODE_8BIT_BYTE: | ||
return 8; | ||
case Mode.MODE_KANJI: | ||
return 8; | ||
default: | ||
throw new Error('mode:' + mode); | ||
} | ||
} else if (type < 27) { | ||
// 10 - 26 | ||
return a; | ||
}, | ||
switch (mode) { | ||
case Mode.MODE_NUMBER: | ||
return 12; | ||
case Mode.MODE_ALPHA_NUM: | ||
return 11; | ||
case Mode.MODE_8BIT_BYTE: | ||
return 16; | ||
case Mode.MODE_KANJI: | ||
return 10; | ||
default: | ||
throw new Error('mode:' + mode); | ||
} | ||
} else if (type < 41) { | ||
// 27 - 40 | ||
getLengthInBits : function(mode:number, type:number) { | ||
switch (mode) { | ||
case Mode.MODE_NUMBER: | ||
return 14; | ||
case Mode.MODE_ALPHA_NUM: | ||
return 13; | ||
case Mode.MODE_8BIT_BYTE: | ||
return 16; | ||
case Mode.MODE_KANJI: | ||
return 12; | ||
default: | ||
throw new Error('mode:' + mode); | ||
} | ||
} else { | ||
throw new Error('type:' + type); | ||
} | ||
}, | ||
if (1 <= type && type < 10) { | ||
getLostPoint: function (qrCode: QRCode) { | ||
const moduleCount = qrCode.getModuleCount(); | ||
// 1 - 9 | ||
let lostPoint = 0; | ||
switch(mode) { | ||
case Mode.MODE_NUMBER : return 10; | ||
case Mode.MODE_ALPHA_NUM : return 9; | ||
case Mode.MODE_8BIT_BYTE : return 8; | ||
case Mode.MODE_KANJI : return 8; | ||
default : | ||
throw new Error("mode:" + mode); | ||
} | ||
// LEVEL1 | ||
} else if (type < 27) { | ||
for (let row = 0; row < moduleCount; row++) { | ||
for (let col = 0; col < moduleCount; col++) { | ||
let sameCount = 0; | ||
const dark = qrCode.isDark(row, col); | ||
// 10 - 26 | ||
for (let r = -1; r <= 1; r++) { | ||
if (row + r < 0 || moduleCount <= row + r) { | ||
continue; | ||
} | ||
switch(mode) { | ||
case Mode.MODE_NUMBER : return 12; | ||
case Mode.MODE_ALPHA_NUM : return 11; | ||
case Mode.MODE_8BIT_BYTE : return 16; | ||
case Mode.MODE_KANJI : return 10; | ||
default : | ||
throw new Error("mode:" + mode); | ||
} | ||
for (let c = -1; c <= 1; c++) { | ||
if (col + c < 0 || moduleCount <= col + c) { | ||
continue; | ||
} | ||
} else if (type < 41) { | ||
if (r == 0 && c == 0) { | ||
continue; | ||
} | ||
// 27 - 40 | ||
if (dark == qrCode.isDark(row + r, col + c)) { | ||
sameCount++; | ||
} | ||
} | ||
} | ||
switch(mode) { | ||
case Mode.MODE_NUMBER : return 14; | ||
case Mode.MODE_ALPHA_NUM : return 13; | ||
case Mode.MODE_8BIT_BYTE : return 16; | ||
case Mode.MODE_KANJI : return 12; | ||
default : | ||
throw new Error("mode:" + mode); | ||
} | ||
if (sameCount > 5) { | ||
lostPoint += 3 + sameCount - 5; | ||
} | ||
} | ||
} | ||
} else { | ||
throw new Error("type:" + type); | ||
} | ||
}, | ||
// LEVEL2 | ||
getLostPoint : function(qrCode: QRCode) { | ||
var moduleCount = qrCode.getModuleCount(); | ||
var lostPoint = 0; | ||
// LEVEL1 | ||
for (var row = 0; row < moduleCount; row++) { | ||
for (let row = 0; row < moduleCount - 1; row++) { | ||
for (let col = 0; col < moduleCount - 1; col++) { | ||
let count = 0; | ||
if (qrCode.isDark(row, col)) count++; | ||
if (qrCode.isDark(row + 1, col)) count++; | ||
if (qrCode.isDark(row, col + 1)) count++; | ||
if (qrCode.isDark(row + 1, col + 1)) count++; | ||
if (count == 0 || count == 4) { | ||
lostPoint += 3; | ||
} | ||
} | ||
} | ||
for (var col = 0; col < moduleCount; col++) { | ||
// LEVEL3 | ||
var sameCount = 0; | ||
var dark = qrCode.isDark(row, col); | ||
for (let row = 0; row < moduleCount; row++) { | ||
for (let col = 0; col < moduleCount - 6; col++) { | ||
if ( | ||
qrCode.isDark(row, col) && | ||
!qrCode.isDark(row, col + 1) && | ||
qrCode.isDark(row, col + 2) && | ||
qrCode.isDark(row, col + 3) && | ||
qrCode.isDark(row, col + 4) && | ||
!qrCode.isDark(row, col + 5) && | ||
qrCode.isDark(row, col + 6) | ||
) { | ||
lostPoint += 40; | ||
} | ||
} | ||
} | ||
for (var r = -1; r <= 1; r++) { | ||
for (let col = 0; col < moduleCount; col++) { | ||
for (let row = 0; row < moduleCount - 6; row++) { | ||
if ( | ||
qrCode.isDark(row, col) && | ||
!qrCode.isDark(row + 1, col) && | ||
qrCode.isDark(row + 2, col) && | ||
qrCode.isDark(row + 3, col) && | ||
qrCode.isDark(row + 4, col) && | ||
!qrCode.isDark(row + 5, col) && | ||
qrCode.isDark(row + 6, col) | ||
) { | ||
lostPoint += 40; | ||
} | ||
} | ||
} | ||
if (row + r < 0 || moduleCount <= row + r) { | ||
continue; | ||
} | ||
// LEVEL4 | ||
for (var c = -1; c <= 1; c++) { | ||
let darkCount = 0; | ||
if (col + c < 0 || moduleCount <= col + c) { | ||
continue; | ||
} | ||
for (let col = 0; col < moduleCount; col++) { | ||
for (let row = 0; row < moduleCount; row++) { | ||
if (qrCode.isDark(row, col)) { | ||
darkCount++; | ||
} | ||
} | ||
} | ||
if (r == 0 && c == 0) { | ||
continue; | ||
} | ||
const ratio = | ||
Math.abs((100 * darkCount) / moduleCount / moduleCount - 50) / 5; | ||
lostPoint += ratio * 10; | ||
if (dark == qrCode.isDark(row + r, col + c) ) { | ||
sameCount++; | ||
} | ||
} | ||
} | ||
if (sameCount > 5) { | ||
lostPoint += (3 + sameCount - 5); | ||
} | ||
} | ||
} | ||
// LEVEL2 | ||
for (var row = 0; row < moduleCount - 1; row++) { | ||
for (var col = 0; col < moduleCount - 1; col++) { | ||
var count = 0; | ||
if (qrCode.isDark(row, col ) ) count++; | ||
if (qrCode.isDark(row + 1, col ) ) count++; | ||
if (qrCode.isDark(row, col + 1) ) count++; | ||
if (qrCode.isDark(row + 1, col + 1) ) count++; | ||
if (count == 0 || count == 4) { | ||
lostPoint += 3; | ||
} | ||
} | ||
} | ||
// LEVEL3 | ||
for (var row = 0; row < moduleCount; row++) { | ||
for (var col = 0; col < moduleCount - 6; col++) { | ||
if (qrCode.isDark(row, col) | ||
&& !qrCode.isDark(row, col + 1) | ||
&& qrCode.isDark(row, col + 2) | ||
&& qrCode.isDark(row, col + 3) | ||
&& qrCode.isDark(row, col + 4) | ||
&& !qrCode.isDark(row, col + 5) | ||
&& qrCode.isDark(row, col + 6) ) { | ||
lostPoint += 40; | ||
} | ||
} | ||
} | ||
for (var col = 0; col < moduleCount; col++) { | ||
for (var row = 0; row < moduleCount - 6; row++) { | ||
if (qrCode.isDark(row, col) | ||
&& !qrCode.isDark(row + 1, col) | ||
&& qrCode.isDark(row + 2, col) | ||
&& qrCode.isDark(row + 3, col) | ||
&& qrCode.isDark(row + 4, col) | ||
&& !qrCode.isDark(row + 5, col) | ||
&& qrCode.isDark(row + 6, col) ) { | ||
lostPoint += 40; | ||
} | ||
} | ||
} | ||
// LEVEL4 | ||
var darkCount = 0; | ||
for (var col = 0; col < moduleCount; col++) { | ||
for (var row = 0; row < moduleCount; row++) { | ||
if (qrCode.isDark(row, col) ) { | ||
darkCount++; | ||
} | ||
} | ||
} | ||
var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5; | ||
lostPoint += ratio * 10; | ||
return lostPoint; | ||
} | ||
return lostPoint; | ||
}, | ||
}; | ||
export default QRUtil; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
3078
1
1
96165
13
18
25
1