142
St
startsInvasion / files
startsInvasion / libs / node_modules / reqwest / build / valentine / src / valentine.js
8.8 KB
1!function (context) { 2 3 var v = function (a, scope) { 4 return new Valentine(a, scope); 5 }, 6 ap = Array.prototype, 7 op = Object.prototype, 8 slice = ap.slice, 9 nativ = !!('map' in ap), 10 nativ18 = !!('reduce' in ap), 11 trimReplace = /(^\s*|\s*$)/g; 12 13 var iters = { 14 each: nativ ? 15 function (a, fn, scope) { 16 ap.forEach.call(a, fn, scope); 17 } : 18 function (a, fn, scope) { 19 for (var i = 0, l = a.length; i < l; i++) { 20 fn.call(scope, a[i], i, a); 21 } 22 }, 23 map: nativ ? 24 function (a, fn, scope) { 25 return ap.map.call(a, fn, scope); 26 } : 27 function (a, fn, scope) { 28 var r = []; 29 for (var i = 0, l = a.length; i < l; i++) { 30 r[i] = fn.call(scope, a[i], i, a); 31 } 32 return r; 33 }, 34 some: nativ ? 35 function (a, fn, scope) { 36 return a.some(fn, scope); 37 } : 38 function (a, fn, scope) { 39 for (var i = 0, l = a.length; i < l; i++) { 40 if (fn.call(scope, a[i], i, a)) { 41 return true; 42 } 43 } 44 return false; 45 }, 46 every: nativ ? 47 function (a, fn, scope) { 48 return a.every(fn, scope); 49 } : 50 function (a, fn, scope) { 51 for (var i = 0, l = a.length; i < l; i++) { 52 if (!fn.call(scope, a[i], i, a)) { 53 return false; 54 } 55 } 56 return true; 57 }, 58 filter: nativ ? 59 function (a, fn, scope) { 60 return a.filter(fn, scope); 61 } : 62 function (a, fn, scope) { 63 var r = []; 64 for (var i = 0, j = 0, l = a.length; i < l; i++) { 65 if (!fn.call(scope, a[i], i, a)) { 66 continue; 67 } 68 r[j++] = a[i]; 69 } 70 return r; 71 }, 72 indexOf: nativ ? 73 function (a, el, start) { 74 return a.indexOf(el, isFinite(start) ? start : 0); 75 } : 76 function (a, el, start) { 77 start = start || 0; 78 for (var i = 0; i < a.length; i++) { 79 if (a[i] === el) { 80 return i; 81 } 82 } 83 return -1; 84 }, 85 86 lastIndexOf: nativ ? 87 function (a, el, start) { 88 return a.lastIndexOf(el, isFinite(start) ? start : a.length); 89 } : 90 function (a, el, start) { 91 start = start || a.length; 92 start = start >= a.length ? a.length : 93 start < 0 ? a.length + start : start; 94 for (var i = start; i >= 0; --i) { 95 if (a[i] === el) { 96 return i; 97 } 98 } 99 return -1; 100 }, 101 102 reduce: nativ18 ? 103 function (o, i, m, c) { 104 return ap.reduce.call(o, i, m, c); 105 } : 106 function (obj, iterator, memo, context) { 107 var initial = !is.und(memo); 108 !obj && (obj = []); 109 iters.each(obj, function (value, index, list) { 110 if (!initial && index === 0) { 111 memo = value; 112 initial = true; 113 } else { 114 memo = iterator.call(context, memo, value, index, list); 115 } 116 }); 117 if (!initial) { 118 throw new TypeError("Reduce of empty array with no initial value"); 119 } 120 return memo; 121 }, 122 123 reduceRight: nativ18 ? 124 function (o, i, m, c) { 125 return ap.reduceRight.call(o, i, m, c); 126 } : 127 function (ob, i, m, c) { 128 !ob && (ob = []); 129 var reversed = (is.arr(ob) ? ob.slice() : o.toArray(ob)).reverse(); 130 return iters.reduce(reversed, i, m, c); 131 }, 132 133 find: function (obj, iterator, context) { 134 var result; 135 iters.some(obj, function (value, index, list) { 136 if (iterator.call(context, value, index, list)) { 137 result = value; 138 return true; 139 } 140 }); 141 return result; 142 }, 143 144 reject: function (a, fn, scope) { 145 var r = []; 146 for (var i = 0, j = 0, l = a.length; i < l; i++) { 147 if (fn.call(scope, a[i], i, a)) { 148 continue; 149 } 150 r[j++] = a[i]; 151 } 152 return r; 153 }, 154 155 size: function (a) { 156 return o.toArray(a).length; 157 }, 158 159 pluck: function (a, k) { 160 return iters.map(a, function (el) { 161 return el[k]; 162 }); 163 }, 164 165 compact: function (a) { 166 return iters.filter(a, function (value) { 167 return !!value; 168 }); 169 }, 170 171 flatten: function (a) { 172 return iters.reduce(a, function (memo, value) { 173 if (is.arr(value)) { 174 return memo.concat(iters.flatten(value)); 175 } 176 memo[memo.length] = value; 177 return memo; 178 }, []); 179 }, 180 181 uniq: function (ar) { 182 var a = [], i, j; 183 label: 184 for (i = 0; i < ar.length; i++) { 185 for (j = 0; j < a.length; j++) { 186 if (a[j] == ar[i]) { 187 continue label; 188 } 189 } 190 a[a.length] = ar[i]; 191 } 192 return a; 193 } 194 195 }; 196 197 function aug(o, o2) { 198 for (var k in o2) { 199 o[k] = o2[k]; 200 } 201 } 202 203 var is = { 204 fun: function (f) { 205 return typeof f === 'function'; 206 }, 207 208 str: function (s) { 209 return typeof s === 'string'; 210 }, 211 212 ele: function (el) { 213 !!(el && el.nodeType && el.nodeType == 1); 214 }, 215 216 arr: function (ar) { 217 return ar instanceof Array; 218 }, 219 220 num: function (n) { 221 return typeof n === 'number'; 222 }, 223 224 bool: function (b) { 225 return (b === true) || (b === false); 226 }, 227 228 args: function (a) { 229 return !!(a && op.hasOwnProperty.call(a, 'callee')); 230 }, 231 232 emp: function (o) { 233 var i = 0; 234 return is.arr(o) ? o.length === 0 : 235 is.obj(o) ? (function () { 236 for (var k in o) { 237 i++; 238 break; 239 } 240 return (i === 0); 241 }()) : 242 o === ''; 243 }, 244 245 dat: function (d) { 246 return !!(d && d.getTimezoneOffset && d.setUTCFullYear); 247 }, 248 249 reg: function (r) { 250 return !!(r && r.test && r.exec && (r.ignoreCase || r.ignoreCase === false)); 251 }, 252 253 nan: function (n) { 254 return n !== n; 255 }, 256 257 nil: function (o) { 258 return o === null; 259 }, 260 261 und: function (o) { 262 return typeof o === 'undefined'; 263 }, 264 265 obj: function (o) { 266 return o instanceof Object && !is.fun(o) && !is.arr(o); 267 } 268 }; 269 270 var o = { 271 each: function (a, fn, scope) { 272 is.arr(a) ? 273 iters.each(a, fn, scope) : (function () { 274 for (var k in a) { 275 op.hasOwnProperty.call(a, k) && fn.call(scope, k, a[k], a); 276 } 277 }()); 278 }, 279 280 map: function (a, fn, scope) { 281 var r = [], i = 0; 282 return is.arr(a) ? 283 iters.map(a, fn, scope) : !function () { 284 for (var k in a) { 285 op.hasOwnProperty.call(a, k) && (r[i++] = fn.call(scope, k, a[k], a)); 286 } 287 }() && r; 288 }, 289 290 toArray: function (a) { 291 if (!a) { 292 return []; 293 } 294 if (a.toArray) { 295 return a.toArray(); 296 } 297 if (is.arr(a)) { 298 return a; 299 } 300 if (is.args(a)) { 301 return slice.call(a); 302 } 303 return iters.map(a, function (k) { 304 return k; 305 }); 306 }, 307 308 first: function (a) { 309 return a[0]; 310 }, 311 312 last: function (a) { 313 return a[a.length - 1]; 314 }, 315 316 keys: Object.keys ? 317 function (o) { 318 return Object.keys(o); 319 } : 320 function (obj) { 321 var keys = []; 322 for (var key in obj) { 323 op.hasOwnProperty.call(obj, key) && (keys[keys.length] = key); 324 } 325 return keys; 326 }, 327 328 values: function (ob) { 329 return o.map(ob, function (k, v) { 330 return v; 331 }); 332 }, 333 334 extend: function (ob) { 335 o.each(slice.call(arguments, 1), function (source) { 336 for (var prop in source) { 337 !is.und(source[prop]) && (ob[prop] = source[prop]); 338 } 339 }); 340 return ob; 341 }, 342 343 trim: String.prototype.trim ? 344 function (s) { 345 return s.trim(); 346 } : 347 function (s) { 348 return s.replace(trimReplace, ''); 349 }, 350 351 bind: function (scope, fn) { 352 return function () { 353 fn.apply(scope, arguments); 354 }; 355 } 356 357 }; 358 359 aug(v, iters); 360 aug(v, o); 361 v.is = is; 362 363 // love thyself 364 v.v = v; 365 366 // peoples like the object style 367 var Valentine = function (a, scope) { 368 this.val = a; 369 this._scope = scope || null; 370 this._chained = 0; 371 }; 372 373 v.each(v.extend({}, iters, o), function (name, fn) { 374 Valentine.prototype[name] = function () { 375 var a = v.toArray(arguments); 376 a.unshift(this.val); 377 var ret = fn.apply(this._scope, a); 378 this.val = ret; 379 return this._chained ? this : ret; 380 }; 381 }); 382 383 // back compact to underscore (peoples like chaining) 384 Valentine.prototype.chain = function () { 385 this._chained = 1; 386 return this; 387 }; 388 389 Valentine.prototype.value = function () { 390 return this.val; 391 }; 392 393 var old = context.v; 394 v.noConflict = function () { 395 context.v = old; 396 return this; 397 }; 398 399 (typeof module !== 'undefined') && module.exports ? 400 (module.exports = v) : 401 (context['v'] = v); 402 403}(this);