1const { DEVICE_LEVEL } = require('../../lite/lite-enum') 2 3/** 4 * rules: 5 * - abc-def -> abcDef 6 * - -abc-def -> AbcDef 7 * 8 * @param {string} value 9 * @return {string} 10 */ 11exports.hyphenedToCamelCase = function hyphenedToCamelCase(value) { 12 return value.replace(/(?<!-)-([a-z])/g, function(s, m) { 13 return m.toUpperCase() 14 }) 15} 16 17/** 18 * rules: 19 * - abcDef -> abc-def 20 * - AbcDef -> -abc-def 21 * 22 * @param {string} value 23 * @return {string} 24 */ 25exports.camelCaseToHyphened = function camelCaseToHyphened(value) { 26 return value.replace(/([A-Z])/g, function(s, m) { 27 if (typeof m === 'string') { 28 return '-' + m.toLowerCase() 29 } 30 return m 31 }) 32} 33 34exports.isValidValue = function isValidValue(value) { 35 return "number" == typeof value || "string" == typeof value 36} 37 38/** 39 * rules: 40 * assign abbreviated values to expanded attributes 41 * margin:1px 2px -> marginTop:1px;marginRight:2px;marginBottom:1px;marginLeft:2px; 42 * 43 * @param {obejct} object 44 * @param {string} value 45 * @param {obejct} spliceName 46 */ 47exports.splitAttr = function (object, value, spliceName) { 48 const values = value.toString().trim().split(/(?<!\+|\-|\*|\/|\,)\s+(?!\+|\-|\*|\/|\,)/) 49 if (values) { 50 switch (values.length) { 51 case 1: 52 spliceName.forEach(function (item) { 53 object[item] = values[0] 54 }) 55 break 56 case 2: 57 spliceName.forEach(function (item, index) { 58 object[item] = index % 2 ? values[1] : values[0] 59 }) 60 break 61 case 3: 62 spliceName.forEach(function (item, index) { 63 object[item] = index % 2 ? values[1] : values[index] 64 }) 65 break 66 default: 67 spliceName.forEach(function (item, index) { 68 object[item] = values[index] 69 }) 70 } 71 } 72} 73 74const RICH_SPLECIAL_ATTR = { 75 MARGIN: 'margin', 76 PADDING: 'padding', 77 BORDER: 'border', 78 BORDER_WIDTH: 'borderWidth', 79 BORDER_COLOR: 'borderColor', 80 BORDER_STYLE: 'borderStyle', 81 BORDER_RADIUS: 'borderRadius', 82 BORDER_BOTTOM: 'borderBottom', 83 BORDER_RIGHT: 'borderRight', 84 BORDER_LEFT: 'borderLeft', 85 BORDER_TOP: 'borderTop', 86 GRID_GAP: 'gridGap', 87 BOX_SHADOW: 'boxShadow', 88 ANIMATION: 'animation' 89} 90 91const LITE_SPLECIAL_ATTR = { 92 MARGIN: 'margin', 93 PADDING: 'padding', 94 BORDER_WIDTH: 'borderWidth', 95 BORDER_COLOR: 'borderColor' 96} 97 98/** 99 * Special style attributes that need to be expanded 100 * 101 */ 102exports.SPLECIAL_ATTR = process.env.DEVICE_LEVEL === DEVICE_LEVEL.LITE ? LITE_SPLECIAL_ATTR : RICH_SPLECIAL_ATTR 103