HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Summarizing and crawling the content of a URL

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thecrawlingsummarizingandcontenturl

Problem

Lately I've been working on a practice project using JS ES6 to understand it better, therefore I've read a lot about JS design patterns and JS coding strategy. My question is about splitting the functionality of the project so I have to briefly explain to what I'm trying to do in this project.

I have an input that gets a command with a URL of a page. The commands are summarize and crawl. summarize actually summarizes the content of the given URL and crawl extracts the words in the URL.

```
class App {

constructor(options) {
this.email = options.email;
this.inputElement = options.inputElement;
this.progressElement = options.progressElement;
this.infoElement = options.infoElement;
this.outputElement = options.outputElement;
this.init();
}

init() {
this.socket = io();
this.socket.emit('join', {email: this.email});
this.progress = {
action: document.querySelector('.progress-action'),
percentage: document.querySelector('.progress-percentage'),
bar: document.querySelector('.progress-bar')
};
this.bindEvents();
}

bindEvents() {
let that = this;

this.inputElement.addEventListener('keyup', function (e) {
if (e.keyCode === 13) {
let compiledInput = that.tools().compileInput(this.value);
if (!compiledInput.target) {
that.tools().showInfo(that.resources().messages.noURL);
}
else if (!compiledInput.command) {
that.tools().showInfo(that.resources().messages.noCommand);
}
else {
that.tools().showInfo(that.resources().messages.syntax);
that.progressElement.classList.add('show');
that.action()compiledInput.command;
}
}
else if (this.value.trim() !== '')

Solution

tools(), resources() and actions() should all be statically implemented as Objects instead of being a function that returns an Object. Otherwise they will get re-created in memory every time they are being used.

Example:

class App { ... }
App.Tools = { tools code... };
App.Resources = { resources code... };
App.Actions = { actions code };


Taking it a bit further; tools often are called Utilities and may be defined in a separate utilities file/module, that can be referenced and re-used thoughout the application. Especially if the utilities contains commonly used functions.

Code Snippets

class App { ... }
App.Tools = { tools code... };
App.Resources = { resources code... };
App.Actions = { actions code };

Context

StackExchange Code Review Q#148367, answer score: 2

Revisions (0)

No revisions yet.