This repository is currently being migrated. It's locked while the migration is in progress.
forked from keinu/dynamodb-driver
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.js
More file actions
127 lines (108 loc) · 2.42 KB
/
utils.js
File metadata and controls
127 lines (108 loc) · 2.42 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module.exports = (function () {
const itemize = function (param) {
if (param === null) {
return { NULL: true };
} else if (param === "") {
return { NULL: true };
} else if (typeof param === "boolean") {
return { BOOL: param };
} else if (typeof param === "string") {
return { S: param };
} else if (param instanceof Date) {
return { S: param.toString() };
} else if (typeof param !== "object" && !Array.isArray(param) && !isNaN(param)) {
return { N: "" + param };
} else if (Array.isArray(param) && param.length > 0) {
if (param.length === 1 && param[0] === null) {
return { NULL: true };
} else if (typeof param[0] === "string") {
return { SS: param };
} else if (typeof param[0] !== "object" && !isNaN(param[0])) {
return {
NS: param.map(function (n) {
return "" + n;
})
};
} else {
return {
L: param.map(function (value) {
return itemize(value);
})
};
}
} else if (Array.isArray(param) && param.length === 0) {
return { NULL: true };
} else if (param && param.prototype) {
const object = {};
for (const key in param) {
if (param.hasOwnProperty(key)) {
const value = itemize(param[key]);
if (value !== false) {
object[key] = value;
}
}
}
return { M: object };
} else {
const object = {};
for (const key in param) {
const value = itemize(param[key]);
if (value !== false) {
object[key] = value;
}
}
return { M: object };
}
};
const deitemize = function (item) {
if (!item) {
return null;
}
if (item.S) {
return item.S;
}
if (item.N) {
return +item.N;
}
if (item.SS) {
return item.SS;
}
if (item.NS) {
return item.NS.map(function (n) {
return +n;
});
}
if (typeof item.BOOL !== "undefined") {
return item.BOOL ? true : false;
}
if (item.NULL === true) {
return null;
}
if (item.L) {
const array = [];
item.L.forEach(function (lItem) {
array.push(deitemize(lItem));
});
return array;
}
if (item.M) {
const object = {};
Object.keys(item.M).forEach(function (key) {
object[key] = deitemize(item.M[key]);
});
if (Object.keys(object).length > 0) {
return object;
}
return null;
}
const items = {};
Object.keys(item).forEach(function (key) {
items[key] = deitemize(item[key]);
});
return items;
};
return {
itemize: itemize,
deitemize: deitemize
};
})();