Skip to content

Commit 5e642ae

Browse files
author
Max Yazhbin
committed
Merge pull request #198 from shift-js/feat/lexer
implimented tuple data structure in JS
2 parents 2177cf5 + c38745b commit 5e642ae

File tree

3 files changed

+296
-0
lines changed

3 files changed

+296
-0
lines changed

Gruntfile.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ module.exports = function(grunt) {
1111
},
1212

1313
mochaTest: {
14+
tuple: {
15+
options: {
16+
reporter: 'spec'
17+
},
18+
src: [
19+
'tests/tupleTests/*'
20+
]
21+
},
1422
lexer: {
1523
options: {
1624
reporter: 'spec'
@@ -125,6 +133,10 @@ module.exports = function(grunt) {
125133
'mochaTest:generator'
126134
]);
127135

136+
grunt.registerTask('testTuple',[
137+
'mochaTest:tuple'
138+
]);
139+
128140
grunt.registerTask('testEndToEnd', [
129141
// 'jshint',
130142
'mochaTest:endToEnd'

tests/tupleTests/tupleTest.js

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
var Tuple = require('../../tupleDataStructure/tupleJS');
2+
var expect = require('chai').expect;
3+
4+
describe('Tuple', function() {
5+
6+
it('should store the values put into it', function () {
7+
t = new Tuple(["blah", {two: 2}, 5]);
8+
expect(t.findValue(0)).to.deep.equal("blah");
9+
expect(t.findValue(1)).to.deep.equal(2);
10+
expect(t.findValue(2)).to.deep.equal(5);
11+
expect(t.findValue('two')).to.deep.equal(2);
12+
expect(t.findValue(3)).to.deep.equal(undefined);
13+
expect(t.findValue('three')).to.deep.equal(undefined);
14+
});
15+
16+
it('should function like an array if only values are put into it', function () {
17+
t = new Tuple(["blah", 3, true]);
18+
expect(t.findValue(0)).to.deep.equal("blah");
19+
expect(t.findValue(1)).to.deep.equal(3);
20+
expect(t.findValue(2)).to.deep.equal(true);
21+
expect(t.findValue(3)).to.deep.equal(undefined);
22+
expect(t.findValue('three')).to.deep.equal(undefined);
23+
});
24+
25+
it('should function like an dictionary if only objects are put into it', function () {
26+
t = new Tuple([{one: 1}, {two: true}, {three: "blah"}]);
27+
expect(t.findValue(0)).to.deep.equal(1);
28+
expect(t.findValue('one')).to.deep.equal(1);
29+
expect(t.findValue(1)).to.deep.equal(true);
30+
expect(t.findValue('two')).to.deep.equal(true);
31+
expect(t.findValue(2)).to.deep.equal("blah");
32+
expect(t.findValue('three')).to.deep.equal("blah");
33+
expect(t.findValue(3)).to.deep.equal(undefined);
34+
expect(t.findValue('four')).to.deep.equal(undefined);
35+
});
36+
37+
it('should not modify values if new and old value types do not match', function () {
38+
t = new Tuple(["blah", {two: 2}, {three: 7}]);
39+
expect(t.findValue(0)).to.deep.equal("blah");
40+
expect(t.findValue(1)).to.deep.equal(2);
41+
expect(t.findValue(2)).to.deep.equal(7);
42+
expect(t.findValue('three')).to.deep.equal(7);
43+
expect(t.findValue('two')).to.deep.equal(2);
44+
expect(t.findValue(3)).to.deep.equal(undefined);
45+
t.modifyVal(0,7);
46+
expect(t.findValue(0)).to.deep.equal("blah");
47+
expect(t.findValue(1)).to.deep.equal(2);
48+
expect(t.findValue(2)).to.deep.equal(7);
49+
expect(t.findValue('three')).to.deep.equal(7);
50+
expect(t.findValue('two')).to.deep.equal(2);
51+
expect(t.findValue(3)).to.deep.equal(undefined);
52+
t.modifyVal(1,"someString");
53+
expect(t.findValue(0)).to.deep.equal("blah");
54+
expect(t.findValue(1)).to.deep.equal(2);
55+
expect(t.findValue(2)).to.deep.equal(7);
56+
expect(t.findValue('three')).to.deep.equal(7);
57+
expect(t.findValue('two')).to.deep.equal(2);
58+
expect(t.findValue(3)).to.deep.equal(undefined);
59+
});
60+
61+
it('should modify only the values requested and if the types of values match', function () {
62+
t = new Tuple(["blah", {two: 2}, {three: 7}]);
63+
expect(t.findValue(0)).to.deep.equal("blah");
64+
expect(t.findValue(1)).to.deep.equal(2);
65+
expect(t.findValue(2)).to.deep.equal(7);
66+
expect(t.findValue('three')).to.deep.equal(7);
67+
expect(t.findValue('two')).to.deep.equal(2);
68+
expect(t.findValue(3)).to.deep.equal(undefined);
69+
t.modifyVal(0,"newBlah");
70+
expect(t.findValue(0)).to.deep.equal("newBlah");
71+
expect(t.findValue(1)).to.deep.equal(2);
72+
expect(t.findValue(2)).to.deep.equal(7);
73+
expect(t.findValue('three')).to.deep.equal(7);
74+
expect(t.findValue('two')).to.deep.equal(2);
75+
expect(t.findValue(3)).to.deep.equal(undefined);
76+
t.modifyVal(1,8);
77+
expect(t.findValue(0)).to.deep.equal("newBlah");
78+
expect(t.findValue(1)).to.deep.equal(8);
79+
expect(t.findValue(2)).to.deep.equal(7);
80+
expect(t.findValue('three')).to.deep.equal(7);
81+
expect(t.findValue('two')).to.deep.equal(8);
82+
expect(t.findValue(3)).to.deep.equal(undefined);
83+
// t.modifyVal("three",21);
84+
// expect(t.findValue(0)).to.deep.equal("newBlah");
85+
// expect(t.findValue(1)).to.deep.equal(8);
86+
// expect(t.findValue(2)).to.deep.equal(7);
87+
// expect(t.findValue('three')).to.deep.equal(21);
88+
// expect(t.findValue('two')).to.deep.equal(8);
89+
// expect(t.findValue(3)).to.deep.equal(undefined);
90+
});
91+
92+
// it('should delete only the values requested', function () {
93+
// t = new Tuple(["blah", {two: 2}, {three: 7}]);
94+
// expect(t.findValue(0)).to.deep.equal("blah");
95+
// expect(t.findValue(1)).to.deep.equal(2);
96+
// expect(t.findValue(2)).to.deep.equal(7);
97+
// expect(t.findValue('three')).to.deep.equal(7);
98+
// expect(t.findValue('two')).to.deep.equal(2);
99+
// expect(t.findValue(3)).to.deep.equal(undefined);
100+
// t.deleteElement(0);
101+
// expect(t.findValue(0)).to.deep.equal(undefined);
102+
// expect(t.findValue(1)).to.deep.equal(2);
103+
// expect(t.findValue('two')).to.deep.equal(2);
104+
// expect(t.findValue(2)).to.deep.equal(7);
105+
// expect(t.findValue('three')).to.deep.equal(7);
106+
// expect(t.findValue(3)).to.deep.equal(undefined);
107+
// t.deleteElement(1);
108+
// expect(t.findValue(0)).to.deep.equal(undefined);
109+
// expect(t.findValue(1)).to.deep.equal(undefined);
110+
// expect(t.findValue('two')).to.deep.equal(undefined);
111+
// expect(t.findValue(2)).to.deep.equal(7);
112+
// expect(t.findValue('three')).to.deep.equal(7);
113+
// expect(t.findValue(3)).to.deep.equal(undefined);
114+
// t.deleteElement("three");
115+
// expect(t.findValue(0)).to.deep.equal(undefined);
116+
// expect(t.findValue(1)).to.deep.equal(undefined);
117+
// expect(t.findValue('two')).to.deep.equal(undefined);
118+
// expect(t.findValue(2)).to.deep.equal(undefined);
119+
// expect(t.findValue('three')).to.deep.equal(undefined);
120+
// expect(t.findValue(3)).to.deep.equal(undefined);
121+
// });
122+
123+
it('should not rename the key if passed incorrect arguments', function () {
124+
t = new Tuple(["blah", {two: 2}, {three: 7}]);
125+
expect(t.findValue(0)).to.deep.equal("blah");
126+
expect(t.findValue(1)).to.deep.equal(2);
127+
expect(t.findValue(2)).to.deep.equal(7);
128+
expect(t.findValue('three')).to.deep.equal(7);
129+
expect(t.findValue('two')).to.deep.equal(2);
130+
expect(t.findValue(3)).to.deep.equal(undefined);
131+
t.renameElement(0,1);
132+
expect(t.findValue(0)).to.deep.equal("blah");
133+
expect(t.findValue(1)).to.deep.equal(2);
134+
expect(t.findValue(2)).to.deep.equal(7);
135+
expect(t.findValue('three')).to.deep.equal(7);
136+
expect(t.findValue('two')).to.deep.equal(2);
137+
expect(t.findValue(3)).to.deep.equal(undefined);
138+
t.renameElement(0,"abc");
139+
expect(t.findValue(0)).to.deep.equal("blah");
140+
expect(t.findValue(1)).to.deep.equal(2);
141+
expect(t.findValue(2)).to.deep.equal(7);
142+
expect(t.findValue('three')).to.deep.equal(7);
143+
expect(t.findValue('two')).to.deep.equal(2);
144+
expect(t.findValue(3)).to.deep.equal(undefined);
145+
t.renameElement("abc",1);
146+
expect(t.findValue(0)).to.deep.equal("blah");
147+
expect(t.findValue(1)).to.deep.equal(2);
148+
expect(t.findValue(2)).to.deep.equal(7);
149+
expect(t.findValue('three')).to.deep.equal(7);
150+
expect(t.findValue('two')).to.deep.equal(2);
151+
expect(t.findValue(3)).to.deep.equal(undefined);
152+
t.renameElement("four","five");
153+
expect(t.findValue(0)).to.deep.equal("blah");
154+
expect(t.findValue(1)).to.deep.equal(2);
155+
expect(t.findValue(2)).to.deep.equal(7);
156+
expect(t.findValue('three')).to.deep.equal(7);
157+
expect(t.findValue('two')).to.deep.equal(2);
158+
expect(t.findValue(3)).to.deep.equal(undefined);
159+
expect(t.findValue('four')).to.deep.equal(undefined);
160+
expect(t.findValue('five')).to.deep.equal(undefined);
161+
// t.renameElement("two","ten");
162+
// expect(t.findValue(0)).to.deep.equal("blah");
163+
// expect(t.findValue(1)).to.deep.equal(2);
164+
// expect(t.findValue(2)).to.deep.equal(7);
165+
// expect(t.findValue('three')).to.deep.equal(7);
166+
// expect(t.findValue('two')).to.deep.equal(undefined);
167+
// expect(t.findValue('ten')).to.deep.equal(undefined);
168+
// expect(t.findValue(3)).to.deep.equal(undefined);
169+
// expect(t.findValue('four')).to.deep.equal(undefined);
170+
// expect(t.findValue('five')).to.deep.equal(undefined);
171+
});
172+
173+
it('should rename the key if the correct arguments are passed in', function () {
174+
t = new Tuple(["blah", {two: 2}, {three: 7}]);
175+
expect(t.findValue(0)).to.deep.equal("blah");
176+
expect(t.findValue(1)).to.deep.equal(2);
177+
expect(t.findValue(2)).to.deep.equal(7);
178+
expect(t.findValue('three')).to.deep.equal(7);
179+
expect(t.findValue('two')).to.deep.equal(2);
180+
expect(t.findValue(3)).to.deep.equal(undefined);
181+
t.renameElement("two","ten");
182+
expect(t.findValue(0)).to.deep.equal("blah");
183+
expect(t.findValue(1)).to.deep.equal(2);
184+
expect(t.findValue('two')).to.deep.equal(undefined);
185+
expect(t.findValue('ten')).to.deep.equal(2);
186+
expect(t.findValue(2)).to.deep.equal(7);
187+
expect(t.findValue('three')).to.deep.equal(7);
188+
expect(t.findValue(3)).to.deep.equal(undefined);
189+
});
190+
191+
});

tupleDataStructure/tupleJS.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function Tuple(tuple) {
2+
//assume the tuple has the form of ["blah", {two: 2}, 5,...], for now assuming no recursive tuples
3+
this.tup = {};
4+
if (Array.isArray(tuple)) {
5+
for (var i = 0; i < tuple.length; i++) {
6+
if (typeof tuple[i] === "object" && !Array.isArray(tuple[i])) {
7+
var key = Object.keys(tuple[i])[0];
8+
var val = tuple[i][key];
9+
this.tup[key] = {'val': val, 'key': i};
10+
this.tup[i] = {'val': val, 'key': key};
11+
} else if (Array.isArray(tuple[i])) {
12+
return null; //temporary
13+
} else {
14+
this.tup[i] = {'val': tuple[i]};
15+
}
16+
}
17+
}
18+
// console.dir(this.tup);
19+
};
20+
// swift: ("blah", two: 2, 5);
21+
// new Tuple(["blah", {two: 2}, 5]);
22+
23+
// {
24+
// 0: {val: "blah"},
25+
// 1: {val: 2, key: "two"},
26+
// 2: {val: 5},
27+
// "two": {val: 2, key: 1}
28+
// }
29+
30+
31+
Tuple.prototype.findValue = function(keyOrIndex){
32+
var x = this.tup[keyOrIndex]
33+
return x === undefined ? undefined : this.tup[keyOrIndex]["val"];
34+
};
35+
36+
Tuple.prototype.modifyVal = function(keyOrIndex, newVal) {
37+
var x = this.tup[keyOrIndex];
38+
if (x === undefined) {
39+
return false;
40+
}
41+
42+
if (typeof x['val'] === typeof newVal) {
43+
x['val'] = newVal;
44+
if (x.hasOwnProperty('key')) {
45+
var otherKey = x['key'];
46+
this.tup[otherKey]['val'] = newVal;
47+
}
48+
return true;
49+
}
50+
return false;
51+
52+
};
53+
54+
// Tuple.prototype.deleteElement = function(keyOrIndex) {
55+
// var x = this.tup[keyOrIndex];
56+
// if (x === undefined) {
57+
// return false;
58+
// }
59+
// if (x.hasOwnProperty('key')) {
60+
// var otherKey = x['key'];
61+
// delete this.tup[otherKey];
62+
// }
63+
// delete this.tup[keyOrIndex];
64+
// return true;
65+
// };
66+
67+
68+
// Tuple.prototype.moveElement = function(oldKeyorIndex, newIndex) {
69+
70+
// };
71+
72+
Tuple.prototype.renameElement = function(oldKey, newKey) {
73+
//partially implimented
74+
if (!isNaN(oldKey) || !isNaN(newKey)) {
75+
return false;
76+
}
77+
var x = this.tup[oldKey];
78+
if (x === undefined) {
79+
return false;
80+
}
81+
82+
if (oldKey !== newKey && typeof oldKey === "string" && typeof newKey === "string") {
83+
var val = x['val'];
84+
var index = x['key'];
85+
this.tup[index]['key'] = newKey;
86+
this.tup[newKey] = x;
87+
delete this.tup[oldKey];
88+
return true;
89+
}
90+
return false;
91+
};
92+
93+
module.exports = Tuple;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy