Skip to content

Commit

Permalink
Merge pull request #3086 from opral/mesdk-197-remove-generatestablebu…
Browse files Browse the repository at this point in the history
…ndleid

refactor: `bundleId` -> `humandId`
  • Loading branch information
samuelstroschein authored Aug 30, 2024
2 parents bb0792c + 70cd7c9 commit 79053af
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 18 deletions.
6 changes: 3 additions & 3 deletions inlang/source-code/sdk2/src/database/initDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CamelCasePlugin, Kysely, ParseJSONResultsPlugin } from "kysely";
import type { InlangDatabaseSchema } from "./schema.js";
import { createDialect, type SqliteDatabase } from "sqlite-wasm-kysely";
import { v4 } from "uuid";
import { generateBundleId } from "../bundle-id/bundle-id.js";
import { humanId } from "../human-id/human-id.js";
import { SerializeJsonPlugin } from "./serializeJsonPlugin.js";

export function initDb(args: { sqlite: SqliteDatabase }) {
Expand All @@ -27,8 +27,8 @@ function initDefaultValueFunctions(args: { sqlite: SqliteDatabase }) {
xFunc: () => v4(),
});
args.sqlite.createFunction({
name: "bundle_id",
name: "human_id",
arity: 0,
xFunc: () => generateBundleId(),
xFunc: () => humanId(),
});
}
4 changes: 2 additions & 2 deletions inlang/source-code/sdk2/src/database/initDbAndSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createInMemoryDatabase } from "sqlite-wasm-kysely";
import { test, expect } from "vitest";
import { initDb } from "./initDb.js";
import { isBundleId } from "../bundle-id/bundle-id.js";
import { isHumanId } from "../human-id/human-id.js";
import { validate as isUuid } from "uuid";
import { createSchema } from "./schema.js";

Expand All @@ -18,7 +18,7 @@ test("bundle default values", async () => {
.returningAll()
.executeTakeFirstOrThrow();

expect(isBundleId(bundle.id)).toBe(true);
expect(isHumanId(bundle.id)).toBe(true);
expect(bundle.alias).toStrictEqual({});
});

Expand Down
2 changes: 1 addition & 1 deletion inlang/source-code/sdk2/src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function createSchema(args: { sqlite: SqliteDatabase }) {
PRAGMA foreign_keys = ON;
CREATE TABLE bundle (
id TEXT PRIMARY KEY DEFAULT (bundle_id()),
id TEXT PRIMARY KEY DEFAULT (human_id()),
alias TEXT NOT NULL DEFAULT '{}'
) strict;
Expand Down
4 changes: 2 additions & 2 deletions inlang/source-code/sdk2/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { v4 as uuid } from "uuid";
import type { ProjectSettings } from "./json-schema/settings.js";
import { generateBundleId } from "./bundle-id/bundle-id.js";
import { humanId } from "./human-id/human-id.js";
import type {
Bundle,
MessageNested,
Expand All @@ -26,7 +26,7 @@ export function createBundle(args: {
alias?: Bundle["alias"];
}): NewBundleNested {
return {
id: args.id ?? generateBundleId(),
id: args.id ?? humanId(),
alias: args.alias ?? {},
messages: args.messages,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import murmurhash3 from "murmurhash3js";
import { adjectives, animals, verbs, adverbs } from "./words.js";

export function generateBundleId() {
export function humanId() {
return `${adjectives[Math.floor(Math.random() * 256)]}_${
adjectives[Math.floor(Math.random() * 256)]
}_${animals[Math.floor(Math.random() * 256)]}_${
verbs[Math.floor(Math.random() * 256)]
}`;
}

export function isBundleId(id: string): boolean {
export function isHumanId(id: string): boolean {
// naive implementation (good enough for now)
return id.split("_").length === 4;
}
Expand All @@ -19,10 +19,10 @@ export function isBundleId(id: string): boolean {
* The function generated a stable bundle id based on the input value.
*
* @example
* generateStableBundleId("login-button-header") // => "..."
* stableHumanId("login-button-header") // => "..."
*
*/
export function generateStableBundleId(value: string, offset: number = 0) {
export function stableHumanId(value: string, offset: number = 0) {
// Seed value can be any arbitrary value
const seed = 42;

Expand Down
7 changes: 3 additions & 4 deletions inlang/source-code/sdk2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ export * from "./mock/index.js";
export * from "./helper.js";
export * from "./query-utilities/index.js";
export {
generateBundleId,
humanId,
/**
* @deprecated use newBundleId instead
* @deprecated use `humandId()` instead (it's a rename)
*/
generateBundleId as randomHumanId,
} from "./bundle-id/bundle-id.js";
} from "./human-id/human-id.js";
export type { InlangDatabaseSchema } from "./database/schema.js";
export type { ResourceFile } from "./project/api.js";
export type { InlangPlugin } from "./plugin/schema.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { generateStableBundleId } from "../../bundle-id/bundle-id.js";
import type {
BundleNested,
MessageNested,
Variant,
} from "../../database/schema.js";
import { stableHumanId } from "../../human-id/human-id.js";
import type { InlangPlugin } from "../../plugin/schema.js";
import type { Declaration, Expression, Pattern } from "../pattern.js";
import type { MessageV1, PatternV1 } from "./schemaV1.js";
Expand All @@ -17,7 +17,7 @@ export function fromMessageV1(
messageV1: MessageV1,
pluginKey: NonNullable<InlangPlugin["key"] | InlangPlugin["id"]>
): BundleNested {
const bundleId = generateStableBundleId(messageV1.id);
const bundleId = stableHumanId(messageV1.id);

const languages = [
...new Set(messageV1.variants.map((variant) => variant.languageTag)),
Expand Down

0 comments on commit 79053af

Please sign in to comment.