This repository has been archived by the owner on May 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
426 lines (369 loc) · 12.1 KB
/
index.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
'use strict';
var assert = require('chai').assert;
var E = require('linq');
var SelectIterator = require('./src/iterators/select');
var MultiIterator = require('./src/iterators/multi');
require('sugar');
var BabyParse = require('babyparse');
var extend = require('extend');
var DataFrame = require('./src/dataframe');
var Series = require('./src/series');
var E = require('linq');
var zip = require('./src/zip');
//
// Records plugins that have been registered.
//
var registeredPlugins = {};
/**
* Main namespace for Data-Forge.
*
* Nodejs:
*
* npm install --save data-forge
*
* var dataForge = require('data-forge');
*
* Browser:
*
* bower install --save data-forge
*
* <script language="javascript" type="text/javascript" src="bower_components/data-forge/data-forge.js"></script>
*
* @namespace dataForge
*/
var dataForge = {
//
// Constructor for DataFrame.
//
DataFrame: DataFrame,
//
// Constructor for Series.
//
Series: Series,
/**
* Install a plugin in the dataForge namespace.
*
* @param {plugin-object} plugin - The plugin to add to data-forge.
*
* @returns {dataForge} Returns the dataForge API object so that calls to 'use' can be chained.
*/
use: function (plugin) {
assert.isFunction(plugin, "Expected 'plugin' parameter to 'use' to be a function.");
if (registeredPlugins[plugin] === plugin) {
return; // Already registered.
}
registeredPlugins[plugin] = plugin;
var self = this;
plugin(self);
return self;
},
/**
* Deserialize a DataFrame from a JSON text string.
*
* @param {string} jsonTextString - The JSON text to deserialize.
* @param {config} [config] - Optional configuration option to pass to the DataFrame.
*
* @returns {DataFrame} Returns a dataframe that has been deserialized from the JSON data.
*/
fromJSON: function (jsonTextString, config) {
assert.isString(jsonTextString, "Expected 'jsonTextString' parameter to 'dataForge.fromJSON' to be a string containing data encoded in the JSON format.");
if (config) {
assert.isObject(config, "Expected 'config' parameter to 'dataForge.fromJSON' to be an object with configuration to pass to the DataFrame.");
}
var baseConfig = {
values: JSON.parse(jsonTextString)
};
var dataFrameConfig = extend({}, config || {}, baseConfig);
return new DataFrame(dataFrameConfig);
},
/**
* Deserialize a DataFrame from a CSV text string.
*
* @param {string} csvTextString - The CSV text to deserialize.
* @param {config} [config] - Optional configuration option to pass to the DataFrame.
*
* @returns {DataFrame} Returns a dataframe that has been deserialized from the CSV data.
*/
fromCSV: function (csvTextString, config) {
assert.isString(csvTextString, "Expected 'csvTextString' parameter to 'dataForge.fromCSV' to be a string containing data encoded in the CSV format.");
if (config) {
assert.isObject(config, "Expected 'config' parameter to 'dataForge.fromJSON' to be an object with configuration to pass to the DataFrame.");
if (config.columnNames) {
assert.isArray(config.columnNames, "Expect 'columnNames' field of 'config' parameter to DataForge.fromCSV to be an array of strings that specify column names.")
config.columnNames.forEach(function (columnName) {
assert.isString(columnName, "Expect 'columnNames' field of 'config' parameter to DataForge.fromCSV to be an array of strings that specify column names.")
});
}
}
var csvConfig = extend({}, config);
var parsed = BabyParse.parse(csvTextString, csvConfig);
var rows = parsed.data;
/* Old csv parsing.
var lines = csvTextString.split('\n');
var rows = E
.from(lines) // Ignore blank lines.
.where(function (line) {
return line.trim().length > 0;
})
.select(function (line) {
return E
.from(line.split(','))
.select(function (col) {
return col.trim();
})
.select(function (col) {
if (col.length === 0) {
return undefined;
}
else {
return col;
}
})
.toArray();
})
.toArray();
*/
if (rows.length === 0) {
return new dataForge.DataFrame({ columnNames: [], values: [] });
}
var columnNames;
rows = E.from(rows)
.select(function (row) {
return E.from(row)
.select(function (cell) {
return cell.trim(); // Trim each cell.
})
.toArray()
})
.toArray();
if (config && config.columnNames) {
columnNames = config.columnNames;
}
else {
columnNames = E.from(E.from(rows).first())
.select(function (columnName) {
return columnName.trim();
})
.toArray();
rows = E.from(rows)
.skip(1) // Skip header.
.toArray();
}
var baseConfig = {
columnNames: columnNames,
values: rows,
};
var dataFrameConfig = extend({}, config || {}, baseConfig);
return new dataForge.DataFrame(dataFrameConfig);
},
/**
* Read a file asynchronously from the file system.
* Works in Nodejs, doesn't work in the browser.
*
* @param {string} filePath - The path to the file to read.
*
* @returns {object} file - Returns an object that represents the file. Use `parseCSV` or `parseJSON` to deserialize to a DataFrame.
*/
readFile: function (filePath) {
assert.isString(filePath, "Expected 'filePath' parameter to dataForge.readFileSync to be a string that specifies the path of the file to read.");
return {
/**
* Deserialize a CSV file to a DataFrame.
* Returns a promise that later resolves to a DataFrame.
*
* @param {object} [config] - Optional configuration file for parsing.
*
* @returns {Promise<DataFrame>} Returns a promise of a dataframe loaded from the file.
*/
parseCSV: function (config) {
if (config) {
assert.isObject(config, "Expected optional 'config' parameter to dataForge.readFile(...).parseCSV(...) to be an object with configuration options for CSV parsing.");
}
return new Promise(function (resolve, reject) {
var fs = require('fs');
fs.readFile(filePath, 'utf8', function (err, csvData) {
if (err) {
reject(err);
return;
}
resolve(dataForge.fromCSV(csvData, config));
});
});
},
/**
* Deserialize a JSON file to a DataFrame.
* Returns a promise that later resolves to a DataFrame.
*
* @param {object} [config] - Optional configuration file for parsing.
*
* @returns {Promise<DataFrame>} Returns a promise of a dataframe loaded from the file.
*/
parseJSON: function (config) {
if (config) {
assert.isObject(config, "Expected optional 'config' parameter to dataForge.readFile(...).parseJSON(...) to be an object with configuration options for JSON parsing.");
}
return new Promise(function (resolve, reject) {
var fs = require('fs');
fs.readFile(filePath, 'utf8', function (err, data) {
if (err) {
reject(err);
return;
}
resolve(dataForge.fromJSON(data, config));
});
});
}
};
},
/**
* Read a file synchronously from the file system.
* Works in Nodejs, doesn't work in the browser.
*
* @param {string} filePath - The path to the file to read.
*
* @returns {object} Returns an object that represents the file. Use `parseCSV` or `parseJSON` to deserialize to a DataFrame.
*/
readFileSync: function (filePath) {
assert.isString(filePath, "Expected 'filePath' parameter to dataForge.readFileSync to be a string that specifies the path of the file to read.");
return {
/**
* Deserialize a CSV file to a DataFrame.
*
* @param {object} [config] - Optional configuration file for parsing.
*
* @returns {DataFrame} Returns a dataframe that was deserialized from the file.
*/
parseCSV: function (config) {
if (config) {
assert.isObject(config, "Expected optional 'config' parameter to dataForge.readFileSync(...).parseCSV(...) to be an object with configuration options for CSV parsing.");
}
var fs = require('fs');
return dataForge.fromCSV(fs.readFileSync(filePath, 'utf8'), config);
},
/**
* Deserialize a JSON file to a DataFrame.
*
* @param {object} [config] - Optional configuration file for parsing.
*
* @returns {DataFrame} Returns a dataframe that was deserialized from the file.
*/
parseJSON: function (config) {
if (config) {
assert.isObject(config, "Expected optional 'config' parameter to dataForge.readFileSync(...).parseJSON(...) to be an object with configuration options for JSON parsing.");
}
var fs = require('fs');
return dataForge.fromJSON(fs.readFileSync(filePath, 'utf8'), config);
}
};
},
/**
* Concatenate multiple dataframes into a single dataframe.
*
* @param {array} dataFrames - Array of dataframes to concatenate.
*
* @returns {DataFrame} Returns the single concatendated dataframe.
*/
concatDataFrames: require('./src/concat-dataframes'),
/**
* Concatenate multiple series into a single series.
*
* @param {array} series - Array of series to concatenate.
*
* @returns {Series} - Returns the single concatendated series.
*/
concatSeries: require('./src/concat-series'),
/**
* Generate a series from a range of numbers.
*
* @param {int} start - The value of the first number in the range.
* @param {int} count - The number of sequential values in the range.
*
* @returns {Series} Returns a series with a sequence of generated values. The series contains 'count' values beginning at 'start'.
*/
range: function (start, count) {
assert.isNumber(start, "Expect 'start' parameter to range function to be a number.");
assert.isNumber(count, "Expect 'count' parameter to range function to be a number.");
return new Series({
values: function () {
var i = -1;
return { //todo: should have a range iterator.
moveNext: function () {
return ++i < count;
},
getCurrent: function () {
return start + i;
},
};
},
});
},
/**
* Generate a data-frame containing a matrix of values.
*
* @param {int} numColumns - The number of columns in the data-frame.
* @param {int} numRows - The number of rows in the data-frame.
* @param {number} start - The starting value.
* @param {number} increment - The value to increment by for each new value.
*
* @returns {DataFrame} Returns a dataframe that contains a matrix of generated values.
*/
matrix: function (numColumns, numRows, start, increment) {
return new DataFrame({
columnNames: E.range(1, numColumns)
.select(function (columnNumber) {
return columnNumber.toString();
})
.toArray(),
values: function () {
var rowIndex = 0;
var nextValue = start;
var curRow = undefined;
return {
moveNext: function () {
if (rowIndex >= numRows) {
return false;
}
curRow = E.range(0, numColumns)
.select(function (columnIndex) {
return nextValue + (columnIndex * increment);
})
.toArray();
nextValue += numColumns * increment;
++rowIndex;
return true;
},
getCurrent: function () {
return curRow;
}
};
},
})
},
/**
* Zip together multiple series to create a new series.
*
* @param {array} series - Array of series to zip together.
* @param {function} selector - Selector function that produces a new series based on the input series.
*
* @returns {Series} Returns a single series that is the combination of multiple input series that have been 'zipped' together by the 'selector' function.
*/
zipSeries: function (series, selector) {
return zip(series, selector, function (config) {
return new Series(config);
});
},
/**
* Zip together multiple data-frames to create a new data-frame.
*
* @param {array} dataFrames - Array of data-frames to zip together.
* @param {function} selector - Selector function that produces a new data-frame based on the input data-frames.
*
* @returns {DataFrame} Returns a single dataframe that is the combination of multiple input dataframes that have been 'zipped' together by the 'selector' function.
*/
zipDataFrames: function (dataFrames, selector) {
return zip(dataFrames, selector, function (config) {
return new DataFrame(config);
});
},
};
module.exports = dataForge;