This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Symbols.js
112 lines (76 loc) · 2.02 KB
/
Symbols.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
var
fs = require('fs'),
path = require('path'),
Symbol = require('./Symbol');
/**
Represents ticks database. Essentially this is just a wrapper around a
list of folders whose names are uppercase symbol names.
@param {String} dbPath - path to the ticks database.
Example:
var symbols = new Symbols('/home/tickers');
if (!symbols.load()) {
console.log("Oops?");
return;
}
var symbol;
while ((symbol=symbols.next())) {
console.log("Symbol: %s", symbol.symbol);
}
*/
function Symbols(dbPath) {
this.dbPath = dbPath;
this.symbols = [];
}
/**
Load list of tickers from the database. Essentially will load all directory names from dbpath, skipping some specials.
@return {Boolean} true if successfully loaded.
*/
Symbols.prototype.load = function() {
if (!fs.existsSync(this.dbPath)) {
return false;
}
var files = fs.readdirSync(this.dbPath);
files = files.sort();
var symbolsUnsorted = [];
files.forEach(function(name) {
if (name.substr(0,1)!=='.' && name.toUpperCase()===name) {
symbolsUnsorted.push(name);
}
});
this.symbols = symbolsUnsorted.sort();
this.position=0;
return true;
};
/**
Checks if the symbol exists in the database. Essentially checks if the folder with such a name (uppercased)
exists in dbpath.
@param {String} symbol - the symbol name to check.
@return {Boolean}
*/
Symbols.prototype.exists = function(symbol) {
return this.symbols.indexOf((symbol || '').toUpperCase()) >= 0;
};
/**
Rewind the iterator position back to zero.
*/
Symbols.prototype.rewind = function() {
this.position=0;
};
/**
Get the next symbol from iterator.
@return {Symbol} the Symbol object created. It's your duty to call <code>load()</code> on it.
*/
Symbols.prototype.next = function() {
if (this.symbols[this.position]) {
return new Symbol(this.dbPath, this.symbols[this.position++]);
}
return null;
};
/**
Will return the count of symbols in tickers database.
@return {Integer}
*/
Symbols.prototype.count = function() {
return this.symbols.length;
};
module.exports = Symbols;