Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 14, 2021
1 parent 85f49e2 commit a408ce9
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 85 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
if: matrix.node-version == 14
with:
fail_ci_if_error: true
# TODO: Disabled until `nyc` supports ESM.
# - uses: codecov/codecov-action@v1
# if: matrix.node-version == 14
# with:
# fail_ci_if_error: true
46 changes: 21 additions & 25 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
declare namespace QuickLRU {
interface Options<KeyType, ValueType> {
/**
The maximum number of milliseconds an item should remain in the cache.
export interface Options<KeyType, ValueType> {
/**
The maximum number of milliseconds an item should remain in the cache.
@default Infinity
@default Infinity
By default, `maxAge` will be `Infinity`, which means that items will never expire.
Lazy expiration upon the next write or read call.
By default, `maxAge` will be `Infinity`, which means that items will never expire.
Lazy expiration upon the next write or read call.
Individual expiration of an item can be specified by the `set(key, value, maxAge)` method.
*/
readonly maxAge?: number;
Individual expiration of an item can be specified by the `set(key, value, maxAge)` method.
*/
readonly maxAge?: number;

/**
The maximum number of items before evicting the least recently used items.
*/
readonly maxSize: number;
/**
The maximum number of items before evicting the least recently used items.
*/
readonly maxSize: number;

/**
Called right before an item is evicted from the cache.
/**
Called right before an item is evicted from the cache.
Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
*/
onEviction?: (key: KeyType, value: ValueType) => void;
}
Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
*/
onEviction?: (key: KeyType, value: ValueType) => void;
}

declare class QuickLRU<KeyType, ValueType> implements Iterable<[KeyType, ValueType]> {
export default class QuickLRU<KeyType, ValueType> implements Iterable<[KeyType, ValueType]> {
/**
The stored item count.
*/
Expand All @@ -39,7 +37,7 @@ declare class QuickLRU<KeyType, ValueType> implements Iterable<[KeyType, ValueTy
@example
```
import QuickLRU = require('quick-lru');
import QuickLRU from 'quick-lru';
const lru = new QuickLRU({maxSize: 1000});
Expand All @@ -52,7 +50,7 @@ declare class QuickLRU<KeyType, ValueType> implements Iterable<[KeyType, ValueTy
//=> '🌈'
```
*/
constructor(options: QuickLRU.Options<KeyType, ValueType>);
constructor(options: Options<KeyType, ValueType>);

[Symbol.iterator](): IterableIterator<[KeyType, ValueType]>;

Expand Down Expand Up @@ -123,5 +121,3 @@ declare class QuickLRU<KeyType, ValueType> implements Iterable<[KeyType, ValueTy
*/
entriesDescending(): IterableIterator<[KeyType, ValueType]>;
}

export = QuickLRU;
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

class QuickLRU {
export default class QuickLRU {
constructor(options = {}) {
if (!(options.maxSize && options.maxSize > 0)) {
throw new TypeError('`maxSize` must be a number greater than 0');
Expand All @@ -10,14 +8,16 @@ class QuickLRU {
throw new TypeError('`maxAge` must be a number greater than 0');
}

// TODO: Use private class fields when ESLint supports them.
this.maxSize = options.maxSize;
this.maxAge = options.maxAge || Infinity;
this.maxAge = options.maxAge || Number.POSITIVE_INFINITY;
this.onEviction = options.onEviction;
this.cache = new Map();
this.oldCache = new Map();
this._size = 0;
}

// TODO: Use private class methods when targeting Node.js 16.
_emitEvictions(cache) {
if (typeof this.onEviction !== 'function') {
return;
Expand Down Expand Up @@ -110,7 +110,7 @@ class QuickLRU {
}
}

set(key, value, {maxAge = this.maxAge === Infinity ? undefined : Date.now() + this.maxAge} = {}) {
set(key, value, {maxAge = this.maxAge === Number.POSITIVE_INFINITY ? undefined : Date.now() + this.maxAge} = {}) {
if (this.cache.has(key)) {
this.cache.set(key, {
value,
Expand Down Expand Up @@ -259,5 +259,3 @@ class QuickLRU {
return Math.min(this._size + oldCacheSize, this.maxSize);
}
}

module.exports = QuickLRU;
3 changes: 1 addition & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import QuickLRU = require('.');
import QuickLRU from './index.js';

const lru = new QuickLRU<string, number>({maxSize: 1000, maxAge: 200});

Expand All @@ -8,7 +8,6 @@ expectType<number | undefined>(lru.get('🦄'));
expectType<boolean>(lru.has('🦄'));
expectType<number | undefined>(lru.peek('🦄'));
expectType<boolean>(lru.delete('🦄'));
expectType<void>(lru.clear());
expectType<number>(lru.size);

for (const [key, value] of lru) {
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && nyc ava && tsd"
"//test": "xo && nyc ava && tsd",
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -34,10 +37,10 @@
"buffer"
],
"devDependencies": {
"ava": "^2.4.0",
"ava": "^3.15.0",
"nyc": "^15.1.0",
"tsd": "^0.13.1",
"xo": "^0.35.0"
"tsd": "^0.14.0",
"xo": "^0.37.1"
},
"nyc": {
"reporter": [
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install quick-lru
## Usage

```js
const QuickLRU = require('quick-lru');
import QuickLRU from 'quick-lru';

const lru = new QuickLRU({maxSize: 1000});

Expand Down
Loading

4 comments on commit a408ce9

@fregante
Copy link

@fregante fregante commented on a408ce9 Jan 17, 2021

Choose a reason for hiding this comment

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

Do you know what could have caused this jump?

Screen Shot 2

@sindresorhus
Copy link
Owner Author

Choose a reason for hiding this comment

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

That's really strange. I have no idea. Maybe worth opening an issue on https://github.com/pastelsky/bundlephobia about it.


Bundlephobia does not include babel or does any transpilation to packages in the process of calculating sizes. As you mentioned, we're talking sizes in bytes here, which could become bloated due to how webpack builds modules (it can rewrite imports / add minimal additional runtime code). - pastelsky/bundlephobia#247 (comment)

Maybe it's Webpack that adds some helpers or junk.

@fregante
Copy link

Choose a reason for hiding this comment

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

I think it's the 2 new features that were added since v5: index.js doubled in length: v5.1.1...v6.0.0#diff-e727e4bdf3

@sindresorhus
Copy link
Owner Author

Choose a reason for hiding this comment

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

Ah of course. Duuh. I forgot about the added features.

Please sign in to comment.