Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shelly #18

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lab-shelly/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"rules": {
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"no-console": "off",
"indent": [ "error", 2 ],
"semi": ["error", "always"]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"globals": {
"__API_URI__": false,
"__DEBUG__": false
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
98 changes: 98 additions & 0 deletions lab-shelly/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# ignore db directorys
db

# Created by https://www.gitignore.io/api/linux,node,osx,vim

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*


### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history


### OSX ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Vim ###
# swap
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
# session
Session.vim
# temporary
.netrwhist
*~
# auto-generated tag files
tags
83 changes: 83 additions & 0 deletions lab-shelly/lib/ll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

const Node = require('./node');

const LinkedList = module.exports = function(array) {
this.head = null;
if(array) array.forEach(val => this.insert(val));
};

LinkedList.prototype.insert = function(val) {
try {
if (typeof val === 'undefined') {
throw new ReferenceError('Please enter a value for the node.');
} else {
this.head = new Node(val, this.head);
return this;
}
} catch (err) {
return err;
}
};

LinkedList.prototype.remove = function(val) {
let current;
try {
if (typeof val === 'undefined') {
throw new ReferenceError('Please enter a value for the node.');
} else if (this.search(val) === false){
throw new ReferenceError('That node does not exist in the list.');
} else {
current = this.head;
if (this.head.val === val) {
this.head = current.next;
current = null;
return this;
} else {
while (current.next) {
if (current.next.val === val) {
current.next = current.next.next;
return this;
} else {
current = current.next;
}
}
}
}
} catch (err) {
return err;
}
};

LinkedList.prototype.shift = function() {
if(!this.head) throw new ReferenceError('The list is empty');
try {
let current = this.head;
this.head = this.head.next;
current.next = null;
return current.val;
} catch (err) {
return err;
}
};

LinkedList.prototype.search = function(val) {
try {
let current;
if (typeof val === 'undefined') {
throw new ReferenceError('Please enter a value for the node.');
} else {
current = this.head;
while (current.next) {
if (current.val === val) {
return current.val;
} else {
current = current.next;
}
}
return current.val === val ? current.val : false;
}
} catch (err) {
return err;
}
};
9 changes: 9 additions & 0 deletions lab-shelly/lib/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

function Node(val, next, prev) {
this.val = val;
this.next = next;
this.prev = prev;
}

module.exports = Node;
22 changes: 22 additions & 0 deletions lab-shelly/lib/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const Queue = module.exports = require('./ll');

Queue.prototype.enqueue = function(val) {
this.insert(val);
return this;
};

Queue.prototype.dequeue = function() {
let current = null;

_setCurrent(this.head);
current.next = null;
return this;

function _setCurrent(node) {
if(!node.next) return;
current = node;
_setCurrent(node.next);
}
};
17 changes: 17 additions & 0 deletions lab-shelly/lib/stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const Stack = module.exports = require('./ll');

Stack.prototype.push = function(val) {
this.insert(val);
return this;
};

Stack.prototype.pop = function() {
this.shift();
return this;
};

Stack.prototype.peek = function() {
return this.head.val;
};
31 changes: 31 additions & 0 deletions lab-shelly/linter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job with this linter function!


//O(n) notation

const Stack = require('./lib/stack');
let openerStack = new Stack();

const Linter = module.exports = function(string) {

for(let i = 0; i <string.length; i ++) {
if(string.charAt(i) === '[' || string.charAt(i) === '{' || string.charAt(i) === '(') {
openerStack.push(string[i]);
}

if(string.charAt(i) === ']' || string.charAt(i) === '}' || string.charAt(i) === ')') {
if(openerStack.head === null) {
return false;
}

let current = string.charAt(i);
let top = openerStack.peek();
if(top === '[' && current === ']' || top === '{' && current === '}' || top === '(' && current === ')') {
openerStack.pop();
}
}
}
if(openerStack.head !== null) {
return false;
}
return true;
};
18 changes: 18 additions & 0 deletions lab-shelly/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lab-shelly",
"version": "1.0.0",
"description": "",
"main": "balanced-bracket.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"author": "shelly",
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.3.0"
}
}
35 changes: 35 additions & 0 deletions lab-shelly/test/linter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const expect = require('chai').expect;
const Linter = require('../linter');

describe('Linter Module', function() {

describe('a string with balanced openers and closers', function() {
it('should return true', done => {
let balancedTestStr = '[]{}()';
let testBalanced = Linter(balancedTestStr);
expect(testBalanced).to.be.true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember these tests...

done();
});
});

describe('a string with an extra opening [ ( or { ', function() {
it('should return false', done => {
let openerTestString = '[]{';
let testOpener = Linter(openerTestString);
expect(testOpener).to.be.false;
done();
});
});

describe('a string with an extra closing ] ) or } ', function() {
it('should return false', done => {
let closerTestString = '[]{})';
let testCloser = Linter(closerTestString);
expect(testCloser).to.be.false;
done();
});
});

});