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

Update index.js #29

Closed
wants to merge 7 commits into from
Closed

Update index.js #29

wants to merge 7 commits into from

Conversation

antoniogs
Copy link

The swap method has been updated, for be able of to swap two elements using the keys, the tomes, or any combination of them.

Now is possible to make:
var myArray = [ 'bread', 'bacon', 'lettuce', 'tomato', 'egg', 'cheese' ];
myArrayTome = Tome.conjure(myArray);

myArrayTome.swap(0, 1);
myArrayTome.swap(myArrayTome[0], myArrayTome[1]);
myArrayTome.swap(0, myArrayTome[1]);
myArrayTome.swap(myArrayTome[0], 1);

The swap method has been updated, for be able of to swap two elements using the keys or the values, something as array.swap(key1, key2) or array.swap(someKey, someValue).

Now we can make:
var myArray = [ 'bread', 'bacon', 'lettuce', 'tomato', 'egg','cheese' ];
myArrayTome.swap(0,1);
myArrayTome.swap('cheese','egg');
myArrayTome.swap(1,'tomato');
myArrayTome.swap('bread',2);
@bjornstar
Copy link
Contributor

Please make sure all the tests pass.

If you add new behavior, please add appropriate tests for the new behavior.

The swap.js test file is updated for to check the new swap method behavior.

The swap method was updated without update the swap test file, so the swap test was launching validation errors.
@antoniogs
Copy link
Author

I changed the swap method but I didn't change the swap test. Sorry.
I am new at node and I didn't know about node automated tests.

I made a new pull, changing only the swap test.

@bjornstar
Copy link
Contributor

We want to be able to swap 2 tomes by either referencing the key or the tome itself.

We would like to be able to do all of the following:

var myArray = [ 'bread', 'bacon', 'lettuce', 'tomato', 'egg', 'cheese' ];
myArrayTome = Tome.conjure(myArray);
myArrayTome.swap(0,1);
myArrayTome.swap(0, myArrayTome[1]);
myArrayTome.swap(myArrayTome[0], 1);
myArrayTome.swap(myArrayTome[0], myArrayTome[1]);

@antoniogs
Copy link
Author

Using the pushed swap method, I ran the next code for test (the console.log output is on the right):

var myArray = [ 'bread', 'bacon', 'lettuce', 'tomato', 'egg','cheese' ];
var myArrayTome = Tome.conjure(myArray);

myArrayTome.swap(0,1);
console.log(JSON.stringify(myArrayTome)); ["bacon","bread","lettuce","tomato","egg","cheese"]
myArrayTome.swap(0, myArrayTome[1]);
console.log(JSON.stringify(myArrayTome)); ["bread","bacon","lettuce","tomato","egg","cheese"]
myArrayTome.swap(myArrayTome[0], 1);
console.log(JSON.stringify(myArrayTome)); ["bacon","bread","lettuce","tomato","egg","cheese"]
myArrayTome.swap(myArrayTome[0], myArrayTome[1]);
console.log(JSON.stringify(myArrayTome)); ["bread","bacon","lettuce","tomato","egg","cheese"]

var b = { b: { c: 1 }, d: { e: 1}, x: { y: 1}, y: { x: 1} };
var b = Tome.conjure(b);

b.swap('b','d');
console.log(JSON.stringify(b)); {"b":{"e":1},"d":{"c":1},"x":{"y":1},"y":{"x":1}}
b.swap('b',b.d);
console.log(JSON.stringify(b)); {"b":{"c":1},"d":{"e":1},"x":{"y":1},"y":{"x":1}}
b.swap(b.b,'d');
console.log(JSON.stringify(b)); {"b":{"e":1},"d":{"c":1},"x":{"y":1},"y":{"x":1}}
b.swap(b.b,b.d);
console.log(JSON.stringify(b)); {"b":{"c":1},"d":{"e":1},"x":{"y":1},"y":{"x":1}}

You can use it over arrays and over objects.

The matter can be when you have a 'b' key and a 'b' target.
The version 0.0.18 swap method uses swap(key, target).

