bysquare 2.5.0 → 2.6.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.
Files changed (3) hide show
  1. package/README.md +21 -63
  2. package/dist/cli.js +74 -76
  3. package/package.json +5 -7
package/README.md CHANGED
@@ -24,12 +24,6 @@ transformed into images depends on how you implement it. See
24
24
  npm install bysquare
25
25
  ```
26
26
 
27
- **CLI**
28
-
29
- ```sh
30
- npm install --global bysquare
31
- ```
32
-
33
27
  **Browser**
34
28
 
35
29
  ```html
@@ -44,13 +38,19 @@ npm install --global bysquare
44
38
  import { generate, parse } from "npm:bysquare@2.1.0"
45
39
  ```
46
40
 
47
- ## How it works
41
+ **CLI** (Node.JS `v18`+)
42
+
43
+ ```sh
44
+ npm install --global bysquare
45
+ ```
46
+
47
+ # How it works
48
48
 
49
- ### Encoding sequence
49
+ ## Encoding sequence
50
50
 
51
51
  ![logic](./docs/uml/logic.svg)
52
52
 
53
- ## API
53
+ # API
54
54
 
55
55
  ```ts
56
56
  generate(model: DataModel, options?: Options): string
@@ -58,14 +58,14 @@ parse(qr: string): DataModel
58
58
  detect(qr: string): Boolean
59
59
  ```
60
60
 
61
- ## Usage
61
+ # Usage
62
62
 
63
- Generate
63
+ ## Generate
64
64
 
65
65
  ```ts
66
66
  import { CurrencyCode, DataModel, generate, PaymentOptions } from "bysquare"
67
67
 
