Skip to content

Commit

Permalink
feat: support sumHash and fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Nov 3, 2024
1 parent 72b398f commit af238bd
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/items/math.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const mathItems = [
{ text: 'sum', link: '/math/sum' },
{ text: 'sumBy', link: '/math/sum-by' },
{ text: 'sumHash', link: '/math/sum-hash' },
{ text: 'minBy', link: '/math/min-by' },
{ text: 'maxBy', link: '/math/max-by' },
{ text: 'mean', link: '/math/mean' },
Expand Down
26 changes: 26 additions & 0 deletions docs/math/sum-hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# sumHash

Calculate a hash sum for a given value. The hash is generated based on the value's properties and content, and it returns a hexadecimal string representation of the hash.

### Usage

```ts
import { sumHash } from 'rattail'

sumHash('123')
// return '1a3a267c'
sumHash({ a: '123' })
// return 'b1c920ac'
```

### Arguments

| Arg | Type | Defaults |
| ------- | :---: | -------: |
| `value` | `any` | |

### Return

| Type |
| :------: |
| `string` |
2 changes: 1 addition & 1 deletion docs/util/mitt.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Event emitter / pubsub. Integrated with [mitt](https://github.com/developit/mitt
### Usage

```ts
import mitt from 'mitt'
import { mitt } from 'rattail'

const emitter = mitt()

Expand Down
26 changes: 26 additions & 0 deletions docs/zh/math/sum-hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# sumHash

为给定的值计算一个哈希值。哈希值是基于值的属性和内容生成的,并返回哈希的十六进制字符串表示。

### 使用

```ts
import { sumHash } from 'rattail'

sumHash('123')
// return '1a3a267c'
sumHash({ a: '123' })
// return 'b1c920ac'
```

### 参数列表

| 参数 | 类型 | 默认值 |
| ------- | :---: | -----: |
| `value` | `any` | |

### 返回值

| 类型 |
| :------: |
| `string` |
2 changes: 1 addition & 1 deletion docs/zh/util/mitt.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### 用法

```ts
import mitt from 'mitt'
import { mitt } from 'rattail'

const emitter = mitt()

Expand Down
14 changes: 5 additions & 9 deletions src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ export function sumHash(value: any): string {
return hash < 0 ? hash * -2 : hash
}

function sumObject(hash: number, object: Record<string, any>, seen: any[]): number {
return Object.keys(object)
.sort()
.reduce((hash, key) => sumValue(hash, (object as Record<string, any>)[key], key, seen), hash)
}

function sumValue(hash: number, value: any, key: string, seen: any[]): number {
function baseSumHash(hash: number, value: any, key: string, seen: any[]): number {
hash = sum(hash, key)
hash = sum(hash, toTypeString(value))
hash = sum(hash, typeof value)
Expand All @@ -78,7 +72,9 @@ export function sumHash(value: any): string {

seen.push(value)

hash = sumObject(hash, value, seen)
hash = Object.keys(value)
.sort()
.reduce((hash, key) => baseSumHash(hash, value[key], key, seen), hash)

if (isFunction(value.valueOf)) {
try {
Expand All @@ -94,5 +90,5 @@ export function sumHash(value: any): string {
return sum(hash, value.toString())
}

return sumValue(0, value, '', []).toString(16).padStart(8, '0')
return baseSumHash(0, value, '', []).toString(16).padStart(8, '0')
}

0 comments on commit af238bd

Please sign in to comment.