Skip to content

Commit

Permalink
feat: Add Object.isThenable
Browse files Browse the repository at this point in the history
Currently Object.isPromise mirrors it 1:1,
but with next major we will also validate its constructor
  • Loading branch information
medikoo committed Mar 16, 2018
1 parent 2682be6 commit 8d5a45c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion object/ensure-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ var safeToString = require("../safe-to-string")
, isPromise = require("./is-promise");

module.exports = function (value) {
if (!isPromise(value)) throw new TypeError(safeToString(value) + " is not a thenable");
if (!isPromise(value)) throw new TypeError(safeToString(value) + " is not a promise");
return value;
};
9 changes: 9 additions & 0 deletions object/ensure-thenable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var safeToString = require("../safe-to-string")
, isThenable = require("./is-thenable");

module.exports = function (value) {
if (!isThenable(value)) throw new TypeError(safeToString(value) + " is not a thenable");
return value;
};
2 changes: 2 additions & 0 deletions object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
ensureNaturalNumberValue: require("./ensure-natural-number-value"),
ensurePlainFunction: require("./ensure-plain-function"),
ensurePromise: require("./ensure-promise"),
ensureThenable: require("./ensure-thenable"),
eq: require("./eq"),
every: require("./every"),
filter: require("./filter"),
Expand All @@ -41,6 +42,7 @@ module.exports = {
isPlainFunction: require("./is-plain-function"),
isPlainObject: require("./is-plain-object"),
isPromise: require("./is-promise"),
isThenable: require("./is-thenable"),
isValue: require("./is-value"),
keyOf: require("./key-of"),
keys: require("./keys"),
Expand Down
8 changes: 2 additions & 6 deletions object/is-promise.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
"use strict";

var isCallable = require("./is-callable")
, isObject = require("./is-object");

module.exports = function (value) {
return isObject(value) && isCallable(value.then);
};
// In next major this check will also confirm on promise constructor
module.exports = require("./is-thenable");
8 changes: 8 additions & 0 deletions object/is-thenable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

var isCallable = require("./is-callable")
, isObject = require("./is-object");

module.exports = function (value) {
return isObject(value) && isCallable(value.then);
};
11 changes: 11 additions & 0 deletions test/object/ensure-thenable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

module.exports = function (t, a) {
// Just sanity checks as proper tests are at isThenable
var thenable = { then: function () {} };

a.throws(function () {
t({});
}, TypeError);
a(t(thenable), thenable);
};
17 changes: 17 additions & 0 deletions test/object/is-thenable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

module.exports = function (t, a) {
var promise;
a(t(), false);
a(t(null), false);
a(t("promise"), false);
a(t({}), false);
a(t(function () {}), false);
a(t({ then: {} }), false);
a(t({ then: function () {} }), true);
promise = function () {};
promise.then = {};
a(t(promise), false);
promise.then = function () {};
a(t(promise), true);
};

0 comments on commit 8d5a45c

Please sign in to comment.