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

fix(utils): adjust the order of getting self #178

Merged
merged 3 commits into from
Jul 18, 2019
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
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 = [
Copy link
Contributor

@stkevintan stkevintan Jul 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NATIVE_KEYS: [string, (obj: any) => any][] = [xxxx]

下面就不用as了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像这样子也是不行的哦,他是有 string 类型的

NATIVE_KEYS: ([string, (obj: any) => any] | string)[] = [xxxx]

这样子解有点问题,下面去掉 as string ,也是会报错。

['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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是在哪里的 else if 呢?

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