Skip to content

Commit

Permalink
fix #3030 and run relateive test in edge-runtime (#3036)
Browse files Browse the repository at this point in the history
Co-authored-by: Toru Kobayashi <[email protected]>
  • Loading branch information
promer94 and koba04 authored Nov 25, 2024
1 parent 9e6c0b6 commit bd839a4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
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

0 comments on commit bd839a4

Please sign in to comment.