Skip to content

Commit

Permalink
fix: export error message from the UPNG lib
Browse files Browse the repository at this point in the history
  • Loading branch information
sinchang committed May 22, 2022
1 parent 4145013 commit c893af7
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 72 deletions.
3 changes: 0 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ jobs:
- run:
name: upload coverage
command: bash <(curl -s https://codecov.io/bash)
- run:
name: build
command: npm run build
- run:
name: release
command: npm run semantic-release || true
58 changes: 31 additions & 27 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
import qrcodeParser from "../src";
import qrcodeParser from '../src'

const fileElement = document.getElementById("file");
const fileElement = document.getElementById('file')
fileElement.addEventListener(
"change",
'change',
(e) => {
let files: FileList = <FileList>(<HTMLInputElement>e.target).files;
qrcodeParser(files[0]).then((res) => {
console.log(res);
document.getElementById("content3").innerText = res;
});
const files: FileList = <FileList>(<HTMLInputElement>e.target).files
qrcodeParser(files[0])
.then((res) => {
document.getElementById('content3').innerText = res
})
.catch((err) => {
// eslint-disable-next-line no-console
console.log('error', err)
})
},
false
);
false,
)

document.getElementById("parse-image-url").addEventListener(
"click",
document.getElementById('parse-image-url').addEventListener(
'click',
() => {
const url = (<HTMLInputElement>document.getElementById("image-url")).value;
const url = (<HTMLInputElement>document.getElementById('image-url')).value

if (!url) return;
if (!url) return

qrcodeParser(url).then((res) => {
document.getElementById("content1").innerText = res;
});
document.getElementById('content1').innerText = res
})
},
false
);
false,
)

document.getElementById("parse-image-base64").addEventListener(
"click",
document.getElementById('parse-image-base64').addEventListener(
'click',
() => {
const url = (<HTMLInputElement>document.getElementById("image-base64"))
.value;
const url = (<HTMLInputElement>document.getElementById('image-base64'))
.value

if (!url) return;
if (!url) return

qrcodeParser(url).then((res) => {
document.getElementById("content2").innerText = res;
});
document.getElementById('content2').innerText = res
})
},
false
);
false,
)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prepublishOnly": "npm run build",
"test:cov": "jest --coverage",
"test": "jest",
"lint": "eslint \"{src,test}/**/*.ts\"",
"lint": "eslint \"{src,test,example}/**/*.ts\"",
"example": "vite",
"build:example": "vite build",
"build": "microbundle && npm run build:umd",
Expand Down
60 changes: 30 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 18 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ export const blob2text = async(blob: Blob): Promise<string> => {
myReader.readAsArrayBuffer(blob)
myReader.addEventListener('loadend', () => {
const buffer = myReader.result
const img = UPNG.decode(buffer as ArrayBuffer)
const rgba = UPNG.toRGBA8(img)[0]
const code = jsQR(new Uint8ClampedArray(rgba), img.width, img.height)
if (code !== null)
resolve(code.data)
else
reject(new Error('decode failed'))
try {
const img = UPNG.decode(buffer as ArrayBuffer)
const rgba = UPNG.toRGBA8(img)[0]
const code = jsQR(new Uint8ClampedArray(rgba), img.width, img.height)
if (code !== null)
return resolve(code.data)
else
return reject(new Error('decode failed'))
}
catch (err) {
if (typeof err === 'string')
return reject(new Error(err))

if (err instanceof Error)
return reject(new Error(err.message))
}
})
})
}
Expand All @@ -29,12 +38,10 @@ export const isBase64 = (str: string): boolean => {

// Copy from https://github.com/sindresorhus/is-url-superb
export const isUrl = (string: string): boolean => {
if (typeof string !== 'string')
throw new TypeError('Expected a string')
if (typeof string !== 'string') throw new TypeError('Expected a string')

string = string.trim()
if (string.includes(' '))
return false
if (string.includes(' ')) return false

try {
new URL(string) // eslint-disable-line no-new
Expand Down

0 comments on commit c893af7

Please sign in to comment.