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