Skip to content

Commit

Permalink
✨ feat: remove nanoid
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Mar 11, 2024
1 parent 3f91102 commit 0633691
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 37 deletions.
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 18 additions & 3 deletions packages/chili-core/src/foundation/id.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions packages/chili-core/src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export abstract class Model<T extends IShape = IShape> 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;
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/chili-core/src/model/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-core/src/model/nodeLinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/chili-occ/src/occHelps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-occ/src/occShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 2 additions & 6 deletions packages/chili-occ/test/occ.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/chili/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0633691

Please sign in to comment.