Skip to content

Commit

Permalink
fix(utils): adjust the order of getting self (#178)
Browse files Browse the repository at this point in the history
* fix(utils): adjust the order of getting self

* refactor: imitate globalThis

* refactor: polyfill globalThis
  • Loading branch information
monkindey authored Jul 18, 2019
1 parent 6995de6 commit 304b1a0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Path, IFormPathMatcher } from '@uform/types'
import { isArr, isStr, getPathSegments, toArr, clone, isFn } from '@uform/utils'
import {
isArr,
isStr,
getPathSegments,
toArr,
clone,
isFn,
globalThisPolyfill
} from '@uform/utils'

export * from '@uform/utils'

const self = window
const self = globalThisPolyfill

const getScheduler = () => {
if (!self.requestAnimationFrame) {
Expand Down
12 changes: 7 additions & 5 deletions packages/utils/src/clone.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { isFn } from '@uform/types'
import { globalThisPolyfill } from './globalThis'

type Filter = (value: any, key: string) => boolean

const self: { [key: string]: any } = this || global || window

const NATIVE_KEYS = [
['Map', (map: any) => new Map(map)],
['WeakMap', (map: any) => new WeakMap(map)],
Expand All @@ -26,13 +25,16 @@ const isNativeObject = (values: any): any => {
const item = NATIVE_KEYS[i]
if (Array.isArray(item) && item[0]) {
if (
self[item[0] as string] &&
values instanceof self[item[0] as string]
globalThisPolyfill[item[0] as string] &&
values instanceof globalThisPolyfill[item[0] as string]
) {
return item[1] ? item[1] : item[0]
}
} else {
if (self[item as string] && values instanceof self[item as string]) {
if (
globalThisPolyfill[item as string] &&
values instanceof globalThisPolyfill[item as string]
) {
return item
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/globalThis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const globalThisPolyfill =
self || window || global || Function('return this')()
3 changes: 2 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './lru'
export * from './isEmpty'
export * from './case'
export * from './defer'
export * from './stringLength'
export * from './stringLength'
export * from './globalThis'
4 changes: 2 additions & 2 deletions packages/validator/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getIn, each } from './utils'
import { getIn, each, globalThisPolyfill } from './utils'
import locales from './locale'

const self: any = this || global || window
const self: any = globalThisPolyfill

export interface ILocaleMessages {
[key: string]: string | ILocaleMessages
Expand Down

0 comments on commit 304b1a0

Please sign in to comment.