From 3b5e79e0b38a4b898aba21d032d0431c54888e0b Mon Sep 17 00:00:00 2001 From: xinhu <1216197016@qq.com> Date: Fri, 22 Sep 2023 15:17:39 +0800 Subject: [PATCH] =?UTF-8?q?ArkTs=E8=AF=AD=E6=B3=95=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zenggaofeng --- AppScope/app.json5 | 2 +- CHANGELOG.md | 3 + build-profile.json5 | 7 +- entry/oh-package.json5 | 2 +- entry/src/ohosTest/ets/test/Ability.test.ets | 12 +- entry/src/ohosTest/ets/test/List.test.ets | 2 +- .../ets/test/qrCodeGenerator.test.ets | 105 +++++++-------- .../ohosTest/ets/testability/TestAbility.ets | 11 +- hvigor/hvigor-config.json5 | 6 +- oh-package.json5 | 2 +- qrcodegen/oh-package.json5 | 2 +- .../ets/components/MainPage/qrcodegen.ets | 125 ++++++++++-------- 12 files changed, 150 insertions(+), 129 deletions(-) diff --git a/AppScope/app.json5 b/AppScope/app.json5 index 37ec308..ab6ca98 100644 --- a/AppScope/app.json5 +++ b/AppScope/app.json5 @@ -3,7 +3,7 @@ "bundleName": "cn.openharmony.qrcodegenerator", "vendor": "example", "versionCode": 1000000, - "versionName": "1.0.0", + "versionName": "2.0.1", "icon": "$media:app_icon", "label": "$string:app_name", "distributedNotificationEnabled": true diff --git a/CHANGELOG.md b/CHANGELOG.md index cfb887b..ff74634 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.0.1 +qr-code-generator:ArkTs语法规则整改 + ## 2.0.0 1.适配DevEco Studio: 3.1Beta2(3.1.0.400), SDK: API9 Release(3.2.11.9) diff --git a/build-profile.json5 b/build-profile.json5 index 80a6bc0..36c37b0 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -1,13 +1,14 @@ { "app": { - "compileSdkVersion": 9, - "compatibleSdkVersion": 9, "products": [ { "name": "default", "signingConfig": "default", + "compileSdkVersion": 10, + "compatibleSdkVersion": 10, + "runtimeOS": "OpenHarmony" } - ] + ], }, "modules": [ { diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 index 04df2a1..9361e75 100644 --- a/entry/oh-package.json5 +++ b/entry/oh-package.json5 @@ -4,7 +4,7 @@ "name": "entry", "description": "example description", "repository": "", - "version": "1.0.1", + "version": "2.0.1", "dependencies": { "@ohos/qr-code-generator": "file:../qrcodegen" } diff --git a/entry/src/ohosTest/ets/test/Ability.test.ets b/entry/src/ohosTest/ets/test/Ability.test.ets index 4152da9..b54dcf2 100644 --- a/entry/src/ohosTest/ets/test/Ability.test.ets +++ b/entry/src/ohosTest/ets/test/Ability.test.ets @@ -22,27 +22,27 @@ import hilog from '@ohos.hilog'; import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium' export default function abilityTest() { - describe('ActsAbilityTest', function () { + describe('ActsAbilityTest', () => { // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(function () { + beforeAll(() => { // Presets an action, which is performed only once before all test cases of the test suite start. // This API supports only one parameter: preset action function. }) - beforeEach(function () { + beforeEach(() =>{ // Presets an action, which is performed before each unit test case starts. // The number of execution times is the same as the number of test cases defined by **it**. // This API supports only one parameter: preset action function. }) - afterEach(function () { + afterEach(() => { // Presets a clear action, which is performed after each unit test case ends. // The number of execution times is the same as the number of test cases defined by **it**. // This API supports only one parameter: clear action function. }) - afterAll(function () { + afterAll(() => { // Presets a clear action, which is performed after all test cases of the test suite end. // This API supports only one parameter: clear action function. }) - it('assertContain',0, function () { + it('assertContain',0, () =>{ // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); let a = 'abc' diff --git a/entry/src/ohosTest/ets/test/List.test.ets b/entry/src/ohosTest/ets/test/List.test.ets index c880a7c..0fe49c1 100644 --- a/entry/src/ohosTest/ets/test/List.test.ets +++ b/entry/src/ohosTest/ets/test/List.test.ets @@ -19,7 +19,7 @@ */ import abilityTest from './Ability.test' -import qrCodeGeneratorTest from './qrCodeGenerator.test' +import qrCodeGeneratorTest from './QrCodeGenerator.test' export default function testsuite() { abilityTest() diff --git a/entry/src/ohosTest/ets/test/qrCodeGenerator.test.ets b/entry/src/ohosTest/ets/test/qrCodeGenerator.test.ets index 4a7f543..d7ec13d 100644 --- a/entry/src/ohosTest/ets/test/qrCodeGenerator.test.ets +++ b/entry/src/ohosTest/ets/test/qrCodeGenerator.test.ets @@ -22,116 +22,117 @@ import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from import { qrcodegen } from '@ohos/qr-code-generator' export default function qrCodeGeneratorTest() { - describe('qrCodeGeneratorTest', function () { + describe('qrCodeGeneratorTest1', () => { // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll( () => { + beforeAll(() => { // Presets an action, which is performed only once before all test cases of the test suite start. // This API supports only one parameter: preset action function. }) - beforeEach( () => { + beforeEach(() => { // Presets an action, which is performed before each unit test case starts. // The number of execution times is the same as the number of test cases defined by **it**. // This API supports only one parameter: preset action function. }) - afterEach( () => { + afterEach(() => { // Presets a clear action, which is performed after each unit test case ends. // The number of execution times is the same as the number of test cases defined by **it**. // This API supports only one parameter: clear action function. }) - afterAll( () => { + afterAll(() => { // Presets a clear action, which is performed after all test cases of the test suite end. // This API supports only one parameter: clear action function. }) - it('encodeText',0, () => { + it('encodeText', 0, () => { let qrcode: qrcodegen.QrCode = qrcodegen.QrCode.encodeText("Hello, world!", qrcodegen.QrCode.Ecc.MEDIUM) expect(qrcode.size).assertEqual(21) expect(qrcode.mask).assertEqual(2) expect(qrcode.version).assertEqual(1) - expect(qrcode.getModule(1,6)).assertTrue() + expect(qrcode.getModule(1, 6)).assertTrue() }) - it('encodeBinary',1, () => { - let array = [1,2,3,4,5,6,7,8,9] + it('encodeBinary', 1, () => { + let array = [1, 2, 3, 4, 5, 6, 7, 8, 9] let qrcode: qrcodegen.QrCode = qrcodegen.QrCode.encodeBinary(array, qrcodegen.QrCode.Ecc.MEDIUM) expect(qrcode.size).assertEqual(21) expect(qrcode.mask).assertEqual(7) expect(qrcode.version).assertEqual(1) - expect(qrcode.getModule(1,6)).assertTrue() + expect(qrcode.getModule(1, 6)).assertTrue() }) - it('makeSegments',2, () => { - let str:string = "Hello, world!" - let Segments:Array = qrcodegen.QrSegment.makeSegments(str) - let result:boolean = Segments ? true : false + it('makeSegments', 2, () => { + let str: string = "Hello, world!" + let Segments: Array = qrcodegen.QrSegment.makeSegments(str) + let result: boolean = Segments ? true : false expect(result).assertTrue() }) - it('makeNumeric',3, () => { - let str:string = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374" - let Segments:qrcodegen.QrSegment = qrcodegen.QrSegment.makeNumeric(str) - let array = [1,0,0,1,1,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,0,1,1,1,0,0,1,0,1,1,1,0,1,1,0,1,1,1,0,1,1,1,1,1,1,0, - 1,1,0,1,0,1,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,0,0,1,0,0,1,0,1,0,1,1,0,1,0,0,0,0,1,0,0,1,0,1,1,0,1,1,0,1,1,0,0,1,1,1, - 1,1,1,0,0,0,0,1,1,1,0,1,0,1,1,0,1,1,0,1,0,0,0,0,0,1,0,0,1,1,0,1,0,1,0,0,1,0,1,1,0,0,1,1,1,1,0,0,1,0,0,1,0,1,1,0, - 1,1,1,1,1,0,1,0,1,1,0,1,0,1,1,1,1,0,0,0,1,0,0,0,0,1,1,1,0,1,1,1,0,0,0,0,0,0,1,0,0,1,1,0,1,1,1,0,1,0,1,1,0,0,0,0, - 0,1,0,1,0,0,0,0,0,1,0,0,0,1,1,1,0,0,1,1,1,0,1,1,0,0,1,1,0,0,1,0,1,1,1,0,0,0,0,1,1,0,0,1,1,1,0,0,0,0,0,1,1,0,1,1, - 0,0,0,0,1,1,0,0,1,1,0,0,1,1,1,1,0,0,1,1,0,0,1,1,0,0,0,0,1,0,1,1,1,1,0,1,0,1,1,0,0,0,0,1,1,1,0,0,1,0,0,0,1,0,0,1, - 0,1,0,0] + it('makeNumeric', 3, () => { + let str: string = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374" + let Segments: qrcodegen.QrSegment = qrcodegen.QrSegment.makeNumeric(str) + let array = [1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, + 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, + 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, + 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, + 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, + 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, + 0, 1, 0, 0] expect(Segments.getData().toString()).assertEqual(array.toString()) expect(Segments.numChars).assertEqual(100) }) - it('makeAlphanumeric',4, () => { - let str:string = "......" - let Segments:qrcodegen.QrSegment = qrcodegen.QrSegment.makeAlphanumeric(str) - let array = [1,1,1,1,0,0,0,1,1,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,1,1,0,0,0,1,1,0,0] + it('makeAlphanumeric', 4, () => { + let str: string = "......" + let Segments: qrcodegen.QrSegment = qrcodegen.QrSegment.makeAlphanumeric(str) + let array = [1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0] expect(Segments.getData().toString()).assertEqual(array.toString()) expect(Segments.numChars).assertEqual(6) }) - it('makeEci',5, () => { - let str:number = 2 - let Segments:qrcodegen.QrSegment = qrcodegen.QrSegment.makeEci(str) - let array = [0,0,0,0,0,0,1,0] + it('makeEci', 5, () => { + let str: number = 2 + let Segments: qrcodegen.QrSegment = qrcodegen.QrSegment.makeEci(str) + let array = [0, 0, 0, 0, 0, 0, 1, 0] expect(Segments.getData().toString()).assertEqual(array.toString()) expect(Segments.numChars).assertEqual(0) }) - it('encodeSegments',6, () => { - let str:string = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374" - let str1:string = "......" + it('encodeSegments', 6, () => { + let str: string = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374" + let str1: string = "......" let segs = [ - qrcodegen.QrSegment.makeNumeric(str), - qrcodegen.QrSegment.makeAlphanumeric(str1) + qrcodegen.QrSegment.makeNumeric(str), + qrcodegen.QrSegment.makeAlphanumeric(str1) ] let qrcode: qrcodegen.QrCode = qrcodegen.QrCode.encodeSegments(segs, qrcodegen.QrCode.Ecc.MEDIUM) expect(qrcode.size).assertEqual(33) expect(qrcode.mask).assertEqual(2) expect(qrcode.version).assertEqual(4) - expect(qrcode.getModule(1,6)).assertTrue() + expect(qrcode.getModule(1, 6)).assertTrue() }) - it('makeBytes',7, () => { - let str:string = "Golden ratio \u03C6 = 1." - let Segments:qrcodegen.QrSegment = qrcodegen.QrSegment.makeBytes(str2UTF8(str)) - let array = [0,1,0,0,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,1,1,0,0,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,1,0,0,0, - 1,0,0,0,0,0,0,1,1,1,0,0,1,0,0,1,1,0,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,0,1,1, - 0,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,0,1,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,1,0,1,1,1,0] + it('makeBytes', 7, () => { + let str: string = "Golden ratio \u03C6 = 1." + let Segments: qrcodegen.QrSegment = qrcodegen.QrSegment.makeBytes(str2UTF8(str)) + let array = [0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, + 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0] expect(Segments.getData().toString()).assertEqual(array.toString()) expect(Segments.numChars).assertEqual(20) }) }) - function str2UTF8(str){ - var bytes = new Array(); - var len,c; + let str2UTF8 = (str:string) => { + let bytes:Array = new Array(); + let len: number = 0 + let c: number = 0; len = str.length; - for(var i = 0; i < len; i++){ + for (let i = 0; i < len; i++) { c = str.charCodeAt(i); - if(c >= 0x010000 && c <= 0x10FFFF){ + if (c >= 0x010000 && c <= 0x10FFFF) { bytes.push(((c >> 18) & 0x07) | 0xF0); bytes.push(((c >> 12) & 0x3F) | 0x80); bytes.push(((c >> 6) & 0x3F) | 0x80); bytes.push((c & 0x3F) | 0x80); - }else if(c >= 0x000800 && c <= 0x00FFFF){ + } else if (c >= 0x000800 && c <= 0x00FFFF) { bytes.push(((c >> 12) & 0x0F) | 0xE0); bytes.push(((c >> 6) & 0x3F) | 0x80); bytes.push((c & 0x3F) | 0x80); - }else if(c >= 0x000080 && c <= 0x0007FF){ + } else if (c >= 0x000080 && c <= 0x0007FF) { bytes.push(((c >> 6) & 0x1F) | 0xC0); bytes.push((c & 0x3F) | 0x80); - }else{ + } else { bytes.push(c & 0xFF); } } diff --git a/entry/src/ohosTest/ets/testability/TestAbility.ets b/entry/src/ohosTest/ets/testability/TestAbility.ets index 5806196..bb2dcd8 100644 --- a/entry/src/ohosTest/ets/testability/TestAbility.ets +++ b/entry/src/ohosTest/ets/testability/TestAbility.ets @@ -24,18 +24,15 @@ import hilog from '@ohos.hilog'; import { Hypium } from '@ohos/hypium'; import testsuite from '../test/List.test'; import window from '@ohos.window'; - +import Want from '@ohos.app.ability.Want'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; export default class TestAbility extends UIAbility { - onCreate(want, launchParam) { + onCreate(want:Want, launchParam:AbilityConstant.LaunchParam) { hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate'); hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:'+ JSON.stringify(launchParam) ?? ''); - var abilityDelegator: any - abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() - var abilityDelegatorArguments: any - abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); - Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) + Hypium.hypiumTest(AbilityDelegatorRegistry.getAbilityDelegator(), AbilityDelegatorRegistry.getArguments(), testsuite) } onDestroy() { diff --git a/hvigor/hvigor-config.json5 b/hvigor/hvigor-config.json5 index ff68812..e75281b 100644 --- a/hvigor/hvigor-config.json5 +++ b/hvigor/hvigor-config.json5 @@ -1,6 +1,6 @@ { - "hvigorVersion": "2.0.0", + "hvigorVersion": "3.0.2", "dependencies": { - "@ohos/hvigor-ohos-plugin": "2.0.0" + "@ohos/hvigor-ohos-plugin": "3.0.2" } -} +} \ No newline at end of file diff --git a/oh-package.json5 b/oh-package.json5 index c81699e..bab9a2e 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -6,6 +6,6 @@ "name": "qrcodegenerator", "description": "example description", "repository": "", - "version": "1.0.1", + "version": "2.0.1", "dependencies": {} } diff --git a/qrcodegen/oh-package.json5 b/qrcodegen/oh-package.json5 index 927ff22..1591dd4 100644 --- a/qrcodegen/oh-package.json5 +++ b/qrcodegen/oh-package.json5 @@ -12,6 +12,6 @@ "description": "High-quality QR Code generator library", "main": "index.ets", "repository": "https://gitee.com/openharmony-sig/qr-code-generator", - "version": "2.0.0", + "version": "2.0.1", "dependencies": {} } diff --git a/qrcodegen/src/main/ets/components/MainPage/qrcodegen.ets b/qrcodegen/src/main/ets/components/MainPage/qrcodegen.ets index eb22b16..a527c43 100644 --- a/qrcodegen/src/main/ets/components/MainPage/qrcodegen.ets +++ b/qrcodegen/src/main/ets/components/MainPage/qrcodegen.ets @@ -91,7 +91,7 @@ namespace qrcodegen { if (!(QrCode.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= QrCode.MAX_VERSION) || mask < -1 || mask > 7) - throw "Invalid value"; + throw new Error("Invalid value"); // Find the minimal version number to use let version: int; @@ -104,7 +104,7 @@ namespace qrcodegen { break; // This version number is found to be suitable } if (version >= maxVersion) // All versions in the range could not fit the given data - throw "Data too long"; + throw new Error("Data too long"); } // Increase the error correction level while the data still fits in the current version number @@ -122,16 +122,16 @@ namespace qrcodegen { bb.push(b); } if (bb.length != dataUsedBits) - throw "Assertion error"; + throw new Error("Assertion error"); // Add terminator and pad up to a byte if applicable const dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8; if (bb.length > dataCapacityBits) - throw "Assertion error"; + throw new Error("Assertion error"); appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb); appendBits(0, (8 - bb.length % 8) % 8, bb); if (bb.length % 8 != 0) - throw "Assertion error"; + throw new Error("Assertion error"); // Pad with alternating bytes until data capacity is reached for (let padByte = 0xEC; bb.length < dataCapacityBits; padByte ^= 0xEC ^ 0x11) @@ -169,26 +169,31 @@ namespace qrcodegen { // error correction level, data codeword bytes, and mask number. // This is a low-level API that most users should not use directly. // A mid-level API is the encodeSegments() function. + public readonly version: int ; + public readonly errorCorrectionLevel: QrCode.Ecc ; + public readonly mask: int ; public constructor( // The version number of this QR Code, which is between 1 and 40 (inclusive). // This determines the size of this barcode. - public readonly version: int, + version: int, // The error correction level used in this QR Code. - public readonly errorCorrectionLevel: QrCode.Ecc, + errorCorrectionLevel: QrCode.Ecc, dataCodewords: Array, // The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive). // Even if a QR Code is created with automatic masking requested (mask = -1), // the resulting object still has a mask value between 0 and 7. - public readonly mask: int) { - + mask: int) { + this.version = version + this.errorCorrectionLevel = errorCorrectionLevel + this.mask = mask // Check scalar arguments if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION) - throw "Version value out of range"; + throw new Error("Version value out of range"); if (mask < -1 || mask > 7) - throw "Mask value out of range"; + throw new Error("Mask value out of range"); this.size = version * 4 + 17; // Initialize both grids to be size*size arrays of Boolean false @@ -220,7 +225,7 @@ namespace qrcodegen { } } if (mask < 0 || mask > 7) - throw "Assertion error"; + throw new Error("Assertion error"); this.mask = mask; this.applyMask(mask); // Apply the final choice of mask this.drawFormatBits(mask); // Overwrite old format bits @@ -247,7 +252,7 @@ namespace qrcodegen { // The scale must be a positive integer and the border must be a non-negative integer. public drawCanvas(scale: int, border: int, canvas: CanvasRenderingContext2D): void { if (scale <= 0 || border < 0) - throw "Value out of range"; + throw new Error("Value out of range"); const width: int = (this.size + border * 2) * scale; let canvasWidth = Math.min(canvas.width + canvas.height) for (let y = -border; y < this.size + border; y++) { @@ -263,7 +268,7 @@ namespace qrcodegen { // of border modules. The string always uses Unix newlines (\n), regardless of the platform. public toSvgString(border: int): string { if (border < 0) - throw "Border must be non-negative"; + throw new Error("Border must be non-negative"); let parts: Array = []; for (let y = 0; y < this.size; y++) { for (let x = 0; x < this.size; x++) { @@ -323,7 +328,7 @@ namespace qrcodegen { rem = (rem << 1) ^ ((rem >>> 9) * 0x537); const bits = (data << 10 | rem) ^ 0x5412; // uint15 if (bits >>> 15 != 0) - throw "Assertion error"; + throw new Error("Assertion error"); // Draw first copy for (let i = 0; i <= 5; i++) @@ -355,7 +360,7 @@ namespace qrcodegen { rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25); const bits: int = this.version << 12 | rem; // uint18 if (bits >>> 18 != 0) - throw "Assertion error"; + throw new Error("Assertion error"); // Draw two copies for (let i = 0; i < 18; i++) { @@ -409,7 +414,7 @@ namespace qrcodegen { const ver: int = this.version; const ecl: QrCode.Ecc = this.errorCorrectionLevel; if (data.length != QrCode.getNumDataCodewords(ver, ecl)) - throw "Invalid argument"; + throw new Error("Invalid argument"); // Calculate parameter numbers const numBlocks: int = QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver]; @@ -440,7 +445,7 @@ namespace qrcodegen { }); } if (result.length != rawCodewords) - throw "Assertion error"; + throw new Error("Assertion error"); return result; } @@ -449,7 +454,7 @@ namespace qrcodegen { // data area of this QR Code. Function modules need to be marked off before this is called. private drawCodewords(data: Array): void { if (data.length != Math.floor(QrCode.getNumRawDataModules(this.version) / 8)) - throw "Invalid argument"; + throw new Error("Invalid argument"); let i: int = 0; // Bit index into the data // Do the funny zigzag scan for (let right = this.size - 1; right >= 1; right -= 2) { // Index of right column in each column pair @@ -470,7 +475,7 @@ namespace qrcodegen { } } if (i != data.length * 8) - throw "Assertion error"; + throw new Error("Assertion error"); } @@ -481,7 +486,7 @@ namespace qrcodegen { // QR Code needs exactly one (not zero, two, etc.) mask applied. private applyMask(mask: int): void { if (mask < 0 || mask > 7) - throw "Mask value out of range"; + throw new Error("Mask value out of range"); for (let y = 0; y < this.size; y++) { for (let x = 0; x < this.size; x++) { let invert: boolean; @@ -511,7 +516,7 @@ namespace qrcodegen { invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break; default: - throw "Assertion error"; + throw new Error("Assertion error"); } if (!this.isFunction[y][x] && invert) this.modules[y][x] = !this.modules[y][x]; @@ -618,7 +623,7 @@ namespace qrcodegen { // The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table. private static getNumRawDataModules(ver: int): int { if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION) - throw "Version number out of range"; + throw new Error("Version number out of range"); let result: int = (16 * ver + 128) * ver + 64; if (ver >= 2) { const numAlign: int = Math.floor(ver / 7) + 2; @@ -627,7 +632,7 @@ namespace qrcodegen { result -= 36; } if (!(208 <= result && result <= 29648)) - throw "Assertion error"; + throw new Error("Assertion error"); return result; } @@ -646,7 +651,7 @@ namespace qrcodegen { // implemented as a lookup table over all possible parameter values, instead of as an algorithm. private static reedSolomonComputeDivisor(degree: int): Array { if (degree < 1 || degree > 255) - throw "Degree out of range"; + throw new Error("Degree out of range"); // Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1. // For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93]. let result: Array = []; @@ -688,7 +693,7 @@ namespace qrcodegen { // are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8. private static reedSolomonMultiply(x: byte, y: byte): byte { if (x >>> 8 != 0 || y >>> 8 != 0) - throw "Byte out of range"; + throw new Error("Byte out of range"); // Russian peasant multiplication let z: int = 0; for (let i = 7; i >= 0; i--) { @@ -696,7 +701,7 @@ namespace qrcodegen { z ^= ((y >>> i) & 1) * x; } if (z >>> 8 != 0) - throw "Assertion error"; + throw new Error("Assertion error"); return z as byte; } @@ -706,7 +711,7 @@ namespace qrcodegen { private finderPenaltyCountPatterns(runHistory: Array): int { const n: int = runHistory[1]; if (n > this.size * 3) - throw "Assertion error"; + throw new Error("Assertion error"); const core: boolean = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n; return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0); @@ -769,7 +774,7 @@ namespace qrcodegen { // to the given buffer. Requires 0 <= len <= 31 and 0 <= val < 2^len. function appendBits(val: int, len: int, bb: Array): void { if (len < 0 || len > 31 || val >>> len != 0) - throw "Value out of range"; + throw new Error("Value out of range"); for (let i = len - 1; i >= 0; i--) // Append bit by bit bb.push((val >>> i) & 1); } @@ -810,13 +815,13 @@ namespace qrcodegen { // Returns a segment representing the given string of decimal digits encoded in numeric mode. - public static makeNumeric(digits: string): QrSegment { - if (!this.NUMERIC_REGEX.test(digits)) - throw "String contains non-numeric characters"; + public static makeNumeric(digits: string): QrSegment{ + if (!QrSegment.NUMERIC_REGEX.test(digits)) + throw new Error("String contains non-numeric characters"); let bb: Array = [] for (let i = 0; i < digits.length; ) { // Consume up to 3 digits per iteration const n: int = Math.min(digits.length - i, 3); - appendBits(parseInt(digits.substr(i, n), 10), n * 3 + 1, bb); + appendBits(Number.parseInt(digits.substr(i, n), 10), n * 3 + 1, bb); i += n; } return new QrSegment(QrSegment.Mode.NUMERIC, digits.length, bb); @@ -827,8 +832,8 @@ namespace qrcodegen { // The characters allowed are: 0 to 9, A to Z (uppercase only), space, // dollar, percent, asterisk, plus, hyphen, period, slash, colon. public static makeAlphanumeric(text: string): QrSegment { - if (!this.ALPHANUMERIC_REGEX.test(text)) - throw "String contains unencodable characters in alphanumeric mode"; + if (!QrSegment.ALPHANUMERIC_REGEX.test(text)) + throw new Error("String contains unencodable characters in alphanumeric mode"); let bb: Array = [] let i: int; for (i = 0; i + 2 <= text.length; i += 2) { // Process groups of 2 @@ -848,9 +853,9 @@ namespace qrcodegen { // Select the most efficient segment encoding automatically if (!text) return []; - else if (this.NUMERIC_REGEX.test(text)) + else if (QrSegment.NUMERIC_REGEX.test(text)) return [QrSegment.makeNumeric(text)]; - else if (this.ALPHANUMERIC_REGEX.test(text)) + else if (QrSegment.ALPHANUMERIC_REGEX.test(text)) return [QrSegment.makeAlphanumeric(text)]; else return [QrSegment.makeBytes(QrSegment.toUtf8ByteArray(text))]; @@ -862,7 +867,7 @@ namespace qrcodegen { public static makeEci(assignVal: int): QrSegment { let bb: Array = [] if (assignVal < 0) - throw "ECI assignment value out of range"; + throw new Error("ECI assignment value out of range"); else if (assignVal < (1 << 7)) appendBits(assignVal, 8, bb); else if (assignVal < (1 << 14)) { @@ -872,7 +877,7 @@ namespace qrcodegen { appendBits(6, 3, bb); appendBits(assignVal, 21, bb); } else - throw "ECI assignment value out of range"; + throw new Error("ECI assignment value out of range"); return new QrSegment(QrSegment.Mode.ECI, 0, bb); } @@ -882,20 +887,27 @@ namespace qrcodegen { // Creates a new QR Code segment with the given attributes and data. // The character count (numChars) must agree with the mode and the bit buffer length, // but the constraint isn't checked. The given bit buffer is cloned and stored. + // The mode indicator of this segment. + public readonly mode: QrSegment.Mode + public readonly numChars: int + private readonly bitData: Array public constructor( // The mode indicator of this segment. - public readonly mode: QrSegment.Mode, + mode: QrSegment.Mode, // The length of this segment's unencoded data. Measured in characters for // numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode. // Always zero or positive. Not the same as the data's bit length. - public readonly numChars: int, + numChars: int, // The data bits of this segment. Accessed through getData(). - private readonly bitData: Array) { + bitData: Array) { + this.mode = mode ; + this.numChars = numChars ; + this.bitData = bitData ; if (numChars < 0) - throw "Invalid argument"; + throw new Error("Invalid argument"); this.bitData = bitData.slice(); // Make defensive copy } @@ -915,7 +927,7 @@ namespace qrcodegen { for (const seg of segs) { const ccbits: int = seg.mode.numCharCountBits(version); if (seg.numChars >= (1 << ccbits)) - return Infinity; // The segment's length doesn't fit the field's bit width + return Number.POSITIVE_INFINITY; // The segment's length doesn't fit the field's bit width result += 4 + ccbits + seg.bitData.length; } return result; @@ -930,7 +942,7 @@ namespace qrcodegen { if (str.charAt(i) != "%") result.push(str.charCodeAt(i)); else { - result.push(parseInt(str.substr(i + 1, 2), 16)); + result.push(Number.parseInt(str.substr(i + 1, 2), 16)); i += 2; } } @@ -943,13 +955,13 @@ namespace qrcodegen { // Describes precisely all strings that are encodable in numeric mode. To test // whether a string s is encodable: let ok: boolean = NUMERIC_REGEX.test(s); // A string is encodable iff each character is in the range 0 to 9. - public static readonly NUMERIC_REGEX: RegExp = /^[0-9]*$/; + public static readonly NUMERIC_REGEX: RegExp =new RegExp ("^[0-9]*$"); // Describes precisely all strings that are encodable in alphanumeric mode. To test // whether a string s is encodable: let ok: boolean = ALPHANUMERIC_REGEX.test(s); // A string is encodable iff each character is in the following set: 0 to 9, A to Z // (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon. - public static readonly ALPHANUMERIC_REGEX: RegExp = /^[A-Z0-9 $%*+.\/:-]*$/; + public static readonly ALPHANUMERIC_REGEX: RegExp = new RegExp("^[A-Z0-9 $%*+.\/:-]*$"); // The set of all legal characters in alphanumeric mode, // where each character value maps to the index in the string. @@ -980,12 +992,16 @@ namespace qrcodegen.QrCode { /*-- Constructor and fields --*/ - + public readonly ordinal: int + public readonly formatBits: int private constructor( // In the range 0 to 3 (unsigned 2-bit integer). - public readonly ordinal: int, + ordinal: int, // (Package-private) In the range 0 to 3 (unsigned 2-bit integer). - public readonly formatBits: int) { + formatBits: int) { + this.ordinal = ordinal ; + this.formatBits = formatBits ; + } } } @@ -1013,12 +1029,15 @@ namespace qrcodegen.QrSegment { /*-- Constructor and fields --*/ - + public readonly modeBits: int ; + private readonly numBitsCharCount:number[] private constructor( // The mode indicator bits, which is a uint4 value (range 0 to 15). - public readonly modeBits: int, + modeBits: int, // Number of character count bits for three different version ranges. - private readonly numBitsCharCount: [int, int, int]) { + numBitsCharCount: number[]) { + this.modeBits = modeBits + this.numBitsCharCount = numBitsCharCount } -- Gitee