1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import test from './test.js'
- function trim(str, pos = 'both') {
- str = String(str)
- if (pos == 'both') {
- return str.replace(/^\s+|\s+$/g, '')
- }
- if (pos == 'left') {
- return str.replace(/^\s*/, '')
- }
- if (pos == 'right') {
- return str.replace(/(\s*$)/g, '')
- }
- if (pos == 'all') {
- return str.replace(/\s+/g, '')
- }
- return str
- }
- function addStyle(customStyle, target = 'object') {
-
- if (test.empty(customStyle) || typeof(customStyle) === 'object' && target === 'object' || target === 'string' && typeof(customStyle) === 'string') {
- return customStyle
- }
-
- if (target === 'object') {
-
- customStyle = trim(customStyle)
-
- const styleArray = customStyle.split(';')
- const style = {}
-
- for (let i = 0; i < styleArray.length; i++) {
-
- if (styleArray[i]) {
- const item = styleArray[i].split(':')
- style[trim(item[0])] = trim(item[1])
- }
- }
- return style
- }
-
- let string = ''
- for (const i in customStyle) {
-
- const key = i.replace(/([A-Z])/g, '-$1').toLowerCase()
- string += `${key}:${customStyle[i]};`
- }
-
- return trim(string)
- }
- export default addStyle
|