Skip to content

Commit

Permalink
feat: add ESM support for named exports
Browse files Browse the repository at this point in the history
* fix variety of missing imports in both TS and CJS endpoints
* add exports testing
* renaming `Set` to `set` to avoid conflicts in CSJ endpoint and harmonize with other endpoints

Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
Jérôme Benoit authored Feb 2, 2024
1 parent 346ff4a commit f23da93
Show file tree
Hide file tree
Showing 21 changed files with 614 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test/*.ts
test/exports/dist/
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ jobs:
# npm will not run under 10.x or earlier and so they cannot be tested
node-version:
- 12.x
- 13.x
- 14.x
- 15.x
- 16.x
- 18.x
- 20.x
- latest

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

Expand All @@ -32,4 +31,5 @@ jobs:
npm i
npm run lint
npm test
npm run test:types
# Typescript does not work with node 12.x
[[ "${{ matrix.node-version }}" != "12.x" ]] && npm run test:exports || true
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.40.0 (provisional)

* Adding ESM named exports support (@jerome-benoit).
* Fixing `Set` operations CommonJS named export collision by renaming it to `set` (@jerome-benoit).
* Fixing missing `Uint8Vector`, `Uint8ClampedVector`, `Int8Vector`, `Uint16Vector`, `Int16Vector`, `Uint32Vector`, `Int32Vector`, `Float32Vector`, `Float64Vector`, `PointerVector` CommonJS named exports (@jerome-benoit).
* Fixing missing `PointerVector` TS exports (@jerome-benoit).

## 0.39.8

* Fixing `Float64Vector` TS exports (@atombrenner).
Expand Down
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
3 changes: 3 additions & 0 deletions bi-map.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export class InverseMap<K, V> implements Iterable<[K, V]> {
size: number;
inverse: BiMap<V, K>;

// Constructor
constructor(original: BiMap<K, V>);

// Methods
clear(): void;
set(key: K, value: V): this;
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
1 change: 1 addition & 0 deletions fibonacci-heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,5 @@ MaxFibonacciHeap.from = function(iterable, comparator) {
*/
FibonacciHeap.MinFibonacciHeap = FibonacciHeap;
FibonacciHeap.MaxFibonacciHeap = MaxFibonacciHeap;

module.exports = FibonacciHeap;
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, Float64Vector} from './vector';
export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Vector, PointerVector} from './vector';
export {default as VPTree} from './vp-tree';
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* Mnemonist Library Endpoint
* ===========================
* Mnemonist Library Endpoint (CommonJS)
* ======================================
*
* Exporting every data structure through a unified endpoint. Consumers
* of this library should prefer the modular access though.
*/
var Heap = require('./heap.js'),
FibonacciHeap = require('./fibonacci-heap.js'),
SuffixArray = require('./suffix-array.js');
SuffixArray = require('./suffix-array.js'),
Vector = require('./vector.js');

module.exports = {
BiMap: require('./bi-map.js'),
Expand Down Expand Up @@ -46,13 +47,23 @@ module.exports = {
Stack: require('./stack.js'),
SuffixArray: SuffixArray,
GeneralizedSuffixArray: SuffixArray.GeneralizedSuffixArray,
Set: require('./set.js'),
set: require('./set.js'),
SparseQueueSet: require('./sparse-queue-set.js'),
SparseMap: require('./sparse-map.js'),
SparseSet: require('./sparse-set.js'),
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')
};
67 changes: 67 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Mnemonist Library Endpoint (ESM)
* =================================
*
* Exporting every data structure through a unified endpoint.
*/
import * as set from './set.js';
import {default as FibonacciHeap} from './fibonacci-heap.js';
const MinFibonacciHeap = FibonacciHeap.MinFibonacciHeap;
const MaxFibonacciHeap = FibonacciHeap.MaxFibonacciHeap;
import {default as Heap} from './heap.js';
const MinHeap = Heap.MinHeap;
const MaxHeap = Heap.MaxHeap;
import {default as SuffixArray} from './suffix-array.js';
const GeneralizedSuffixArray = SuffixArray.GeneralizedSuffixArray;
import {default as Vector} from './vector.js';
const Uint8Vector = Vector.Uint8Vector;
const Uint8ClampedVector = Vector.Uint8ClampedVector;
const Int8Vector = Vector.Int8Vector;
const Uint16Vector = Vector.Uint16Vector;
const Int16Vector = Vector.Int16Vector;
const Uint32Vector = Vector.Uint32Vector;
const Int32Vector = Vector.Int32Vector;
const Float32Vector = Vector.Float32Vector;
const Float64Vector = Vector.Float64Vector;
const PointerVector = Vector.PointerVector;

export {default as BiMap} from './bi-map.js';
export {default as BitSet} from './bit-set.js';
export {default as BitVector} from './bit-vector.js';
export {default as BloomFilter} from './bloom-filter.js';
export {default as BKTree} from './bk-tree.js';
export {default as CircularBuffer} from './circular-buffer.js';
export {default as DefaultMap} from './default-map.js';
export {default as DefaultWeakMap} from './default-weak-map.js';
export {default as FixedDeque} from './fixed-deque.js';
export {default as StaticDisjointSet} from './static-disjoint-set.js';
export {FibonacciHeap, MinFibonacciHeap, MaxFibonacciHeap};
export {default as FixedReverseHeap} from './fixed-reverse-heap.js';
export {default as FuzzyMap} from './fuzzy-map.js';
export {default as FuzzyMultiMap} from './fuzzy-multi-map.js';
export {default as HashedArrayTree} from './hashed-array-tree.js';
export {Heap, MinHeap, MaxHeap};
export {default as StaticIntervalTree} from './static-interval-tree.js';
export {default as InvertedIndex} from './inverted-index.js';
export {default as KDTree} from './kd-tree.js';
export {default as LinkedList} from './linked-list.js';
export {default as LRUCache} from './lru-cache.js';
export {default as LRUCacheWithDelete} from './lru-cache-with-delete.js';
export {default as LRUMap} from './lru-map.js';
export {default as LRUMapWithDelete} from './lru-map-with-delete.js';
export {default as MultiMap} from './multi-map.js';
export {default as MultiSet} from './multi-set.js';
export {default as PassjoinIndex} from './passjoin-index.js';
export {default as Queue} from './queue.js';
export {default as FixedStack} from './fixed-stack.js';
export {default as Stack} from './stack.js';
export {SuffixArray, GeneralizedSuffixArray};
export {set};
export {default as SparseQueueSet} from './sparse-queue-set.js';
export {default as SparseMap} from './sparse-map.js';
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 {Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Vector, PointerVector};
export {default as VPTree} from './vp-tree.js';
Loading

0 comments on commit f23da93

Please sign in to comment.