-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.js
179 lines (125 loc) · 6.46 KB
/
demo.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
clog = console.log;
cdir = function ($val) {
console.dir($val, {
depth: null
})
};
const TableJs = require("./bin/TableJs");
// -------------------------------------------------------------------------
// ---[ I ]-----------------------------------------------------------------
// -------------------------------------------------------------------------
clog("-------------------------------------------------------");
console.log(">>>> Initializing a new TableJs: \n\n");
let cameras = new TableJs(
// List of Fields
['Brand', 'Camera', 'Date', 'Format', 'Purpose'],
// Indicating which field compose the keys
['Camera', 'Brand'],
// Table Data
[
[ 'Nikon', 'D3', '2007', 'Full Frame', 'Professional' ],
[ 'Nikon', 'D750', '2014', 'Full Frame', 'Action' ],
[ 'Nikon', 'D800', '2011', 'Full Frame', 'Semi-Professional' ],
[ 'Nikon', 'D810A', '2015', 'Full Frame', 'Astro' ],
[ 'Nikon', 'D7100', '2013', 'APS-C', 'Expert' ],
[ 'Nikon', 'D6', '2020', 'Full Frame', 'Professional' ],
[ 'Canon', '1Ds Mark III', '2007', 'Full Frame', 'Professional' ],
[ 'Canon', '5D Mark II', '2008', 'Full Frame', 'Semi-Professional' ],
[ 'Canon', '60Da', '2012', 'APS-C', 'Astro' ],
[ 'Canon', '250D', '2019', 'APS-C', 'Compact' ],
]
);
// console.log(cameras);
// return false;
clog("\n-------------------------------------------------------");
// -------------------------------------------------------------------------
// ---[ II ]----------------------------------------------------------------
// -------------------------------------------------------------------------
console.log(">>>> Get distinct values: \n\n");
console.log('Brand List:', cameras.Brand());
console.log('Camera List:', cameras.Camera());
console.log('Date List:', cameras.Date());
console.log('Format List:', cameras.Format());
console.log('Purpose List:', cameras.Purpose());
clog("\n-------------------------------------------------------");
// -------------------------------------------------------------------------
// ---[ III ]---------------------------------------------------------------
// -------------------------------------------------------------------------
console.log(">>>> Get rows: \n\n");
let NikonProCam = cameras.Brand('Nikon').Purpose('Professional');
console.log('Table Result:', NikonProCam);
console.log('New Camera List:', NikonProCam.Camera());
clog("\n-------------------------------------------------------");
// -------------------------------------------------------------------------
// ---[ IV ]----------------------------------------------------------------
// -------------------------------------------------------------------------
console.log(">>>> Get field value of row: \n\n");
// Return the camera name of the first entry
let cameraName = NikonProCam[0].Camera();
console.log("Camera name of the first row:", cameraName);
NikonProCam.forEach(function($row){
console.log('Camera: ', $row.Camera());
});
clog("\n-------------------------------------------------------");
// -------------------------------------------------------------------------
// ---[ V ]-----------------------------------------------------------------
// -------------------------------------------------------------------------
console.log(">>>> Appending new rows: \n\n");
cameras.push('Sony Alpha');
console.log("Updated Cameras table:", cameras);
cameras.push([
"Sony Alpha", "α 9 II", "2019", "Full Frame", "Sport-Pro"
]);
console.log("Updated 2 Cameras table:", cameras);
console.log("Updated Cameras List:", cameras.Camera());
clog("\n-------------------------------------------------------");
// -------------------------------------------------------------------------
// ---[ VI ]----------------------------------------------------------------
// -------------------------------------------------------------------------
console.log(">>>> Setting (Updating) field value: \n\n");
let sonyCamera = cameras.Brand('Sony Alpha');
// Update Row where there is no Camera Name
let inc = 1;
sonyCamera.Camera('').forEach(function($row){
// Camera field is a component of key,
// So we have to set a "unique" name for brand (here sony)
$row.Camera(`Camera ${inc}`);
inc++;
});
console.log("Updated Sony Alpha Camera Table", cameras.Brand('Sony Alpha'));
console.log("Updated Camera Table", cameras);
clog("\n-------------------------------------------------------");
// -------------------------------------------------------------------------
// ---[ VII ]---------------------------------------------------------------
// -------------------------------------------------------------------------
console.log(">>>> Setting (Updating) field value of result of rows: \n\n");
cameras.Format('Full Frame').update({
Format: 'Full Frame(24x36)'
});
console.log("Updated Table:", cameras);
console.log("\n-------------------------------------------------------");
// -------------------------------------------------------------------------
// ---[ VIII ]--------------------------------------------------------------
// -------------------------------------------------------------------------
console.log(">>>> Make a copy of table: \n\n");
let D3Cam = cameras.Camera('D3'); // Table with 1 row
let D3Cam2 = D3Cam; // This is not a copy
D3Cam2[0].Camera('D3Rename'); // Rename the camera name
console.log("D3Cam: ", D3Cam); // D3 --> D3Rename
console.log("D3Cam2: ", D3Cam2); // D3 --> D3Rename
console.log("Cameras Table: ", cameras); // D3 --> D3Rename
let D6Cam = cameras.Camera('D6'); // Table with 1 row
let D6Cam2 = D6Cam.copy(); // Make a true copy of the table
D6Cam2[0].Camera('D6Rename'); // Rename the camera name
console.log("D6Cam: ", D6Cam); // D6 --> D6
console.log("D6Cam2: ", D6Cam2); // D6 --> D6Rename
console.log("Cameras Table: ", cameras); // D6 --> D6
console.log("\n-------------------------------------------------------");
// -------------------------------------------------------------------------
// ---[ IX ]----------------------------------------------------------------
// -------------------------------------------------------------------------
console.log(">>>> Delete rows from the table: \n\n");
cameras.Format('APS-C').delete();
console.log(cameras);
console.log("\n-------------------------------------------------------");