Equation.js/lib/readstream.js

44 lines
781 B
JavaScript
Raw Normal View History

2015-04-20 09:28:11 +00:00
export default function(string) {
let i = 0, buffer = [];
return {
next() {
buffer.push(string[i]);
2015-04-20 09:29:59 +00:00
if (i >= string.length) {
return null;
}
2015-04-20 09:28:11 +00:00
return string[i++];
},
current() {
return string[i - 1];
},
index() {
return i - 1;
},
to(n) {
let temp = '';
const dest = i + n;
for (i = i; i < dest; ++i) {
temp += [string[i]];
}
return temp;
},
drain() {
return buffer.splice(0, buffer.length);
},
replace(start, end, replace) {
let temp = string.split('');
temp.splice(start, end, replace);
string = temp.join('');
i = i - (end - start);
},
go(n) {
i += n;
},
all() {
return string;
}
};
}