Skip to content

Commit

Permalink
new packages: codecs for QR Codes (mode=numeric) URL (#20)
Browse files Browse the repository at this point in the history
* new packages: codecs for QR Codes URL

* fix ci

* note about qr mode
  • Loading branch information
cometkim committed Apr 4, 2024
1 parent b3b7281 commit f47c286
Show file tree
Hide file tree
Showing 22 changed files with 347 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/good-jobs-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@urlpack/base10": major
"@urlpack/qrjson": major
---

New codecs for QR Codes URL (See https://huonw.github.io/blog/2024/03/qr-base10-base64/)
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Build and validate packages
run: |
yarn workspaces foreach -t run build
yarn workspaces foreach --worktree --interlaced --topological run build
- name: Run all tests
run: |
Expand Down
1 change: 1 addition & 0 deletions packages/base10/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib/
21 changes: 21 additions & 0 deletions packages/base10/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Danggeun Market Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions packages/base10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @urlpack/base10

[![Package Version](https://img.shields.io/npm/v/@urlpack/base10)](https://npm.im/@urlpack/base10)
[![License](https://img.shields.io/npm/l/@urlpack/base10)](#License)
[![Bundle Size](https://img.shields.io/bundlephobia/minzip/@urlpack/base10)](https://bundlephobia.com/package/@urlpack/base10)

Pure JavaScript implementation of the Base10 codec.

- Zero dependencies
- ES Modules & Browser compatible
- Tree-shakable encoder and decoder

## License

MIT
48 changes: 48 additions & 0 deletions packages/base10/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@urlpack/base10",
"version": "0.0.0",
"license": "MIT",
"homepage": "https://github.com/daangn/urlpack/tree/main/packages/base10",
"repository": {
"type": "git",
"url": "https://github.com/daangn/urlpack.git",
"directory": "packages/base10"
},
"source": "./src/index.ts",
"type": "module",
"main": "./src/index.ts",
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.mjs",
"require": "./lib/index.cjs"
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public",
"main": "./lib/index.mjs"
},
"files": [
"src",
"lib"
],
"scripts": {
"prepack": "yarn build",
"build": "nanobundle build",
"test": "uvu -r tsm",
"test:watch": "yarn test || true && watchlist src tests -- yarn test"
},
"dependencies": {
"@urlpack/base-codec": "workspace:^2.0.0"
},
"devDependencies": {
"nanobundle": "^2.0.0",
"tsm": "^2.3.0",
"typescript": "^5.4.3",
"uvu": "^0.5.6",
"watchlist": "^0.3.1"
}
}
6 changes: 6 additions & 0 deletions packages/base10/src/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { makeBaseDecoder } from '@urlpack/base-codec';

import { baseAlphabet } from './util';

const defaultDecoder = makeBaseDecoder(baseAlphabet);
export const decode: (encoding: string) => Uint8Array = defaultDecoder.decode;
6 changes: 6 additions & 0 deletions packages/base10/src/encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { makeBaseEncoder } from '@urlpack/base-codec';

import { baseAlphabet } from './util';

const defaultEncoder = makeBaseEncoder(baseAlphabet);
export const encode: (binary: Uint8Array) => string = defaultEncoder.encode;
2 changes: 2 additions & 0 deletions packages/base10/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './encode';
export * from './decode';
1 change: 1 addition & 0 deletions packages/base10/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const baseAlphabet = '0123456789';
35 changes: 35 additions & 0 deletions packages/base10/tests/codec.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';

import { encode, decode } from '@urlpack/base10';

const textEncoder = new TextEncoder();

const cases: Array<[binary: Uint8Array, text: string]> = [
[
textEncoder.encode('안녕하세요!'),
'314474236304828881015048610782331442209',
],
[
textEncoder.encode(
'The quick brown fox jumps over the lazy dog.',
),
'3024830571690175283291907639196436031967763819210983988162282536502237781693262640684650930677706176554798',
],
[
new Uint8Array([0x00, 0x00, 0x28, 0x7f, 0xb4, 0xcd]),
'00679457997',
],
];

for (const [binary, text] of cases) {
test('base10.encode', () => {
assert.equal(encode(binary), text);
});

test('base10.decode', () => {
assert.equal(decode(text), binary);
});
}

test.run();
10 changes: 10 additions & 0 deletions packages/base10/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "lib"
},
"include": [
"src"
]
}
1 change: 1 addition & 0 deletions packages/qrjson/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib/
21 changes: 21 additions & 0 deletions packages/qrjson/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Danggeun Market Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions packages/qrjson/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# @urlpack/qrjson

[![Package Version](https://img.shields.io/npm/v/@urlpack/qrjson)](https://npm.im/@urlpack/qrjson)
[![License](https://img.shields.io/npm/l/@urlpack/qrjson)](#License)
[![Bundle Size](https://img.shields.io/bundlephobia/minzip/@urlpack/qrjson)](https://bundlephobia.com/package/@urlpack/qrjson)

Compress JSON data into compact & optimized to URL for QR Codes with numeric mode encoding

- ES Modules & Browser compatible
- Compact output using [MessagePack](https://msgpack.org/)
- Use Base10 encoding to get clear QR Code (mode=numeric) image

## Usage

```ts
import { makeQrJsonEncoder } from '@urlpack/qrjson';

const encoder = makeQrJsonEncoder();

encoder.encode({
href: 'http://daangn.com',
uid: 1234567,
context: {
foo: 'bar',
baz: [1, 2, 3, 4, 5],
},
});
// => '1605288693315933041592216384647639863862606035591552983841613971370651694842366403819686780144511394090067728031488880352135548504776147881624581'
```

## LICENSE

MIT
50 changes: 50 additions & 0 deletions packages/qrjson/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@urlpack/qrjson",
"version": "0.0.0",
"license": "MIT",
"homepage": "https://github.com/daangn/urlpack/tree/main/packages/qrjson",
"repository": {
"type": "git",
"url": "https://github.com/daangn/urlpack.git",
"directory": "packages/qrjson"
},
"source": "./src/index.ts",
"type": "module",
"main": "./src/index.ts",
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.mjs",
"require": "./lib/index.cjs"
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public",
"main": "./lib/index.mjs"
},
"files": [
"src",
"lib"
],
"scripts": {
"prepack": "yarn build",
"build": "nanobundle build",
"test": "uvu -r tsm",
"test:watch": "yarn test || true && watchlist src tests -- yarn test"
},
"dependencies": {
"@urlpack/base10": "workspace:^0.0.0",
"@urlpack/msgpack": "workspace:^2.0.0"
},
"devDependencies": {
"@urlpack/base-codec": "workspace:^2.0.0",
"nanobundle": "^2.0.0",
"tsm": "^2.3.0",
"typescript": "^5.4.3",
"uvu": "^0.5.6",
"watchlist": "^0.3.1"
}
}
12 changes: 12 additions & 0 deletions packages/qrjson/src/decoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { decode as decodeBase10 } from '@urlpack/base10';
import { makeMessagePackDecoder } from '@urlpack/msgpack';

export function makeQrJsonDecoder<Data>(): {
decode: (str: string) => Data,
} {
const decodeString = decodeBase10;
const decodeBinary = makeMessagePackDecoder().decode;
return {
decode: str => decodeBinary(decodeString(str)) as Data,
};
}
12 changes: 12 additions & 0 deletions packages/qrjson/src/encoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { encode as encodeToBase10 } from '@urlpack/base10';
import { makeMessagePackEncoder } from '@urlpack/msgpack';

export function makeQrJsonEncoder<Data>(): {
encode: (data: Data) => string,
} {
const encodeData = makeMessagePackEncoder().encode;
const encodeBinary = encodeToBase10;
return {
encode: data => encodeBinary(encodeData(data as any)),
};
}
2 changes: 2 additions & 0 deletions packages/qrjson/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './encoder';
export * from './decoder';
26 changes: 26 additions & 0 deletions packages/qrjson/tests/codec.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';

import { makeBaseEncoder, makeBaseDecoder } from '@urlpack/base-codec';
import { makeQrJsonEncoder, makeQrJsonDecoder } from '@urlpack/qrjson';

test('pack json', () => {
const data = {
href: 'http://daangn.com',
uid: 1234567,
context: {
foo: 'bar',
baz: [1, 2, 3, 4, 5],
},
};

const { encode } = makeQrJsonEncoder<typeof data>();
const { decode } = makeQrJsonDecoder<typeof data>();

const stored = encode(data);
console.log(stored);

assert.equal(decode(stored), data);
});

test.run();
10 changes: 10 additions & 0 deletions packages/qrjson/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "lib"
},
"include": [
"src"
]
}
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,19 @@ __metadata:
languageName: unknown
linkType: soft

"@urlpack/base10@workspace:^0.0.0, @urlpack/base10@workspace:packages/base10":
version: 0.0.0-use.local
resolution: "@urlpack/base10@workspace:packages/base10"
dependencies:
"@urlpack/base-codec": "workspace:^2.0.0"
nanobundle: "npm:^2.0.0"
tsm: "npm:^2.3.0"
typescript: "npm:^5.4.3"
uvu: "npm:^0.5.6"
watchlist: "npm:^0.3.1"
languageName: unknown
linkType: soft

"@urlpack/base58@workspace:^2.0.0, @urlpack/base58@workspace:packages/base58":
version: 0.0.0-use.local
resolution: "@urlpack/base58@workspace:packages/base58"
Expand Down Expand Up @@ -899,6 +912,21 @@ __metadata:
languageName: unknown
linkType: soft

"@urlpack/qrjson@workspace:packages/qrjson":
version: 0.0.0-use.local
resolution: "@urlpack/qrjson@workspace:packages/qrjson"
dependencies:
"@urlpack/base-codec": "workspace:^2.0.0"
"@urlpack/base10": "workspace:^0.0.0"
"@urlpack/msgpack": "workspace:^2.0.0"
nanobundle: "npm:^2.0.0"
tsm: "npm:^2.3.0"
typescript: "npm:^5.4.3"
uvu: "npm:^0.5.6"
watchlist: "npm:^0.3.1"
languageName: unknown
linkType: soft

"abbrev@npm:1":
version: 1.1.1
resolution: "abbrev@npm:1.1.1"
Expand Down

0 comments on commit f47c286

Please sign in to comment.