diff --git a/src/address-edit/index.js b/src/address-edit/index.js index d0be37062..8151f7252 100644 --- a/src/address-edit/index.js +++ b/src/address-edit/index.js @@ -1,5 +1,5 @@ // Utils -import { createNamespace, isObj } from '../utils'; +import { createNamespace, isObject } from '../utils'; import { isMobile } from '../utils/validate/mobile'; // Components @@ -95,7 +95,7 @@ export default createComponent({ computed: { areaListLoaded() { - return isObj(this.areaList) && Object.keys(this.areaList).length; + return isObject(this.areaList) && Object.keys(this.areaList).length; }, areaText() { diff --git a/src/circle/index.js b/src/circle/index.js index dbc17151e..2e70ef031 100644 --- a/src/circle/index.js +++ b/src/circle/index.js @@ -1,4 +1,4 @@ -import { createNamespace, isObj, addUnit } from '../utils'; +import { createNamespace, isObject, addUnit } from '../utils'; import { raf, cancelRaf } from '../utils/dom/raf'; import { BLUE, WHITE } from '../utils/constant'; @@ -101,7 +101,7 @@ export default createComponent({ }, gradient() { - return isObj(this.color); + return isObject(this.color); }, LinearGradient() { diff --git a/src/field/index.js b/src/field/index.js index 57bcf1704..e45666d08 100644 --- a/src/field/index.js +++ b/src/field/index.js @@ -3,7 +3,7 @@ import { formatNumber } from './utils'; import { isIOS } from '../utils/validate/system'; import { preventDefault } from '../utils/dom/event'; import { resetScroll } from '../utils/dom/reset-scroll'; -import { createNamespace, isObj, isDef, addUnit } from '../utils'; +import { createNamespace, isObject, isDef, addUnit } from '../utils'; // Components import Icon from '../icon'; @@ -206,7 +206,7 @@ export default createComponent({ input.style.height = 'auto'; let height = input.scrollHeight; - if (isObj(this.autosize)) { + if (isObject(this.autosize)) { const { maxHeight, minHeight } = this.autosize; if (maxHeight) { height = Math.min(height, maxHeight); diff --git a/src/notify/index.ts b/src/notify/index.ts index dfd1d7491..8e33dc360 100644 --- a/src/notify/index.ts +++ b/src/notify/index.ts @@ -1,7 +1,7 @@ import Vue from 'vue'; import VanNotify from './Notify'; import { WHITE } from '../utils/constant'; -import { isObj, isServer } from '../utils'; +import { isObject, isServer } from '../utils'; import { mount } from '../utils/functional'; import { NotifyOptions } from 'types/notify'; @@ -9,7 +9,7 @@ let timer: number | NodeJS.Timeout; let instance: any; function parseOptions(message: NotifyOptions): NotifyOptions { - return isObj(message) ? message : ({ message } as NotifyOptions); + return isObject(message) ? message : { message }; } function Notify(options: NotifyOptions) { diff --git a/src/picker/PickerColumn.js b/src/picker/PickerColumn.js index 1e9c4af02..b2de06b74 100644 --- a/src/picker/PickerColumn.js +++ b/src/picker/PickerColumn.js @@ -1,5 +1,5 @@ import { deepClone } from '../utils/deep-clone'; -import { createNamespace, isObj } from '../utils'; +import { createNamespace, isObject } from '../utils'; import { range } from '../utils/format/number'; import { preventDefault } from '../utils/dom/event'; import { TouchMixin } from '../mixins/touch'; @@ -23,7 +23,7 @@ function getElementTranslateY(element) { } function isOptionDisabled(option) { - return isObj(option) && option.disabled; + return isObject(option) && option.disabled; } export default createComponent({ @@ -176,9 +176,10 @@ export default createComponent({ }, getOptionText(option) { - return isObj(option) && this.valueKey in option - ? option[this.valueKey] - : option; + if (isObject(option) && this.valueKey in option) { + return option[this.valueKey]; + } + return option; }, setIndex(index, userAction) { diff --git a/src/tabbar-item/index.js b/src/tabbar-item/index.js index f5bf826a2..cbafc9a25 100644 --- a/src/tabbar-item/index.js +++ b/src/tabbar-item/index.js @@ -1,5 +1,5 @@ // Utils -import { createNamespace, isObj, isDef } from '../utils'; +import { createNamespace, isObject, isDef } from '../utils'; import { route, routeProps } from '../utils/router'; // Mixins @@ -32,7 +32,7 @@ export default createComponent({ routeActive() { const { to, $route } = this; if (to && $route) { - const config = isObj(to) ? to : { path: to }; + const config = isObject(to) ? to : { path: to }; const pathMatched = config.path === $route.path; const nameMatched = isDef(config.name) && config.name === $route.name; diff --git a/src/toast/index.js b/src/toast/index.js index 420c6d0bd..87ff23f38 100644 --- a/src/toast/index.js +++ b/src/toast/index.js @@ -1,6 +1,6 @@ import Vue from 'vue'; import VueToast from './Toast'; -import { isObj, isServer } from '../utils'; +import { isObject, isServer } from '../utils'; const defaultOptions = { icon: '', @@ -35,7 +35,7 @@ let currentOptions = { }; function parseOptions(message) { - if (isObj(message)) { + if (isObject(message)) { return message; } diff --git a/src/utils/create/component.ts b/src/utils/create/component.ts index 46f338a29..5754ca181 100644 --- a/src/utils/create/component.ts +++ b/src/utils/create/component.ts @@ -2,6 +2,7 @@ * Create a basic component with common options */ import '../../locale'; +import { isFunction } from '..'; import { camelize } from '../format/string'; import { SlotsMixin } from '../../mixins/slots'; import Vue, { @@ -68,7 +69,7 @@ export function createComponent(name: string) { return function( sfc: VantComponentOptions | FunctionComponent ): TsxComponent { - if (typeof sfc === 'function') { + if (isFunction(sfc)) { sfc = transformFunctionComponent(sfc); } diff --git a/src/utils/create/i18n.ts b/src/utils/create/i18n.ts index a1bf18ddb..2045901a2 100644 --- a/src/utils/create/i18n.ts +++ b/src/utils/create/i18n.ts @@ -1,4 +1,4 @@ -import { get } from '..'; +import { get, isFunction } from '..'; import { camelize } from '../format/string'; import locale from '../../locale'; @@ -9,7 +9,7 @@ export function createI18N(name: string) { const messages = locale.messages(); const message = get(messages, prefix + path) || get(messages, path); - return typeof message === 'function' ? message(...args) : message; + return isFunction(message) ? message(...args) : message; }; } diff --git a/src/utils/deep-assign.ts b/src/utils/deep-assign.ts index 6ce75ac8d..0cb51d0d6 100644 --- a/src/utils/deep-assign.ts +++ b/src/utils/deep-assign.ts @@ -1,4 +1,4 @@ -import { isDef, isObj } from '.'; +import { isDef, isObject } from '.'; import { ObjectIndex } from './types'; const { hasOwnProperty } = Object.prototype; @@ -10,11 +10,7 @@ function assignKey(to: ObjectIndex, from: ObjectIndex, key: string) { return; } - if ( - !hasOwnProperty.call(to, key) || - !isObj(val) || - typeof val === 'function' - ) { + if (!hasOwnProperty.call(to, key) || !isObject(val)) { to[key] = val; } else { // eslint-disable-next-line no-use-before-define diff --git a/src/utils/index.ts b/src/utils/index.ts index 07f0a361b..c78525d6a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -7,13 +7,16 @@ export const isServer: boolean = Vue.prototype.$isServer; export function noop() {} -export function isDef(value: any): boolean { - return value !== undefined && value !== null; +export function isDef(val: any): boolean { + return val !== undefined && val !== null; } -export function isObj(x: any): boolean { - const type = typeof x; - return x !== null && (type === 'object' || type === 'function'); +export function isFunction(val: unknown): val is Function { + return typeof val === 'function'; +} + +export function isObject(val: any): val is Record { + return val !== null && typeof val === 'object'; } export function get(object: any, path: string): any {