@rbxts/hash 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,58 @@
1
- # Package template
1
+ # @rbxts/hash
2
2
 
3
- [![CI Status](https://github.com/R-unic/package-template/actions/workflows/test.yml/badge.svg)](https://github.com/R-unic/package-template/actions/workflows)
4
- [![Coverage Status](https://coveralls.io/repos/github/R-unic/package-template/badge.svg?branch=master)](https://coveralls.io/github/R-unic/package-template)
3
+ [![CI Status](https://github.com/R-unic/hash/actions/workflows/test.yml/badge.svg)](https://github.com/R-unic/hash/actions/workflows)
4
+ [![Coverage Status](https://coveralls.io/repos/github/R-unic/hash/badge.svg?branch=master)](https://coveralls.io/github/R-unic/hash)
5
5
 
6
- Package template for roblox-ts with rUnit testing, coverage tracking, and file structure already set up
6
+ Utility for computing and combining hash codes.
7
+
8
+ ### Example
9
+
10
+ ```ts
11
+ import { Hash } from "@rbxts/hash";
12
+
13
+
14
+ class User {
15
+ public constructor(
16
+ public readonly name: string,
17
+ public readonly age: number
18
+ ) { }
19
+
20
+ public hashCode(): Hash {
21
+ return Hash.combine(this.name, this.age);
22
+ }
23
+ }
24
+
25
+ const bob = new User("Bob", 20);
26
+ const alice = new User("Alice", 21);
27
+ print(bob.hashCode() === bob.hashCode()); // true
28
+ print(bob.hashCode() === alice.hashCode()); // false
29
+ ```
30
+
31
+ ### Incremental combining
32
+
33
+ ```ts
34
+ import { Hash } from "@rbxts/hash";
35
+
36
+
37
+ class Employee {
38
+ public constructor(
39
+ public readonly name: string,
40
+ public readonly reviews: string[]
41
+ ) { }
42
+
43
+ public hashCode(): Hash {
44
+ const hash = Hash.incremental();
45
+ hash.add(name);
46
+ hash.add(reviews.size());
47
+ for (const review of reviews)
48
+ hash.add(review)
49
+
50
+ return hash.toHash();
51
+ }
52
+ }
53
+
54
+ const bob = new Employee("Bob", ["great dude"]);
55
+ const alice = new User("Alice", ["polite woman"]);
56
+ print(bob.hashCode() === bob.hashCode()); // true
57
+ print(bob.hashCode() === alice.hashCode()); // false
58
+ ```
package/out/index.d.ts CHANGED
@@ -4,6 +4,14 @@ export interface Hashable {
4
4
  }
5
5
  export type HashableType = number | string | boolean | Hashable | undefined;
6
6
  export declare namespace Hash {
7
- function of(hashable: HashableType): Hash;
8
- function combine(...hashables: HashableType[]): Hash;
7
+ class IncrementalHash {
8
+ private hash;
9
+ private count;
10
+ add(value: HashableType): this;
11
+ toHash(): Hash;
12
+ }
13
+ export function incremental(): IncrementalHash;
14
+ export function of(hashable: HashableType): Hash;
15
+ export function combine(...hashables: HashableType[]): Hash;
16
+ export {};
9
17
  }
package/out/init.luau CHANGED
@@ -15,8 +15,38 @@ local MAX32 = 0xFFFFFFFF
15
15
  local Hash = {}
16
16
  do
17
17
  local _container = Hash
18
+ local queueRound, of, mixFinal, mul32
19
+ local IncrementalHash
20
+ do
21
+ IncrementalHash = setmetatable({}, {
22
+ __tostring = function()
23
+ return "IncrementalHash"
24
+ end,
25
+ })
26
+ IncrementalHash.__index = IncrementalHash
27
+ function IncrementalHash.new(...)
28
+ local self = setmetatable({}, IncrementalHash)
29
+ return self:constructor(...) or self
30
+ end
31
+ function IncrementalHash:constructor()
32
+ self.hash = PRIME5
33
+ self.count = 0
34
+ end
35
+ function IncrementalHash:add(value)
36
+ self.hash = queueRound(self.hash, of(value))
37
+ self.count += 1
38
+ return self
39
+ end
40
+ function IncrementalHash:toHash()
41
+ return mixFinal(band(self.hash + mul32(self.count, 4), MAX32))
42
+ end
43
+ end
44
+ local function incremental()
45
+ return IncrementalHash.new()
46
+ end
47
+ _container.incremental = incremental
18
48
  local hashString
19
- local function of(hashable)
49
+ function of(hashable)
20
50
  local _hashable = hashable
21
51
  local _exp = typeof(_hashable)
22
52
  repeat
@@ -36,7 +66,7 @@ do
36
66
  until true
37
67
  end
38
68
  _container.of = of
39
- local round, mixState, mul32, queueRound, mixFinal
69
+ local round, mixState
40
70
  local function combine(...)
41
71
  local hashables = { ... }
42
72
  local length = #hashables
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rbxts/hash",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A utility for generating hash codes for objects",
5
5
  "author": "me",
6
6
  "main": "out/init.lua",