copy-style.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. const oneDepthCopy = (obj, nestKeys) => ({
  3. ...obj,
  4. ...nestKeys.reduce((memo, key) => {
  5. if (obj[key]) memo[key] = {
  6. ...obj[key]
  7. };
  8. return memo;
  9. }, {})
  10. });
  11. const setIfExists = function (src, dst, key) {
  12. let nestKeys = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
  13. if (src[key]) dst[key] = oneDepthCopy(src[key], nestKeys);
  14. };
  15. const isEmptyObj = obj => Object.keys(obj).length === 0;
  16. const copyStyle = style => {
  17. if (!style) return style;
  18. if (isEmptyObj(style)) return {};
  19. const copied = {
  20. ...style
  21. };
  22. setIfExists(style, copied, 'font', ['color']);
  23. setIfExists(style, copied, 'alignment');
  24. setIfExists(style, copied, 'protection');
  25. if (style.border) {
  26. setIfExists(style, copied, 'border');
  27. setIfExists(style.border, copied.border, 'top', ['color']);
  28. setIfExists(style.border, copied.border, 'left', ['color']);
  29. setIfExists(style.border, copied.border, 'bottom', ['color']);
  30. setIfExists(style.border, copied.border, 'right', ['color']);
  31. setIfExists(style.border, copied.border, 'diagonal', ['color']);
  32. }
  33. if (style.fill) {
  34. setIfExists(style, copied, 'fill', ['fgColor', 'bgColor', 'center']);
  35. if (style.fill.stops) {
  36. copied.fill.stops = style.fill.stops.map(s => oneDepthCopy(s, ['color']));
  37. }
  38. }
  39. return copied;
  40. };
  41. exports.copyStyle = copyStyle;
  42. //# sourceMappingURL=copy-style.js.map