'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']; |