Skip to content

Commit

Permalink
Add one nontrivial test
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreedcmu committed Mar 20, 2024
1 parent 6f2a806 commit 3fd5e3c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"devDependencies": {
"@codemirror/language": "^6.10.1",
"@lezer/highlight": "^1.2.0",
"typescript": "^5.3.3",
"@types/jest": "^29.2.3",
"@types/jest": "^29.5.12",
"jest": "^29.3.1",
"ts-jest": "^29.0.3"
"ts-jest": "^29.0.3",
"typescript": "^5.3.3"
}
}
14 changes: 9 additions & 5 deletions src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ async function decompressedOf(bytes: Uint8Array): Promise<Uint8Array> {
return await bytesOfStream(streamOfBytes(bytes).pipeThrough(new DecompressionStream('gzip')));
}

export async function encode(text: string): Promise<string> {
// // v1:
// return encodeURIComponent(btoa(text));
export function encodeWithV1(text: string): string {
return encodeURIComponent(btoa(text));
}

// v2:
export async function encodeWithV2(text: string): Promise<string> {
const x1 = bytesOfString(text);
const encoded = encodeURIComponent(base64OfBytes(await compressedOf(bytesOfString(text))).str);
return 'v2/' + encoded;
}

export async function decode(fragment: string) {
export async function encode(text: string): Promise<string> {
return encodeWithV2(text);
}

export async function decode(fragment: string): Promise<string> {
const uridecoded = decodeURIComponent(fragment);
if (uridecoded.match(/^v2\//)) {
const stripPrefix = uridecoded.replace(/v2\//, '');
Expand Down
21 changes: 21 additions & 0 deletions tests/test-encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { decode, encodeWithV1, encodeWithV2 } from "../src/encoding";


const exampleTwelf = `
a: type. c: a.
%% some comment text with some punctuation: # $ % ^ &
`;

describe('url encoding', () => {
test('should roundtrip with v1', async () => {
expect(await decode(encodeWithV1(exampleTwelf))).toEqual(exampleTwelf);
});

// I'd like to test v2 in nodejs, but I need to figure out a strategy
// for polyfilling Blob and CompressionStream

// test('should roundtrip with v2', async () => {
// expect(await decode(await encodeWithV2(exampleTwelf))).toEqual(exampleTwelf);
// });

});
5 changes: 0 additions & 5 deletions tests/test-vacuous.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": ["./typings"], /* Specify multiple folders that act like './node_modules/@types'. */
// "typeRoots": ["./typings"], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": ['ace'], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
Expand Down

0 comments on commit 3fd5e3c

Please sign in to comment.