68
- // long string ready to be encoded to QR
68
+ // string ready to be encoded to QR
69
69
  const qrString = generate({
70
70
  invoiceId: "random-id",
71
71
  payments: [
@@ -82,7 +82,7 @@ const qrString = generate({
82
82
  })
83
83
  ```
84
84
 
85
- Parse
85
+ ## Parse
86
86
 
87
87
  ```ts
88
88
  import { parse } from "bysquare"
@@ -107,44 +107,20 @@ const model =
107
107
  //
108
108
  ```
109
109
 
110
- You can use json file with valid model to generate qr-string.
110
+ ## CLI
111
+
112
+ Encode JSON data from a file and print the corresponding QR code. The file
113
+ argument should be a path to a JSON file.
111
114
 
112
115
  ```sh
113
- # example.json
114
- # {
115
- # "invoiceId": "random-id",
116
- # "payments": [
117
- # {
118
- # "type": 1,
119
- # "amount": 100.0,
120
- # "bankAccounts": [{ "iban": "SK9611000000002918599669" }],
121
- # "currencyCode": "EUR",
122
- # "variableSymbol": "123"
123
- # }
124
- # ]
125
- # }
126
-
127
- $ npx bysquare ./example.json
128
- $ 0405QH8090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6CB54HSI0KH9FCRASHNQBSKAQD2LJ4AU400UVKDNDPFRKLOBEVVVU0QJ000
116
+ npx bysquare --encode <file>
129
117
  ```
130
118
 
131
- You can also use stdin.
119
+ Decode the specified QR code string and print the corresponding JSON data. The
120
+ qrstring argument should be a valid QR code string.
132
121
 
133
122
  ```sh
134
- $ npx bysquare <<< '{
135
- "invoiceId": "random-id",
136
- "payments": [
137
- {
138
- "type": 1,
139
- "amount": 100.0,
140
- "bankAccounts": [{ "iban": "SK9611000000002918599669" }],
141
- "currencyCode": "EUR",
142
- "variableSymbol": "123"
143
- }
144
- ]
145
- }'
146
-
147
- $ 0405QH8090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6CB54HSI0KH9FCRASHNQBSKAQD2LJ4AU400UVKDNDPFRKLOBEVVVU0QJ000
123
+ npx bysquare --decode <qrstring>
148
124
  ```
149
125
 
150
126
  ## Related
@@ -156,23 +132,5 @@ $ 0405QH8090IFU27IV0J6HGGLIOTIBVHNQQJQ6LAVGNBT363HR13JC6CB54HSI0KH9FCRASHNQBSKAQ
156
132
  - <https://www.sbaonline.sk/wp-content/uploads/2020/03/pay-by-square-specifications-1_1_0.pdf>
157
133
  - <https://www.vutbr.cz/studenti/zav-prace/detail/78439>
158
134
 
159
- <!--
160
- Versioning
161
- ----------
162
-
163
- https://github.com/dherges/npm-version-git-flow
164
-
165
- - Stash unfinished work
166
- - Run `npm test`
167
- - Run `npm version <patch, minor, major>`
168
- - Commit and push
169
- - Follow git-flow instructions
170
- - Checkout to master
171
- - Build artefacts
172
- - Push commits and tag, git push && git push --tags
173
- - Validate with `npm publish --dry-run`
174
- - Publish to npm, `npm publish`
175
- -->
176
-
177
135
  [build]: https://img.shields.io/github/actions/workflow/status/xseman/bysquare/tests.yml
178
136
  [version]: https://img.shields.io/npm/v/bysquare
package/dist/cli.js CHANGED
@@ -1,83 +1,81 @@
1
1
  #!/usr/bin/env node
2
2
  import { existsSync, readFileSync } from "node:fs";
3
- import path from "node:path";
4
- import { createInterface } from "node:readline";
3
+ import { parseArgs } from "node:util";
5
4
  import { generate } from "./generate.js";
6
- if /** bysquare "file" */ (process.stdin.isTTY) {
7
- handleInput(process.argv[2]);
8
- } /** bysquare <<< "data" */
9
- else {
10
- ;
11
- (async () => {
12
- const stdin = await handleStdin();
13
- console.log(fromJsonString(stdin));
14
- process.exit(0);
15
- })();
16
- }
17
- function handleInput(input) {
18
- if (input === undefined || input === "-h" || input === "--help") {
19
- console.log(help());
20
- process.exit(0);
5
+ import { parse } from "./parse.js";
6
+ const args = parseArgs({
7
+ allowPositionals: true,
8
+ options: {
9
+ decode: {
10
+ type: "string",
11
+ short: "d"
12
+ },
13
+ encode: {
14
+ type: "string",
15
+ short: "e"
16
+ },
17
+ help: {
18
+ type: "boolean",
19
+ short: "h"
20
+ }
21
21
  }
22
- if (existsSync(process.argv[2])) {
23
- const file = readFileSync(process.argv[2], "utf8");
24
- console.log(fromJsonString(file));
22
+ });
23
+ if (process.stdin.isTTY) {
24
+ /** json file */
25
+ if (args.values.encode) {
26
+ const file = args.values.encode;
27
+ if (existsSync(file)) {
28
+ const data = readFileSync(file, "utf8");
29
+ console.log(generate(JSON.parse(data)));
30
+ }
31
+ else {
32
+ console.error(`File ${file} doesn't exists`);
33
+ process.exit(1);
34
+ }
25
35
  }
