Skip to content

Commit

Permalink
Make swr zero dependency (#1024)
Browse files Browse the repository at this point in the history
* Make swr zero dependencies

* mark license
  • Loading branch information
huozhi authored Mar 10, 2021
1 parent 14b8708 commit 061db1d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
"ts-jest": "25.5.1",
"typescript": "3.6.4"
},
"dependencies": {
"dequal": "2.0.2"
},
"peerDependencies": {
"react": "^16.11.0 || ^17.0.0"
}
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dequal } from 'dequal/lite'
import deepEqual from './libs/deep-equal'
import { Configuration, RevalidatorOptions, Revalidator } from './types'
import Cache from './cache'
import webPreset from './libs/web-preset'
Expand Down Expand Up @@ -64,7 +64,7 @@ const defaultConfig = {
refreshWhenOffline: false,
shouldRetryOnError: true,
suspense: false,
compare: dequal,
compare: deepEqual,

isPaused: () => false,
...webPreset
Expand Down
36 changes: 36 additions & 0 deletions src/libs/deep-equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* deep equal lite version from dequal
* https://github.com/lukeed/dequal/blob/master/license
*/

const has = Object.prototype.hasOwnProperty

export default function deepEqual(foo: any, bar: any) {
let ctor, len
if (foo === bar) return true

if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
if (ctor === Date) return foo.getTime() === bar.getTime()
if (ctor === RegExp) return foo.toString() === bar.toString()

if (ctor === Array) {
if ((len = foo.length) === bar.length) {
while (len && deepEqual(foo[len], bar[len])) {
len--
}
}
return len === -1
}

if (!ctor || typeof foo === 'object') {
len = 0
for (ctor in foo) {
if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false
if (!(ctor in bar) || !deepEqual(foo[ctor], bar[ctor])) return false
}
return Object.keys(bar).length === len
}
}

return foo !== foo && bar !== bar
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1737,11 +1737,6 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=

[email protected]:
version "2.0.2"
resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.2.tgz#85ca22025e3a87e65ef75a7a437b35284a7e319d"
integrity sha512-q9K8BlJVxK7hQYqa6XISGmBZbtQQWVXSrRrWreHC94rMt1QL/Impruc+7p2CYSYuVIUr+YCt6hjrs1kkdJRTug==

detect-newline@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
Expand Down

0 comments on commit 061db1d

Please sign in to comment.