Skip to content

Commit

Permalink
test: make named require test works
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Jan 8, 2024
1 parent 2e442de commit 3ca8235
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion benchmark/interval-tree/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ console.log('Intervals:', SIZE);
console.time('StaticIntervalTree.from');
var tree = StaticIntervalTree.from(INTERVALS);
console.timeEnd('StaticIntervalTree.from');
console.log('Tree height: ' + tree.height)
console.log('Tree height: ' + tree.height);

p = random(0, MAX);

Expand Down
2 changes: 1 addition & 1 deletion benchmark/misc/dynamic-arrays.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var SIZE = 100000
var SIZE = 100000;

function Wrapper() {
this.array = new Float64Array(SIZE);
Expand Down
32 changes: 16 additions & 16 deletions benchmark/misc/hashmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ function exponentialSearch(array, key) {
return -1;
}

function interpolationSearch(arrayToSearch, valueToSearch){
function interpolationSearch(arrayToSearch, valueToSearch) {
var length = arrayToSearch.length;
var low = 0;
var high = length-1;
var high = length - 1;
var position = -1;
var delta = -1;
while(low <= high
while (low <= high
&& valueToSearch >= arrayToSearch[low]
&& valueToSearch <= arrayToSearch[high]){
delta = (valueToSearch-arrayToSearch[low])/(arrayToSearch[high]-arrayToSearch[low]);
position = low + Math.floor((high-low)*delta);
if (arrayToSearch[position] == valueToSearch){
&& valueToSearch <= arrayToSearch[high]) {
delta = (valueToSearch - arrayToSearch[low]) / (arrayToSearch[high] - arrayToSearch[low]);
position = low + Math.floor((high - low) * delta);
if (arrayToSearch[position] == valueToSearch) {
return position;
}
if (arrayToSearch[position] < valueToSearch){
if (arrayToSearch[position] < valueToSearch) {
low = position + 1;
} else {
high = position - 1;
Expand Down Expand Up @@ -104,7 +104,7 @@ function optimized(array, value, lo, hi) {
}

return -1;
};
}

function better(array, key) {
var mid = -1;
Expand All @@ -123,7 +123,7 @@ function better(array, key) {
else if (key > current)
lo = mid + 1;
else
return mid
return mid;
}

return -1;
Expand All @@ -145,7 +145,7 @@ function PerfectMap(strings) {
PerfectMap.prototype.set = function(key, value) {
var index = binarySearch(this.hashes, fnv32a(key));
this.values[index] = value;
}
};

PerfectMap.prototype.get = function(key, value) {
var index = binarySearch(this.hashes, fnv32a(key));
Expand All @@ -159,7 +159,7 @@ var map = new Map();
var sorted = words.slice().sort();

perfect.set('hello', 36);
console.log(perfect.get('hello'))
console.log(perfect.get('hello'));

console.time('Perfect set');
for (var i = 0; i < words.length; i++)
Expand Down Expand Up @@ -217,9 +217,9 @@ function hash(str) {
'use asm';

var h = 5381,
i = str.length;
i = str.length;

while(i) {
while (i) {
h *= 33;
h ^= str.charCodeAt(--i);
}
Expand Down Expand Up @@ -298,7 +298,7 @@ console.timeEnd('Get');

console.time('GetObject');
for (var w = 0, y = words.length; w < y; w++)
v = ob[words[w]]
v = ob[words[w]];
console.timeEnd('GetObject');

console.time('GetMap');
Expand All @@ -313,7 +313,7 @@ console.timeEnd('GetMap');

console.time('MissesObject');
for (var w = 0, y = words.length; w < y; w++)
v = ob[randomString(4, 10)]
v = ob[randomString(4, 10)];
console.timeEnd('MissesObject');

console.time('MissesMap');
Expand Down
6 changes: 3 additions & 3 deletions experiments/critbit.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function criticalGt(a, b) {
}

function InternalNode(critical) {
this.critical = critical
this.critical = critical;
this.left = null;
this.right = null;
}
Expand Down Expand Up @@ -270,7 +270,7 @@ CritBitTree.prototype.delete = function(key) {

var node = this.root,
parent = null,
wentRightForParent = false
wentRightForParent = false;
grandparent = null,
wentRightForGranparent = false;

Expand Down Expand Up @@ -358,7 +358,7 @@ function log(tree) {

const title = printNode;

const children = node => (node instanceof ExternalNode ? null : [node.left , node.right]);
const children = node => (node instanceof ExternalNode ? null : [node.left, node.right]);

console.log(asciitree(tree.root, title, children));
}
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export {default as SuffixArray, GeneralizedSuffixArray} from './suffix-array';
export {default as SymSpell} from './symspell';
export {default as Trie} from './trie';
export {default as TrieMap} from './trie-map';
export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Array} from './vector';
export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Vector} from './vector';
export {default as VPTree} from './vp-tree';
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
var Heap = require('./heap.js'),
FibonacciHeap = require('./fibonacci-heap.js'),
SuffixArray = require('./suffix-array.js'),
BiMap = require('./bi-map.js');
BiMap = require('./bi-map.js'),
Vector = require('./vector.js');

module.exports = {
BiMap: BiMap,
Expand Down Expand Up @@ -55,6 +56,16 @@ module.exports = {
SymSpell: require('./symspell.js'),
Trie: require('./trie.js'),
TrieMap: require('./trie-map.js'),
Vector: require('./vector.js'),
Vector: Vector,
Uint8Vector: Vector.Uint8Vector,
Uint8ClampedVector: Vector.Uint8ClampedVector,
Int8Vector: Vector.Int8Vector,
Uint16Vector: Vector.Uint16Vector,
Int16Vector: Vector.Int16Vector,
Uint32Vector: Vector.Uint32Vector,
Int32Vector: Vector.Int32Vector,
Float32Vector: Vector.Float32Vector,
Float64Vector: Vector.Float64Vector,
PointerVector: Vector.PointerVector,
VPTree: require('./vp-tree.js')
};
2 changes: 1 addition & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export {default as SparseSet} from './sparse-set.js';
export {default as SymSpell} from './symspell.js';
export {default as Trie} from './trie.js';
export {default as TrieMap} from './trie-map.js';
export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Array} from './vector.js';
export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Vector} from './vector.js';
export {default as VPTree} from './vp-tree.js';
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"version": "0.39.7",
"description": "Curated collection of data structures for the JavaScript/TypeScript.",
"scripts": {
"lint": "eslint --cache ./*.js ./*.mjs ./utils ./test",
"lint:fix": "eslint --cache --fix ./*.js ./*.mjs ./utils ./test",
"lint": "eslint --cache --ext .js,.mjs ./*.js ./*.mjs ./utils ./test",
"lint:fix": "eslint --cache --fix --ext .js,.mjs ./*.js ./*.mjs ./utils ./test",
"prepublishOnly": "npm run lint && npm test && npm run test:types",
"test": "mocha",
"test:cjs": "cd test/cjs && npm i && node require.js",
"test:cjs": "cd test/cjs && npm i && node named-require.js",
"test:esm": "cd test/esm && npm i && node import.mjs",
"test:types": "tsc --target es2015 --noEmit --noImplicitAny --noImplicitReturns ./test/types.ts"
},
Expand Down
8 changes: 5 additions & 3 deletions test/cjs/require.js → test/cjs/named-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ fuzzymultimapadd.add({title: 'Hello', n: 45});
* HashedArrayTree.
*/
let hashedArrayTree = new HashedArrayTree(Int8Array, 34);
hashedArrayTree.set(3, 4);
hashedArrayTree.push(1);
hashedArrayTree.set(0, 4);

/**
* Heap.
Expand Down Expand Up @@ -189,7 +190,7 @@ let lrumwdWasRemoved = lrumwd.delete('one');
let multiset = new MultiSet();
multiset.add(45);
multiset = MultiSet.from([1, 2, 3]);
multiset = MultiSet.from({'one': 1});
multiset = MultiSet.from({one: 1});

/**
* MultiMap.
Expand Down Expand Up @@ -307,7 +308,8 @@ arrayTrieMap.set(['the', 'cat', 'eats', 'the', 'mouse'], 45);
* Vector.
*/
let vector = new Vector(Uint32Array, 10);
vector.set(45, 2);
vector.push(1);
vector.set(0, 2);

let uint16vector = Uint16Vector.from([1, 2, 3]);
uint16vector.pop();
Expand Down
8 changes: 5 additions & 3 deletions test/esm/import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ fuzzymultimapadd.add({title: 'Hello', n: 45});
* HashedArrayTree.
*/
let hashedArrayTree = new HashedArrayTree(Int8Array, 34);
hashedArrayTree.set(3, 4);
hashedArrayTree.push(1);
hashedArrayTree.set(0, 4);

/**
* Heap.
Expand Down Expand Up @@ -189,7 +190,7 @@ let lrumwdWasRemoved = lrumwd.delete('one');
let multiset = new MultiSet();
multiset.add(45);
multiset = MultiSet.from([1, 2, 3]);
multiset = MultiSet.from({'one': 1});
multiset = MultiSet.from({one: 1});

/**
* MultiMap.
Expand Down Expand Up @@ -307,7 +308,8 @@ arrayTrieMap.set(['the', 'cat', 'eats', 'the', 'mouse'], 45);
* Vector.
*/
let vector = new Vector(Uint32Array, 10);
vector.set(45, 2);
vector.push(1);
vector.set(0, 2);

let uint16vector = Uint16Vector.from([1, 2, 3]);
uint16vector.pop();
Expand Down
6 changes: 4 additions & 2 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ fuzzymultimapadd.add({title: 'Hello', n: 45});
* HashedArrayTree.
*/
let hashedArrayTree: HashedArrayTree<number> = new HashedArrayTree(Int8Array, 34);
hashedArrayTree.set(3, 4);
hashedArrayTree.push(1);
hashedArrayTree.set(0, 4);

/**
* Heap.
Expand Down Expand Up @@ -308,7 +309,8 @@ arrayTrieMap.set(['the', 'cat', 'eats', 'the', 'mouse'], 45);
* Vector.
*/
let vector = new Vector(Uint32Array, 10);
vector.set(45, 2);
vector.push(1);
vector.set(0, 2);

let uint16vector = Uint16Vector.from([1, 2, 3]);
uint16vector.pop();
Expand Down
2 changes: 1 addition & 1 deletion vector.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ export class Uint16Vector extends TypedVector {}
export class Int32Vector extends TypedVector {}
export class Uint32Vector extends TypedVector {}
export class Float32Vector extends TypedVector {}
export class Float64Array extends TypedVector {}
export class Float64Vector extends TypedVector {}

0 comments on commit 3ca8235

Please sign in to comment.