Skip to content

Commit 51e6a98

Browse files
fix: correctly defaults configuration options (#50)
1 parent 5e05f3c commit 51e6a98

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"build": "tsc",
4141
"test": "npm run lint && npm run test:raw",
4242
"lint": "eslint --ext .js,.ts src/**/* test/**/*",
43-
"pretty": "prettier src/**/*.ts test/**/*.js --write",
43+
"pretty": "prettier src test --write",
4444
"docs": "typedoc",
4545
"test:raw": "tap test/**/*-test.js"
4646
},

src/Pool.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Deferred } from './Deferred';
22
import { AggregateError } from './AggregateError';
3+
import { defaultTo } from './helpers/defaultTo';
34

45
export type LogLevel = 'verbose' | 'info' | 'error';
56
export type FactoryLogger = (message: string, level: LogLevel) => void;
@@ -164,11 +165,11 @@ export class Pool<RawResource> {
164165
}
165166

166167
// defaults
167-
this.idleTimeoutMillis = factory.idleTimeoutMillis || 30000;
168-
this.acquireTimeoutMillis = factory.acquireTimeoutMillis || 30000;
169-
this.reapIntervalMillis = factory.reapIntervalMillis || 1000;
170-
this.maxUsesPerResource = factory.maxUses || Infinity;
171-
this.log = factory.log || false;
168+
this.idleTimeoutMillis = defaultTo(factory.idleTimeoutMillis, 30000);
169+
this.acquireTimeoutMillis = defaultTo(factory.acquireTimeoutMillis, 30000);
170+
this.reapIntervalMillis = defaultTo(factory.reapIntervalMillis, 1000);
171+
this.maxUsesPerResource = defaultTo(factory.maxUses, Infinity);
172+
this.log = defaultTo(factory.log, false);
172173

173174
this._factory = factory;
174175
this._count = 0;

src/helpers/defaultTo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function defaultTo<T>(value: T | null | undefined, defaultValue: T): T {
2+
return value == null || value !== value ? defaultValue : value;
3+
}

test/integration/config-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,25 @@ tap.test('fail without factory.validate', (t) => {
6161
}, 'validate function is required');
6262
t.end();
6363
});
64+
65+
tap.test('correctly defaults', (t) => {
66+
const factory = {
67+
name: 'test-config-defaults',
68+
create: () => {},
69+
destroy: () => {},
70+
validate: () => true,
71+
max: 1,
72+
min: 0,
73+
idleTimeoutMillis: 0,
74+
acquireTimeoutMillis: undefined,
75+
reapIntervalMillis: null,
76+
};
77+
78+
const pool = new Pool(factory);
79+
80+
t.equal(pool.idleTimeoutMillis, 0);
81+
t.equal(pool.acquireTimeoutMillis, 30000);
82+
t.equal(pool.reapIntervalMillis, 1000);
83+
84+
t.end();
85+
});

0 commit comments

Comments
 (0)