Sync objMap from upstream
Paul O’Shannessy committed
Aug 27, 2013 at 14:43 UTC
688f5051e67ec37c75dacf837e40526544ee5c68
1 file changed
+47
src/vendor/core/objMap.js
new
+47
@@ -0,0 +1,47 @@
1
+/**
2
+ * Copyright 2013 Facebook, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * @providesModule objMap
17
+ */
18
+
19
+"use strict";
20
+
21
+/**
22
+ * For each key/value pair, invokes callback func and constructs a resulting
23
+ * object which contains, for every key in obj, values that are the result of
24
+ * of invoking the function:
25
+ *
26
+ * func(value, key, iteration)
27
+ *
28
+ * @param {?object} obj Object to map keys over
29
+ * @param {function} func Invoked for each key/val pair.
30
+ * @param {?*} context
31
+ * @return {?object} Result of mapping or null if obj is falsey
32
+ */
33
+function objMap(obj, func, context) {
34
+ if (!obj) {
35
+ return null;
36
+ }
37
+ var i = 0;
38
+ var ret = {};
39
+ for (var key in obj) {
40
+ if (obj.hasOwnProperty(key)) {
41
+ ret[key] = func.call(context, obj[key], key, i++);
42
+ }
43
+ }
44
+ return ret;
45
+}
46
+
47
+module.exports = objMap;