<commit_before>import { FieldInfo } from './DataPanel';

//Helper to fetch format options for each attribute and format it according to the instruction found
export function formatAttributeValues(
  attributes: __esri.Graphic['attributes'],
  fields: FieldInfo[] | null,
  esriIntl: any
): object {
  // const [esriIntl] = await loadModules(['esri/intl']);
  const formatAttributeObject = {} as object;
  Object.keys(attributes).forEach((attribute) => {
    const attributeField = fields?.find((f) => f.fieldName === attribute);
    if (attributeField?.format?.dateFormat) {
      //format the date if formatting options exist
      const dateFormatIntlOptions = esriIntl.convertDateFormatToIntlOptions(attributeField?.format?.dateFormat);
      if (!attributes[attribute]) return;
      const formattedDate = esriIntl.formatDate(Number(attributes[attribute]), dateFormatIntlOptions);
      formatAttributeObject[attribute] = formattedDate;
    } else if (attributeField?.format?.digitSeparator) {
      //format the number if formatting options exist
      const numberFormatIntlOptions = esriIntl.convertNumberFormatToIntlOptions({
        places: attributeField?.format?.places,
        digitSeparator: attributeField?.format?.digitSeparator,
      });
      if (!attributes[attribute]) return;

      const formattedNumber = esriIntl.formatNumber(attributes[attribute], numberFormatIntlOptions);
      formatAttributeObject[attribute] = formattedNumber;
    } else {
      //We are doing a string matching against attribute name, this is not ideal and should be addressed on the API level with new formatting options compatible with 4x esri api
      //TODO: This is a short term workaround as proper solution needs to modify all datasets with correct formatting options as described in this issue:
      // https://github.com/wri/gfw-mapbuilder/issues/1051
      if (attribute.toLowerCase().includes('date')) {
        const dateFormatIntlOptions = esriIntl.convertDateFormatToIntlOptions('long-date');
        if (!attributes[attribute]) return;

        const formatDate = esriIntl.formatDate(attributes[attribute], dateFormatIntlOptions);
        formatAttributeObject[attribute] = formatDate;
      } else {
        //no formatting options found,  use original value
        formatAttributeObject[attribute] = attributes[attribute];
      }
    }
  });
  return formatAttributeObject;
}<commit_msg>refactor: is value is null then add empty string as value<commit_after>import { FieldInfo } from './DataPanel';

//Helper to fetch format options for each attribute and format it according to the instruction found
export function formatAttributeValues(
  attributes: __esri.Graphic['attributes'],
  fields: FieldInfo[] | null,
  esriIntl: any
): object {
  // const [esriIntl] = await loadModules(['esri/intl']);
  const formatAttributeObject = {} as object;
  Object.keys(attributes).forEach((attribute) => {
    const attributeField = fields?.find((f) => f.fieldName === attribute);
    if (attributeField?.format?.dateFormat) {
      if (attributes[attribute] === null || attributes[attribute] === undefined) {
        formatAttributeObject[attribute] = '';
      } else {
        //format the date if formatting options exist
        const dateFormatIntlOptions = esriIntl.convertDateFormatToIntlOptions(attributeField?.format?.dateFormat);
        // if (!attributes[attribute]) return;
        const formattedDate = esriIntl.formatDate(Number(attributes[attribute]), dateFormatIntlOptions);
        formatAttributeObject[attribute] = formattedDate;
      }
    } else if (attributeField?.format?.digitSeparator) {
      //format the number if formatting options exist
      const numberFormatIntlOptions = esriIntl.convertNumberFormatToIntlOptions({
        places: attributeField?.format?.places,
        digitSeparator: attributeField?.format?.digitSeparator,
      });
      if (attributes[attribute] === null || attributes[attribute] === undefined) {
        formatAttributeObject[attribute] = '';
      } else {
        const formattedNumber = esriIntl.formatNumber(attributes[attribute], numberFormatIntlOptions);
        formatAttributeObject[attribute] = formattedNumber;
      }
    } else {
      //We are doing a string matching against attribute name, this is not ideal and should be addressed on the API level with new formatting options compatible with 4x esri api
      //TODO: This is a short term workaround as proper solution needs to modify all datasets with correct formatting options as described in this issue:
      // https://github.com/wri/gfw-mapbuilder/issues/1051
      if (attribute.toLowerCase().includes('date')) {
        const dateFormatIntlOptions = esriIntl.convertDateFormatToIntlOptions('long-date');
        if (attributes[attribute] === null || attributes[attribute] === undefined) {
          formatAttributeObject[attribute] = '';
        } else {
          const formatDate = esriIntl.formatDate(attributes[attribute], dateFormatIntlOptions);
          formatAttributeObject[attribute] = formatDate;
        }
      } else {
        //no formatting options found,  use original value
        formatAttributeObject[attribute] = attributes[attribute];
      }
    }
  });
  return formatAttributeObject;
}
{'fraction_non_alphanumeric': 0.09946747812856599, 'fraction_numerical': 0.001901863826550019, 'mean_word_length': 3.8694444444444445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2002435', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1299, 'n_words': 373}