Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from vercel:main #176

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
]
},
"devDependencies": {
"@edge-runtime/jest-environment": "^3.0.4",
"@arethetypeswrong/cli": "^0.15.3",
"@playwright/test": "^1.34.3",
"@swc/core": "^1.3.62",
Expand Down
73 changes: 69 additions & 4 deletions pnpm-lock.yaml

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

15 changes: 9 additions & 6 deletions src/_internal/utils/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { OBJECT, isUndefined } from './shared'
// complexity is almost O(1).
const table = new WeakMap<object, number | string>()

const isObjectType = (value: any, type: string) =>
OBJECT.prototype.toString.call(value) === `[object ${type}]`

// counter of the key
let counter = 0

Expand All @@ -19,13 +22,13 @@ let counter = 0
// parsable.
export const stableHash = (arg: any): string => {
const type = typeof arg
const constructor = arg && arg.constructor
const isDate = constructor == Date

const isDate = isObjectType(arg, 'Date')
const isRegex = isObjectType(arg, 'RegExp')
const isPlainObject = isObjectType(arg, 'Object')
let result: any
let index: any

if (OBJECT(arg) === arg && !isDate && constructor != RegExp) {
if (OBJECT(arg) === arg && !isDate && !isRegex) {
// Object/function, not null/date/regexp. Use WeakMap to store the id first.
// If it's already hashed, directly return the result.
result = table.get(arg)
Expand All @@ -37,15 +40,15 @@ export const stableHash = (arg: any): string => {
result = ++counter + '~'
table.set(arg, result)

if (constructor == Array) {
if (Array.isArray(arg)) {
// Array.
result = '@'
for (index = 0; index < arg.length; index++) {
result += stableHash(arg[index]) + ','
}
table.set(arg, result)
}
if (constructor == OBJECT) {
if (isPlainObject) {
// Object, sort keys.
result = '#'
const keys = OBJECT.keys(arg).sort()
Expand Down
3 changes: 3 additions & 0 deletions test/unit/serialize.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @jest-environment @edge-runtime/jest-environment
*/
import { unstable_serialize } from 'swr'
import { stableHash } from 'swr/_internal'

Expand Down
3 changes: 3 additions & 0 deletions test/unit/utils.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @jest-environment @edge-runtime/jest-environment
*/
import {
stableHash as hash,
serialize,
Expand Down
Loading