Skip to content

Commit

Permalink
#61, starting StringStringMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Mar 22, 2024
1 parent 921edc1 commit 3a93c1c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
44 changes: 42 additions & 2 deletions use-cases/build/StringStringMap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
export default function buildStringStringMap(): Promise<void>
import path from "path";
import {
distDir,
project,
removeDistFile,
//getExistingSourceFile,
} from "./sharedProject.js";
import { SourceFile } from "ts-morph";

export default async function buildStringStringMap(): Promise<void>
{
console.log("Hello World!");
await removeDistFile("StringStringMap.ts");

/* This is a starting point, based on our needs:
- hashing two keys into one
- retrieving two keys from one
- storing the hashed keys and values in a private map
*/
const moduleFile: SourceFile = project.createSourceFile(
path.join(distDir, "StringStringMap.ts"),
`
interface StringStringKey {
readonly firstKey: string,
readonly secondKey: string
}
export default class StringStringMap<V> {
static #hashKeys(firstKey: string, secondKey: string): string {
return JSON.stringify({firstKey, secondKey});
}
static #parseKeys(hashedKey: string): [string, string]
{
const { firstKey, secondKey } = JSON.parse(hashedKey) as StringStringKey;
return [firstKey, secondKey];
}
readonly #hashMap = new Map<string, V>;
}
`.trim()
);

await moduleFile.save();
return Promise.resolve();
}
48 changes: 48 additions & 0 deletions use-cases/build/sharedProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fs from "fs/promises";
import path from "path";
import url from "url";

import {
ModuleKind,
ModuleResolutionKind,
Project,
type ProjectOptions,
ScriptTarget,
SourceFile,
} from "ts-morph";

const TSC_CONFIG: ProjectOptions = {
"compilerOptions": {
"lib": ["es2022"],
"module": ModuleKind.ESNext,
"target": ScriptTarget.ESNext,
"moduleResolution": ModuleResolutionKind.NodeNext,
"sourceMap": true,
"declaration": true,
},
skipAddingFilesFromTsConfig: true,
skipFileDependencyResolution: true,
};

export const stageDir = path.normalize(path.join(url.fileURLToPath(import.meta.url), "../../"));
export const distDir = path.join(stageDir, "dist");

export const project = new Project(TSC_CONFIG);

export function getFullPath(pathToLocalFile: string): string {
return path.join(stageDir, "dist", pathToLocalFile);
}

export async function removeDistFile(
pathToLocalFile: string
): Promise<void>
{
await fs.rm(getFullPath(pathToLocalFile), { force: true, recursive: true });
}

export function getExistingSourceFile(
absolutePathToFile: string
): SourceFile
{
return project.getSourceFile(absolutePathToFile) ?? project.addSourceFileAtPath(absolutePathToFile);
}
18 changes: 18 additions & 0 deletions use-cases/dist/StringStringMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interface StringStringKey {
readonly firstKey: string,
readonly secondKey: string
}

export default class StringStringMap<V> {
static #hashKeys(firstKey: string, secondKey: string): string {
return JSON.stringify({firstKey, secondKey});
}

static #parseKeys(hashedKey: string): [string, string]
{
const { firstKey, secondKey } = JSON.parse(hashedKey) as StringStringKey;
return [firstKey, secondKey];
}

readonly #hashMap = new Map<string, V>;
}

0 comments on commit 3a93c1c

Please sign in to comment.