chore: add isFunction utils and improve isObject

This commit is contained in:
陈嘉涵 2020-01-19 20:13:50 +08:00
parent 3a4107ef49
commit 4b54ec6a18
11 changed files with 32 additions and 31 deletions

View File

@ -1,5 +1,5 @@
// Utils // Utils
import { createNamespace, isObj } from '../utils'; import { createNamespace, isObject } from '../utils';
import { isMobile } from '../utils/validate/mobile'; import { isMobile } from '../utils/validate/mobile';
// Components // Components
@ -95,7 +95,7 @@ export default createComponent({
computed: { computed: {
areaListLoaded() { areaListLoaded() {
return isObj(this.areaList) && Object.keys(this.areaList).length; return isObject(this.areaList) && Object.keys(this.areaList).length;
}, },
areaText() { areaText() {

View File

@ -1,4 +1,4 @@
import { createNamespace, isObj, addUnit } from '../utils'; import { createNamespace, isObject, addUnit } from '../utils';
import { raf, cancelRaf } from '../utils/dom/raf'; import { raf, cancelRaf } from '../utils/dom/raf';
import { BLUE, WHITE } from '../utils/constant'; import { BLUE, WHITE } from '../utils/constant';
@ -101,7 +101,7 @@ export default createComponent({
}, },
gradient() { gradient() {
return isObj(this.color); return isObject(this.color);
}, },
LinearGradient() { LinearGradient() {

View File

@ -3,7 +3,7 @@ import { formatNumber } from './utils';
import { isIOS } from '../utils/validate/system'; import { isIOS } from '../utils/validate/system';
import { preventDefault } from '../utils/dom/event'; import { preventDefault } from '../utils/dom/event';
import { resetScroll } from '../utils/dom/reset-scroll'; import { resetScroll } from '../utils/dom/reset-scroll';
import { createNamespace, isObj, isDef, addUnit } from '../utils'; import { createNamespace, isObject, isDef, addUnit } from '../utils';
// Components // Components
import Icon from '../icon'; import Icon from '../icon';
@ -206,7 +206,7 @@ export default createComponent({
input.style.height = 'auto'; input.style.height = 'auto';
let height = input.scrollHeight; let height = input.scrollHeight;
if (isObj(this.autosize)) { if (isObject(this.autosize)) {
const { maxHeight, minHeight } = this.autosize; const { maxHeight, minHeight } = this.autosize;
if (maxHeight) { if (maxHeight) {
height = Math.min(height, maxHeight); height = Math.min(height, maxHeight);

View File

@ -1,7 +1,7 @@
import Vue from 'vue'; import Vue from 'vue';
import VanNotify from './Notify'; import VanNotify from './Notify';
import { WHITE } from '../utils/constant'; import { WHITE } from '../utils/constant';
import { isObj, isServer } from '../utils'; import { isObject, isServer } from '../utils';
import { mount } from '../utils/functional'; import { mount } from '../utils/functional';
import { NotifyOptions } from 'types/notify'; import { NotifyOptions } from 'types/notify';
@ -9,7 +9,7 @@ let timer: number | NodeJS.Timeout;
let instance: any; let instance: any;
function parseOptions(message: NotifyOptions): NotifyOptions { function parseOptions(message: NotifyOptions): NotifyOptions {
return isObj(message) ? message : ({ message } as NotifyOptions); return isObject(message) ? message : { message };
} }
function Notify(options: NotifyOptions) { function Notify(options: NotifyOptions) {

View File

@ -1,5 +1,5 @@
import { deepClone } from '../utils/deep-clone'; import { deepClone } from '../utils/deep-clone';
import { createNamespace, isObj } from '../utils'; import { createNamespace, isObject } from '../utils';
import { range } from '../utils/format/number'; import { range } from '../utils/format/number';
import { preventDefault } from '../utils/dom/event'; import { preventDefault } from '../utils/dom/event';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
@ -23,7 +23,7 @@ function getElementTranslateY(element) {
} }
function isOptionDisabled(option) { function isOptionDisabled(option) {
return isObj(option) && option.disabled; return isObject(option) && option.disabled;
} }
export default createComponent({ export default createComponent({
@ -176,9 +176,10 @@ export default createComponent({
}, },
getOptionText(option) { getOptionText(option) {
return isObj(option) && this.valueKey in option if (isObject(option) && this.valueKey in option) {
? option[this.valueKey] return option[this.valueKey];
: option; }
return option;
}, },
setIndex(index, userAction) { setIndex(index, userAction) {

View File

@ -1,5 +1,5 @@
// Utils // Utils
import { createNamespace, isObj, isDef } from '../utils'; import { createNamespace, isObject, isDef } from '../utils';
import { route, routeProps } from '../utils/router'; import { route, routeProps } from '../utils/router';
// Mixins // Mixins
@ -32,7 +32,7 @@ export default createComponent({
routeActive() { routeActive() {
const { to, $route } = this; const { to, $route } = this;
if (to && $route) { if (to && $route) {
const config = isObj(to) ? to : { path: to }; const config = isObject(to) ? to : { path: to };
const pathMatched = config.path === $route.path; const pathMatched = config.path === $route.path;
const nameMatched = isDef(config.name) && config.name === $route.name; const nameMatched = isDef(config.name) && config.name === $route.name;

View File

@ -1,6 +1,6 @@
import Vue from 'vue'; import Vue from 'vue';
import VueToast from './Toast'; import VueToast from './Toast';
import { isObj, isServer } from '../utils'; import { isObject, isServer } from '../utils';
const defaultOptions = { const defaultOptions = {
icon: '', icon: '',
@ -35,7 +35,7 @@ let currentOptions = {
}; };
function parseOptions(message) { function parseOptions(message) {
if (isObj(message)) { if (isObject(message)) {
return message; return message;
} }

View File

@ -2,6 +2,7 @@
* Create a basic component with common options * Create a basic component with common options
*/ */
import '../../locale'; import '../../locale';
import { isFunction } from '..';
import { camelize } from '../format/string'; import { camelize } from '../format/string';
import { SlotsMixin } from '../../mixins/slots'; import { SlotsMixin } from '../../mixins/slots';
import Vue, { import Vue, {
@ -68,7 +69,7 @@ export function createComponent(name: string) {
return function<Props = DefaultProps, Events = {}, Slots = {}>( return function<Props = DefaultProps, Events = {}, Slots = {}>(
sfc: VantComponentOptions | FunctionComponent sfc: VantComponentOptions | FunctionComponent
): TsxComponent<Props, Events, Slots> { ): TsxComponent<Props, Events, Slots> {
if (typeof sfc === 'function') { if (isFunction(sfc)) {
sfc = transformFunctionComponent(sfc); sfc = transformFunctionComponent(sfc);
} }

View File

@ -1,4 +1,4 @@
import { get } from '..'; import { get, isFunction } from '..';
import { camelize } from '../format/string'; import { camelize } from '../format/string';
import locale from '../../locale'; import locale from '../../locale';
@ -9,7 +9,7 @@ export function createI18N(name: string) {
const messages = locale.messages(); const messages = locale.messages();
const message = get(messages, prefix + path) || get(messages, path); const message = get(messages, prefix + path) || get(messages, path);
return typeof message === 'function' ? message(...args) : message; return isFunction(message) ? message(...args) : message;
}; };
} }

View File

@ -1,4 +1,4 @@
import { isDef, isObj } from '.'; import { isDef, isObject } from '.';
import { ObjectIndex } from './types'; import { ObjectIndex } from './types';
const { hasOwnProperty } = Object.prototype; const { hasOwnProperty } = Object.prototype;
@ -10,11 +10,7 @@ function assignKey(to: ObjectIndex, from: ObjectIndex, key: string) {
return; return;
} }
if ( if (!hasOwnProperty.call(to, key) || !isObject(val)) {
!hasOwnProperty.call(to, key) ||
!isObj(val) ||
typeof val === 'function'
) {
to[key] = val; to[key] = val;
} else { } else {
// eslint-disable-next-line no-use-before-define // eslint-disable-next-line no-use-before-define

View File

@ -7,13 +7,16 @@ export const isServer: boolean = Vue.prototype.$isServer;
export function noop() {} export function noop() {}
export function isDef(value: any): boolean { export function isDef(val: any): boolean {
return value !== undefined && value !== null; return val !== undefined && val !== null;
} }
export function isObj(x: any): boolean { export function isFunction(val: unknown): val is Function {
const type = typeof x; return typeof val === 'function';
return x !== null && (type === 'object' || type === 'function'); }
export function isObject(val: any): val is Record<any, any> {
return val !== null && typeof val === 'object';
} }
export function get(object: any, path: string): any { export function get(object: any, path: string): any {