xonecas.io
142
St

startsInvasion / files

startsInvasion / libs / node_modules / reqwest / build / valentine / valentine.js

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