All files / src createShortnames.js

91.67% Statements 11/12
83.33% Branches 5/6
100% Functions 1/1
91.67% Lines 11/12
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                  1x     10x     10x         10x   10x 6x       10x 2x 2x   8x     10x    
/**
 * @copyright   2017, Miles Johnson
 * @license     https://opensource.org/licenses/MIT
 * @flow
 */
 
import cleanName from './cleanName';
import extractSkinTone from './extractSkinTone';
 
const USED_SHORTNAMES = {};
 
export default function createShortnames(name: string): string[] {
  let shortname = cleanName(name).replace(/\s/g, '_');
 
  // Currently doesnt have a shortname defined
  Iif (shortname === 'regional_indicator_symbol_letter_u_regional_indicator_symbol_letter_n') {
    return ['flag_un'];
  }
 
  // Add skin tone and match EmojiOne format
  const skinTone = extractSkinTone(name);
 
  if (skinTone) {
    shortname += `_tone${skinTone}`;
  }
 
  // Check if the shortname has been used
  if (USED_SHORTNAMES[shortname]) {
    USED_SHORTNAMES[shortname] += 1;
    shortname += USED_SHORTNAMES[shortname];
  } else {
    USED_SHORTNAMES[shortname] = 1;
  }
 
  return [shortname];
}