All files ogham.ts

100% Statements 30/30
100% Branches 16/16
100% Functions 7/7
100% Lines 30/30

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 117 118 119 120 121 122 123 124 125 126 127 128 129  1x     1x     1x                                                                                   1x 10x             10x   10x 1x     9x 1x   6x         8x 2x     8x 2x     8x 5x     8x               2x 12x     2x                 7x 38x 1x       6x               10x   178x 178x   178x     10x    
// Our mapping of alphabetical characters to ogham
import ogham from './ogham-symbols';
 
// Inputs can only contain alphabetical characters and spaces
const validateInputRgx = /[a-z ]+$/g;
 
// These characters are not supported by default
const phoneticReplacements = [
  {
    original: 'j',
    replacement: 'g'
  },
  {
    original: 'k',
    replacement: 'q'
  },
  {
    original: 'v',
    replacement: 'f'
  },
  {
    original: 'w',
    replacement: 'uu'
  },
  {
    original: 'x',
    replacement: 'z'
  },
  {
    original: 'y',
    replacement: 'i'
  }
];
 
/**
 * Interface representing options that can be passed to the convert
 */
export interface OghamOptions {
  addBoundary?: boolean;
  useForfeda?: boolean;
  usePhonetics?: boolean;
}
 
/**
 * Given an input of latin characters and spaces, this will return a string
 * containing the corresponding ogham characters, e.g "eire" => "᚛ᚓᚔᚏᚓ᚜"
 * @param text
 * @param headAndTail
 */
export function convert(input: string, opts?: OghamOptions) {
  const options: OghamOptions = Object.assign(
    {
      addBoundary: true
    },
    opts
  );
 
  let text = input.toLowerCase();
 
  if (text.length !== 0 && !text.match(validateInputRgx)) {
    throw new Error('input can only contain alphabetic characters');
  }
 
  if (!options.usePhonetics && containsInvalidCharacters(text)) {
    throw new Error(
      `input cannot contain ${phoneticReplacements
        .map(ch => ch.original)
        .join(', ')} unless "usePhonetics" option is passed`
    );
  }
 
  if (options.useForfeda) {
    text = replaceCharacters(text, ogham.combination);
  }
 
  if (options.usePhonetics) {
    text = replaceInvalidCharactersWithPhonetics(text);
  }
 
  if (options.addBoundary) {
    text = `${ogham.head.char}${text}${ogham.tail.char}`;
  }
 
  return replaceCharacters(text, ogham.individual);
}
 
/**
 * Replaces occurences of invalid characters with phonetics equivalents
 * @param text
 */
function replaceInvalidCharactersWithPhonetics(text: string) {
  phoneticReplacements.forEach(ch => {
    text = text.replace(new RegExp(ch.original, 'gi'), ch.replacement);
  });
 
  return text;
}
 
/**
 * Determines if the given input contains letters that are missing from ogham
 * or the irish alphabet
 * @param text
 */
function containsInvalidCharacters(text: string) {
  for (let i = 0; i < phoneticReplacements.length; i++) {
    if (text.indexOf(phoneticReplacements[i].original) !== -1) {
      return true;
    }
  }
 
  return false;
}
 
/**
 * Replaces characters that match ogham forfeda patterns, e.g "ng" becomes "ᚍ"
 * @param text
 */
function replaceCharacters(text: string, patterns: any) {
  Object.keys(patterns).forEach(letter => {
    // This is the object containing the ogham char and char code
    const charInfo = patterns[letter];
    const rgx = new RegExp(`${letter}`, 'ig');
 
    text = text.replace(rgx, charInfo.char);
  });
 
  return text;
}