-
Notifications
You must be signed in to change notification settings - Fork 1
/
super-t9.js
104 lines (88 loc) · 2.18 KB
/
super-t9.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
/**
* Created by joelsaxton on 2/24/15.
*/
"use strict";
var wordList = {};
var map = {
2 : ['a', 'b', 'c'],
3 : ['d', 'e', 'f'],
4 : ['g', 'h', 'i'],
5 : ['j', 'k', 'l'],
6 : ['m', 'n', 'o'],
7 : ['p', 'q', 'r', 's'],
8 : ['t', 'u', 'v'],
9 : ['w', 'x', 'y', 'z']
};
/**
*
* Sets a word list by name from a csv file or an array
* @param name is the name given to the word list
* @param type is 'csv' or 'array'
* @param source is the csv file or the array
*/
module.exports.setWordList = function (name, type, source) {
switch (type){
case 'csv':
var fs = require('fs');
try {
var contents = fs.readFileSync(source).toString();
wordList[name] = contents.split('\r\n');
} catch (e){
console.error(e);
}
break;
case 'array':
if(Object.prototype.toString.call(source) !== '[object Array]') {
throw "Param is not an array";
}
wordList[name] = source;
break;
default:
return false;
}
return true;
};
/**
*
* @param name
* @returns {*}
*/
module.exports.getWordList = function(name){
return wordList[name] ? wordList[name] : false;
};
/**
*
* @param number
* @param name
* @returns {*}
*/
module.exports.getWordsFromNumber = function (number, name){
var finalList = wordList[name];
var digits = number.toString().split('');
var wordLength = digits.length;
// Search loop
for (var i = 0; i < digits.length; i++){
if (digits[i] <= 1) return false;
finalList = getWordsFromLetters(digits[i], i, wordLength, finalList);
}
return finalList;
};
/**
*
* @param digit
* @param index
* @param wordLength
* @param wordList
* @returns {Array}
*/
function getWordsFromLetters(digit, index, wordLength, wordList) {
var matches = [];
for (var letter in map[digit]) {
for (var word in wordList) {
if (wordLength == wordList[word].length && map[digit][letter] == wordList[word][index]) {
matches.push(wordList[word]);
}
}
}
return matches;
}