All files / dist acronym.js

52% Statements 26/50
13.04% Branches 3/23
18.18% Functions 2/11
54.35% Lines 25/46
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    1x       1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   5x         1x       1x     1x     1x     1x           1x 1x 1x       26x         274926x                                                                                                                       1x 1x 1x
'use strict';
 
Object.defineProperty(exports, "__esModule", {
	value: true
});
 
var _wordList = require('word-list');
 
var _wordList2 = _interopRequireDefault(_wordList);
 
var _fs = require('fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
var _lodash = require('lodash.capitalize');
 
var _lodash2 = _interopRequireDefault(_lodash);
 
var _lodash3 = require('lodash.lowercase');
 
var _lodash4 = _interopRequireDefault(_lodash3);
 
var _lodash5 = require('lodash.defaults');
 
var _lodash6 = _interopRequireDefault(_lodash5);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
// Dictionary of words
// This is indexed by first letter.
// dictionary.a = [ 'apricots', 'always', ...]
var dictionary = {};
 
// Helper Functions
// returns the path to the word list which is separated by `\n`
var isString = function isString(text) {
	return typeof text === 'string';
};
var isArray = function isArray(text) {
	return text instanceof Array;
};
var getWord = function getWord(letter) {
	return isAlpha(letter) ? dictionary[letter][Math.floor(Math.random() * dictionary[letter].length)] : letter;
};
var isAlpha = function isAlpha(letter) {
	return (/^[a-zA-Z]+$/g.test(letter)
	);
};
 
// Set up the dictionary of words using word-list
function initialize() {
	var wordArray = _fs2.default.readFileSync(_wordList2.default, 'utf8').split('\n');
	var alphabet = 'abcdefghijklmnopqrstuvwxyz';
 
	// Empty array for each letter
	for (var i = 0; i < alphabet.length; i++) {
		dictionary[alphabet.charAt(i)] = [];
	}
 
	// Put all the words into the right spot.
	for (var _i = 0; _i < wordArray.length; _i++) {
		dictionary[wordArray[_i].charAt(0)].push(wordArray[_i]);
	}
}
 
// Main function
function acronym(text, options) {
	var actual = (0, _lodash6.default)(options || {}, {
		capitalize: true,
		separator: ' '
	});
 
	if (isString(text)) {
		// Return acronym for this word
		return processWord(text, actual);
	} else if (isArray(text)) {
		// Return acronym for each word in the array.
		return text;
	} else {
		// ???
		return text;
	}
}
 
function processWord(text, options) {
	// Split word into array of characters
	var arr = text.split('');
 
	// Transform each character into a whole word
	arr = arr.map(function (letter) {
		return getWord(letter.toLowerCase());
	});
 
	// Correctly capitalize
	if (options.capitalize) {
		arr = arr.map(function (word) {
			return isAlpha(word) ? (0, _lodash2.default)(word) : word;
		});
	} else {
		arr = arr.map(function (word) {
			return isAlpha(word) ? (0, _lodash4.default)(word) : word;
		});
	}
 
	// Re-join using separator, but only join adjacent words.
	// Otherwise, spit out cahracters verbatim
	var result = '';
 
	for (var i = 0; i < arr.length; i++) {
		result += arr[i];
 
		// Add separator?
		if (i !== arr.length - 1 && (isAlpha(arr[i]) || isAlpha(arr[i + 1]))) {
			result += options.separator;
		}
	}
 
	return result;
}
 
// Exports
initialize();
exports.default = acronym;
module.exports = exports['default'];