Skip to content

Commit 2035bb8

Browse files
committed
ajax: functional style predicates
1 parent 60f8d78 commit 2035bb8

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

ajax-test.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,16 @@ if(typeof XDomainRequest === 'undefined') {
114114

115115
// CORS simple requests: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests
116116
var isSimpleRequest = true, restore;
117-
var simpleMethods = ['GET', 'POST', 'HEAD'];
118-
var simpleHeaders = [
119-
'Accept','Accept-Language','Content-Language','Content-Type',
117+
var isSimpleMethod = makePredicateContains(['GET', 'POST', 'HEAD']);
118+
var isSimpleHeader = makePredicateContains([
119+
'Accept', 'Accept-Language', 'Content-Language', 'Content-Type',
120120
'DPR', 'Downlink', 'Save-Data', 'Viewport-Width', 'Width'
121-
];
122-
var simpleContentType = 'application/x-www-form-urlencoded';
121+
]);
122+
var isSimpleContentType = makePredicateContains(['application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain']);
123123

124124
restore = makeFixture(function () {
125125
this.open = function (type, url) {
126-
if (simpleMethods.indexOf(type) === -1){
126+
if (!isSimpleMethod(type)){
127127
isSimpleRequest = false;
128128
}
129129
};
@@ -137,10 +137,10 @@ if(typeof XDomainRequest === 'undefined') {
137137
};
138138

139139
this.setRequestHeader = function (header, value) {
140-
if (header === "Content-Type" && value !== simpleContentType){
140+
if (header === "Content-Type" && !isSimpleHeader(value)){
141141
isSimpleRequest = false;
142142
}
143-
if (simpleHeaders.indexOf(header) === -1){
143+
if (isSimpleContentType(header)){
144144
isSimpleRequest = false;
145145
}
146146
response[header] = value;
@@ -241,3 +241,11 @@ if (__dirname !== '/') {
241241
});
242242
});
243243
}
244+
245+
246+
// A helper to make a predicate for a given array that checks whether it contains a given value:
247+
function makePredicateContains(arr){
248+
return function(val){
249+
return arr.indexOf(val) !== -1;
250+
}
251+
}

0 commit comments

Comments
 (0)