Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-sorted-set

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-sorted-set - npm Package Compare versions

Comparing version 0.6.0 to 0.7.0

2

lib/SortedSet/AbstractBinaryTreeStrategy.js

@@ -59,3 +59,3 @@ "use strict";

return node !== null && node.value === value;
return node !== null && comparator(node.value, value) === 0;
}

@@ -62,0 +62,0 @@

@@ -105,3 +105,3 @@ "use strict";

if (this.data[index] !== value) {
if (this.comparator(this.data[index], value) !== 0) {
throw 'Value not in set';

@@ -119,3 +119,3 @@ }

const index = binarySearchForIndex(this.data, value, this.comparator);
return this.index !== this.data.length && this.data[index] === value;
return this.index !== this.data.length && this.comparator(this.data[index], value) === 0;
}

@@ -122,0 +122,0 @@

@@ -72,3 +72,3 @@ "use strict";

if (this.root != null) {
if (this.root !== null) {
let parent = this.root;

@@ -75,0 +75,0 @@ let leftOrRight = null;

@@ -160,3 +160,3 @@ "use strict";

if (h.value !== value && compare(value, h.value) < 0) {
if (compare(value, h.value) < 0) {
if (h.left === null) {

@@ -177,3 +177,3 @@ throw 'Value not in set';

if (h.right === null) {
if (value === h.value) {
if (compare(value, h.value) === 0) {
return null; // leaf node; LLRB assures no left value here

@@ -189,3 +189,3 @@ } else {

if (value === h.value) {
if (compare(value, h.value) === 0) {
h.value = findMinNode(h.right).value;

@@ -192,0 +192,0 @@ h.right = removeMinNode(h.right);

{
"name": "js-sorted-set",
"version": "0.6.0",
"version": "0.7.0",
"description": "Sorted set data structures",

@@ -5,0 +5,0 @@ "main": "./lib/SortedSet.js",

@@ -206,4 +206,4 @@ Sorted Set

3. Write the behavior you expect in `test/`
4. Edit files in `coffee/` until `npm test` says you're done
5. Run `npm run dist` to update build products in the `dist/` folder
4. Edit files in `src/` until `npm test` says you're done
5. Run `npm run build` to update build products
6. Submit a pull request

@@ -210,0 +210,0 @@

@@ -221,3 +221,3 @@ (function (global, factory) {

if (this.data[index] !== value) {
if (this.comparator(this.data[index], value) !== 0) {
throw 'Value not in set';

@@ -235,3 +235,3 @@ }

const index = binarySearchForIndex(this.data, value, this.comparator);
return this.index !== this.data.length && this.data[index] === value;
return this.index !== this.data.length && this.comparator(this.data[index], value) === 0;
}

@@ -462,3 +462,3 @@

return node !== null && node.value === value;
return node !== null && comparator(node.value, value) === 0;
}

@@ -538,3 +538,3 @@

if (this.root != null) {
if (this.root !== null) {
let parent = this.root;

@@ -718,3 +718,3 @@ let leftOrRight = null;

if (h.value !== value && compare(value, h.value) < 0) {
if (compare(value, h.value) < 0) {
if (h.left === null) {

@@ -735,3 +735,3 @@ throw 'Value not in set';

if (h.right === null) {
if (value === h.value) {
if (compare(value, h.value) === 0) {
return null; // leaf node; LLRB assures no left value here

@@ -747,3 +747,3 @@ } else {

if (value === h.value) {
if (compare(value, h.value) === 0) {
h.value = findMinNode(h.right).value;

@@ -750,0 +750,0 @@ h.right = removeMinNode(h.right);

@@ -48,3 +48,3 @@

}
return node !== null && node.value === value;
return node !== null && comparator(node.value, value) === 0;
}

@@ -51,0 +51,0 @@

@@ -90,3 +90,3 @@

const index = binarySearchForIndex(this.data, value, this.comparator);
if (this.data[index] !== value) {
if (this.comparator(this.data[index], value) !== 0) {
throw 'Value not in set';

@@ -103,3 +103,3 @@ }

const index = binarySearchForIndex(this.data, value, this.comparator);
return this.index !== this.data.length && this.data[index] === value;
return this.index !== this.data.length && this.comparator(this.data[index], value) === 0;
}

@@ -106,0 +106,0 @@

@@ -55,3 +55,3 @@ import AbstractBinaryTreeStrategy from './AbstractBinaryTreeStrategy';

const compare = this.comparator;
if (this.root != null) {
if (this.root !== null) {
let parent = this.root;

@@ -58,0 +58,0 @@ let leftOrRight = null;

@@ -135,3 +135,3 @@

}
if (h.value !== value && compare(value, h.value) < 0) {
if (compare(value, h.value) < 0) {
if (h.left === null) {

@@ -149,3 +149,3 @@ throw 'Value not in set';

if (h.right === null) {
if (value === h.value) {
if (compare(value, h.value) === 0) {
return null; // leaf node; LLRB assures no left value here

@@ -159,3 +159,3 @@ } else {

}
if (value === h.value) {
if (compare(value, h.value) === 0) {
h.value = findMinNode(h.right).value;

@@ -162,0 +162,0 @@ h.right = removeMinNode(h.right);

@@ -0,312 +1,349 @@

const SortedSet = require('../../lib/SortedSet')
const SortedSet = require('../../lib/SortedSet');
const numberComparator = (a, b) => {
return a - b;
};
return a - b
}
const describeStrategy = (description, strategy) => {
return describe(description, function() {
var priv;
priv = void 0;
describe('starting empty', function() {
beforeEach(function() {
return priv = new strategy({
describe(description, function () {
let priv
describe('starting empty', function () {
beforeEach(function () {
priv = new strategy({
comparator: numberComparator
});
});
it('should not contain a value', function() {
return expect(priv.contains(2)).to.eq(false);
});
it('should store its data in an array for easy testing', function() {
return expect(priv.toArray()).to.deep.eq([]);
});
it('should insert an element', function() {
priv.insert(4);
return expect(priv.toArray()).to.deep.eq([4]);
});
it('should fail to remove an element', function() {
return expect(function() {
return priv.remove(4);
}).to.throw('Value not in set');
});
it('should return an iterator with no next or previous', function() {
var iterator;
iterator = priv.findIterator(4);
expect(iterator.hasNext()).to.eq(false);
expect(iterator.hasPrevious()).to.eq(false);
expect(iterator.next()).to.eq(null);
expect(iterator.previous()).to.eq(null);
return expect(iterator.value()).to.eq(null);
});
it('should return a beginIterator', function() {
var iterator;
iterator = priv.beginIterator();
return expect(iterator.value()).to.eq(null);
});
it('should return an endIterator', function() {
var iterator;
iterator = priv.endIterator();
return expect(iterator.value()).to.eq(null);
});
return it('should do nothing in forEachImpl()', function() {
var callback;
callback = sinon.spy();
priv.forEachImpl(callback);
return expect(callback).not.to.have.been.called;
});
});
describe('with some numbers', function() {
beforeEach(function() {
})
})
it('should not contain a value', function () {
expect(priv.contains(2)).to.eq(false)
})
it('should store its data in an array for easy testing', function () {
expect(priv.toArray()).to.deep.eq([])
})
it('should insert an element', function () {
priv.insert(4)
expect(priv.toArray()).to.deep.eq([4])
})
it('should fail to remove an element', function () {
expect(function () {
priv.remove(4)
}).to.throw('Value not in set')
})
it('should return an iterator with no next or previous', function () {
const iterator = priv.findIterator(4)
expect(iterator.hasNext()).to.eq(false)
expect(iterator.hasPrevious()).to.eq(false)
expect(iterator.next()).to.eq(null)
expect(iterator.previous()).to.eq(null)
expect(iterator.value()).to.eq(null)
})
it('should return a beginIterator', function () {
const iterator = priv.beginIterator()
expect(iterator.value()).to.eq(null)
})
it('should return an endIterator', function () {
const iterator = priv.endIterator()
expect(iterator.value()).to.eq(null)
})
it('should do nothing in forEachImpl()', function () {
const callback = sinon.spy()
priv.forEachImpl(callback)
expect(callback).not.to.have.been.called
})
})
describe('with some numbers', function () {
beforeEach(function () {
priv = new strategy({
comparator: numberComparator
});
})
// Insert in this order so binary tree isn't one-sided
priv.insert(2);
priv.insert(1);
return priv.insert(3);
});
it('should insert at the beginning', function() {
priv.insert(0);
return expect(priv.toArray()).to.deep.eq([0, 1, 2, 3]);
});
it('should insert in the middle', function() {
priv.insert(2.5);
return expect(priv.toArray()).to.deep.eq([1, 2, 2.5, 3]);
});
it('should insert at the end', function() {
priv.insert(4);
return expect(priv.toArray()).to.deep.eq([1, 2, 3, 4]);
});
it('should remove from the beginning', function() {
priv.remove(1);
return expect(priv.toArray()).to.deep.eq([2, 3]);
});
it('should remove from the end', function() {
priv.remove(3);
return expect(priv.toArray()).to.deep.eq([1, 2]);
});
it('should remove from the middle', function() {
priv.remove(2);
return expect(priv.toArray()).to.deep.eq([1, 3]);
});
it('should clear', function() {
priv.clear();
return expect(priv.toArray()).to.deep.eq([]);
});
it('should allow insert after clear', function() {
priv.clear();
priv.insert(4);
priv.insert(2);
return expect(priv.toArray()).to.deep.eq([2, 4]);
});
it('should contain the first value', function() {
return expect(priv.contains(1)).to.eq(true);
});
it('should contain the last value', function() {
return expect(priv.contains(3)).to.eq(true);
});
it('should contain a middle value', function() {
return expect(priv.contains(2)).to.eq(true);
});
it('should not contain a value below the lowest', function() {
return expect(priv.contains(0)).to.eq(false);
});
it('should not contain a value above the highest', function() {
return expect(priv.contains(4)).to.eq(false);
});
it('should not contain a value in between two values', function() {
return expect(priv.contains(1.5)).to.eq(false);
});
it('should return false from contain', function() {
return expect(priv.contains(4)).to.eq(false);
});
it('should return a begin iterator', function() {
var iterator;
iterator = priv.beginIterator();
expect(iterator.previous()).to.eq(null);
return expect(iterator.value()).to.eq(1);
});
it('should return an end iterator', function() {
var iterator;
iterator = priv.endIterator();
expect(iterator.next()).to.eq(null);
return expect(iterator.value()).to.eq(null);
});
it('should find an iterator', function() {
var iterator;
iterator = priv.findIterator(2);
return expect(iterator.value()).to.eq(2);
});
it('should find an iterator between values', function() {
var iterator;
iterator = priv.findIterator(1.5);
return expect(iterator.value()).to.eq(2);
});
it('should find an iterator with a value above the max', function() {
var iterator;
iterator = priv.findIterator(3.5);
return expect(iterator.value()).to.eq(null);
});
it('should find an iterator with a value below the min', function() {
var iterator;
iterator = priv.findIterator(0.5);
return expect(iterator.value()).to.eq(1);
});
it('should find a previous iterator', function() {
var iterator;
iterator = priv.findIterator(2).previous();
return expect(iterator.value()).to.eq(1);
});
it('should find a next iterator', function() {
var iterator;
iterator = priv.findIterator(2).next();
return expect(iterator.value()).to.eq(3);
});
it('should step to previous from the end iterator', function() {
var iterator;
iterator = priv.endIterator().previous();
return expect(iterator.value()).to.eq(3);
});
it('should step to end from a previous iterator', function() {
var iterator;
iterator = priv.findIterator(3).next();
return expect(iterator.value()).to.eq(null);
});
it('should fail to setValue()', function() {
var iterator;
iterator = priv.findIterator(2);
return expect(function() {
return iterator.setValue(2.5);
}).to.throw();
});
return it('should iterate in forEachImpl', function() {
var set, spy, thisArg;
set = 'foo';
thisArg = 'moo';
spy = sinon.spy();
priv.forEachImpl(spy, set, thisArg);
expect(spy).to.have.callCount(3);
expect(spy.thisValues[0]).to.eq(thisArg);
expect(spy.args[0]).to.deep.eq([1, 0, set]);
expect(spy.args[1]).to.deep.eq([2, 1, set]);
return expect(spy.args[2]).to.deep.eq([3, 2, set]);
});
});
describe('with allowSetValue', function() {
beforeEach(function() {
priv.insert(2)
priv.insert(1)
priv.insert(3)
})
it('should insert at the beginning', function () {
priv.insert(0)
expect(priv.toArray()).to.deep.eq([0, 1, 2, 3])
})
it('should insert in the middle', function () {
priv.insert(2.5)
expect(priv.toArray()).to.deep.eq([1, 2, 2.5, 3])
})
it('should insert at the end', function () {
priv.insert(4)
expect(priv.toArray()).to.deep.eq([1, 2, 3, 4])
})
it('should remove from the beginning', function () {
priv.remove(1)
expect(priv.toArray()).to.deep.eq([2, 3])
})
it('should remove from the end', function () {
priv.remove(3)
expect(priv.toArray()).to.deep.eq([1, 2])
})
it('should remove from the middle', function () {
priv.remove(2)
expect(priv.toArray()).to.deep.eq([1, 3])
})
it('should clear', function () {
priv.clear()
expect(priv.toArray()).to.deep.eq([])
})
it('should allow insert after clear', function () {
priv.clear()
priv.insert(4)
priv.insert(2)
expect(priv.toArray()).to.deep.eq([2, 4])
})
it('should contain the first value', function () {
expect(priv.contains(1)).to.eq(true)
})
it('should contain the last value', function () {
expect(priv.contains(3)).to.eq(true)
})
it('should contain a middle value', function () {
expect(priv.contains(2)).to.eq(true)
})
it('should not contain a value below the lowest', function () {
expect(priv.contains(0)).to.eq(false)
})
it('should not contain a value above the highest', function () {
expect(priv.contains(4)).to.eq(false)
})
it('should not contain a value in between two values', function () {
expect(priv.contains(1.5)).to.eq(false)
})
it('should return false from contain', function () {
expect(priv.contains(4)).to.eq(false)
})
it('should return a begin iterator', function () {
const iterator = priv.beginIterator()
expect(iterator.previous()).to.eq(null)
expect(iterator.value()).to.eq(1)
})
it('should return an end iterator', function () {
const iterator = priv.endIterator()
expect(iterator.next()).to.eq(null)
expect(iterator.value()).to.eq(null)
})
it('should find an iterator', function () {
const iterator = priv.findIterator(2)
expect(iterator.value()).to.eq(2)
})
it('should find an iterator between values', function () {
const iterator = priv.findIterator(1.5)
expect(iterator.value()).to.eq(2)
})
it('should find an iterator with a value above the max', function () {
const iterator = priv.findIterator(3.5)
expect(iterator.value()).to.eq(null)
})
it('should find an iterator with a value below the min', function () {
const iterator = priv.findIterator(0.5)
expect(iterator.value()).to.eq(1)
})
it('should find a previous iterator', function () {
const iterator = priv.findIterator(2).previous()
expect(iterator.value()).to.eq(1)
})
it('should find a next iterator', function () {
const iterator = priv.findIterator(2).next()
expect(iterator.value()).to.eq(3)
})
it('should step to previous from the end iterator', function () {
const iterator = priv.endIterator().previous()
expect(iterator.value()).to.eq(3)
})
it('should step to end from a previous iterator', function () {
const iterator = priv.findIterator(3).next()
expect(iterator.value()).to.eq(null)
})
it('should fail to setValue()', function () {
const iterator = priv.findIterator(2)
expect(function () {
iterator.setValue(2.5)
}).to.throw()
})
it('should iterate in forEachImpl', function () {
const set = 'foo'
const thisArg = 'moo'
const spy = sinon.spy()
priv.forEachImpl(spy, set, thisArg)
expect(spy).to.have.callCount(3)
expect(spy.thisValues[0]).to.eq(thisArg)
expect(spy.args[0]).to.deep.eq([1, 0, set])
expect(spy.args[1]).to.deep.eq([2, 1, set])
expect(spy.args[2]).to.deep.eq([3, 2, set])
})
})
describe('with objects for which cmp(a, b) === 0 and a !== b', function () {
beforeEach(function () {
priv = new strategy({
comparator: (a, b) => a.id - b.id
})
// Insert in this order so binary tree isn't one-sided
priv.insert({ id: 2 })
priv.insert({ id: 1 })
priv.insert({ id: 3 })
})
it('should insert in the middle', function () {
priv.insert({ id: 2.5 })
expect(priv.toArray()).to.deep.eq([{ id: 1 }, { id: 2 }, { id: 2.5 }, { id: 3 }])
})
it('should remove from the beginning', function () {
priv.remove({ id: 1 })
expect(priv.toArray()).to.deep.eq([{ id: 2 }, { id: 3 }])
})
it('should remove from the end', function () {
priv.remove({ id: 3 })
expect(priv.toArray()).to.deep.eq([{ id: 1 }, { id: 2 }])
})
it('should remove from the middle', function () {
priv.remove({ id: 2 })
expect(priv.toArray()).to.deep.eq([{ id: 1 }, { id: 3 }])
})
it('should contain a middle value', function () {
expect(priv.contains({ id: 2 })).to.eq(true)
})
it('should not contain a value in between two values', function () {
expect(priv.contains({ id: 1.5 })).to.eq(false)
})
it('should find an iterator', function () {
const iterator = priv.findIterator({ id: 2 })
expect(iterator.value()).to.deep.eq({ id: 2 })
})
it('should find an iterator between values', function () {
const iterator = priv.findIterator({ id: 1.5 })
expect(iterator.value()).to.deep.eq({ id: 2 })
})
})
describe('with allowSetValue', function () {
beforeEach(function () {
priv = new strategy({
comparator: numberComparator,
allowSetValue: true
});
priv.insert(1);
return priv.insert(2);
});
it('should allow you to use setValue(), even to do something stupid', function() {
var iterator;
iterator = priv.findIterator(2);
iterator.setValue(0);
return expect(priv.toArray()).to.deep.eq([1, 0]);
});
return it('should not allow setValue() on an end iterator', function() {
var iterator;
iterator = priv.endIterator();
return expect(function() {
return iterator.setValue(2.5);
}).to.throw();
});
});
describe('with throw behavior on insert conflict', function() {
beforeEach(function() {
var comparator, onInsertConflict;
comparator = function(a, b) {
return a.v - b.v;
};
onInsertConflict = SortedSet.OnInsertConflictThrow;
priv = new strategy({comparator, onInsertConflict});
priv.insert({
v: 1,
q: 'a'
});
return priv.insert({
v: 2,
q: 'b'
});
});
return it('should throw when inserting an element that matches another', function() {
var err;
return expect(() => priv.insert({ v: 1, q: 'c' })).to.throw('Value already in set');
});
});
describe('with replace behavior on insert conflict', function() {
beforeEach(function() {
var comparator, onInsertConflict;
comparator = function(a, b) {
return a.v - b.v;
};
onInsertConflict = SortedSet.OnInsertConflictReplace;
priv = new strategy({comparator, onInsertConflict});
priv.insert({
v: 1,
q: 'a'
});
return priv.insert({
v: 2,
q: 'b'
});
});
return it('should replace a matching element with the new element', function() {
priv.insert({
v: 1,
q: 'c'
});
return expect(priv.toArray()).to.deep.eq([
{
v: 1,
q: 'c'
},
{
v: 2,
q: 'b'
}
]);
});
});
return describe('with ignore behavior on insert conflict', function() {
beforeEach(function() {
var comparator, onInsertConflict;
comparator = function(a, b) {
return a.v - b.v;
};
onInsertConflict = SortedSet.OnInsertConflictIgnore;
priv = new strategy({comparator, onInsertConflict});
priv.insert({
v: 1,
q: 'a'
});
return priv.insert({
v: 2,
q: 'b'
});
});
return it('should ignore the new element when inserting an element that matches another ', function() {
priv.insert({
v: 1,
q: 'c'
});
return expect(priv.toArray()).to.deep.eq([
{
v: 1,
q: 'a'
},
{
v: 2,
q: 'b'
}
]);
});
});
});
})
priv.insert(1)
priv.insert(2)
})
it('should allow you to use setValue(), even to do something stupid', function () {
const iterator = priv.findIterator(2)
iterator.setValue(0)
expect(priv.toArray()).to.deep.eq([1, 0])
})
it('should not allow setValue() on an end iterator', function () {
const iterator = priv.endIterator()
expect(function () {
iterator.setValue(2.5)
}).to.throw()
})
})
describe('with throw behavior on insert conflict', function () {
beforeEach(function () {
const comparator = function (a, b) {
return a.v - b.v
}
const onInsertConflict = SortedSet.OnInsertConflictThrow
priv = new strategy({ comparator, onInsertConflict })
priv.insert({ v: 1, q: 'a' })
priv.insert({ v: 2, q: 'b' })
})
it('should throw when inserting an element that matches another', function () {
expect(() => priv.insert({ v: 1, q: 'c' })).to.throw('Value already in set')
})
})
describe('with replace behavior on insert conflict', function () {
beforeEach(function () {
let comparator, onInsertConflict
comparator = function (a, b) {
return a.v - b.v
}
onInsertConflict = SortedSet.OnInsertConflictReplace
priv = new strategy({ comparator, onInsertConflict })
priv.insert({ v: 1, q: 'a' })
priv.insert({ v: 2, q: 'b' })
})
it('should replace a matching element with the new element', function () {
priv.insert({ v: 1, q: 'c' })
expect(priv.toArray()).to.deep.eq([
{ v: 1, q: 'c' },
{ v: 2, q: 'b' }
])
})
})
describe('with ignore behavior on insert conflict', function () {
beforeEach(function () {
const comparator = function (a, b) {
return a.v - b.v
}
const onInsertConflict = SortedSet.OnInsertConflictIgnore
priv = new strategy({ comparator, onInsertConflict })
priv.insert({ v: 1, q: 'a' })
priv.insert({ v: 2, q: 'b' })
})
it('should ignore the new element when inserting an element that matches another ', function () {
priv.insert({ v: 1, q: 'c' })
expect(priv.toArray()).to.deep.eq([
{ v: 1, q: 'a' },
{ v: 2, q: 'b' }
])
})
})
})
}
module.exports.describeStrategy = describeStrategy;
module.exports.describeStrategy = describeStrategy

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc