Comparing version 0.7.12 to 0.7.13
@@ -8,3 +8,6 @@ /* | ||
/* | ||
* Blob | ||
* Blob - Immutable data object. Contains a property bag as well as a private result property. | ||
* Any properties in the property bag are merged, while result is concatenated. Result can be | ||
* Buffer or String. Immutability is necessary due to Blobs being able to be parallel | ||
* processed. | ||
* | ||
@@ -15,4 +18,5 @@ * Loosely based on W3C Blob: | ||
* | ||
* @param parts {Buffer|String|Blob|Array} Create new Blob from String/Blob or Array of String/Blob. | ||
* @param parts {Buffer|String|Blob|Array} Create new Blob from Buffer/String/Blob or Array of Buffer/String/Blob. | ||
*/ | ||
var Blob = exports.Blob = function Blob(parts, properties) { | ||
@@ -22,2 +26,8 @@ parts = typeof parts === 'undefined' ? [] : Array.prototype.concat(parts); | ||
function mergeProps(object, props) { | ||
Object.keys(props).forEach(function(prop) { | ||
object[prop] = props[prop]; | ||
}); | ||
} | ||
var result = parts.length ? parts.shift() : '', | ||
@@ -27,12 +37,4 @@ props = {}, | ||
function getProps(part) { | ||
if (part instanceof Blob) { | ||
Object.keys(part).forEach(function(attr) { | ||
props[attr] = part[attr]; | ||
}); | ||
} | ||
} | ||
getProps(result); | ||
if (result instanceof Blob) { | ||
mergeProps(props, result); | ||
result = result.result; | ||
@@ -42,17 +44,22 @@ } | ||
parts.forEach(function(part) { | ||
result += part instanceof Blob ? part.result : part; | ||
getProps(part); | ||
if (part instanceof Blob) { | ||
mergeProps(props, part); | ||
result += part.result; | ||
} else { | ||
result += part; | ||
} | ||
}); | ||
Object.keys(properties).forEach(function(attr) { | ||
props[attr] = properties[attr]; | ||
}); | ||
mergeProps(props, properties); | ||
Object.keys(props).forEach(function(attr) { | ||
if (attr !== 'result') { | ||
Object.defineProperty(self, attr, {enumerable: true, value: props[attr]}); | ||
// Define immutable properties | ||
Object.keys(props).forEach(function(prop) { | ||
if (prop !== 'result') { | ||
Object.defineProperty(self, prop, {enumerable: true, value: props[prop]}); | ||
} | ||
}); | ||
Object.defineProperty(this, 'result', {value: result}); | ||
// v8 performance of seal is not good :( | ||
// http://jsperf.com/freeze-vs-seal-vs-normal/3 | ||
Object.seal(this); | ||
}; | ||
@@ -143,2 +150,3 @@ | ||
}, | ||
client: function(name, blob, encoding, callback) { | ||
@@ -145,0 +153,0 @@ localStorage[name] = blob.result; |
@@ -10,3 +10,3 @@ /* | ||
// Zip two arrays together ala Python | ||
// Zip two arrays together a la Python | ||
function zip(arr1, arr2) { | ||
@@ -49,7 +49,12 @@ var zipped = []; | ||
Queue.prototype._dispatch = function(name, options, blobs, done) { | ||
var task = this._registry.task(name); | ||
var task = this._registry.task(name), | ||
types = { // Allow task type to be inferred based on task params | ||
2: 'append', | ||
3: 'map', | ||
4: 'reduce' | ||
}, | ||
type = task.type ? task.type : types[task.length]; | ||
// Task parameters determine how blobs are processed | ||
switch (task.length) { | ||
case 2: // Add blobs to queue | ||
switch (type) { | ||
case 'append': // Concats new blobs to existing queue | ||
async.map(arrayize(options), task.bind(this), function(err, results) { | ||
@@ -60,15 +65,17 @@ done(err, blobs.concat(results)); | ||
case 3: | ||
if (task.type === 'collect') { // Task can look at all blobs at once | ||
task.call(this, options, blobs, done); | ||
} else if (task.type === 'slice') { // Select up to options.length blobs | ||
async.map(zip(arrayize(options), blobs), (function(arr, cb) { | ||
task.call(this, arr[0], arr[1], cb); | ||
}).bind(this), done); | ||
} else { // Transform blob on a per task basis | ||
async.map(blobs, task.bind(this, options), done); | ||
} | ||
case 'collect': // Task can inspect entire queue | ||
task.call(this, options, blobs, done); | ||
break; | ||
case 4: // Reduce blobs operating on a per task basis | ||
case 'slice': // Slice options.length blobs from queue | ||
async.map(zip(arrayize(options), blobs), (function(arr, cb) { | ||
task.call(this, arr[0], arr[1], cb); | ||
}).bind(this), done); | ||
break; | ||
case 'map': // Task transforms one blob at a time | ||
async.map(blobs, task.bind(this, options), done); | ||
break; | ||
case 'reduce': // Merges blobs from left to right | ||
async.reduce(blobs, new Blob(), task.bind(this, options), function(err, results) { | ||
@@ -78,2 +85,5 @@ done(err, [results]); | ||
break; | ||
default: | ||
throw new Error('Task "' + name + '" has unknown type. Add a type property to the task function.'); | ||
} | ||
@@ -80,0 +90,0 @@ }; |
{ | ||
"name": "gear", | ||
"version": "0.7.12", | ||
"description": "Gear.js - Task-based build system.", | ||
"version": "0.7.13", | ||
"description": "Gear.js - Build System for Node.js and the Browser", | ||
"author": "Stephen Murphy <stephen@hypernaut.com>", | ||
@@ -11,2 +11,5 @@ "keywords": ["task", "build"], | ||
"main": "index", | ||
"scripts": { | ||
"test": "mocha --require should" | ||
}, | ||
"dependencies": { | ||
@@ -13,0 +16,0 @@ "async": "0.1.x", |
@@ -30,3 +30,15 @@ var Blob = require('../lib/blob').Blob, | ||
}); | ||
it('should merge properties', function() { | ||
var res = new Blob([ | ||
new Blob('abc', {name: 'foo', b1: true, arr: [1], result: 'xyz'}), | ||
new Blob('def', {name: 'bar', b2: false, arr: [2]}) | ||
]); | ||
res.result.should.equal('abcdef'); | ||
res.name.should.equal('bar'); | ||
res.b1.should.equal(true); | ||
res.b2.should.equal(false); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
553685
2544