Skip to content

Commit

Permalink
feat: introduce Object.ensurePromise
Browse files Browse the repository at this point in the history
Allows validation of thenable objects
  • Loading branch information
medikoo committed May 15, 2017
1 parent 27aecc8 commit 46a2f45
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions object/ensure-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

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");
return value;
};
1 change: 1 addition & 0 deletions object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
create: require('./create'),
ensureNaturalNumber: require('./ensure-natural-number'),
ensureNaturalNumberValue: require('./ensure-natural-number-value'),
ensurePromise: require('./ensure-promise'),
eq: require('./eq'),
every: require('./every'),
filter: require('./filter'),
Expand Down
18 changes: 18 additions & 0 deletions test/object/ensure-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

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

0 comments on commit 46a2f45

Please sign in to comment.