PageSourceSearch

https://f1000research.com/1789640493630/js/shared_scripts/helpers.js

js f1000research.com collected 2026-09-24 08:34:57 UTC 3,350 bytes, 130 lines download raw bytes

1'use strict';
2
3/*!
4 * A global namespace for F1000 JavaScript
5 * Should eventually be in a separate file
6 */
7
8var F1000 = F1000 || {};
9
10if (!Object.assign) {
11  Object.defineProperty(Object, 'assign', {
12    enumerable: false,
13    configurable: true,
14    writable: true,
15    value: function(target) {
16      'use strict';
17      if (target === undefined || target === null) {
18        throw new TypeError('Cannot convert first argument to object');
19      }
20
21      var to = Object(target);
22      for (var i = 1; i < arguments.length; i++) {
23        var nextSource = arguments[i];
24        if (nextSource === undefined || nextSource === null) {
25          continue;
26        }
27        nextSource = Object(nextSource);
28
29        var keysArray = Object.keys(Object(nextSource));
30        for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
31          var nextKey = keysArray[nextIndex];
32          var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
33          if (desc !== undefined && desc.enumerable) {
34            to[nextKey] = nextSource[nextKey];
35          }
36        }
37      }
38      return to;
39    }
40  });
41}
42
43
44/*!
45 * Common Helper functions
46 */
47
48F1000._ = {
49  find: function($elem, selector) {
50    return F1000._.toArray($elem.querySelectorAll(selector));
51  },
52
53  toArray: function(nodeList) {
54    return Array.prototype.slice.call(nodeList);
55  },
56
57  _addEventListener: function(type, fn) {
58    return function(element) {
59        element.addEventListener(type, fn);
60        return element;
61    }
62  },
63
64  addEventListener: function(elements, type, fn) {
65    return elements.map(F1000._._addEventListener(type, fn));
66  },
67
68  _removeEventListener: function(type, fn) {
69    return function(element) {
70        element.removeEventListener(type, fn);
71        return element;
72    }
73  },
74
75  removeEventListener: function(elements, type, fn) {
76    return elements.map(F1000._._removeEventListener(type, fn));
77  },
78
79  applyStyles: function(styles) {
80    return function(element) {
81      for (var key in styles) {
82        if(styles.hasOwnProperty(key)) {
83          element.style[key] = styles[key];
84        }
85      }
86
87      return element;
88    }
89  },
90
91  css: function(elements, styles) {
92    if(Array.isArray(elements))
93      return elements.map(this.applyStyles(styles));
94
95    return this.applyStyles(styles)(elements);
96  },
97
98  getDirectChildrenWithClassName: function(element, className) {
99    return Array.prototype.filter.call(element.childNodes, function(childNode) {
100      return childNode.classList && childNode.classList.contains(className);
101    });
102  },
103
104
105  /**
106   * Deep diff between two object, using lodash
107   * @param  {Object} object Object compared
108   * @param  {Object} base   Object to compare with
109   * @return {Object}        Return a new object who represent the diff
110   */
111  difference: function(object, base) {
112    return _.transform(object, function(result, value, key) {
113      if (!_.isEqual(value, base[key])) {
114        result[key] = _.isObject(value) && _.isObject(base[key]) ? F1000._.difference(value, base[key]) : value;
115      }
116    });
117  },
118
119  /**
120   * Get a string that is guaranteed to be unique
121   * @return {String} The unique ID
122   */
123  getUniqueId: function() {
124    if(!window.__uniqueCounter) {
125      window.__uniqueCounter = 0;
126    }
127
128    return '__' + (++window.__uniqueCounter) + '__';
129  },
130}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.