• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { int32 } from "@koalaui/compat"
17import { createSha1 } from "./sha1";
18
19export class UniqueId {
20    private sha = createSha1()
21
22    public addString(data: string): UniqueId {
23        this.sha.updateString(data)
24        return this
25    }
26
27    public addI32(data: int32): UniqueId {
28        this.sha.updateInt32(data)
29        return this
30    }
31
32    public addF32Array(data: Float32Array): UniqueId {
33        this.sha.update(data)
34        return this
35    }
36
37    public addI32Array(data: Int32Array): UniqueId {
38        this.sha.update(data)
39        return this
40    }
41
42    public addU32Array(data: Uint32Array): UniqueId {
43        this.sha.update(data)
44        return this
45    }
46
47    public addU8Array(data: Uint8Array): UniqueId {
48        this.sha.update(data)
49        return this
50    }
51
52    public addPtr(data: Uint32Array | number): UniqueId {
53        if (data instanceof Uint32Array) {
54            return this.addU32Array(data)
55        }
56        return this.addI32(data as int32)
57    }
58
59    public compute(): string {
60        return this.sha.digest("hex") as string
61    }
62}
63