1 | export function bind(fn, thisArg) {
|
2 | return function wrap() {
|
3 | let args = new Array(arguments.length);
|
4 | for (let i = 0; i < args.length; i++) {
|
5 | args[i] = arguments[i];
|
6 | }
|
7 | return fn.apply(thisArg, args);
|
8 | };
|
9 | }
|
10 |
|
11 | /**
|
12 | * Iterate over an Array or an Object invoking a function for each item.
|
13 | *
|
14 | * If `obj` is an Array callback will be called passing
|
15 | * the value, index, and complete array for each item.
|
16 | *
|
17 | * If 'obj' is an Object callback will be called passing
|
18 | * the value, key, and complete object for each property.
|
19 | *
|
20 | * @param {Object|Array} obj The object to iterate
|
21 | * @param {Function} fn The callback to invoke for each item
|
22 | */
|
23 | export function forEach(obj, fn) {
|
24 | // Don't bother if no value provided
|
25 | if (obj === null || typeof obj === 'undefined') {
|
26 | return;
|
27 | }
|
28 |
|
29 | // Force an array if not already something iterable
|
30 | if (typeof obj !== 'object') {
|
31 | /*eslint no-param-reassign:0*/
|
32 | obj = [obj];
|
33 | }
|
34 |
|
35 | if (Array.isArray(obj)) {
|
36 | // Iterate over array values
|
37 | for (var i = 0, l = obj.length; i < l; i++) {
|
38 | fn.call(null, obj[i], i, obj);
|
39 | }
|
40 | } else {
|
41 | // Iterate over object keys
|
42 | for (var key in obj) {
|
43 | if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
44 | fn.call(null, obj[key], key, obj);
|
45 | }
|
46 | }
|
47 | }
|
48 | }
|
49 |
|
50 | /**
|
51 | * Extends object a by mutably adding to it the properties of object b.
|
52 | *
|
53 | * @param {Object} a The object to be extended
|
54 | * @param {Object} b The object to copy properties from
|
55 | * @param {Object} thisArg The object to bind function to
|
56 | * @return {Object} The resulting value of object a
|
57 | */
|
58 | export function extend(a, b, thisArg) {
|
59 | forEach(b, function assignValue(val, key) {
|
60 | if (thisArg && typeof val === 'function') {
|
61 | a[key] = bind(val, thisArg);
|
62 | } else {
|
63 | a[key] = val;
|
64 | }
|
65 | });
|
66 | return a;
|
67 | } |
\ | No newline at end of file |