1 | const Streamz = require('streamz');
|
2 | const util = require('util');
|
3 |
|
4 | function expand(convert) {
|
5 | if (!(this instanceof Streamz))
|
6 | return new expand(convert);
|
7 |
|
8 | Streamz.call(this);
|
9 |
|
10 | if (convert == 'uppercase')
|
11 | this.convert = function(d) {
|
12 | return String(d).toUpperCase();
|
13 | };
|
14 |
|
15 | else if (convert == 'lowercase')
|
16 | this.convert = function(d) {
|
17 | return String(d).toLowerCase();
|
18 | };
|
19 |
|
20 | else
|
21 | this.convert = convert;
|
22 | }
|
23 |
|
24 | util.inherits(expand,Streamz);
|
25 |
|
26 | expand.prototype.expand = function(d) {
|
27 | for (let key in d) {
|
28 | const oldKey = key;
|
29 | if (typeof this.convert === 'function')
|
30 | key = this.convert(key);
|
31 |
|
32 | if (key) {
|
33 | if (typeof d[key] === 'object')
|
34 | d[key] = this.expand(d[oldKey]);
|
35 | else
|
36 | d[key] = d[oldKey];
|
37 | if (oldKey !== key)
|
38 | delete d[oldKey];
|
39 | } else
|
40 | delete d[oldKey];
|
41 | }
|
42 | return d;
|
43 | };
|
44 |
|
45 | expand.prototype._fn = function(d) {
|
46 | return this.expand(Object.create(d));
|
47 | };
|
48 |
|
49 | module.exports = expand; |
\ | No newline at end of file |