main.js (2783B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var removePunctuation = require( './../../remove-punctuation' ); 24 var tokenize = require( '@stdlib/nlp/tokenize' ); 25 var replace = require( './../../replace' ); 26 var uppercase = require( './../../uppercase' ); 27 var lowercase = require( './../../lowercase' ); 28 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 29 var format = require( './../../format' ); 30 var validate = require( './validate.js' ); 31 var STOPWORDS = require( './stopwords.json' ); 32 33 34 // VARIABLES // 35 36 var RE_HYPHEN = /-/g; 37 38 39 // MAIN // 40 41 /** 42 * Generates an acronym for a given string. 43 * 44 * ## Notes 45 * 46 * - The acronym is generated by capitalizing the first letter of each word in the string. 47 * - The function removes stop words from the string before generating the acronym. 48 * - The function splits hyphenated words and uses the first character of each hyphenated part. 49 * 50 * @param {string} str - input string 51 * @param {Options} [options] - function options 52 * @param {StringArray} [options.stopwords] - custom stop words 53 * @throws {TypeError} must provide a string primitive 54 * @throws {TypeError} must provide valid options 55 * @returns {string} generated acronym 56 * 57 * @example 58 * var out = acronym( 'the quick brown fox' ); 59 * // returns 'QBF' 60 * 61 * @example 62 * var out = acronym( 'Hard-boiled eggs' ); 63 * // returns 'HBE' 64 * 65 * @example 66 * var out = acronym( 'National Association of Securities Dealers Automated Quotation' ); 67 * // returns 'NASDAQ' 68 */ 69 function acronym( str, options ) { 70 var stopwords; 71 var words; 72 var opts; 73 var err; 74 var out; 75 var i; 76 77 if ( !isString( str ) ) { 78 throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); 79 } 80 opts = {}; 81 if ( arguments.length > 1 ) { 82 err = validate( opts, options ); 83 if ( err ) { 84 throw err; 85 } 86 } 87 stopwords = opts.stopwords || STOPWORDS; 88 str = removePunctuation( str ); 89 str = replace( str, RE_HYPHEN, ' ' ); 90 words = tokenize( str ); 91 out = ''; 92 for ( i = 0; i < words.length; i++ ) { 93 if ( stopwords.indexOf( lowercase( words[ i ] ) ) === -1 ) { 94 out += uppercase( words[ i ].charAt( 0 ) ); 95 } 96 } 97 return out; 98 } 99 100 101 // EXPORTS // 102 103 module.exports = acronym;