Skip to content

Commit

Permalink
[Refactor] reject earlier when the URL is not a string
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 16, 2024
1 parent 240fc6e commit f2d9797
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var jsonp = require('jsonp');

module.exports = function getJSON(url, callback) {
return new Promise(function (resolve, reject) {
if (typeof url !== 'string') {
throw new TypeError('`url` is not a string');
}

jsonp(url, function (error, body) {
if (error) {
reject(error);
Expand Down
4 changes: 4 additions & 0 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var internalGetJSON = function _getJSON(url, callback) {

module.exports = function getJSON(url, callback) {
return new Promise(function (resolve, reject) {
if (typeof url !== 'string') {
throw new TypeError('`url` is not a string');
}

internalGetJSON(url, function (error, body) {
if (error) {
reject(error);
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
"@ljharb/eslint-config": "^21.1.1",
"aud": "^2.0.4",
"auto-changelog": "^2.4.0",
"es-value-fixtures": "^1.4.2",
"es5-shim": "^4.6.7",
"es6-shim": "^0.35.8",
"eslint": "=8.8.0",
"npmignore": "^0.3.1",
"tape": "^5.8.0"
"object-inspect": "^1.13.1",
"tape": "^5.8.1"
},
"testling": {
"files": "test/index.js"
Expand Down
33 changes: 30 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,46 @@ require('es5-shim');
require('es6-shim');

var test = require('tape');
var json = require('../');
var v = require('es-value-fixtures');
var inspect = require('object-inspect');

var getJSON = require('../');

var rejects = function assertRejects(fn, expected, msg, extra) {
var t = this;
/* eslint no-invalid-this: 0 */
return new Promise(function (resolve) { resolve(fn()); }).then(
function () {
throw new SyntaxError('expected promise to reject; it fulfilled');
},
function (err) {
t['throws'](function () { throw err; }, expected, msg, extra);
}
);
};

test('rejects on a non-string', function (t) {
return Promise.all(v.nonStrings.map(function (nonString) {
return t.assertion(
rejects,
function () { return getJSON(nonString); },
TypeError,
inspect(nonString) + ' is not a string'
);
}));
});

test('Get IP from JSONTest API', function (t) {
t.plan(2);

json('http://ip.jsontest.com/', function (error, body) {
getJSON('http://ip.jsontest.com/', function (error, body) {
t.error(error);
t.ok(body.ip);
});
});

test('Get IP from JSONTest API (with Promise)', function (t) {
return json('http://ip.jsontest.com/').then(function (body) {
return getJSON('http://ip.jsontest.com/').then(function (body) {
t.ok(body.ip);
})['catch'](function (error) {
t.ok(error);
Expand Down

0 comments on commit f2d9797

Please sign in to comment.