Beispiel #1
0
def indexOf(arr, obj, opt_fromIndex):
  if ARRAY_PROTOTYPE_.indexOf:
    #goog.asserts.assert(arr.length != null);
    return ARRAY_PROTOTYPE_.indexOf.call(arr, obj, opt_fromIndex)
  else:
    
    fromIndex = (
                  0
                  if opt_fromIndex == None else
                  (
                    Math.max(0, arr.length + opt_fromIndex)
                    if opt_fromIndex < 0 else
                    opt_fromIndex))
    
    if isString(arr):
      # Array.prototype.indexOf uses === so only strings should be found.
      if not isString(obj) or len(obj) != 1:
        return -1
      return arr.indexOf(obj, fromIndex)
    
    for i in range(fromIndex, len(arr)):
      if (i in arr) and (arr[i] == obj):
        return i
    
    return -1
Beispiel #2
0
def forEach(arr, f, opt_obj):
  #DIFF: goog runs this if-statement at load-time
  
  if ARRAY_PROTOTYPE_.forEach:
    # TODO assert(arr.length != None)
    ARRAY_PROTOTYPE_.forEach.call(arr, f, opt_obj)
  
  else:
    arr2 = (arr.split('') if isString(arr) else arr)
    for i in range(len(arr)):
      if i in arr2:
        f.call(opt_obj, arr2[i], i, arr)
Beispiel #3
0
def map(arr, f, opt_obj):
  #DIFF: goog runs this if-statement at load-time
  
  if ARRAY_PROTOTYPE_.map:
    #TODO goog.asserts.assert(arr.length != null);
    return ARRAY_PROTOTYPE_.map.call(arr, f, opt_obj)
  
  else:
    l = len(arr)
    res = Array(l)
    arr2 = (arr.split('') if isString(arr) else arr)
    for i in range(l):
      if i in arr2:
        res[i] = f.call(opt_obj, arr2[i], i, arr)
      return res
Beispiel #4
0
def filter(arr, f, opt_obj):
  if ARRAY_PROTOTYPE_.filter:
    #goog.asserts.assert(arr.length != null);
    return ARRAY_PROTOTYPE_.filter.call(arr, f, opt_obj)
  else:
    res = []
    resLength = 0
    arr2 = arr.split('') if isString(arr) else arr
    for i in range(len(arr)):
      if i in arr2:
        val = arr2[i]
        if f.call(opt_obj, val, i, arr):
          # Is this better than .push?
          resLength += 1
          res[resLength] = val
    return res