Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows for first new Response argument to be options #577

Closed
wants to merge 4 commits into from
Closed

Allows for first new Response argument to be options #577

wants to merge 4 commits into from

Conversation

spearmootz
Copy link

@spearmootz spearmootz commented Oct 23, 2017

Fixes #576

var getClassOf = Function.prototype.call.bind(Object.prototype.toString)

var isClassOf = function (instance, className) {
return getClassOf(instance).split(' ')[1] === className + ']'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get away with a simpler check?

function isPlainObject(obj) {
  return obj != null && typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Object]'
}

var options

// if the first argument is options and not body
if (isClassOf(arguments[0], 'Object')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like

function Response(bodyInit, options) {
  if (arguments.length === 1 && isPlainObject(bodyInit)) {
    options = bodyInit
    bodyInit = null
  }
  // ...
}

@@ -603,6 +603,12 @@ suite('Response', function() {

assert.equal(r.headers.get('content-type'), 'text/plain')
})

test('accepts options as first parameter', function () {
var r = new Response({ status: 200 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a non-default status such as 204 to verify that the status was successfully assigned.

@spearmootz spearmootz closed this Oct 27, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Response contructor is incompatible with other browser implementations
2 participants