// 'match' - Promise extensions
//
// promise.match(onsuccess, onerror)
//
// Matches eventual list result onto function arguments,
// otherwise works same as 'then' (promise function itself)

'use strict';

var match      = require('es5-ext/function/#/match')
  , callable   = require('es5-ext/object/valid-callable')
  , isCallable = require('es5-ext/object/is-callable')
  , isPromise  = require('../../is-promise')
  , deferred   = require('../../deferred');

deferred.extend('match', function (win, fail) {
	var def;
	((win == null) || callable(win));
	if (!win && (fail == null)) {
		return this;
	}
	if (!this.pending) {
		this.pending = [];
	}
	def = deferred();
	this.pending.push('match', [win, fail, def.resolve]);
	return def.promise;
}, function (win, fail, resolve) {
	var cb, value;
	cb = this.failed ? fail : win;
	if (cb == null) {
		resolve(this);
	}
	if (isCallable(cb)) {
		if (isPromise(cb)) {
			if (cb.resolved) {
				resolve(cb.value);
			} else {
				cb.end(resolve, resolve);
			}
			return;
		}
		if (!this.failed) {
			cb = match.call(cb);
		}
		try {
			value = cb(this.value);
		} catch (e) {
			value = e;
		}
		resolve(value);
	} else {
		resolve(cb);
	}
}, function (win, fail) {
	var cb, value;
	cb = this.failed ? fail : win;
	if (cb == null) {
		return this;
	}
	if (isCallable(cb)) {
		if (isPromise(cb)) return cb;
		if (!this.failed) cb = match.call(cb);
		try {
			value = cb(this.value);
		} catch (e) {
			value = e;
		}
		return deferred(value);
	}
	return deferred(cb);
});

@@ -209,14 +209,14 @@
 var 
-match 
+spread
     
@@ -246,21 +246,22 @@
 ction/#/
-match
+spread
 ')%0A  , c
@@ -1042,29 +1042,30 @@
 ) %7B%0A%09%09%09cb = 
-match
+spread
 .call(cb);%0A%09
@@ -1373,21 +1373,22 @@
 d) cb = 
-match
+spread
 .call(cb

Update up to changes in es5-ext