-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.js
52 lines (44 loc) · 1.08 KB
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
} else {
root["mu-create/transform"] = factory();
}
})(this, function () {
var slice = Array.prototype.slice;
var toString = Object.prototype.toString
function value(key) {
return {
"key": key,
"value": this[key]
};
}
function transpose(keys) {
return keys.map(value, this);
}
return function (data) {
var type = toString.call(data);
var transformed;
if (type === "[object Arguments]") {
return {
"key": "[object Array]",
"value": slice.call(data)
};
}
if (type === "[object Object]") {
if (data.hasOwnProperty("key")) {
return data;
}
transformed = transpose.call(data, Object.keys(data));
if (transformed.length) {
return transformed;
}
}
return {
"key": type,
"value": data
};
}
});