UNPKG

ogham

Version:

convert an input string to its Ogham equivalent

115 lines (114 loc) 2.97 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); // Our mapping of alphabetical characters to ogham var ogham_symbols_1 = require('./ogham-symbols'); // Inputs can only contain alphabetical characters and spaces var validateInputRgx = /[a-z ]+$/g; // These characters are not supported by default var 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' } ]; /** * 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 */ function convert(input, opts) { var options = Object.assign( { addBoundary: true }, opts ); var 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(function(ch) { return ch.original; }) .join(', ') + ' unless "usePhonetics" option is passed' ); } if (options.useForfeda) { text = replaceCharacters(text, ogham_symbols_1.default.combination); } if (options.usePhonetics) { text = replaceInvalidCharactersWithPhonetics(text); } if (options.addBoundary) { text = '' + ogham_symbols_1.default.head.char + text + ogham_symbols_1.default.tail.char; } return replaceCharacters(text, ogham_symbols_1.default.individual); } exports.convert = convert; /** * Replaces occurences of invalid characters with phonetics equivalents * @param text */ function replaceInvalidCharactersWithPhonetics(text) { phoneticReplacements.forEach(function(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) { for (var 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, patterns) { Object.keys(patterns).forEach(function(letter) { // This is the object containing the ogham char and char code var charInfo = patterns[letter]; var rgx = new RegExp('' + letter, 'ig'); text = text.replace(rgx, charInfo.char); }); return text; } //# sourceMappingURL=ogham.js.map