Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14217 anikendra 1
/*
2
 * typeahead.js
3
 * https://github.com/twitter/typeahead.js
4
 * Copyright 2013-2014 Twitter, Inc. and other contributors; Licensed MIT
5
 */
6
 
7
var _ = (function() {
8
  'use strict';
9
 
10
  return {
11
    isMsie: function() {
12
      // from https://github.com/ded/bowser/blob/master/bowser.js
13
      return (/(msie|trident)/i).test(navigator.userAgent) ?
14
        navigator.userAgent.match(/(msie |rv:)(\d+(.\d+)?)/i)[2] : false;
15
    },
16
 
17
    isBlankString: function(str) { return !str || /^\s*$/.test(str); },
18
 
19
    // http://stackoverflow.com/a/6969486
20
    escapeRegExChars: function(str) {
21
      return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
22
    },
23
 
24
    isString: function(obj) { return typeof obj === 'string'; },
25
 
26
    isNumber: function(obj) { return typeof obj === 'number'; },
27
 
28
    isArray: $.isArray,
29
 
30
    isFunction: $.isFunction,
31
 
32
    isObject: $.isPlainObject,
33
 
34
    isUndefined: function(obj) { return typeof obj === 'undefined'; },
35
 
36
    toStr: function toStr(s) {
37
      return (_.isUndefined(s) || s === null) ? '' : s + '';
38
    },
39
 
40
    bind: $.proxy,
41
 
42
    each: function(collection, cb) {
43
      // stupid argument order for jQuery.each
44
      $.each(collection, reverseArgs);
45
 
46
      function reverseArgs(index, value) { return cb(value, index); }
47
    },
48
 
49
    map: $.map,
50
 
51
    filter: $.grep,
52
 
53
    every: function(obj, test) {
54
      var result = true;
55
 
56
      if (!obj) { return result; }
57
 
58
      $.each(obj, function(key, val) {
59
        if (!(result = test.call(null, val, key, obj))) {
60
          return false;
61
        }
62
      });
63
 
64
      return !!result;
65
    },
66
 
67
    some: function(obj, test) {
68
      var result = false;
69
 
70
      if (!obj) { return result; }
71
 
72
      $.each(obj, function(key, val) {
73
        if (result = test.call(null, val, key, obj)) {
74
          return false;
75
        }
76
      });
77
 
78
      return !!result;
79
    },
80
 
81
    mixin: $.extend,
82
 
83
    getUniqueId: (function() {
84
      var counter = 0;
85
      return function() { return counter++; };
86
    })(),
87
 
88
    templatify: function templatify(obj) {
89
      return $.isFunction(obj) ? obj : template;
90
 
91
      function template() { return String(obj); }
92
    },
93
 
94
    defer: function(fn) { setTimeout(fn, 0); },
95
 
96
    debounce: function(func, wait, immediate) {
97
      var timeout, result;
98
 
99
      return function() {
100
        var context = this, args = arguments, later, callNow;
101
 
102
        later = function() {
103
          timeout = null;
104
          if (!immediate) { result = func.apply(context, args); }
105
        };
106
 
107
        callNow = immediate && !timeout;
108
 
109
        clearTimeout(timeout);
110
        timeout = setTimeout(later, wait);
111
 
112
        if (callNow) { result = func.apply(context, args); }
113
 
114
        return result;
115
      };
116
    },
117
 
118
    throttle: function(func, wait) {
119
      var context, args, timeout, result, previous, later;
120
 
121
      previous = 0;
122
      later = function() {
123
        previous = new Date();
124
        timeout = null;
125
        result = func.apply(context, args);
126
      };
127
 
128
      return function() {
129
        var now = new Date(),
130
            remaining = wait - (now - previous);
131
 
132
        context = this;
133
        args = arguments;
134
 
135
        if (remaining <= 0) {
136
          clearTimeout(timeout);
137
          timeout = null;
138
          previous = now;
139
          result = func.apply(context, args);
140
        }
141
 
142
        else if (!timeout) {
143
          timeout = setTimeout(later, remaining);
144
        }
145
 
146
        return result;
147
      };
148
    },
149
 
150
    noop: function() {}
151
  };
152
})();