diff --git a/package-lock.json b/package-lock.json index 41282993..e7710da0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,6 @@ "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "lint-staged": "^15.2.2", - "nanoid": "^5.0.6", "prettier": "^3.2.5", "simple-git-hooks": "^2.10.0", "ts-jest": "^29.1.2", @@ -6317,18 +6316,6 @@ "multicast-dns": "cli.js" } }, - "node_modules/nanoid": { - "version": "5.0.6", - "resolved": "https://registry.npmmirror.com/nanoid/-/nanoid-5.0.6.tgz", - "integrity": "sha512-rRq0eMHoGZxlvaFOUdK1Ev83Bd1IgzzR+WJ3IbDJ7QOSdAxYjlurSPqFs9s4lJg29RT6nPwizFtJhQS6V5xgiA==", - "dev": true, - "bin": { - "nanoid": "bin/nanoid.js" - }, - "engines": { - "node": "^18 || >=20" - } - }, "node_modules/natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmmirror.com/natural-compare/-/natural-compare-1.4.0.tgz", @@ -14136,12 +14123,6 @@ "thunky": "^1.0.2" } }, - "nanoid": { - "version": "5.0.6", - "resolved": "https://registry.npmmirror.com/nanoid/-/nanoid-5.0.6.tgz", - "integrity": "sha512-rRq0eMHoGZxlvaFOUdK1Ev83Bd1IgzzR+WJ3IbDJ7QOSdAxYjlurSPqFs9s4lJg29RT6nPwizFtJhQS6V5xgiA==", - "dev": true - }, "natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmmirror.com/natural-compare/-/natural-compare-1.4.0.tgz", diff --git a/package.json b/package.json index 91640a64..8a9590ce 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "lint-staged": "^15.2.2", - "nanoid": "^5.0.6", "prettier": "^3.2.5", "simple-git-hooks": "^2.10.0", "ts-jest": "^29.1.2", diff --git a/packages/chili-core/src/foundation/id.ts b/packages/chili-core/src/foundation/id.ts index fa5e8e72..e1f119db 100644 --- a/packages/chili-core/src/foundation/id.ts +++ b/packages/chili-core/src/foundation/id.ts @@ -1,9 +1,24 @@ // Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license. -import { nanoid } from "nanoid"; +// Copy from nanoid: https://github.com/ai/nanoid/blob/main/non-secure/index.js + +// This alphabet uses `A-Za-z0-9_-` symbols. +// The order of characters is optimized for better gzip and brotli compression. +// References to the same file (works both for gzip and brotli): +// `'use`, `andom`, and `rict'` +// References to the brotli default dictionary: +// `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf` +let alphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"; export class Id { - static new() { - return nanoid(); + static generate(size = 21) { + let id = ""; + // A compact alternative for `for (var i = 0; i < step; i++)`. + let i = size; + while (i--) { + // `| 0` is more compact and faster than `Math.floor()`. + id += alphabet[(Math.random() * 64) | 0]; + } + return id; } } diff --git a/packages/chili-core/src/model/model.ts b/packages/chili-core/src/model/model.ts index 9e5f0c7f..925ea79f 100644 --- a/packages/chili-core/src/model/model.ts +++ b/packages/chili-core/src/model/model.ts @@ -66,7 +66,7 @@ export abstract class Model extends Node implements I this.setProperty("opacity", value); } - constructor(document: IDocument, name: string, body: Body, id: string = Id.new()) { + constructor(document: IDocument, name: string, body: Body, id: string = Id.generate()) { super(document, name, id); this.body = body; } @@ -81,7 +81,7 @@ export class GeometryModel extends Model { return this._error; } - constructor(document: IDocument, name: string, body: Body, id: string = Id.new()) { + constructor(document: IDocument, name: string, body: Body, id: string = Id.generate()) { super(document, name, body, id); this.drawShape(); body.onShapeChanged(this.onShapeChanged); diff --git a/packages/chili-core/src/model/node.ts b/packages/chili-core/src/model/node.ts index e26da7f4..7575b12f 100644 --- a/packages/chili-core/src/model/node.ts +++ b/packages/chili-core/src/model/node.ts @@ -73,7 +73,7 @@ export abstract class Node extends HistoryObservable implements INode { constructor( document: IDocument, private _name: string, - id: string = Id.new(), + id: string = Id.generate(), ) { super(document); this.id = id; @@ -110,7 +110,7 @@ export abstract class Node extends HistoryObservable implements INode { clone(): this { let serialized = Serializer.serializeObject(this); - serialized.properties["id"] = Id.new(); + serialized.properties["id"] = Id.generate(); serialized.properties["name"] = `${this._name}_copy`; let cloned: this = Serializer.deserializeObject(this.document, serialized); this.parent?.add(cloned); diff --git a/packages/chili-core/src/model/nodeLinkedList.ts b/packages/chili-core/src/model/nodeLinkedList.ts index 6c349218..32600658 100644 --- a/packages/chili-core/src/model/nodeLinkedList.ts +++ b/packages/chili-core/src/model/nodeLinkedList.ts @@ -27,7 +27,7 @@ export class NodeLinkedList extends Node implements INodeLinkedList { return this._lastChild; } - constructor(document: IDocument, name: string, id: string = Id.new()) { + constructor(document: IDocument, name: string, id: string = Id.generate()) { super(document, name, id); } diff --git a/packages/chili-occ/src/occHelps.ts b/packages/chili-occ/src/occHelps.ts index 26740aed..a6274c3f 100644 --- a/packages/chili-occ/src/occHelps.ts +++ b/packages/chili-occ/src/occHelps.ts @@ -185,7 +185,7 @@ export class OccHelps { } } - static wrapShape(shape: TopoDS_Shape, id: string = Id.new()): IShape { + static wrapShape(shape: TopoDS_Shape, id: string = Id.generate()): IShape { switch (shape.ShapeType()) { case occ.TopAbs_ShapeEnum.TopAbs_COMPOUND: return new OccCompound(occ.TopoDS.Compound_1(shape), id); diff --git a/packages/chili-occ/src/occShape.ts b/packages/chili-occ/src/occShape.ts index c7882729..ccfb6d4c 100644 --- a/packages/chili-occ/src/occShape.ts +++ b/packages/chili-occ/src/occShape.ts @@ -62,7 +62,7 @@ export class OccShape implements IShape { } constructor(shape: TopoDS_Shape, id?: string) { - this._id = id ?? Id.new(); + this._id = id ?? Id.generate(); this._shape = shape; this.shapeType = OccHelps.getShapeType(shape); } diff --git a/packages/chili-occ/test/occ.test.ts b/packages/chili-occ/test/occ.test.ts index f37bd911..b5b46ec2 100644 --- a/packages/chili-occ/test/occ.test.ts +++ b/packages/chili-occ/test/occ.test.ts @@ -1,17 +1,13 @@ // Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license. -import { expect, jest, test } from "@jest/globals"; -import { CurveType, Id, Matrix4, Ray, ShapeType, XYZ } from "chili-core"; +import { expect, test } from "@jest/globals"; +import { CurveType, Matrix4, Ray, ShapeType, XYZ } from "chili-core"; import initOpenCascade, { OpenCascadeInstance } from "opencascade.js/dist/node.js"; import { TopAbs_ShapeEnum, TopoDS_Edge } from "../occ-wasm/chili_occ"; import { OccCurve } from "../src/occGeometry"; import { OccHelps } from "../src/occHelps"; import { OccEdge, OccSolid } from "../src/occShape"; -const newId = jest.spyOn(Id, "new").mockImplementation(() => { - return "asfas"; -}); - let occ: OpenCascadeInstance; beforeAll(async () => { diff --git a/packages/chili/src/document.ts b/packages/chili/src/document.ts index eb161213..552a2cbe 100644 --- a/packages/chili/src/document.ts +++ b/packages/chili/src/document.ts @@ -68,7 +68,7 @@ export class Document extends Observable implements IDocument { constructor( readonly application: IApplication, name: string, - readonly id: string = Id.new(), + readonly id: string = Id.generate(), ) { super(); this._name = name;