34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
/**
|
|
* @file Replace the comments in a string.
|
|
* @version 2.0.0
|
|
* @author Xotic750 <Xotic750@gmail.com>
|
|
* @copyright Xotic750
|
|
* @license {@link <https://opensource.org/licenses/MIT> MIT}
|
|
* @module replace-comments-x
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var toStr = require('to-string-x');
|
|
var requireCoercibleToString = require('require-coercible-to-string-x');
|
|
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
|
|
var replace = ''.replace;
|
|
|
|
/**
|
|
* This method replaces comments in a string.
|
|
*
|
|
* @param {string} string - The string to be stripped.
|
|
* @param {string} [replacement] - The string to be used as a replacement.
|
|
* @throws {TypeError} If string is null or undefined or not coercible.
|
|
* @throws {TypeError} If replacement is not coercible.
|
|
* @returns {string} The new string with the comments replaced.
|
|
* @example
|
|
* var replaceComments = require('replace-comments-x');
|
|
*
|
|
* replaceComments(test;/* test * /, ''), // 'test;'
|
|
* replaceComments(test; // test, ''), // 'test;'
|
|
*/
|
|
module.exports = function replaceComments(string) {
|
|
return replace.call(requireCoercibleToString(string), STRIP_COMMENTS, arguments.length > 1 ? toStr(arguments[1]) : '');
|
|
};
|