Skip to content

Commit

Permalink
fix: perferred as option instaed of array
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Aug 31, 2024
1 parent 8f58d74 commit fc79a96
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Returns the most preferred encoding from the client.

Returns the most preferred encoding from a list of available encodings.

##### encoding(availableEncodings, preferred)
##### encoding(availableEncodings, { preferred })

Returns the most preferred encoding from a list of available encodings, while prioritizing based on `preferred` array between same-quality encodings.

Expand All @@ -185,7 +185,7 @@ Returns an array of preferred encodings ordered by the client preference.
Returns an array of preferred encodings ordered by priority from a list of
available encodings.

##### encodings(availableEncodings, preferred)
##### encodings(availableEncodings, { preferred })

Returns an array of preferred encodings ordered by priority from a list of
available encodings, while prioritizing based on `preferred` array between same-quality encodings.
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ Negotiator.prototype.charsets = function charsets(available) {
return preferredCharsets(this.request.headers['accept-charset'], available);
};

Negotiator.prototype.encoding = function encoding(available, preferred) {
var set = this.encodings(available, preferred);
Negotiator.prototype.encoding = function encoding(available, opts) {
var set = this.encodings(available, opts);
return set && set[0];
};

Negotiator.prototype.encodings = function encodings(available, preferred) {
Negotiator.prototype.encodings = function encodings(available, { preferred } = {}) {
return preferredEncodings(this.request.headers['accept-encoding'], available, preferred);
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"scripts": {
"lint": "eslint .",
"test": "mocha --reporter spec --check-leaks --bail test/",
"test:debug": "mocha --reporter spec --check-leaks --inspect --inspect-brk test/",
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
"test-cov": "nyc --reporter=html --reporter=text npm test"
}
Expand Down
20 changes: 10 additions & 10 deletions test/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ describe('negotiator.encoding(array)', function () {
})

it('should return developer-preferred encodings', function () {
assert.strictEqual(this.negotiator.encoding(['gzip', 'deflate'], ['deflate']), 'deflate')
assert.strictEqual(this.negotiator.encoding(['deflate', 'gzip'], ['deflate']), 'deflate')
assert.strictEqual(this.negotiator.encoding(['gzip', 'deflate'], ['gzip']), 'gzip')
assert.strictEqual(this.negotiator.encoding(['deflate', 'gzip'], ['gzip']), 'gzip')
assert.strictEqual(this.negotiator.encoding(['gzip'], ['gzip']), 'gzip')
assert.strictEqual(this.negotiator.encoding(['gzip', 'deflate'], { preferred: ['deflate'] }), 'deflate')
assert.strictEqual(this.negotiator.encoding(['deflate', 'gzip'], { preferred: ['deflate'] }), 'deflate')
assert.strictEqual(this.negotiator.encoding(['gzip', 'deflate'], { preferred: ['gzip'] }), 'gzip')
assert.strictEqual(this.negotiator.encoding(['deflate', 'gzip'], { preferred: ['gzip'] }), 'gzip')
assert.strictEqual(this.negotiator.encoding(['gzip'], { preferred: ['gzip'] }), 'gzip')
})
})

Expand Down Expand Up @@ -419,11 +419,11 @@ describe('negotiator.encodings(array)', function () {
})

it('should return developer-preferred encodings', function () {
assert.deepEqual(this.negotiator.encodings(['gzip', 'deflate'], ['deflate']), ['deflate', 'gzip'])
assert.deepEqual(this.negotiator.encodings(['deflate', 'gzip'], ['deflate']), ['deflate', 'gzip'])
assert.deepEqual(this.negotiator.encodings(['gzip', 'deflate'], ['gzip']), ['gzip', 'deflate'])
assert.deepEqual(this.negotiator.encodings(['deflate', 'gzip'], ['gzip']), ['gzip', 'deflate'])
assert.deepEqual(this.negotiator.encodings(['gzip'], ['gzip']), ['gzip'])
assert.deepEqual(this.negotiator.encodings(['gzip', 'deflate'], { preferred: ['deflate'] }), ['deflate', 'gzip'])
assert.deepEqual(this.negotiator.encodings(['deflate', 'gzip'], { preferred: ['deflate'] }), ['deflate', 'gzip'])
assert.deepEqual(this.negotiator.encodings(['gzip', 'deflate'], { preferred: ['gzip'] }), ['gzip', 'deflate'])
assert.deepEqual(this.negotiator.encodings(['deflate', 'gzip'], { preferred: ['gzip'] }), ['gzip', 'deflate'])
assert.deepEqual(this.negotiator.encodings(['gzip'], { preferred: ['gzip'] }), ['gzip'])
})
})

Expand Down

0 comments on commit fc79a96

Please sign in to comment.