Completed
Push — master ( 539e21...edbb3f )
by Rafael S.
01:56
created

scripts/polyfills.js   A

Complexity

Total Complexity 17
Complexity/F 4.25

Size

Lines of Code 68
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
wmc 17
eloc 39
c 0
b 0
f 0
nc 272
mnd 3
bc 17
fnc 4
dl 0
loc 68
bpm 4.25
cpm 4.25
noi 4
rs 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
B Object.defineProperty 0 16 8
A Object.getOwnPropertyDescriptor 0 23 4
A 0 1 1
1
;
2
/**
3
 * @fileoverview Polyfills for old browsers.
4
 * @see https://github.com/inexorabletash/polyfill/blob/master/es5.js
5
 * @see https://gist.github.com/jhermsmeier/9a34b06a107bbf5d2c91
6
 */
7
8
// ES 15.2.3.6 Object.defineProperty ( O, P, Attributes )
9
// Partial support for most common case - getters, setters, and values
10
(function() {
11
  if (!Object.defineProperty ||
12
      !(function () { try { Object.defineProperty({}, 'x', {}); return true; } catch (e) { return false; } } ())) {
13
    var orig = Object.defineProperty;
14
    Object.defineProperty = function (o, prop, desc) {
0 ignored issues
show
Compatibility Best Practice introduced by
You are extending the built-in type Object. This may have unintended consequences on other objects using this built-in type. Consider subclassing instead.
Loading history...
15
      // In IE8 try built-in implementation for defining properties on DOM prototypes.
16
      if (orig) { try { return orig(o, prop, desc); } catch (e) {} }
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
17
18
      if (o !== Object(o)) { throw TypeError("Object.defineProperty called on non-object"); }
19
      if (Object.prototype.__defineGetter__ && ('get' in desc)) {
20
        Object.prototype.__defineGetter__.call(o, prop, desc.get);
21
      }
22
      if (Object.prototype.__defineSetter__ && ('set' in desc)) {
23
        Object.prototype.__defineSetter__.call(o, prop, desc.set);
24
      }
25
      if ('value' in desc) {
26
        o[prop] = desc.value;
27
      }
28
      return o;
29
    };
30
  }
31
}());
32
33
// On older versions of IE Object.getOwnPropertyDescriptor can only be
34
// called with DOM elements; Here it is tested against a non-DOM object.
35
// If an error is raised, the method is replaced.
36
// by @rochars based on https://gist.github.com/jhermsmeier/9a34b06a107bbf5d2c91
37
var testObject = {"t":"o"};
38
var replaceGetOwnPropertyDescriptor = false;
39
try {
40
  Object.getOwnPropertyDescriptor(testObject, "t");
41
} catch(err) {
42
  replaceGetOwnPropertyDescriptor = true;
43
}
44
if (replaceGetOwnPropertyDescriptor) {
45
  Object.getOwnPropertyDescriptor = function( object, key ) {
0 ignored issues
show
Compatibility Best Practice introduced by
You are extending the built-in type Object. This may have unintended consequences on other objects using this built-in type. Consider subclassing instead.
Loading history...
46
    
47
    var hasSupport =
48
      typeof object.__lookupGetter__ === 'function' &&
49
      typeof object.__lookupSetter__ === 'function'
50
    
51
    // TODO: How does one determine this?!
52
    var isGetterSetter = !hasSupport ? null :
53
      object.__lookupGetter__( key ) ||
54
      object.__lookupSetter__( key )
55
    
56
    return isGetterSetter != null ? {
0 ignored issues
show
Best Practice introduced by
Comparing isGetterSetter to null using the != operator is not safe. Consider using !== instead.
Loading history...
57
      configurable: true,
58
      enumerable: true,
59
      get: object.__lookupGetter__( key ),
60
      set: object.__lookupSetter__( key )
61
    } : {
62
      configurable: true,
63
      writable: true,
64
      enumerable: true,
65
      value: object[ key ]
66
    }
67
  }
68
}
69