ExamplesPlaygroundReference Source

coral-spectrum/coral-utils/src/scripts/Transformation.js

  1. /**
  2. * Copyright 2019 Adobe. All rights reserved.
  3. * This file is licensed to you under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software distributed under
  8. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
  9. * OF ANY KIND, either express or implied. See the License for the specific language
  10. * governing permissions and limitations under the License.
  11. */
  12.  
  13. /**
  14. Set of property value transformation functions.
  15. */
  16. class Transformation {
  17. /**
  18. Transform the provided value into a boolean. Follows the behavior of JavaScript thruty/falsy.
  19.  
  20. @param {*} value
  21. The value to convert to Boolean.
  22.  
  23. @returns {Boolean} The corresponding boolean value.
  24. */
  25. boolean(value) {
  26. return !!value;
  27. }
  28.  
  29. /**
  30. Transform the provided value into a boolean. Follows the behavior of the HTML specification, in which the existence of
  31. the attribute indicates <code>true</code> regardless of the attribute's value. If the value is a boolean, it ignores
  32. the transformation.
  33.  
  34. @param {*} value
  35. The value to convert to Boolean.
  36.  
  37. @returns {Boolean} The corresponding boolean value.
  38. */
  39. booleanAttr(value) {
  40. return typeof value === 'boolean' ? value : !(value === null || typeof value === 'undefined');
  41. }
  42.  
  43. /**
  44. Transforms the provided value into a floating point number.
  45.  
  46. @param {*} value
  47. The value to convert to a Number.
  48.  
  49. @returns {?Number} The corresponding number or <code>null</code> if the passed value cannot be converted to a number.
  50. */
  51. number(value) {
  52. value = parseFloat(value);
  53. return isNaN(value) ? null : value;
  54. }
  55.  
  56. /**
  57. Transforms the provided value into a floating number. The conversion is strict in the sense that if non numeric values
  58. are detected, <code>null</code> is returned instead.
  59.  
  60. @param {*} value
  61. The value to be converted to a Number.
  62.  
  63. @retuns {?Number} The corresponding number or <code>null</code> if the passed value cannot be converted to number.
  64. */
  65. float(value) {
  66. if (/^(-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/.test(value)) {
  67. return Number(value);
  68. }
  69.  
  70. return null;
  71. }
  72.  
  73. /**
  74. Transform the provided value into a string. When given <code>null</code> or <code>undefined</code> it will be
  75. converted to an empty string("").
  76.  
  77. @param {*} value
  78. The value to convert to String.
  79.  
  80. @returns {String} The corresponding string value.
  81. */
  82. string(value) {
  83. if (value === null || typeof value === 'undefined') {
  84. return '';
  85. }
  86.  
  87. return typeof value === 'string' ? value : String(value);
  88. }
  89. }
  90.  
  91. /**
  92. A type transform utility.
  93.  
  94. @type {Transformation}
  95. */
  96. const transform = new Transformation();
  97.  
  98. export default transform;