z-core
Advanced tools
Comparing version 0.5.0 to 0.5.1
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
var Promise, init, isArray, isPrimitive, keys, makeZ, object, objectCreate, overrides, pairs, proc, resolveAll, resolveCompletely, tools, values, | ||
var Promise, init, isArray, isPrimitive, keys, makeZ, object, objectCreate, pairs, proc, resolveAll, resolveCompletely, tools, values, | ||
__slice = [].slice; | ||
@@ -12,18 +12,19 @@ | ||
resolveCompletely = function(unresolved) { | ||
resolveCompletely = function(unresolved, depth) { | ||
return resolveAll([unresolved]).then(function(_arg) { | ||
var resolved, unresolvedKeys, unresolvedValues; | ||
var resolved, unresolvedValues; | ||
resolved = _arg[0]; | ||
if ((resolved == null) || isPrimitive(resolved)) { | ||
if (depth <= 0 || (resolved == null) || isPrimitive(resolved)) { | ||
return resolved; | ||
} | ||
if (isArray(resolved)) { | ||
return resolveAll(resolved.map(resolveCompletely)); | ||
return resolveAll(resolved.map(function(x) { | ||
return resolveCompletely(x, depth - 1); | ||
})); | ||
} | ||
unresolvedKeys = resolveAll(keys(resolved)); | ||
unresolvedValues = resolveAll(values(resolved).map(resolveCompletely)); | ||
return resolveAll([unresolvedKeys, unresolvedValues]).then(function(_arg1) { | ||
var resolvedKeys, resolvedValues; | ||
resolvedKeys = _arg1[0], resolvedValues = _arg1[1]; | ||
return object(resolvedKeys, resolvedValues); | ||
unresolvedValues = resolveAll(values(resolved).map(function(x) { | ||
return resolveCompletely(x, depth - 1); | ||
})); | ||
return unresolvedValues.then(function(resolvedValues) { | ||
return object(keys(resolved), resolvedValues); | ||
}); | ||
@@ -33,36 +34,48 @@ }); | ||
overrides = ['then']; | ||
init = function() { | ||
var Z, mixedIn; | ||
init = function(defaultConf) { | ||
var Z, mixedIn, mixinObj, updateMixinObj; | ||
mixedIn = {}; | ||
Z = function(obj) { | ||
var overrideLayer, resolvedObject, resultingPromise; | ||
resolvedObject = resolveCompletely(obj); | ||
overrideLayer = objectCreate(resolvedObject); | ||
resultingPromise = objectCreate(overrideLayer); | ||
overrides.forEach(function(name) { | ||
return overrideLayer[name] = function() { | ||
var args; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
return Z(resolvedObject[name].apply(this, args)); | ||
}; | ||
}); | ||
pairs(mixedIn).forEach(function(_arg) { | ||
mixinObj = {}; | ||
updateMixinObj = function() { | ||
return pairs(mixedIn).forEach(function(_arg) { | ||
var func, name; | ||
name = _arg[0], func = _arg[1]; | ||
return resultingPromise[name] = function() { | ||
return mixinObj[name] = function() { | ||
var args; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
return resultingPromise.then(function(resolved) { | ||
return func.apply({ | ||
value: resolved | ||
}, args); | ||
return this.then(function(resolved) { | ||
return resolveCompletely(args, 1).then(function(args) { | ||
return func.apply({ | ||
value: resolved | ||
}, args); | ||
}); | ||
}); | ||
}; | ||
}); | ||
}; | ||
Z = function(obj, conf) { | ||
var key, overrideLayer, resolvedObject, resultingPromise, value, _ref; | ||
conf = (_ref = conf != null ? conf : defaultConf) != null ? _ref : {}; | ||
if (typeof conf.depth === 'undefined') { | ||
conf.depth = 1; | ||
} | ||
if (conf.depth === null) { | ||
conf.depth = 1000000; | ||
} | ||
resolvedObject = resolveCompletely(obj, conf.depth); | ||
overrideLayer = objectCreate(resolvedObject); | ||
resultingPromise = objectCreate(overrideLayer); | ||
overrideLayer.then = function() { | ||
var args; | ||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
return Z(resolvedObject.then.apply(resolvedObject, args)); | ||
}; | ||
for (key in mixinObj) { | ||
value = mixinObj[key]; | ||
resultingPromise[key] = value; | ||
} | ||
return resultingPromise; | ||
}; | ||
Z.mixin = proc(function(hash) { | ||
return pairs(hash).forEach(function(_arg) { | ||
pairs(hash).forEach(function(_arg) { | ||
var func, name, oldOne; | ||
@@ -82,2 +95,3 @@ name = _arg[0], func = _arg[1]; | ||
}); | ||
return updateMixinObj(); | ||
}); | ||
@@ -84,0 +98,0 @@ Z.bindSync = function(func, context) { |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"author": "Jakob Mattsson <jakob.mattsson@gmail.com> (jakobmattsson.se)", | ||
@@ -68,2 +68,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"browserify": "^3.30.2", | ||
"bucketful": "^0.15.1", | ||
@@ -75,3 +76,3 @@ "coffeeify": "^0.6.0", | ||
"mocha-term-cov-reporter": "^0.2.0", | ||
"chai-as-promised": "^4.1.0", | ||
"chai-as-promised": "^3.3.1", | ||
"mocha-as-promised": "^2.0.0", | ||
@@ -78,0 +79,0 @@ "coffee-script": "^1.7.1" |
# Z [![Build Status](https://secure.travis-ci.org/jakobmattsson/z-core.png)](http://travis-ci.org/jakobmattsson/z-core) | ||
Utility library for JavaScript promises | ||
### Installation | ||
Use npm: `npm install z-core` | ||
Or bower: (not uploaded yet) | ||
Or download it manually from the `dist` folder of this repo. | ||
### Wrapping functions to accept promises as parameters and return promises | ||
Use `bindSync` to create promise-friendly functions from sync functions. | ||
var pmin = Z.bindSync(Math.min); | ||
// It can still be called with regular values | ||
pmin(10, 5).then(function(minValue) { | ||
console.log(minValue); // 5 | ||
}); | ||
// But is can also be called with promises | ||
var promise = returnsTheValue2AsPromise(); | ||
pmin(promise, 5).then(function(minValue) { | ||
console.log(minValue); // 2 | ||
}); | ||
Use `bindAsync` to create promise-friendly functions from async functions. | ||
var postPromisedJSON = Z.bindAsync(postJSON); | ||
var agePromise = returnsTheValue28AsPromise(); | ||
// Note that it's called with a mix of regular values an promises | ||
postPromisedJSON('/people', { name: 'Jakob', age: agePromise }).then(function(res) { | ||
console.log(res); // the result of the request | ||
}) | ||
### Augmenting promises | ||
Extend the promises returned by Z using mixins. | ||
Z.mixin({ | ||
get: function(prop) { | ||
return this.value[prop]; | ||
}, | ||
toLowerCase: function() { | ||
return this.value.toLowerCase(); | ||
}, | ||
first: function(n) { | ||
if (n == null) { | ||
n = 1; | ||
} | ||
return this.value.slice(0, n); | ||
}, | ||
log: function() { | ||
console.log(this.value); | ||
} | ||
}); | ||
var getPromisedJSON = Z.bindAsync(getJSON); | ||
var firstThreeInName = getPromisedJSON('/cookies/123').get('name').toLowerCase().first(3); | ||
firstThreeInName.log(); | ||
### Use a prepackaged version of Z | ||
There are several mixin packages available for your convenience: | ||
* [Builtins](https://github.com/jakobmattsson/z-builtins) | ||
* [Underscore](https://github.com/jakobmattsson/z-underscore) | ||
* more to come... | ||
And even bundles where certain set of mixins have already been applied: | ||
* [z-std-pack](https://github.com/jakobmattsson/z-std-pack): Z, builtins and underscore bundled together. | ||
### Additional resources | ||
[Slides from 2014-02-25 presentation on Z](https://speakerdeck.com/jakobmattsson/how-to-star-actually-star-use-promises-in-javascript) | ||
[Code from the above presentation](https://github.com/jakobmattsson/z-presentation) |
12815
217
93
10