-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cssConverter 补充函数:cssConverterString、cssConverterObject
cssConverter 补充函数:cssConverterString、cssConverterObject
- Loading branch information
Showing
3 changed files
with
121 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,86 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-return */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable guard-for-in */ | ||
/* eslint-disable no-restricted-syntax */ | ||
import type { CSSProperties } from '@vue/runtime-dom'; | ||
|
||
export const cssToObj = (css: string) => { | ||
function cssToObj(css: string) { | ||
const obj: Record<string, string> = {}; | ||
css.split(';').forEach((prop) => { | ||
const parts = prop.trim().split(':'); | ||
if (parts.length === 2) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access | ||
const cssProp = parts[0].trim().replace(/-([a-z])/g, (match, letter) => letter.toUpperCase()); | ||
const cssProp = parts[0] | ||
.trim() | ||
.replace(/-([a-z])/g, (match, letter) => letter.toUpperCase()); | ||
obj[cssProp] = parts[1].trim(); | ||
} | ||
}); | ||
return obj as CSSProperties; | ||
}; | ||
} | ||
|
||
export const objToCss = (obj: CSSProperties) => { | ||
function objToCss(obj: CSSProperties) { | ||
let css = ''; | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
const cssProp = key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`); | ||
const cssProp = key.replace( | ||
/[A-Z]/g, | ||
(match) => `-${match.toLowerCase()}`, | ||
); | ||
css += `${cssProp}:${obj[key as keyof CSSProperties]};`; | ||
} | ||
} | ||
return css; | ||
}; | ||
} | ||
|
||
/** | ||
* @name 样式转换器 | ||
* @description 短横线命名的css字符串<=>驼峰命名的css对象之间进行相关转换 | ||
* @param {String||Object} value 短横线命名的css字符串|| 驼峰命名的css对象 | ||
* @param {String} target =['string'|'object'] 指定输出类型 默认: string | ||
* @return {Object||String} 依据target | ||
*/ | ||
export default function cssConverter(value: CSSProperties | string, target: 'string' | 'object' = 'string') { | ||
if (target === 'string') { | ||
if (typeof value === 'string') return value; | ||
if (typeof value === 'object') return objToCss(value); | ||
} | ||
|
||
if (target === 'object') { | ||
if (typeof value === 'string') return cssToObj(value); | ||
if (typeof value === 'object') return value; | ||
} | ||
* 转换样式指定为短横线命名的css字符串 | ||
*/ | ||
function cssConverterString(value?: string | CSSProperties): string { | ||
if (typeof value === 'object') return objToCss(value); | ||
if (typeof value === 'string') return value; | ||
return ''; | ||
} | ||
|
||
/** | ||
* 转换样式指定为驼峰命名的css对象 | ||
*/ | ||
function cssConverterObject(value?: string | CSSProperties): CSSProperties { | ||
if (typeof value === 'string') return cssToObj(value); | ||
if (typeof value === 'object') return value; | ||
return {}; | ||
} | ||
|
||
// TODO:ts专用函数 cssConverterString cssConverterObject 解决在ts中使用时类型准确性 | ||
/** | ||
* @name 样式转换器(指定为短横线命名的css字符串) | ||
* @param {String||Object} value 短横线命名的css字符串|| 驼峰命名的css对象 | ||
* @return {Object} 驼峰命名的css对象 | ||
*/ | ||
// export const cssConverterString = (value:CSSProperties|string): CSSProperties => ({}); | ||
* 转换样式指定为短横线命名的css字符串/指定为驼峰命名的css对象 | ||
*/ | ||
function cssConverter( | ||
value?: CSSProperties | string, | ||
target: 'string' | 'object' = 'string', | ||
):CSSProperties | string { | ||
if (value) { | ||
if (target === 'string') { | ||
if (typeof value === 'string') return value; | ||
if (typeof value === 'object') return objToCss(value); | ||
} | ||
|
||
/** | ||
* @name 样式转换器(指定为驼峰命名的css对象) | ||
* @param {String||Object} value 短横线命名的css字符串|| 驼峰命名的css对象 | ||
* @return {Object} 短横线命名的css字符串 | ||
*/ | ||
// export const cssConverterObject = (value:CSSProperties|string): string => ''; | ||
if (target === 'object') { | ||
if (typeof value === 'string') return cssToObj(value); | ||
if (typeof value === 'object') return value; | ||
} | ||
|
||
Error('target must be "string" or "object"'); | ||
return ''; | ||
} | ||
|
||
if (target === 'string') return ''; | ||
if (target === 'object') return {}; | ||
Error('target must be "string" or "object"'); | ||
return ''; | ||
} | ||
|
||
export { cssConverterString, cssConverterObject, cssConverter }; | ||
|
||
export default cssConverter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as Hhttp } from './Hhttp/Hhttp'; | ||
export { default as cssConverter } from './cssConverter/cssConverter'; | ||
export * from './cssConverter/cssConverter'; |