btrz-liquid-templates 2.0.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.githooks/pre-commit +18 -0
- package/docs/index.md +12 -5
- package/package.json +5 -4
- package/src/barcode-generator.js +24 -3
- package/test/barcode-test.js +33 -1
- package/test/helpers/barcode-decode.js +8 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# btrz-banned-libs-hook-v1
|
|
3
|
+
set -eu
|
|
4
|
+
|
|
5
|
+
DIFF_CONTENT=$(git diff --cached -U0 -- package.json package-lock.json npm-shrinkwrap.json yarn.lock pnpm-lock.yaml 2>/dev/null || true)
|
|
6
|
+
|
|
7
|
+
if [ -z "$DIFF_CONTENT" ]; then
|
|
8
|
+
exit 0
|
|
9
|
+
fi
|
|
10
|
+
|
|
11
|
+
if printf '%s
|
|
12
|
+
' "$DIFF_CONTENT" | rg -q '^\+.*("(lodash|mocha|chai|sinon)"|lodash@|mocha@|chai@|sinon@)'; then
|
|
13
|
+
echo ""
|
|
14
|
+
echo "Commit blocked: adding banned libraries is not allowed (lodash, mocha, chai, sinon)."
|
|
15
|
+
echo "Remove the staged dependency/lockfile additions and retry."
|
|
16
|
+
echo ""
|
|
17
|
+
exit 1
|
|
18
|
+
fi
|
package/docs/index.md
CHANGED
|
@@ -267,26 +267,33 @@ Parameters
|
|
|
267
267
|
|------|------------|----------|---------|
|
|
268
268
|
| data | The string to use to generate the barcode, it can be hardcoded or some of the 'data' given to the template | Y | |
|
|
269
269
|
| type | The barcode type | N | code128
|
|
270
|
-
| height |
|
|
271
|
-
| width |
|
|
270
|
+
| height | For linear and rectangular barcodes: bar/module height (mm) passed to bwip-js. For square matrix types (see below): ignored so the PNG stays 1:1 | N | 30
|
|
271
|
+
| width | Display width of the image in the PDF (points). For square matrix types this is what controls rendered size | N | 200
|
|
272
272
|
| margin | A list of 4 values for the margin | N | 0,0,0,0
|
|
273
273
|
|
|
274
|
+
For square matrix types (`qrcode`, `microqr`, `datamatrix`, `aztec`, `azrune`, `hanxin`, `maxicode`, `hibc_qr`, `hibc_dm`, `hibc_aztec`, and `codeone`), `height` is not applied when generating the image. The PNG remains square and the PDF scales it using `width` only. Passing equal `height` and `width` values (for example `50 50`) is fine and matches common templates, but only `width` affects the drawn size.
|
|
274
275
|
|
|
275
276
|
It will use the value of ticket.code to generate the barcode with all the defaults
|
|
276
277
|
|
|
277
278
|
```liquid
|
|
278
|
-
{% raw %}
|
|
279
|
+
{% raw %}{% barcode ticket.code %}{% endraw %}
|
|
279
280
|
```
|
|
280
281
|
|
|
281
282
|
It will use the value given and use generate a 'code11' barcode with a height of 50 and a width of 300
|
|
282
283
|
|
|
283
284
|
```liquid
|
|
284
|
-
{% raw %}
|
|
285
|
+
{% raw %}{% barcode 1234 code11 50 300 20,20,20,0 %}{% endraw %}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
A QR code drawn at 50 points wide (height argument is accepted but does not stretch the image):
|
|
289
|
+
|
|
290
|
+
```liquid
|
|
291
|
+
{% raw %}{% barcode ticket.qrCodeInfo qrcode 50 50 0,0,0,5 %}{% endraw %}
|
|
285
292
|
```
|
|
286
293
|
|
|
287
294
|
### Supported types
|
|
288
295
|
|
|
289
|
-
The `{% barcode %}` tag accepts legacy Symbology-style type names and maps them to [bwip-js](https://github.com/metafloor/bwip-js) barcode identifiers. Common examples include `code128`, `code11`, `code39`, `qrcode`, `pdf417`, and `datamatrix`.
|
|
296
|
+
The `{% raw %}{% barcode %}{% endraw %}` tag accepts legacy Symbology-style type names and maps them to [bwip-js](https://github.com/metafloor/bwip-js) barcode identifiers. Common examples include `code128`, `code11`, `code39`, `qrcode`, `pdf417`, and `datamatrix`.
|
|
290
297
|
|
|
291
298
|
The following legacy type names are not supported and will throw an explicit error: `gridmatrix`, `koreapost`, `upnqr`, and `vin`.
|
|
292
299
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "btrz-liquid-templates",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Extension and helpers for liquid templates to deal with our data and other things we need to encapsulate",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"test": "node --test test/*-test.js",
|
|
9
9
|
"preversion": "npm test",
|
|
10
10
|
"postversion": "git push origin main && git push --tags",
|
|
11
|
-
"version": "git tag $1"
|
|
11
|
+
"version": "git tag $1",
|
|
12
|
+
"prepare": "git config core.hooksPath .githooks"
|
|
12
13
|
},
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|
|
@@ -22,9 +23,9 @@
|
|
|
22
23
|
"homepage": "https://github.com/Betterez/btrz-liquid-templates#readme",
|
|
23
24
|
"dependencies": {
|
|
24
25
|
"axios": "1.18.1",
|
|
25
|
-
"btrz-formatter": "1.
|
|
26
|
+
"btrz-formatter": "1.8.0",
|
|
26
27
|
"bwip-js": "^4.11.2",
|
|
27
|
-
"bz-date": "2.
|
|
28
|
+
"bz-date": "2.4.0",
|
|
28
29
|
"liquidjs": "10.27.1",
|
|
29
30
|
"moment": "2.30.1",
|
|
30
31
|
"moment-timezone": "0.6.2",
|
package/src/barcode-generator.js
CHANGED
|
@@ -1,17 +1,38 @@
|
|
|
1
1
|
const bwipjs = require("bwip-js");
|
|
2
2
|
const {resolveBarcodeType} = require("./barcode-types.js");
|
|
3
3
|
|
|
4
|
+
const SQUARE_BCIDS = new Set([
|
|
5
|
+
"qrcode",
|
|
6
|
+
"microqrcode",
|
|
7
|
+
"datamatrix",
|
|
8
|
+
"azteccode",
|
|
9
|
+
"aztecrune",
|
|
10
|
+
"hanxin",
|
|
11
|
+
"maxicode",
|
|
12
|
+
"hibcqrcode",
|
|
13
|
+
"hibcdatamatrix",
|
|
14
|
+
"hibcazteccode",
|
|
15
|
+
"codeone"
|
|
16
|
+
]);
|
|
17
|
+
|
|
4
18
|
async function generateBarcodePng({type, content, height}) {
|
|
5
19
|
const bcid = resolveBarcodeType(type);
|
|
6
|
-
const
|
|
20
|
+
const opts = {
|
|
7
21
|
bcid,
|
|
8
22
|
text: String(content),
|
|
9
23
|
scale: 2,
|
|
10
|
-
height: Number(height) || 30,
|
|
11
24
|
includetext: false,
|
|
12
25
|
backgroundcolor: "FFFFFF"
|
|
13
|
-
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Linear (and rectangular 2D) barcodes: height is bar/module height in mm.
|
|
29
|
+
// Square matrix codes: omit height so bwip keeps a 1:1 PNG; display size
|
|
30
|
+
// is controlled by the image width in the document definition.
|
|
31
|
+
if (!SQUARE_BCIDS.has(bcid)) {
|
|
32
|
+
opts.height = Number(height) || 30;
|
|
33
|
+
}
|
|
14
34
|
|
|
35
|
+
const pngBuffer = await bwipjs.toBuffer(opts);
|
|
15
36
|
return `data:image/png;base64,${pngBuffer.toString("base64")}`;
|
|
16
37
|
}
|
|
17
38
|
|
package/test/barcode-test.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
const assert = require("node:assert/strict");
|
|
2
2
|
const {describe, it} = require("node:test");
|
|
3
3
|
const {generateBarcodePng} = require("../src/barcode-generator.js");
|
|
4
|
-
const {
|
|
4
|
+
const {
|
|
5
|
+
decodePngDataUri,
|
|
6
|
+
isValidPngDataUri,
|
|
7
|
+
getPngDimensionsFromDataUri
|
|
8
|
+
} = require("./helpers/barcode-decode.js");
|
|
5
9
|
|
|
6
10
|
describe("barcode-generator.js", () => {
|
|
7
11
|
it("should generate a valid PNG data URI for code128", async () => {
|
|
@@ -15,6 +19,34 @@ describe("barcode-generator.js", () => {
|
|
|
15
19
|
assert.equal(isValidPngDataUri(image), true);
|
|
16
20
|
});
|
|
17
21
|
|
|
22
|
+
it("should keep qrcode PNGs square when a height is provided", async () => {
|
|
23
|
+
const image = await generateBarcodePng({
|
|
24
|
+
type: "qrcode",
|
|
25
|
+
content: "test-payload",
|
|
26
|
+
height: 50
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const {width, height} = getPngDimensionsFromDataUri(image);
|
|
30
|
+
assert.equal(width, height);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should apply height to linear barcodes so taller height produces a taller PNG", async () => {
|
|
34
|
+
const shortImage = await generateBarcodePng({
|
|
35
|
+
type: "code128",
|
|
36
|
+
content: "1234",
|
|
37
|
+
height: 10
|
|
38
|
+
});
|
|
39
|
+
const tallImage = await generateBarcodePng({
|
|
40
|
+
type: "code128",
|
|
41
|
+
content: "1234",
|
|
42
|
+
height: 50
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const short = getPngDimensionsFromDataUri(shortImage);
|
|
46
|
+
const tall = getPngDimensionsFromDataUri(tallImage);
|
|
47
|
+
assert.ok(tall.height > short.height);
|
|
48
|
+
});
|
|
49
|
+
|
|
18
50
|
it("should decode generated code128 barcodes to the original payload", async () => {
|
|
19
51
|
const image = await generateBarcodePng({
|
|
20
52
|
type: "code128",
|
|
@@ -24,7 +24,14 @@ function isValidPngDataUri(dataUri) {
|
|
|
24
24
|
return signature.equals(pngSignature);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
function getPngDimensionsFromDataUri(dataUri) {
|
|
28
|
+
const base64 = dataUri.replace(/^data:image\/png;base64,/, "");
|
|
29
|
+
const png = PNG.sync.read(Buffer.from(base64, "base64"));
|
|
30
|
+
return {width: png.width, height: png.height};
|
|
31
|
+
}
|
|
32
|
+
|
|
27
33
|
module.exports = {
|
|
28
34
|
decodePngDataUri,
|
|
29
|
-
isValidPngDataUri
|
|
35
|
+
isValidPngDataUri,
|
|
36
|
+
getPngDimensionsFromDataUri
|
|
30
37
|
};
|