All files / packages/sds-components/src/sds/utils utils.js

84.84% Statements 28/33
81.81% Branches 27/33
88.88% Functions 8/9
84.84% Lines 28/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 11734x 1024x     1024x 864x 160x 119x 41x 41x         34x 186x     471x 471x                                     3x 3x 3x 3x 1x   3x     3x     3x           27x 27x 27x 27x             27x         27x         34x                   72x                                                            
export const reflectAttribute = (element, attrName, value) => {
  Iif (!element) {
    return;
  }
  if (typeof value === 'string') {
    element.setAttribute(attrName, value);
  } else if (value === true) {
    element.setAttribute(attrName, '');
  } else if (!value) {
    element.removeAttribute(attrName);
  } else E{
    console.warn(`Invalid attribute value for "${attrName}": ${value}`);
  }
};
 
export const normalizeBoolean = (value) => {
  return typeof value === 'string' || !!value;
};
E
export function normalizeInput(value) {
  if (typeof value === 'number' || typeof value === 'string') {
    return String(value);
  }
  return '';
}
 
/**
 * A string normalization utility for attributes.
 *
 * @param {string} value - The value to normalize.
 * @param {object} config - The optional configuration object.
 * @param {string} [config.fallbackValue] - The optional fallback value to use if the given value is not provided or invalid. Defaults to an empty string.
 * @param {array} [config.validValues] - An optional array of valid values. Assumes all input is valid if not provided.
 * @return {string} - The normalized value.
 */
export function normalizeString(value, config = {}) {
  const { fallbackValue = '', validValues, toLowerCase = true } = config;
  let normalized = (typeof value === 'string' && value.trim()) || '';
  normalized = toLowerCase ? normalized.toLowerCase() : normalized;
  if (validValues && validValues.indexOf(normalized) === -1) {
    normalized = fallbackValue;
  }
  return normalized;
}
 
export function oneOf(value, options) {
  if (!options.includes(value)) {
  I  throw new Error(`Invalid value: ${value}. Valid options are: ${options.join(', ')}`);
  }
  return value;
}
 
// TODO: Move to date utils
export class DateFormatter {
  locale = 'en-US';
 
  constructor(locale, day, month, year, config) {
    this.date = new Date(year, month - 1, day);
    this.locale = locale;
    this.config = config || { day: 'numeric', month: 'long', year: 'numeric' };
  }
 
  format(locale = this.locale) {
    const options = {
      day: this.config.day,
      month: this.config.month,
      year: this.config.year,
    };
    return new Intl.DateTimeFormat(locale, options).format(this.date);
  }
}
 
// TODO: Move to date utils
export const dateRegexIso = /^(\d{4})[-](\d{1,2})[-](\d{1,2})$/;
 
/**
 * Check if the given value is defined.
 *
 * @param {*} value - The value to check.
 *
 * @returns {boolean} - True if the value is defined.
 */
export function isDefined(value) {
  return typeof value !== 'undefined' && value !== null;
}
 
/**
 * Check if the SLDS2 (Cosmos) theme is enabled.
 * Currently used only for sds-figure component
 *
 * @returns {boolean} - True if the value is defined.
 */
export function sldsIsV2Enabled() {
  return getComputedStyle(document.head).getPropertyValue('--_slds-is-v2-enabled');
}

/**
 * Library exports.
 *
 * @TODO: needs clean up when we get a breather. 'index' is required cause LWC
 * looks for the module name.
 */
export { autoResize } from './resize/index.js';
export {
  createControlAndTargetContract,
  validateControlType,
  validateIsButtonType,
  validateIsInputType,
} from './contract/index.js';
export { setToMidnight } from './date/index.js';
export { dispatchCustomEvent } from './event/index.js';
export { getOverflowContainer } from './overflow/index.js';
export { closePopover, openPopover, togglePopover } from './popover/index.js';
export { position, validatePlacement } from './position/index.js';
export { handleSlotChangeWithState } from './slot/index.js';
export { hasSlotText } from './slot/index.js';