Skip to content

Commit de13d63

Browse files
author
Max Yazhbin
committed
Merge pull request #199 from shift-js/feat/lexer
saving progress in tuple recursive implimentation
2 parents 5e642ae + d8eb3b9 commit de13d63

File tree

2 files changed

+224
-23
lines changed

2 files changed

+224
-23
lines changed

tests/tupleTests/tupleTest.js

Lines changed: 180 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var Tuple = require('../../tupleDataStructure/tupleJS');
22
var expect = require('chai').expect;
33

4-
describe('Tuple', function() {
4+
describe('Simple Tuple', function() {
55

66
it('should store the values put into it', function () {
77
t = new Tuple(["blah", {two: 2}, 5]);
@@ -158,16 +158,185 @@ describe('Tuple', function() {
158158
expect(t.findValue(3)).to.deep.equal(undefined);
159159
expect(t.findValue('four')).to.deep.equal(undefined);
160160
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);
161+
});
162+
163+
it('should rename the key if the correct arguments are passed in', function () {
164+
t = new Tuple(["blah", {two: 2}, {three: 7}]);
165+
expect(t.findValue(0)).to.deep.equal("blah");
166+
expect(t.findValue(1)).to.deep.equal(2);
167+
expect(t.findValue(2)).to.deep.equal(7);
168+
expect(t.findValue('three')).to.deep.equal(7);
169+
expect(t.findValue('two')).to.deep.equal(2);
170+
expect(t.findValue(3)).to.deep.equal(undefined);
171+
t.renameElement("two","ten");
172+
expect(t.findValue(0)).to.deep.equal("blah");
173+
expect(t.findValue(1)).to.deep.equal(2);
174+
expect(t.findValue('two')).to.deep.equal(undefined);
175+
expect(t.findValue('ten')).to.deep.equal(2);
176+
expect(t.findValue(2)).to.deep.equal(7);
177+
expect(t.findValue('three')).to.deep.equal(7);
178+
expect(t.findValue(3)).to.deep.equal(undefined);
179+
});
180+
181+
});
182+
183+
describe('Nested Tuple', function() {
184+
185+
it('should store the values put into it', function () {
186+
t = new Tuple(["blah", {two: 2}, 5]);
187+
expect(t.findValue(0)).to.deep.equal("blah");
188+
expect(t.findValue(1)).to.deep.equal(2);
189+
expect(t.findValue(2)).to.deep.equal(5);
190+
expect(t.findValue('two')).to.deep.equal(2);
191+
expect(t.findValue(3)).to.deep.equal(undefined);
192+
expect(t.findValue('three')).to.deep.equal(undefined);
193+
});
194+
195+
it('should function like an array if only values are put into it', function () {
196+
t = new Tuple(["blah", 3, true]);
197+
expect(t.findValue(0)).to.deep.equal("blah");
198+
expect(t.findValue(1)).to.deep.equal(3);
199+
expect(t.findValue(2)).to.deep.equal(true);
200+
expect(t.findValue(3)).to.deep.equal(undefined);
201+
expect(t.findValue('three')).to.deep.equal(undefined);
202+
});
203+
204+
it('should function like an dictionary if only objects are put into it', function () {
205+
t = new Tuple([{one: 1}, {two: true}, {three: "blah"}]);
206+
expect(t.findValue(0)).to.deep.equal(1);
207+
expect(t.findValue('one')).to.deep.equal(1);
208+
expect(t.findValue(1)).to.deep.equal(true);
209+
expect(t.findValue('two')).to.deep.equal(true);
210+
expect(t.findValue(2)).to.deep.equal("blah");
211+
expect(t.findValue('three')).to.deep.equal("blah");
212+
expect(t.findValue(3)).to.deep.equal(undefined);
213+
expect(t.findValue('four')).to.deep.equal(undefined);
214+
});
215+
216+
it('should not modify values if new and old value types do not match', function () {
217+
t = new Tuple(["blah", {two: 2}, {three: 7}]);
218+
expect(t.findValue(0)).to.deep.equal("blah");
219+
expect(t.findValue(1)).to.deep.equal(2);
220+
expect(t.findValue(2)).to.deep.equal(7);
221+
expect(t.findValue('three')).to.deep.equal(7);
222+
expect(t.findValue('two')).to.deep.equal(2);
223+
expect(t.findValue(3)).to.deep.equal(undefined);
224+
t.modifyVal(0,7);
225+
expect(t.findValue(0)).to.deep.equal("blah");
226+
expect(t.findValue(1)).to.deep.equal(2);
227+
expect(t.findValue(2)).to.deep.equal(7);
228+
expect(t.findValue('three')).to.deep.equal(7);
229+
expect(t.findValue('two')).to.deep.equal(2);
230+
expect(t.findValue(3)).to.deep.equal(undefined);
231+
t.modifyVal(1,"someString");
232+
expect(t.findValue(0)).to.deep.equal("blah");
233+
expect(t.findValue(1)).to.deep.equal(2);
234+
expect(t.findValue(2)).to.deep.equal(7);
235+
expect(t.findValue('three')).to.deep.equal(7);
236+
expect(t.findValue('two')).to.deep.equal(2);
237+
expect(t.findValue(3)).to.deep.equal(undefined);
238+
});
239+
240+
it('should modify only the values requested and if the types of values match', function () {
241+
t = new Tuple(["blah", {two: 2}, {three: 7}]);
242+
expect(t.findValue(0)).to.deep.equal("blah");
243+
expect(t.findValue(1)).to.deep.equal(2);
244+
expect(t.findValue(2)).to.deep.equal(7);
245+
expect(t.findValue('three')).to.deep.equal(7);
246+
expect(t.findValue('two')).to.deep.equal(2);
247+
expect(t.findValue(3)).to.deep.equal(undefined);
248+
t.modifyVal(0,"newBlah");
249+
expect(t.findValue(0)).to.deep.equal("newBlah");
250+
expect(t.findValue(1)).to.deep.equal(2);
251+
expect(t.findValue(2)).to.deep.equal(7);
252+
expect(t.findValue('three')).to.deep.equal(7);
253+
expect(t.findValue('two')).to.deep.equal(2);
254+
expect(t.findValue(3)).to.deep.equal(undefined);
255+
t.modifyVal(1,8);
256+
expect(t.findValue(0)).to.deep.equal("newBlah");
257+
expect(t.findValue(1)).to.deep.equal(8);
258+
expect(t.findValue(2)).to.deep.equal(7);
259+
expect(t.findValue('three')).to.deep.equal(7);
260+
expect(t.findValue('two')).to.deep.equal(8);
261+
expect(t.findValue(3)).to.deep.equal(undefined);
262+
// t.modifyVal("three",21);
263+
// expect(t.findValue(0)).to.deep.equal("newBlah");
264+
// expect(t.findValue(1)).to.deep.equal(8);
164265
// 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);
266+
// expect(t.findValue('three')).to.deep.equal(21);
267+
// expect(t.findValue('two')).to.deep.equal(8);
168268
// 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);
269+
});
270+
271+
// it('should delete only the values requested', function () {
272+
// t = new Tuple(["blah", {two: 2}, {three: 7}]);
273+
// expect(t.findValue(0)).to.deep.equal("blah");
274+
// expect(t.findValue(1)).to.deep.equal(2);
275+
// expect(t.findValue(2)).to.deep.equal(7);
276+
// expect(t.findValue('three')).to.deep.equal(7);
277+
// expect(t.findValue('two')).to.deep.equal(2);
278+
// expect(t.findValue(3)).to.deep.equal(undefined);
279+
// t.deleteElement(0);
280+
// expect(t.findValue(0)).to.deep.equal(undefined);
281+
// expect(t.findValue(1)).to.deep.equal(2);
282+
// expect(t.findValue('two')).to.deep.equal(2);
283+
// expect(t.findValue(2)).to.deep.equal(7);
284+
// expect(t.findValue('three')).to.deep.equal(7);
285+
// expect(t.findValue(3)).to.deep.equal(undefined);
286+
// t.deleteElement(1);
287+
// expect(t.findValue(0)).to.deep.equal(undefined);
288+
// expect(t.findValue(1)).to.deep.equal(undefined);
289+
// expect(t.findValue('two')).to.deep.equal(undefined);
290+
// expect(t.findValue(2)).to.deep.equal(7);
291+
// expect(t.findValue('three')).to.deep.equal(7);
292+
// expect(t.findValue(3)).to.deep.equal(undefined);
293+
// t.deleteElement("three");
294+
// expect(t.findValue(0)).to.deep.equal(undefined);
295+
// expect(t.findValue(1)).to.deep.equal(undefined);
296+
// expect(t.findValue('two')).to.deep.equal(undefined);
297+
// expect(t.findValue(2)).to.deep.equal(undefined);
298+
// expect(t.findValue('three')).to.deep.equal(undefined);
299+
// expect(t.findValue(3)).to.deep.equal(undefined);
300+
// });
301+
302+
it('should not rename the key if passed incorrect arguments', function () {
303+
t = new Tuple(["blah", {two: 2}, {three: 7}]);
304+
expect(t.findValue(0)).to.deep.equal("blah");
305+
expect(t.findValue(1)).to.deep.equal(2);
306+
expect(t.findValue(2)).to.deep.equal(7);
307+
expect(t.findValue('three')).to.deep.equal(7);
308+
expect(t.findValue('two')).to.deep.equal(2);
309+
expect(t.findValue(3)).to.deep.equal(undefined);
310+
t.renameElement(0,1);
311+
expect(t.findValue(0)).to.deep.equal("blah");
312+
expect(t.findValue(1)).to.deep.equal(2);
313+
expect(t.findValue(2)).to.deep.equal(7);
314+
expect(t.findValue('three')).to.deep.equal(7);
315+
expect(t.findValue('two')).to.deep.equal(2);
316+
expect(t.findValue(3)).to.deep.equal(undefined);
317+
t.renameElement(0,"abc");
318+
expect(t.findValue(0)).to.deep.equal("blah");
319+
expect(t.findValue(1)).to.deep.equal(2);
320+
expect(t.findValue(2)).to.deep.equal(7);
321+
expect(t.findValue('three')).to.deep.equal(7);
322+
expect(t.findValue('two')).to.deep.equal(2);
323+
expect(t.findValue(3)).to.deep.equal(undefined);
324+
t.renameElement("abc",1);
325+
expect(t.findValue(0)).to.deep.equal("blah");
326+
expect(t.findValue(1)).to.deep.equal(2);
327+
expect(t.findValue(2)).to.deep.equal(7);
328+
expect(t.findValue('three')).to.deep.equal(7);
329+
expect(t.findValue('two')).to.deep.equal(2);
330+
expect(t.findValue(3)).to.deep.equal(undefined);
331+
t.renameElement("four","five");
332+
expect(t.findValue(0)).to.deep.equal("blah");
333+
expect(t.findValue(1)).to.deep.equal(2);
334+
expect(t.findValue(2)).to.deep.equal(7);
335+
expect(t.findValue('three')).to.deep.equal(7);
336+
expect(t.findValue('two')).to.deep.equal(2);
337+
expect(t.findValue(3)).to.deep.equal(undefined);
338+
expect(t.findValue('four')).to.deep.equal(undefined);
339+
expect(t.findValue('five')).to.deep.equal(undefined);
171340
});
172341

173342
it('should rename the key if the correct arguments are passed in', function () {
@@ -188,4 +357,5 @@ describe('Tuple', function() {
188357
expect(t.findValue(3)).to.deep.equal(undefined);
189358
});
190359

191-
});
360+
});
361+

tupleDataStructure/tupleJS.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,61 @@
1+
// swift tuple example: ("blah", two: 2, (5, three: 3))
2+
// input into Tuple class, expected in form of an array: var myTupleInput = ["blah", {two: 2}, [5, {three: 3}]];
3+
// creating the tuple object: var t = new Tuple(myTupleInput);
4+
// internal state of the tuple:
5+
/*
6+
{
7+
0: {val: "blah"},
8+
1: {val: 2, key: "two"},
9+
"two": {val: 2, key: 1},
10+
2: {
11+
0: {val: "blah"},
12+
1: {val: 3, key: "three"},
13+
"three": {val: 3, key: 1},
14+
},
15+
16+
17+
}
18+
*/
19+
20+
//TODO:
21+
/*
22+
1.) Create special error messages
23+
2.)
24+
*/
25+
126
function Tuple(tuple) {
2-
//assume the tuple has the form of ["blah", {two: 2}, 5,...], for now assuming no recursive tuples
327
this.tup = {};
428
if (Array.isArray(tuple)) {
529
for (var i = 0; i < tuple.length; i++) {
630
if (typeof tuple[i] === "object" && !Array.isArray(tuple[i])) {
31+
//Assuming that JS still typecasts the key of any object into a string
732
var key = Object.keys(tuple[i])[0];
833
var val = tuple[i][key];
934
this.tup[key] = {'val': val, 'key': i};
1035
this.tup[i] = {'val': val, 'key': key};
36+
} else if (typeof tuple[i] === "boolean" || typeof tuple[i] === "number" || typeof tuple[i] === "string") {
37+
this.tup[i] = {'val': tuple[i]};
1138
} else if (Array.isArray(tuple[i])) {
12-
return null; //temporary
39+
this.tup[i] = (new Tuple(tuple[i]))._tupleToObject();
1340
} else {
14-
this.tup[i] = {'val': tuple[i]};
41+
//return error if the input is incorrect
42+
return null;
1543
}
44+
1645
}
1746
}
18-
// console.dir(this.tup);
19-
};
20-
// swift: ("blah", two: 2, 5);
21-
// new Tuple(["blah", {two: 2}, 5]);
2247

23-
// {
24-
// 0: {val: "blah"},
25-
// 1: {val: 2, key: "two"},
26-
// 2: {val: 5},
27-
// "two": {val: 2, key: 1}
28-
// }
48+
console.dir(this.tup);
49+
2950

51+
};
52+
53+
Tuple.prototype._tupleToObject = function() {
54+
if (this instanceof Tuple) {
55+
return this.tup;
56+
}
57+
return false;
58+
};
3059

3160
Tuple.prototype.findValue = function(keyOrIndex){
3261
var x = this.tup[keyOrIndex]
@@ -90,4 +119,6 @@ function Tuple(tuple) {
90119
return false;
91120
};
92121

122+
Tuple.prototype.constructor = Tuple;
123+
93124
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