diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..9175c0f Binary files /dev/null and b/.DS_Store differ diff --git a/data/lorem.txt b/data/lorem.txt new file mode 100644 index 0000000..cee7a92 --- /dev/null +++ b/data/lorem.txt @@ -0,0 +1 @@ +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. diff --git a/data/test.txt b/data/test.txt new file mode 100644 index 0000000..b861aad --- /dev/null +++ b/data/test.txt @@ -0,0 +1 @@ +Hello!Hello again! \ No newline at end of file diff --git a/emitter.js b/emitter.js new file mode 100644 index 0000000..b667f5e --- /dev/null +++ b/emitter.js @@ -0,0 +1,56 @@ + +class Emitter { + constructor () { + this.domain = null; + this._events = {}; + this._eventsCount = 0; + this._maxListeners = undefined; + } + + on (eventType, callback) { + if (this._events[eventType] !== undefined) { + this._events[eventType] = [this._events[eventType], callback]; + } else { + this._events[eventType] = callback; + } + this._eventsCount = Object.keys(this._events).length; + } + + emit (eventType) { + if (this._events[eventType] !== undefined && typeof this._events[eventType] === 'object') { + this._events[eventType].forEach (function (i) { + i(); + }) + } else { + this._events[eventType](); + } + } + + removeListener(eventType, callback) { + if (eventType in this._events) { + if (typeof this._events[eventType] === 'object') { + var index = this._events[eventType].indexOf(callback); + this._events[eventType].splice(index,1); + } else { + delete this._events[eventType]; + } + } + this._eventsCount = Object.keys(this._events).length; + } + + + removeAllListeners(eventType) { + if (eventType in this._events) { + delete this._events[eventType]; + this._eventsCount += 1; + } + this._eventsCount = Object.keys(this._events).length; + } + +} + + + + + +module.exports = Emitter; diff --git a/fsp.js b/fsp.js new file mode 100644 index 0000000..f4799b8 --- /dev/null +++ b/fsp.js @@ -0,0 +1,42 @@ +//This part of the assignment takes the fs module in Node.js and has you wrap it +//with a promise based interface. + +var fs = require('fs'); + + +var fsp = { + readFile: function (path) { + return new Promise (function (resolve,reject) { + fs.readFile (path, 'utf8', (err,data) => { + err ? console.log(error) : resolve(data); + }); + }) +}, + writeFile : function (filename, message) { + return new Promise ( function (resolve, reject) { + fs.writeFile(filename, message, 'utf8', (err,data) => { + resolve(data); + fsp.readFile(filename) + .then( data => { + console.log(data); + }) + }) + }) +}, + appendFile : function (filename, data) { + return new Promise ( function (resolve, reject) { + fs.appendFile (filename, data, 'utf8', (err,data) => { + resolve(data); + fsp.readFile(filename) + .then( data => { + console.log(data); + }) + }) + }) + } + + +} + + +module.exports = fsp; diff --git a/index.js b/index.js new file mode 100644 index 0000000..235e36a --- /dev/null +++ b/index.js @@ -0,0 +1,212 @@ +// WARM UPS +// +// +// +// + + + +// warm up #1 +// Create a promise that resolves the message "Hello Promise!" after 1 second + +var p = Promise.resolve('Hello Promise!'); + + +p.then(function(message){ + setTimeout(function(){ + console.log(message); + }, 1); +}); + + +// warm up #2 +// Your delay function should return a promise that resolves the value +// milliseconds after delaying for the specified number of milliseconds + + +var delay = function (ms) { + return new Promise( (resolve,reject) => { + setTimeout ( resolve(ms) , ms) + }); +} + +var countDown = (remaining) => { + if (remaining > 0 ) { + console.log (remaining) + remaining -= 100; + return remaining; + } else { + console.log("Done!") + } +}; + + +delay(1000) + .then(countDown) //=> 1000 + .then(countDown) //=> 900 + .then(countDown) //=> 800 + .then(countDown) //=> 700 + .then(countDown) //=> 600 + .then(countDown) //=> 500 + .then(countDown) //=> 400 + .then(countDown) //=> 300 + .then(countDown) //=> 200 + .then(countDown) //=> 100 + .then(countDown); //=> Done! + + + +// warm up #3 +// Create a function that accepts a number and returns a promise that resolves that number squared + // The promise should reject if it is not passed a number + // Now map an array of integers 1 to 9 to an array of promises using the function above + // Use Promise.all to get the result of all of the promises in the array + + +var squares = [1,2,3,4,5,6,7,8,9]; + +var squared = function (n) { + return new Promise (function (resolve,reject) { + if (typeof n === 'number') { + resolve(n*n); + } else { + reject(n + " is not a number") + } + }); + }; + +squares = squares.map (function (i) { + return squared(i); +}); + +Promise.all(squares) + .then(function(result){ + console.log(result + " is the result") + }); + + +// warm up #4 +// Create a function with this signature doBadThing(forRealz) + // Return a promise that resolves to "Yay!" when forRealz is falsy + // The promise should reject when forRealz is truthy + // Now call doBadThing with both a true and false value chaining on .then and .catch + // Experiment with using .catch vs supplying a reject handler in your .then call + // Experiment using now try throwing an error in the resolve handler of your .then call + // What do you notice about when the .catch handler and the reject handler are invoked? + +var doBadThing = function (value) { + return new Promise (function (resolve,reject) { + value == false ? resolve("Yay!") : reject (value + " => evalutes to true, got to rejection"); + }); +}; + +doBadThing(false).then (function(result) { + console.log(result); +}) + .catch(function(err) { + console.log(err); + }) + + +doBadThing(true).then (function(result) { + console.log(result); +}) + .catch(function(err) { + console.log(err); + }) + + + + + +doBadThing(true).then (function(result) { + console.log(result); + throw "Hi there." +}) + .catch(function(err) { + console.log(err); + }) + + + + +// FILE OPERATIONS +// +// +// +// +//This part of the assignment takes the fs module in Node.js and has you wrap it +//with a promise based interface. + +var fsp = require('./fsp'); + + +fsp.readFile('./data/lorem.txt') + .then(function(data) { + // Outputs the file data + console.log(data); + }) + .catch(function(err) { + console.error(err); + }); + +fsp.writeFile('./data/test.txt', 'Hello!') + .then(function(res) { + // Outputs the file data + // after writing + console.log(res); + }) + .catch(function(err) { + console.error(err); + }); + +fsp.appendFile('./data/test.txt', 'Hello again!') + .then(function(res) { + // Outputs the file data + // after appending + console.log(res); + }) + .catch(function(err) { + console.error(err); + }); + + + + +// CREATE AN EMITTER FROM SCRATCH +// +// +// +// + +// var Emitter = require('./emitter.js'); +// var emitter = new Emitter(); + +// #1 create new instance +var events = require ('events'); +var Emitter = require ('./emitter.js') + + +var myEmitter = new Emitter(); + + +// callback functions +var unce = function() {console.log('unce unce unce')} +var bass = () => {console.log('bass line')} +var wubs = () => {console.log('wub wub')} + +// #2 - 4 adding listeners and emitting +myEmitter.on('party', unce); +myEmitter.on('party', bass); +myEmitter.on('bass_drop', wubs); + +myEmitter.emit('party'); + + +// # 5- 7 removing listeners + +myEmitter.removeListener('bass_drop', wubs); +console.log(myEmitter); + +myEmitter.removeAllListeners('party') +console.log(myEmitter) diff --git a/test.js b/test.js new file mode 100644 index 0000000..37f79e1 --- /dev/null +++ b/test.js @@ -0,0 +1,20 @@ +class test { + constructor () { + this.a = "test"; + this.b = "exam"; + } +} + + + +var example = new test (); + +example.a = [example.a,'b'] + + +var array = [1,2,2, 5, 9]; +var index = array.indexOf(5); + +if (2 in array) { + console.log('hi there') +}