-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a74b6c9
commit 1f75c87
Showing
15 changed files
with
184 additions
and
17 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
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,87 @@ | ||
import type { ParsedMessage } from '@eartharoid/i18n/types'; | ||
import { encode as encodeVarInt } from 'varint'; | ||
|
||
// const version = 1; | ||
|
||
// const textEncoder = new TextEncoder(); | ||
// const encodeText = (text: string) => textEncoder.encode(text); // ? encodeInto | ||
|
||
// export default function* compile(messages: Iterable<[string, ParsedMessage]>): Iterable<number> { | ||
// let prefix_parts = []; | ||
|
||
// yield version; | ||
|
||
// for (const [k, v] of messages) { | ||
// const key_bytes = encodeText(k); | ||
// yield* encodeVarInt(key_bytes.byteLength); | ||
// yield* key_bytes; | ||
// } | ||
// } | ||
|
||
// compile.toBuffer = function (messages: Iterable<[string, ParsedMessage]>) { | ||
// return new Uint8Array(this(messages)); | ||
// }; | ||
|
||
// interface Compiler { | ||
// messages: Iterable<[string, ParsedMessage]>; | ||
// compile(messages: Iterable<[string, ParsedMessage]>): this; | ||
// toBuffer(messages: Iterable<[string, ParsedMessage]>): Uint8Array; | ||
// [Symbol.iterator](): Iterable<number>; | ||
// } | ||
|
||
// const compiler: Compiler = { | ||
// messages: [], | ||
// compile(messages: Iterable<[string, ParsedMessage]>) { | ||
// this.messages = messages; | ||
// return this; | ||
// }, | ||
// toBuffer(): Uint8Array { | ||
// return new Uint8Array(this); | ||
// }, | ||
// // [Symbol.iterator]: function* () {}, | ||
// *[Symbol.iterator](): Iterable<number> { | ||
// const prefix_parts = []; | ||
|
||
// yield version; | ||
|
||
// for (const [k, v] of this.messages) { | ||
// const key_bytes = encodeText(k); | ||
// yield* encodeVarInt(key_bytes.byteLength); | ||
// yield* key_bytes; | ||
// } | ||
// }, | ||
// }; | ||
|
||
type ParsedMessages = Iterable<[string, ParsedMessage]>; | ||
|
||
export default class Compiler { | ||
#messages: ParsedMessages; | ||
#textEncoder = new TextEncoder(); | ||
|
||
public version = 1; | ||
|
||
constructor(messages: ParsedMessages) { | ||
this.#messages = messages; | ||
return this; | ||
} | ||
|
||
#encodeText(text: string): Uint8Array { | ||
return this.#textEncoder.encode(text); | ||
} | ||
|
||
public toBuffer(): Uint8Array { | ||
return new Uint8Array(this as unknown as Iterable<number>); | ||
} | ||
|
||
public *[Symbol.iterator](): Iterable<number> { | ||
// const prefix_parts = []; | ||
|
||
yield this.version; | ||
|
||
for (const [k] of this.#messages) { | ||
const key_bytes = this.#encodeText(k); | ||
yield* encodeVarInt(key_bytes.byteLength); | ||
yield* key_bytes; | ||
} | ||
} | ||
} |
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,7 @@ | ||
import type { ParsedMessage } from '@eartharoid/i18n/types'; | ||
|
||
export default function* fromBuffer(): Iterable<[string, ParsedMessage]> { | ||
// ! ReadableStream.from() is not widely supported -- https://www.npmjs.com/package/@sec-ant/readable-stream | ||
|
||
yield; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import type { ParsedMessage } from '@eartharoid/i18n/types'; | ||
|
||
export default function* fromStream(): Generator<ParsedMessage> { | ||
export default function* fromStream(): Iterable<[string, ParsedMessage]> { | ||
// ! ReadableStream.@@asyncIterator (check support) -- https://www.npmjs.com/package/@sec-ant/readable-stream | ||
// ! try ReadableStreamBYOBReader else default | ||
|
||
yield; | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { Compiler } from '../dist/index.js'; | ||
|
||
|
||
// const bytes = compile([[1, 2], [2, 3]]); | ||
|
||
// for (const byte of bytes) { | ||
// console.log(byte); | ||
// } | ||
|
||
// const buffer = compile.toBuffer([[1, 2], [2, 3]]); | ||
// console.log(buffer); | ||
|
||
|
||
|
||
|
||
// const bytes = compile.compile([[1, 2], [2, 3]]); | ||
|
||
// for (const byte of bytes) { | ||
// console.log(byte); | ||
// } | ||
|
||
// const buffer = bytes.toBuffer(); | ||
// console.log(buffer); | ||
|
||
|
||
const bytes = new Compiler([[1, 'A'], [2, 'B']]); | ||
|
||
for (const byte of bytes) { | ||
console.log(byte); | ||
} | ||
|
||
const buffer = bytes.toBuffer(); | ||
console.log(buffer); |
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
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 @@ | ||
import type { ParsedMessage } from '@eartharoid/i18n/types'; | ||
type ParsedMessages = Iterable<[string, ParsedMessage]>; | ||
export default class Compiler { | ||
#private; | ||
version: number; | ||
constructor(messages: ParsedMessages); | ||
toBuffer(): Uint8Array; | ||
[Symbol.iterator](): Iterable<number>; | ||
} | ||
export {}; |
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,9 @@ | ||
import type { ParsedMessage } from '@eartharoid/i18n/types'; | ||
interface Compiler { | ||
messages: Iterable<[string, ParsedMessage]>; | ||
compile(messages: Iterable<[string, ParsedMessage]>): this; | ||
toBuffer(messages: Iterable<[string, ParsedMessage]>): Uint8Array; | ||
[Symbol.iterator](): Iterable<number>; | ||
} | ||
declare const compiler: Compiler; | ||
export default compiler; |
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 @@ | ||
import type { ParsedMessage } from '@eartharoid/i18n/types'; | ||
export default function fromBuffer(): Iterable<[string, ParsedMessage]>; |
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 @@ | ||
import type { ParsedMessage } from '@eartharoid/i18n/types'; | ||
export default function fromStream(): Iterable<[string, ParsedMessage]>; |
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 { default as fromStream } from './fromStream.js'; | ||
export { default as Compiler } from './Compiler.js'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.