8
0

table-properties.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module table/utils/table-properties
  7. */
  8. import { isObject } from 'lodash-es';
  9. /**
  10. * Returns a string if all four values of box sides are equal.
  11. *
  12. * If a string is passed, it is treated as a single value (pass-through).
  13. *
  14. * // Returns 'foo':
  15. * getSingleValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } );
  16. * getSingleValue( 'foo' );
  17. *
  18. * // Returns undefined:
  19. * getSingleValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } );
  20. * getSingleValue( { top: 'foo', right: 'foo' } );
  21. *
  22. * @param objectOrString
  23. * @returns {module:engine/view/stylesmap~BoxSides|String}
  24. */
  25. export function getSingleValue( objectOrString ) {
  26. if ( !objectOrString || !isObject( objectOrString ) ) {
  27. return objectOrString;
  28. }
  29. const { top, right, bottom, left } = objectOrString;
  30. if ( top == right && right == bottom && bottom == left ) {
  31. return top;
  32. }
  33. }
  34. /**
  35. * Adds a unit to a value if the value is a number or a string representing a number.
  36. *
  37. * **Note**: It does nothing to non-numeric values.
  38. *
  39. * getSingleValue( 25, 'px' ); // '25px'
  40. * getSingleValue( 25, 'em' ); // '25em'
  41. * getSingleValue( '25em', 'px' ); // '25em'
  42. * getSingleValue( 'foo', 'px' ); // 'foo'
  43. *
  44. * @param {*} value
  45. * @param {String} defaultUnit A default unit added to a numeric value.
  46. * @returns {String|*}
  47. */
  48. export function addDefaultUnitToNumericValue( value, defaultUnit ) {
  49. const numericValue = parseFloat( value );
  50. if ( Number.isNaN( numericValue ) ) {
  51. return value;
  52. }
  53. if ( String( numericValue ) !== String( value ) ) {
  54. return value;
  55. }
  56. return `${ numericValue }${ defaultUnit }`;
  57. }