hexo/node_modules/stylus/lib/functions/replace.js

26 lines
744 B
JavaScript
Raw Normal View History

2023-09-25 15:58:56 +08:00
var utils = require('../utils')
, nodes = require('../nodes');
/**
* Returns string with all matches of `pattern` replaced by `replacement` in given `val`
*
* @param {String} pattern
* @param {String} replacement
* @param {String|Ident} val
* @return {String|Ident}
* @api public
*/
function replace(pattern, replacement, val){
utils.assertString(pattern, 'pattern');
utils.assertString(replacement, 'replacement');
utils.assertString(val, 'val');
pattern = new RegExp(pattern.string, 'g');
var res = val.string.replace(pattern, replacement.string);
return val instanceof nodes.Ident
? new nodes.Ident(res)
: new nodes.String(res);
}
replace.params = ['pattern', 'replacement', 'val'];
module.exports = replace;