-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new packages: codecs for QR Codes URL
- Loading branch information
Showing
21 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './encode'; | ||
export * from './decode'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const baseAlphabet = '0123456789'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"declarationDir": "lib" | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# @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 | ||
|
||
- ES Modules & Browser compatible | ||
- Compact output using [MessagePack](https://msgpack.org/) | ||
- Use Base10 encoding to get clear QR Code 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], | ||
}, | ||
}); | ||
// => 'QL3sGqgSwhebCV6jsPsxSCG6DPGZUAo7qtLbEFxFN3bequ3qABcg6pxvpvr36FveMxCtD4zNSWSpHmxgz8' | ||
// | ||
// Only 82 characters, 35% smaller output than JSON.stringify + lz-string | ||
``` | ||
|
||
## LICENSE | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './encoder'; | ||
export * from './decoder'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"declarationDir": "lib" | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters