mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +03:00
fix(headers): fixed & optimized clear method; (#5507)
This commit is contained in:
+1
-1
@@ -33,7 +33,7 @@ declare class AxiosHeaders {
|
||||
|
||||
delete(header: string | string[], matcher?: AxiosHeaderMatcher): boolean;
|
||||
|
||||
clear(): boolean;
|
||||
clear(matcher?: AxiosHeaderMatcher): boolean;
|
||||
|
||||
normalize(format: boolean): AxiosHeaders;
|
||||
|
||||
|
||||
Vendored
+1
-1
@@ -28,7 +28,7 @@ export class AxiosHeaders {
|
||||
|
||||
delete(header: string | string[], matcher?: AxiosHeaderMatcher): boolean;
|
||||
|
||||
clear(): boolean;
|
||||
clear(matcher?: AxiosHeaderMatcher): boolean;
|
||||
|
||||
normalize(format: boolean): AxiosHeaders;
|
||||
|
||||
|
||||
@@ -174,8 +174,20 @@ class AxiosHeaders {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
clear() {
|
||||
return Object.keys(this).forEach(this.delete.bind(this));
|
||||
clear(matcher) {
|
||||
const keys = Object.keys(this);
|
||||
let i = keys.length;
|
||||
let deleted = false;
|
||||
|
||||
while (i--) {
|
||||
const key = keys[i];
|
||||
if(!matcher || matchHeaderValue(this, this[key], key, matcher)) {
|
||||
delete this[key];
|
||||
deleted = true;
|
||||
}
|
||||
}
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
normalize(format) {
|
||||
|
||||
@@ -236,6 +236,26 @@ describe('AxiosHeaders', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('clear', ()=> {
|
||||
it('should clear all headers', () => {
|
||||
const headers = new AxiosHeaders({x: 1, y:2});
|
||||
|
||||
headers.clear();
|
||||
|
||||
assert.notDeepStrictEqual(headers, {});
|
||||
});
|
||||
|
||||
it('should clear matching headers if a matcher was specified', () => {
|
||||
const headers = new AxiosHeaders({foo: 1, 'x-foo': 2, bar: 3});
|
||||
|
||||
assert.notDeepStrictEqual(headers, {foo: 1, 'x-foo': 2, bar: 3});
|
||||
|
||||
headers.clear(/^x-/);
|
||||
|
||||
assert.notDeepStrictEqual(headers, {foo: 1, bar: 3});
|
||||
});
|
||||
});
|
||||
|
||||
describe('toJSON', function () {
|
||||
it('should return headers object with original headers case', function () {
|
||||
const headers = new AxiosHeaders({
|
||||
|
||||
Reference in New Issue
Block a user