remove_words.js (3116B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 isStringArray = require( '@stdlib/assert/is-string-array' ); 24 var uppercase = require( './../../uppercase' ); 25 var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; 26 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 27 var tokenize = require( '@stdlib/nlp/tokenize' ); 28 var format = require( './../../format' ); 29 30 31 // MAIN // 32 33 /** 34 * Removes a list of words from a string. 35 * 36 * @param {string} str - input string 37 * @param {StringArray} words - array of words to be removed 38 * @param {boolean} [ignoreCase=false] - boolean indicating whether to perform a case-insensitive operation 39 * @throws {TypeError} first argument must be a string 40 * @throws {TypeError} second argument must be an array of strings 41 * @throws {TypeError} third argument must be a boolean 42 * @returns {string} output string 43 * 44 * @example 45 * var str = 'beep boop Foo bar'; 46 * var out = removeWords( str, [ 'boop', 'foo' ] ); 47 * // returns 'beep Foo bar' 48 * 49 * @example 50 * var str = 'beep boop Foo bar'; 51 * var out = removeWords( str, [ 'boop', 'foo' ], true ); 52 * // returns 'beep bar' 53 */ 54 function removeWords( str, words, ignoreCase ) { 55 var tokens; 56 var token; 57 var list; 58 var flg; 59 var out; 60 var N; 61 var i; 62 var j; 63 64 if ( !isString( str ) ) { 65 throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); 66 } 67 if ( !isStringArray( words ) ) { 68 throw new TypeError( format( 'invalid argument. Second argument must be an array of strings. Value: `%s`.', words ) ); 69 } 70 if ( arguments.length > 2 ) { 71 if ( !isBoolean( ignoreCase ) ) { 72 throw new TypeError( format( 'invalid argument. Third argument must be a boolean. Value: `%s`.', ignoreCase ) ); 73 } 74 } 75 tokens = tokenize( str, true ); 76 N = words.length; 77 out = []; 78 if ( ignoreCase ) { 79 list = words.slice(); 80 for ( i = 0; i < N; i++ ) { 81 list[ i ] = uppercase( list[ i ] ); 82 } 83 for ( i = 0; i < tokens.length; i++ ) { 84 flg = true; 85 token = uppercase( tokens[ i ] ); 86 for ( j = 0; j < N; j++ ) { 87 if ( list[ j ] === token ) { 88 flg = false; 89 break; 90 } 91 } 92 if ( flg ) { 93 out.push( tokens[ i ] ); 94 } 95 } 96 return out.join( '' ); 97 } 98 // Case: case-sensitive 99 for ( i = 0; i < tokens.length; i++ ) { 100 token = tokens[ i ]; 101 flg = true; 102 for ( j = 0; j < N; j++ ) { 103 if ( words[ j ] === token ) { 104 flg = false; 105 break; 106 } 107 } 108 if ( flg ) { 109 out.push( token ); 110 } 111 } 112 return out.join( '' ); 113 } 114 115 116 // EXPORTS // 117 118 module.exports = removeWords;