github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/utils/properties/glimmer-style-string.js (about) 1 import { htmlSafe } from '@ember/template'; 2 3 // A decorator for transforming an object into an html 4 // compatible style attribute string. 5 // 6 // ex. @styleString 7 // get styleStr() { 8 // return { color: '#FF0', 'border-width': '1px' } 9 // } 10 export default function styleString(target, name, descriptor) { 11 if (!descriptor.get) throw new Error('styleString only works on getters'); 12 const orig = descriptor.get; 13 descriptor.get = function() { 14 const styles = orig.apply(this); 15 16 let str = ''; 17 18 if (styles) { 19 str = Object.keys(styles) 20 .reduce(function(arr, key) { 21 const val = styles[key]; 22 arr.push(key + ':' + (typeof val === 'number' ? val.toFixed(2) + 'px' : val)); 23 return arr; 24 }, []) 25 .join(';'); 26 } 27 28 return htmlSafe(str); 29 }; 30 return descriptor; 31 }