The pushed swap uses swap(arg1,arg2).
For arg1 check the key and, if doesnt match, check other options.
For arg2 check the target and, if doesn't match, check the key.
So, for 'b' key and 'b' target, the pushed swap makes the same than the current swap, doesn't have a different and potentially problematic behavior.

@bjornstar
Copy link
Contributor

Swapping tomes based on their values is very problematic.

Case in point:

var a = { b: 'a', c: 'a', d: 'b' };
var b = Tome.conjure(a);

b.swap('a', b.d);

console.log(JSON.stringify(b));
> {"b":"b","c":"a","d":"a"}

I have no idea which tome I am going to swap b.d with, it could be b.b or b.c

@antoniogs
Copy link
Author

If there are many times the same value, swap checks the keys for to find the value and uses the first key.
It can be changed for to swap every times, not only the first. But it means a performance penalization.
If there is a general agreement about this, I can modify the value replacement: replace only once, replace all, to create a swapAll...

I agree with you about swapping values can be very problematic, in similar situations I prefer to use only keys. But after to read the issue #22, I see a logic about being able to make swap(key1,key2) or swap(value1,value2). I prefer don't use values, but If there is general agreement about this, the swap values capacity could remain and you uses "at your own risk".

@bjornstar
Copy link
Contributor

I don't want it to swap values, I only want it to swap keys and tomes.

@antoniogs antoniogs closed this Nov 27, 2013
@antoniogs antoniogs reopened this Nov 27, 2013
Swap method updated, for to allow only keys or Tomes.
Swap method has been updated for to use only keys or Tomes.

Swap test file has been updated for to check than you can't use other kind of parameters, as values.
@antoniogs
Copy link
Author

I modified the swap method for to swap only keys or tomes, and pushed the changes.

To swap any combination of key/tomes is possible, but if you try to swap a value you will obtain a exception.

@bjornstar
Copy link
Contributor

Before I get into the actual code that you've prepared, could you format the whitespace to match the style of the whitespace in the project?

While you're at it, see if there's not a more elegant way of doing this, multiple nested if statements are hard to follow.

@antoniogs
Copy link
Author

Yes, of course.
Today I am a bit busy, I'll try later or tomorrow 29/11.

Updated swap method, for for to eliminate the multiple nested if statements and to format the whitespaces.
@antoniogs
Copy link
Author

Updated swap method, for for to eliminate the multiple nested if statements and to format the whitespaces.

var arg2IsKey = false;
var arg2IsTome = false;
var obj2;
var key2;

Choose a reason for hiding this comment

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

I think you're having a lot of indentation issues on this PR. This project uses single tab indentation.

@antoniogs
Copy link
Author

I see that, but it is a surprise for me.

I edit the file using gedit, using only tab for indentation , from the beginning of lines, no spaces. When I finish, I paste the text in Github using the "Edit" Github option over the file text.

When I edit the file, I see the same indentation at my lines and at the other lines. And after to put the file in Github, it is the same. I see this.

indentation

By default gedit is using 8 spaces for tabulation. Could this be the problem?. Should I change the tabulation spaces value?. At the styleguide there is not info about a indentation space value.

@ronkorving
Copy link

That explains the title of your pull request :)

Please set up git on your development machine and try to use git instead of pasting code into github. GitHub supports this, but it's really not meant for this kind of contribution. It's useful when making a one-character change in a Readme file for example, but not for code.

You can find a free e-book on using Git here: http://git-scm.com/book

@antoniogs antoniogs closed this Dec 2, 2013
@antoniogs antoniogs reopened this Dec 2, 2013
@antoniogs
Copy link
Author

Thank you very much, ronkorving.

About git, I use it at the work. Push, branch.... no problem, but about to install and to configure a git repository, I made once looooooong time ago, and I forgot. I supposed than, for to edit node code and to push, a simple text editor and Git facilities were going to be the less problematic. Finally, it isn't so. I apologize for the inconvenience.

I'll make to maky using git as soon as possible, but now I am having a pair of days very busy, so maybe it delays a bit.

@antoniogs
Copy link
Author

Finally, I updated the indentation and pushed using only git, not github. I was very busy with the classical very urgent project, so I couldn't make the changes before.

I changed the push name to a more descriptive name, too. Now it is Swap method updated.

@bjornstar bjornstar closed this Nov 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants