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

Collections.js is not loading correctly #158

Open
bugs181 opened this issue Jul 21, 2016 · 2 comments
Open

Collections.js is not loading correctly #158

bugs181 opened this issue Jul 21, 2016 · 2 comments
Milestone

Comments

@bugs181
Copy link

bugs181 commented Jul 21, 2016

This was a tricky bug to track down.

Taking the code directly from the examples gives me some very weird results. The issue (in this case) is because the library isn't overriding built-ins.

Let me elaborate.

var Set = require("collections/set")

var object = { x: "hello" }
var set = new Set(["a", object])

console.log( set.add("b") )
// Expected: true
// Result: Set { 'a', { x: 'hello' }, 'b' }

console.log( set.toArray() )
// Expected: ["a",{"x":"hello"},"b"]
// Result: [ 'a', { x: 'hello' }, 'b' ]

console.log( set.add(object) )
// Expected: false
// Result: Set { 'a', { x: 'hello' }, 'b' }

console.log( set.toArray() )
// Expected: ["a",{"x":"hello"},"b"]
// Result: [ 'a', { x: 'hello' }, 'b' ]

In fact, removing the first line gives the exact same output. This leads me to believe that Set is using the built-in operator.

My setup is as follows:

OS:
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.10.1
BuildVersion: 14B25

Install:
$ npm install --save collections

Node version:
$ node -v
v5.10.0

npm version:
$ npm -v
3.8.3

@hendrul
Copy link

hendrul commented Mar 17, 2017

@bugs181 it seems the documentation is not correct, looking at the code I see this module adds attribute CollectionsSet to built-in Set if this is defined, else it define CollectionsSet as Set globally, so it should be...:

require('collections')
var nameSet = new Set.CollectionsSet(null, function (a, b) {
    return a.name === b.name;
}, function (object) {
    return object.name;
});
nameSet.add({name: "Kris", github: "kriskowal"});
// true
nameSet.add({name: "Stuart", github: "stuk"});
// true
nameSet.get({name: "Kris"});
// {"name":"Kris","github":"kriskowal"}
nameSet.add({name: "Stuart", github: "wrong"});
// false
nameSet.get({name: "Stuart"});
// {"name":"Stuart","github":"stuk"}

@kriskowal
Copy link
Member

Does anyone have an opinion of whether the documented behavior or the implemented behavior is better? Anyone else the time to make it so?

@hthetiot hthetiot added this to the 5.0.x milestone Dec 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants