Skip to content

Commit

Permalink
Merge pull request #6 from cto-af/export-js
Browse files Browse the repository at this point in the history
Generate JS module from builder
  • Loading branch information
hildjj authored Jun 14, 2023
2 parents b16eeac + 333760f commit d846c93
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 7 deletions.
55 changes: 55 additions & 0 deletions builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1164,4 +1164,59 @@ export class UnicodeTrieBuilder {

return buf;
}

/**
* @typedef {object} ModuleOptions
* @prop {string=} [version] Version of the source file, usually the Unicode
* version.
* @prop {string=} [date] Date the source file was created. Can be parsed
* from most UCD files.
* @prop {string} [name="Trie"] Name exported from the module with the Trie
* instance.
* @prop {string} [quot='"'] Quote. Should be single or double.
* @prop {string} [semi=";"] Include semicolons? Pass in "" to disable.
* @prop {string} [pkg="@cto.af/unicode-trie"] Package name for this
* package. Mostly useful for internal tooling.
*/

/**
* Create a string version of a JS module that will reconstitute this trie.
* Suitable for saving to a .mjs file.
*
* @param {ModuleOptions} [opts={}]
* @returns {string}
*/
toModule(opts = {}) {
const { name, quot: q, semi: s, version, date, pkg } = {
name: "Trie",
quot: '"',
semi: ";",
pkg: "@cto.af/unicode-trie",
...opts,
};
const buf = this.toBuffer();
let ret = `import { Buffer } from ${q}buffer${q}${s}\n`;
ret += `import { UnicodeTrie } from ${q}${pkg}${q}${s}\n\n`;
if (version) {
ret += `export const version = ${q}${version}${q}${s}\n`;
}
if (date) {
ret += `export const inputFileDate = new Date(${q}${new Date(date).toISOString()}${q})${s}\n`;
}
ret += `\
export const generatedDate = new Date(${q}${new Date().toISOString()}${q})${s}
export const ${name} = new UnicodeTrie(Buffer.from(
\`${buf.toString("base64").split(/(.{72})/).filter(x => x).join("\n ")}\`,
${q}base64${q}
))${s}
/**
* @type {Record<string, number>}
*/
export const names = Object.fromEntries(
${name}.values.map((v, i) => [v, i])
)${s}
export const values = ${name}.values${s}
`;
return ret;
}
}
7 changes: 1 addition & 6 deletions examples/genLineBreak.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ for (const line of txt.split("\n")) {
}
}

const buf = t.toBuffer();

await fs.writeFile("lineBreak.js", `\
import { UnicodeTrie } from "../index.js";
export const LineBreak = new UnicodeTrie(Buffer.from("${buf.toString("base64")}", "base64"));
`);
await fs.writeFile("lineBreak.js", t.toModule({ name: "LineBreak", pkg: "../index.js" }));

console.log(`Ranges: ${ranges}\nSingle: ${single}\nTotal: ${total}`);
3 changes: 2 additions & 1 deletion examples/getLineBreak.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
import { LineBreak } from "./lineBreak.js";

for (const num of process.argv.slice(2)) {
console.log(LineBreak.get(parseInt(num, 16)));
const n = parseInt(num, 16);
console.log(`U+${n.toString(16).padStart(4, "0")}:`, LineBreak.getString(n));
}
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,12 @@ describe("unicode trie", () => {
}));
}
});

it("generates a module", () => {
const trie = new UnicodeTrieBuilder(0, 99);
let m = trie.toModule();
assert.match(m, /export const Trie/);
m = trie.toModule({ version: "1.0.0", date: 1, name: "Foo", quot: "'", semi: "" });
assert.match(m, /export const Foo/);
});
});
50 changes: 50 additions & 0 deletions types/builder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,56 @@ export class UnicodeTrieBuilder {
* @returns {Buffer}
*/
toBuffer(): Buffer;
/**
* @typedef {object} ModuleOptions
* @prop {string=} [version] Version of the source file, usually the Unicode
* version.
* @prop {string=} [date] Date the source file was created. Can be parsed
* from most UCD files.
* @prop {string} [name="Trie"] Name exported from the module with the Trie
* instance.
* @prop {string} [quot='"'] Quote. Should be single or double.
* @prop {string} [semi=";"] Include semicolons? Pass in "" to disable.
* @prop {string} [pkg="@cto.af/unicode-trie"] Package name for this
* package. Mostly useful for internal tooling.
*/
/**
* Create a string version of a JS module that will reconstitute this trie.
* Suitable for saving to a .mjs file.
*
* @param {ModuleOptions} [opts={}]
* @returns {string}
*/
toModule(opts?: {
/**
* Version of the source file, usually the Unicode
* version.
*/
version?: string | undefined;
/**
* Date the source file was created. Can be parsed
* from most UCD files.
*/
date?: string | undefined;
/**
* Name exported from the module with the Trie
* instance.
*/
name?: string | undefined;
/**
* Quote. Should be single or double.
*/
quot?: string | undefined;
/**
* Include semicolons? Pass in "" to disable.
*/
semi?: string | undefined;
/**
* Package name for this
* package. Mostly useful for internal tooling.
*/
pkg?: string | undefined;
} | undefined): string;
#private;
}
import { UnicodeTrie } from "./index.js";

0 comments on commit d846c93

Please sign in to comment.