remove_punctuation.js (1926B)
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; 24 var replace = require( './../../replace' ); 25 var format = require( './../../format' ); 26 27 28 // VARIABLES // 29 30 var RE = /[!"'(),–.:;<>?`{}|~\/\\\[\]]/g; // eslint-disable-line no-useless-escape 31 32 33 // MAIN // 34 35 /** 36 * Removes punctuation characters from a string. 37 * 38 * @param {string} str - input string 39 * @throws {TypeError} must provide a string primitive 40 * @returns {string} output string 41 * 42 * @example 43 * var str = 'Sun Tzu said: "A leader leads by example not by force."'; 44 * var out = removePunctuation( str ); 45 * // returns 'Sun Tzu said A leader leads by example not by force' 46 * 47 * @example 48 * var str = 'Double, double, toil and trouble; Fire burn, and cauldron bubble!'; 49 * var out = removePunctuation( str ); 50 * // returns 'Double double toil and trouble Fire burn and cauldron bubble' 51 * 52 * @example 53 * var str = 'This module removes these characters: `{}[]:,!/<>().;~|?\'"'; 54 * var out = removePunctuation( str ); 55 * // returns 'This module removes these characters ' 56 */ 57 function removePunctuation( str ) { 58 if ( !isString( str ) ) { 59 throw new TypeError( format( 'invalid argument. Must provide a string. Value: `%s`.', str ) ); 60 } 61 return replace( str, RE, '' ); 62 } 63 64 65 // EXPORTS // 66 67 module.exports = removePunctuation;