diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..f9f60d0 Binary files /dev/null and b/.DS_Store differ diff --git a/data/dictionarytest.json b/data/dictionarytest.json new file mode 100644 index 0000000..e69de29 diff --git a/lib/.cli_user.js.swp b/lib/.cli_user.js.swp new file mode 100644 index 0000000..523a167 Binary files /dev/null and b/lib/.cli_user.js.swp differ diff --git a/lib/.dictionary_data.js.swp b/lib/.dictionary_data.js.swp new file mode 100644 index 0000000..58f565a Binary files /dev/null and b/lib/.dictionary_data.js.swp differ diff --git a/lib/.loading.js.swp b/lib/.loading.js.swp new file mode 100644 index 0000000..dd4ebfa Binary files /dev/null and b/lib/.loading.js.swp differ diff --git a/lib/.search.js.swp b/lib/.search.js.swp new file mode 100644 index 0000000..0032d94 Binary files /dev/null and b/lib/.search.js.swp differ diff --git a/lib/cli_user.js b/lib/cli_user.js new file mode 100644 index 0000000..a786d91 --- /dev/null +++ b/lib/cli_user.js @@ -0,0 +1,113 @@ +let path = require('path'); + +console.log(` + Welcome to the node dictionary reader + ===================================== + Type q to quit +`) + +let loader = require('./loading.js'); + + +function one() { + + // Start listening to STDIN + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + + // Inline function to handle + // message output + + var dictarr; + + var showMessage = (err) => { + console.log('Select from available dictionaries'); + loader.availableDictionaries().then((result) => { + let i = 0; + result.forEach((item) => { + console.log(i + ". " + item); + i++; + }) + + dictarr = result + }); +} +/* console.log(item)} + console.log("1: " + result[0])}); + loader.availableDictionaries().then( + if (err) { + console.error(err); + } + }; +*/ + // Display message + showMessage(); + + + // Handler for STDIN data + // event + var onData = (data) => { + data = data.trim(); + + // If user input "next" + // let's go to the next + // state + if (data === 'next') { + process.stdin.pause(); + process.stdin.removeListener('data', onData); + + // ---------------------------------------- + // Go to next view here + // ---------------------------------------- + + } else if (Number(data) != NaN) { + + let num = Number(data) + + //console.log('this is the output of the array' + ' ' + dictarr[num]); + var pathname = path.join(__dirname, "..", "/data" + `/${dictarr[num]}`) + + //loader.loadFile(pathname) + + loader.loadFile(pathname).then((result) => { + let letterCount = (words) => { + letter_count = {}; + for (let i = 0; i < words.length; i++) { + if (letter_count[words[i][0].toLowerCase()] === undefined) { + letter_count[words[i][0].toLowerCase()] = 1; + } else { + letter_count[words[i][0].toLowerCase()] += 1; + } + } + return letter_count; + } + // Logging letter counts + console.log(letterCount(Object.keys(result))); + + //creating variable for the dictionary's data + + var dictionaryData = result; + + let search = require('./search.js') + + //console.log(search.exactMatch(dictionaryData, 'abate')); + + console.log(search.beginMatch(dictionaryData, 'ab')); + //return result + }) + + + //console.log(search.exactMatch(dictionaryData, 'abate')) + } else { + + // All other input is invalid + showMessage(`Invalid: ${ data }`); + } + }; + + // Set the listener + process.stdin.on('data', onData); +} + +// Start the app +one(); diff --git a/lib/dictionary_data.js b/lib/dictionary_data.js new file mode 100644 index 0000000..1d42efa --- /dev/null +++ b/lib/dictionary_data.js @@ -0,0 +1,22 @@ + +// Dictionary data module + +let letterCount = (words) => { + letter_count = {}; + for (let i = 0; i < words.length; i++) { + if (letter_count[words[i][0].toLowerCase()] === undefined) { + letter_count[words[i][0].toLowerCase()] = 1; + } else { + letter_count[words[i][0].toLowerCase()] += 1; + } + } + return letter_count; +} + + + + +module.exports.loadInformation = (data) => { + console.log("Successfully loaded data"); + console.log("Word count: "); +} diff --git a/lib/loading.js b/lib/loading.js new file mode 100644 index 0000000..c6ce5ca --- /dev/null +++ b/lib/loading.js @@ -0,0 +1,50 @@ +var fs = require('fs') +let path = require('path'); + +//let dictionaryPath = path.join(__dirname, "..", "/data/dictionary.json"); + +module.exports.loadFile = (chosenPath) => { + /* + if(num == 1){ + var chosenPath = path.join(__dirname, "..", "/data/dictionary.json") + } + else if(num == 2){ + var chosenPath = path.join(__dirname, "..", "/data/dictionarytest.json") + } + else + */ + return new Promise((resolve, reject) => { + fs.readFile(chosenPath, 'utf8', (err, data) => { + //if(err){throw 'file not found'} + /* + return new Promise((resolve, reject) => { + resolve(JSON.parse(data)) + }) + */ + if(err){ + reject('loadfile failed') + } + resolve(JSON.parse(data)); + }) + }) +} + +let dirPath = path.join(__dirname, "..", "/data"); + +module.exports.availableDictionaries = () => { + return new Promise((resolve, reject) => { + fs.readdir(dirPath, (err, data) => { + resolve(data) + //console.log(data); + }); + }) +} + +// module.exports.loadFile = loadFile; +// module.exports.availableDictionaries = availableDictionaries; + + +// = { +// "loadFile": loadFile, +// "availableDictionaries": availableDictionaries +// } diff --git a/lib/search.js b/lib/search.js new file mode 100644 index 0000000..b824898 --- /dev/null +++ b/lib/search.js @@ -0,0 +1,21 @@ +const exactMatch = (dictionaryData, searchWord) => { + let keys = Object.keys(dictionaryData) + let match = new RegExp(`${searchWord}`) + return match.exec(keys)[0] +} + +const beginMatch = (dictionaryData, beginsWith) => { + let keys = Object.keys(dictionaryData) + return keys.map((item) => { + if(item.indexOf(beginsWith) === 0){ + return item + } + }) + //return match.exec(keys)[0] + +} + +module.exports = { + beginMatch, + exactMatch +} diff --git a/warmup/index.js b/warmup/index.js new file mode 100644 index 0000000..b27765f --- /dev/null +++ b/warmup/index.js @@ -0,0 +1,139 @@ +function one() { + + // Start listening to STDIN + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + + // Inline function to handle + // message output + var showMessage = (err) => { + console.log('State one'); + console.log('Type "next" to continue'); + if (err) { + console.error(err); + } + }; + + // Display message + showMessage(); + + + // Handler for STDIN data + // event + var onData = (data) => { + data = data.trim(); + + // If user input "next" + // let's go to the next + // state + if (data === 'next') { + process.stdin.pause(); + process.stdin.removeListener('data', onData); + + two(); + + } else { + + // All other input is invalid + showMessage(`Invalid: ${ data }`); + } + }; + + // Set the listener + process.stdin.on('data', onData); +} + +// Start the app +one(); + +function two() { + // Start listening to STDIN + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + + // Inline function to handle + // message output + var showMessage = (err) => { + console.log('State two'); + console.log('Type "next" to continue'); + if (err) { + console.error(err); + } + }; + + showMessage(); + + // Handler for STDIN data + // event + var onData = (data) => { + data = data.trim(); + + // If user input "next" + // let's go to the next + // state + if (data === 'next') { + process.stdin.pause(); + process.stdin.removeListener('data', onData); + + three(); + + } else { + + // All other input is invalid + showMessage(`Invalid: ${ data }`); + } + + }; + + // Set the listener + process.stdin.on('data', onData); + +} + + +function three() { + // Start listening to STDIN + process.stdin.resume(); + process.stdin.setEncoding('utf8'); + + // Inline function to handle + // message output + var showMessage = (err) => { + console.log('State three'); + console.log('Type "next" to continue'); + if (err) { + console.error(err); + } + }; + + showMessage(); + + // Handler for STDIN data + // event + var onData = (data) => { + data = data.trim(); + + // If user input "next" + // let's go to the next + // state + if (data === 'next') { + process.stdin.pause(); + process.stdin.removeListener('data', onData); + + console.log("Goodbye"); + process.exit(); + + } else { + + // All other input is invalid + showMessage(`Invalid: ${ data }`); + } + + }; + + // Set the listener + process.stdin.on('data', onData); + +} + +