From 540a02bea59ed8e7ff1cf2966ab5c729df6cf9a2 Mon Sep 17 00:00:00 2001 From: kooriookami <38392315+kooriookami@users.noreply.github.com> Date: Thu, 28 Jan 2021 08:10:45 -0600 Subject: [PATCH] feat(other): add addUnit util (#1382) --- packages/utils/util.ts | 55 +++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/packages/utils/util.ts b/packages/utils/util.ts index e8fe0187f9..0388acf0b4 100644 --- a/packages/utils/util.ts +++ b/packages/utils/util.ts @@ -1,25 +1,15 @@ +import type { Ref } from 'vue' import { getCurrentInstance } from 'vue' -import { - isObject, - isArray, - isString, - capitalize, - hyphenate, - looseEqual, - extend, - camelize, - hasOwn, - toRawType, -} from '@vue/shared' +import { camelize, capitalize, extend, hasOwn, hyphenate, isArray, isObject, isString, looseEqual, toRawType } from '@vue/shared' import isServer from './isServer' import type { AnyFunction } from './types' -import type { Ref } from 'vue' +import { warn } from './error' -export type PartialCSSStyleDeclaration = Partial< - Pick -> +export const SCOPE = 'Util' + +export type PartialCSSStyleDeclaration = Partial> export function toObject(arr: Array): Record { const res = {} @@ -86,23 +76,25 @@ export const escapeRegexpString = (value = ''): string => // coerce truthy value to array export const coerceTruthyValueToArray = arr => { - if (!arr && arr !== 0) { return [] } + if (!arr && arr !== 0) { + return [] + } return Array.isArray(arr) ? arr : [arr] } -export const isIE = function(): boolean { +export const isIE = function (): boolean { return !isServer && !isNaN(Number(document.DOCUMENT_NODE)) } -export const isEdge = function(): boolean { +export const isEdge = function (): boolean { return !isServer && navigator.userAgent.indexOf('Edge') > -1 } -export const isFirefox = function(): boolean { +export const isFirefox = function (): boolean { return !isServer && !!window.navigator.userAgent.match(/firefox/i) } -export const autoprefixer = function( +export const autoprefixer = function ( style: PartialCSSStyleDeclaration, ): PartialCSSStyleDeclaration { const rules = ['transform', 'transition', 'animation'] @@ -140,7 +132,7 @@ export const isHTMLElement = (val: unknown) => toRawType(val).startsWith('HTML') export function rafThrottle>(fn: T): AnyFunction { let locked = false - return function(...args: any[]) { + return function (...args: any[]) { if (locked) return locked = true window.requestAnimationFrame(() => { @@ -182,14 +174,15 @@ export function useGlobalConfig() { } return {} } -export const arrayFindIndex = function ( + +export const arrayFindIndex = function ( arr: Array, pred: (args: T) => boolean, ): number { return arr.findIndex(pred) } -export const arrayFind = function ( +export const arrayFind = function ( arr: Array, pred: (args: T) => boolean, ): any { @@ -221,6 +214,18 @@ export function deduplicate(arr: T[]) { * Unwraps refed value * @param ref Refed value */ -export function $(ref: Ref) { +export function $(ref: Ref) { return ref.value } + +export function addUnit(value: string | number) { + if (isString(value)) { + return value + } else if (isNumber(value)) { + return value + 'px' + } + if (process.env.NODE_ENV === 'development') { + warn(SCOPE, 'binding value must be a string or number') + } + return '' +}