Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

join now matches Array#join and replace is streaming.

  • Loading branch information...
commit ae64b0ec8c84f854c9682721635eae04f4306a17 1 parent 5dcb680
@dominictarr authored
Showing with 30 additions and 10 deletions.
  1. +30 −10 index.js
View
40 index.js
@@ -501,25 +501,45 @@ es.stringify = function () {
// I need this for shadow-npm so it's only relatively small json files.
es.replace = function (from, to) {
+ return es.connect(es.split(from), es.join(to))
+}
+
+//
+// join chunks with a joiner. just like Array#join
+// also accepts a callback that is passed the chunks appended together
+// this is still supported for legacy reasons.
+//
+
+es.join = function (str) {
+
+ //legacy api
+ if('function' === typeof str)
+ return es.wait(str)
+
var stream = new Stream()
- var body = ''
- stream.readable = true
- stream.writable = true
- stream.write = function (data) { body += data; return true }
+ var first = true
+ stream.readable = stream.writable = true
+ stream.write = function (data) {
+ if(!first)
+ stream.emit('data', str)
+ first = false
+ stream.emit('data', data)
+ return true
+ }
stream.end = function (data) {
if(data)
- body += data
- if(body) stream.emit('data', body.split(from).join(to))
- stream.emit('end')
+ this.write(data)
+ this.emit('end')
}
return stream
-}
+}
+
//
-// join.
+// wait. callback when 'end' is emitted, with all chunks appended as string.
//
-es.join = function (callback) {
+es.wait = function (callback) {
var stream = new Stream()
var body = ''
stream.readable = true
Please sign in to comment.
Something went wrong with that request. Please try again.