var Transform = require('pipestream').Transform;
var util = require('util');

function ReplaceStringTransform(str, value) {
	if (!(this instanceof ReplaceStringTransform)) {
		  return new ReplaceStringTransform(options);
	  }
	
	Transform.call(this);
	this._str = str;
	this._length = this._str.length;
	this._value = value == null ? '' : value + '';
	this._rest = '';
}

util.inherits(ReplaceStringTransform, Transform);

var proto = ReplaceStringTransform.prototype;
proto._transform = function(chunk, encoding, callback) {
	if (chunk != null) {
		chunk = this._rest + chunk;
		var minIndex = chunk.length + 1 - this._length;
		var index = chunk.lastIndexOf(this._str);
		
		if (index != -1) {
			index = Math.max(minIndex, index + this._length);
		} else {
			index = minIndex;
		}
		this._rest = chunk.substring(index);
		chunk = chunk.substring(0, index);
	} else {
		chunk = this._rest;
	}
	
	if (chunk) {
		chunk = chunk.split(this._str).join(this._value);
	}
	
	callback(null, chunk);
};

module.exports = ReplaceStringTransform;
var Transform = require('pipestream').Transform;
var util = require('util');

function ReplaceStringTransform(str, value) {
	if (!(this instanceof ReplaceStringTransform)) {
		  return new ReplaceStringTransform(options);
	  }
	
	Transform.call(this);
	this._str = str;
	this._length = this._str.length;
	this._value = value == null ? '' : value + '';
	this._rest = '';
}

util.inherits(ReplaceStringTransform, Transform);

var proto = ReplaceStringTransform.prototype;
proto._transform = function(chunk, encoding, callback) {
	if (chunk != null) {
		chunk = this._rest + chunk;
		var minIndex = chunk.length + 1 - this._length;
		var index = chunk.lastIndexOf(this._str);
		
		if (index != -1) {
			index = Math.max(minIndex, index + this._length);
		} else {
			index = minIndex;
		}
		this._rest = chunk.substring(index);
		chunk = replace(chunk.substring(0, index), this._str, this._value);
	} else if(this._rest) {
		chunk = replace(this._rest, this._str, this._value);
	}
	
	callback(null, chunk);
};

function replace(chunk, str, value) {
	return chunk.split(str).join(value);
}

module.exports = ReplaceStringTransform;
replace content