<commit_before>import _ from 'underscore';
import createFlash from '~/flash';
import { s__ } from '~/locale';
import emojiAliases from 'emojis/aliases.json';
import axios from '../lib/utils/axios_utils';

import AccessorUtilities from '../lib/utils/accessor';

let emojiMap = null;
let validEmojiNames = null;

export const EMOJI_VERSION = '1';
const EMOJI_VERSION_LOCALSTORAGE = `EMOJIS_${EMOJI_VERSION}`;

const isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();

export function initEmojiMap() {
  return new Promise((resolve, reject) => {
    if (emojiMap) {
      resolve(emojiMap);
    } else if (isLocalStorageAvailable && window.localStorage.getItem(EMOJI_VERSION_LOCALSTORAGE)) {
      emojiMap = JSON.parse(window.localStorage.getItem(EMOJI_VERSION_LOCALSTORAGE));
      validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
      resolve(emojiMap);
    } else {
      // We load the JSON from server
      axios
        .get(
          `${gon.asset_host || ''}${gon.relative_url_root ||
            ''}/-/emojis/${EMOJI_VERSION}/emojis.json`,
        )
        .then(({ data }) => {
          emojiMap = data;
          validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
          resolve(emojiMap);
          if (isLocalStorageAvailable) {
            window.localStorage.setItem(EMOJI_VERSION_LOCALSTORAGE, JSON.stringify(emojiMap));
          }
        })
        .catch(err => {
          createFlash(s__('Emojis|Something went wrong while loading emojis.'));
          reject(err);
        });
    }
  });
}

export function normalizeEmojiName(name) {
  return Object.prototype.hasOwnProperty.call(emojiAliases, name) ? emojiAliases[name] : name;
}

export function getValidEmojiNames() {
  return validEmojiNames;
}

export function isEmojiNameValid(name) {
  return validEmojiNames.indexOf(name) >= 0;
}<commit_msg>Removes the CSRF token if the emojis.json is on a CDN<commit_after>import _ from 'underscore';
import createFlash from '~/flash';
import { s__ } from '~/locale';
import emojiAliases from 'emojis/aliases.json';
import axios from '../lib/utils/axios_utils';
import csrf from '../lib/utils/csrf';

import AccessorUtilities from '../lib/utils/accessor';

let emojiMap = null;
let validEmojiNames = null;

export const EMOJI_VERSION = '1';
const EMOJI_VERSION_LOCALSTORAGE = `EMOJIS_${EMOJI_VERSION}`;

const isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();

export function initEmojiMap() {
  return new Promise((resolve, reject) => {
    if (emojiMap) {
      resolve(emojiMap);
    } else if (isLocalStorageAvailable && window.localStorage.getItem(EMOJI_VERSION_LOCALSTORAGE)) {
      emojiMap = JSON.parse(window.localStorage.getItem(EMOJI_VERSION_LOCALSTORAGE));
      validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
      resolve(emojiMap);
    } else {
      // We load the JSON from server
      const axiosInstance = axios.create();

      // If the static JSON file is on a CDN we don't want to send the default CSRF token
      if (gon.asset_host) {
        delete axiosInstance.defaults.headers.common[csrf.headerKey];
      }

      axiosInstance
        .get(
          `${gon.asset_host || ''}${gon.relative_url_root ||
            ''}/-/emojis/${EMOJI_VERSION}/emojis.json`,
        )
        .then(({ data }) => {
          emojiMap = data;
          validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
          resolve(emojiMap);
          if (isLocalStorageAvailable) {
            window.localStorage.setItem(EMOJI_VERSION_LOCALSTORAGE, JSON.stringify(emojiMap));
          }
        })
        .catch(err => {
          createFlash(s__('Emojis|Something went wrong while loading emojis.'));
          reject(err);
        });
    }
  });
}

export function normalizeEmojiName(name) {
  return Object.prototype.hasOwnProperty.call(emojiAliases, name) ? emojiAliases[name] : name;
}

export function getValidEmojiNames() {
  return validEmojiNames;
}

export function isEmojiNameValid(name) {
  return validEmojiNames.indexOf(name) >= 0;
}
{'fraction_non_alphanumeric': 0.12808716707021792, 'fraction_numerical': 0.0009685230024213075, 'mean_word_length': 3.303125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '2216565', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1352, 'n_words': 259}