26
- else {
27
- console.error(`File ${process.argv[2]} doesn't exists`);
28
- process.exit(1);
36
+ /** input string */
37
+ if (args.values.decode) {
38
+ const qrstring = args.values.decode;
39
+ console.log(JSON.stringify(parse(qrstring), null, 4));
40
+ }
41
+ if (args.values.help || Object.keys(args.values).length === 0) {
42
+ console.log([
43
+ "NAME",
44
+ " bysquare - Simple Node.js library to generate and parse PAY bysquare standard",
45
+ "",
46
+ "SYNOPSIS",
47
+ " bysquare [OPTIONS]",
48
+ "",
49
+ "DESCRIPTION",
50
+ " bysquare is a command-line tool that provides a simple Node.js library to generate ",
51
+ " and parse PAY bysquare standard. It offers functionality to encode JSON data into a ",
52
+ " corresponding QR code and decode a QR code string to obtain the associated JSON data.",
53
+ "",
54
+ "OPTIONS",
55
+ " -d, --decode <qrstring>",
56
+ " Decode the specified QR code string and print the corresponding JSON data.",
57
+ " The qrstring argument should be a valid QR code string.",
58
+ "",
59
+ " -e, --encode <file>",
60
+ " Encode JSON data from a file and print the corresponding QR code.",
61
+ " The file argument should be a path to a JSON file.",
62
+ "",
63
+ " -h, --help",
64
+ " Display the help message and exit.",
65
+ "",
66
+ "USAGE",
67
+ " Encoding JSON data from a file",
68
+ "",
69
+ ` ${process.argv[1]} --encode <file>`,
70
+ " The <file> argument should be the path to the JSON file you want to encode.",
71
+ " The tool will read the file, generate a QR code representing the JSON data",
72
+ "",
73
+ " Decoding a QR code string",
74
+ "",
75
+ ` ${process.argv[1]} --decode <qrstring>`,
76
+ " Replace qrstring with the QR code string you want to decode.",
77
+ " The program will parse the QR code string and print the resulting JSON data.",
78
+ ""
79
+ ].join("\n"));
29
80
  }
30
- }
31
- function fromJsonString(stdin) {
32
- const data = JSON.parse(stdin);
33
- return generate(data);
34
- }
35
- async function handleStdin() {
36
- const readline = createInterface({
37
- input: process.stdin,
38
- output: process.stdout,
39
- terminal: false
40
- });
41
- const lines = [];
42
- return new Promise((resolve, reject) => {
43
- readline
44
- .on("line", (line) => {
45
- lines.push(line);
46
- })
47
- .on("close", () => {
48
- resolve(lines.join(""));
49
- })
50
- .on("SIGINT", /* CTRL+C */ reject);
51
- });
52
- }
53
- function help() {
54
- const exe = path.basename(process.argv[1]);
55
- return [
56
- "Simple Node.js library to generate 'PAY by square' QR string.",
57
- "",
58
- "Usage:",
59
- ` ${exe} file`,
60
- "",
61
- "File:",
62
- " Valid json file",
63
- "",
64
- "Flags:",
65
- " -h, --help display this help and exit",
66
- "",
67
- "If <file> is omitted, reads from stdin.",
68
- "",
69
- "Examples:",
70
- " bysquare <<< \"{",
71
- " \"invoiceId\": \"random-id\",",
72
- " \"payments\": [",
73
- " {",
74
- " \"type\": 1,",
75
- " \"amount\": 100.0,",
76
- " \"bankAccounts\": [{ \"iban\": \"SK9611000000002918599669\" }],",
77
- " \"currencyCode\": \"EUR\",",
78
- " \"variableSymbol\": \"123\"",
79
- " }",
80
- " ]",
81
- " }\""
82
- ].join("\n");
83
81
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bysquare",
3
3
  "description": "It's a national standard for payment QR codes adopted by Slovak Banking Association (SBA)",
4
- "version": "2.5.0",
4
+ "version": "2.6.0",
5
5
  "license": "Apache-2.0",
6
6
  "funding": "https://github.com/sponsors/xseman",
7
7
  "homepage": "https://github.com/xseman/bysquare#readme",
@@ -21,11 +21,10 @@
21
21
  "build": "tsc --build",
22
22
  "format": "dprint fmt ./src/**/*",
23
23
  "typecheck": "tsc --noEmit",
24
- "test": "xv --loader=tsx ./src",
25
- "test:watch": "xv --loader=tsx --watch ./src",
26
- "cli:local": "npm run build && npm link bysquare",
27
24
  "version": "git checkout develop && npm test",
28
- "postversion": "echo 'Now run npm run build && npm publish'"
25
+ "postversion": "echo 'Now run npm run build && npm publish'",
26
+ "test": "node --test --loader tsx --no-warnings src/*.test.ts",
27
+ "test:watch": "node --test --watch --loader tsx --no-warnings src/*.test.ts"
29
28
  },
30
29
  "dependencies": {
31
30
  "crc-32": "~1.2.0",
@@ -38,8 +37,7 @@
38
37
  "@types/node": ">=16.14",
39
38
  "dprint": "~0.36.0",
40
39
  "tsx": "~3.12.0",
41
- "typescript": "~5.0.0",
42
- "xv": "~2.1.0"
40
+ "typescript": "~5.0.0"
43
41
  },
44
42
  "type": "module",
45
43
  "bin": "./dist/cli.js",