9e937aa416a6a87240896c33ccafbc0d71c30730
[yaffs-website] / node_modules / video.js / es5 / utils / obj.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
6
7 exports.each = each;
8 exports.reduce = reduce;
9 exports.assign = assign;
10 exports.isObject = isObject;
11 exports.isPlain = isPlain;
12 /**
13  * @file obj.js
14  * @module obj
15  */
16
17 /**
18  * @callback obj:EachCallback
19  *
20  * @param {Mixed} value
21  *        The current key for the object that is being iterated over.
22  *
23  * @param {string} key
24  *        The current key-value for object that is being iterated over
25  */
26
27 /**
28  * @callback obj:ReduceCallback
29  *
30  * @param {Mixed} accum
31  *        The value that is accumulating over the reduce loop.
32  *
33  * @param {Mixed} value
34  *        The current key for the object that is being iterated over.
35  *
36  * @param {string} key
37  *        The current key-value for object that is being iterated over
38  *
39  * @return {Mixed}
40  *         The new accumulated value.
41  */
42 var toString = Object.prototype.toString;
43
44 /**
45  * Get the keys of an Object
46  *
47  * @param {Object}
48  *        The Object to get the keys from
49  *
50  * @return {string[]}
51  *         An array of the keys from the object. Returns an empty array if the
52  *         object passed in was invalid or had no keys.
53  *
54  * @private
55  */
56 var keys = function keys(object) {
57   return isObject(object) ? Object.keys(object) : [];
58 };
59
60 /**
61  * Array-like iteration for objects.
62  *
63  * @param {Object} object
64  *        The object to iterate over
65  *
66  * @param {obj:EachCallback} fn
67  *        The callback function which is called for each key in the object.
68  */
69 function each(object, fn) {
70   keys(object).forEach(function (key) {
71     return fn(object[key], key);
72   });
73 }
74
75 /**
76  * Array-like reduce for objects.
77  *
78  * @param {Object} object
79  *        The Object that you want to reduce.
80  *
81  * @param {Function} fn
82  *         A callback function which is called for each key in the object. It
83  *         receives the accumulated value and the per-iteration value and key
84  *         as arguments.
85  *
86  * @param {Mixed} [initial = 0]
87  *        Starting value
88  *
89  * @return {Mixed}
90  *         The final accumulated value.
91  */
92 function reduce(object, fn) {
93   var initial = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
94
95   return keys(object).reduce(function (accum, key) {
96     return fn(accum, object[key], key);
97   }, initial);
98 }
99
100 /**
101  * Object.assign-style object shallow merge/extend.
102  *
103  * @param  {Object} target
104  * @param  {Object} ...sources
105  * @return {Object}
106  */
107 function assign(target) {
108   for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
109     sources[_key - 1] = arguments[_key];
110   }
111
112   if (Object.assign) {
113     return Object.assign.apply(Object, [target].concat(sources));
114   }
115
116   sources.forEach(function (source) {
117     if (!source) {
118       return;
119     }
120
121     each(source, function (value, key) {
122       target[key] = value;
123     });
124   });
125
126   return target;
127 }
128
129 /**
130  * Returns whether a value is an object of any kind - including DOM nodes,
131  * arrays, regular expressions, etc. Not functions, though.
132  *
133  * This avoids the gotcha where using `typeof` on a `null` value
134  * results in `'object'`.
135  *
136  * @param  {Object} value
137  * @return {Boolean}
138  */
139 function isObject(value) {
140   return !!value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object';
141 }
142
143 /**
144  * Returns whether an object appears to be a "plain" object - that is, a
145  * direct instance of `Object`.
146  *
147  * @param  {Object} value
148  * @return {Boolean}
149  */
150 function isPlain(value) {
151   return isObject(value) && toString.call(value) === '[object Object]' && value.constructor === Object;
152 }