]> git.ardis.lu - csvio/commitdiff
Refactor extra arguments into options object
authorArdis Lu <ardisglu@gmail.com>
Tue, 25 Feb 2025 09:32:49 +0000 (01:32 -0800)
committerArdis Lu <ardisglu@gmail.com>
Tue, 25 Feb 2025 09:32:49 +0000 (01:32 -0800)
src/errorStrategies.js
test/errorStrategies.test.js

index a136b41deb271d423d4cfb8c20f38f6faba66689..4c9d08a0fd98238ab7a6534760629070ddb433fa 100644 (file)
@@ -31,17 +31,23 @@ function placeholder(value) {
   }
 }
 
+/**
+ * @typedef {Object} RetryOptions
+ * @property {string} [value] If provided, a placeholder value to set if the function still errors after the final iteration.
+ * If `value` is unset, the final error thrown by the function is re-thrown.
+ */
+
 /**
  * Create a `TransformationErrorFunction` that handles errors by immediately retrying it `iterations` number of times.
  * 
  * @param {number} iterations The number of times to retry the function.
- * @param {string} [value] If provided, a placeholder value to set if the function still errors after the final iteration.
- * If `value` is unset, the final error thrown by the function is re-thrown.
+ * @param {RetryOptions} [options] Extra configuration options for the retry.
  * @returns {TransformationErrorFunction} A `TransformationErrorFunction` that may be passed to a `CSVTransformer`'s
  * `onError` option.
  * @throws If `value` is unset and the function throws an error after the final iteration, the error is re-thrown.
  */
-function retry(iterations, value) {
+function retry(iterations, options = {}) {
+  let { value } = options;
   return async (row, e, fn) => {
     while (iterations--) {
       try { return await fn(row); }
@@ -54,24 +60,28 @@ function retry(iterations, value) {
   }
 }
 
+/**
+ * @typedef {Object} BackoffOptions
+ * @property {number} [maxExponent] If provided, the maximum exponent used to calculate the retry duration. In other words,
+ * the `iterations` number at which the retry duration stops increasing. If `maxExponent` is unset, the retry duration
+ * is unbounded and will increase after each retry.
+ * @property {string} [value] If provided, a placeholder value to set if the function still errors after the final iteration.
+ * If `value` is unset, the final error thrown by the function is re-thrown.
+ */
+
 /**
  * Create a `TransformationErrorFunction` that handles errors by retrying it an `iterations` number of times, with an
  * exponentially increasing duration in between each retry (i.e., an exponential backoff).
  * 
  * @param {number} iterations The number of times to retry the function.
- * @param {number} [maxExponent] If provided, the maximum exponent used to calculate the retry duration. In other words,
- * the `iterations` number at which the retry duration stops increasing. If `maxExponent` is unset, the retry duration
- * is unbounded and will increase after each retry.
- * @param {string} [value] If provided, a placeholder value to set if the function still errors after the final iteration.
- * If `value` is unset, the final error thrown by the function is re-thrown.
+ * @param {BackoffOptions} [options] Extra configuration options for the backoff.
  * @returns {TransformationErrorFunction} A `TransformationErrorFunction` that may be passed to a `CSVTransformer`'s
  * `onError` option.
  * @throws If `value` is unset and the function throws an error after the final iteration, the error is re-thrown.
  */
-function backoff(iterations, maxExponent, value) {
-  if (maxExponent === undefined) {
-    maxExponent = Infinity;
-  }
+function backoff(iterations, options = {}) {
+  let { maxExponent, value } = options;
+  maxExponent ??= Infinity;
   return async (row, e, fn) => {
     for (let n = 0; n < iterations; n++) {
       const exponent = n > maxExponent ? maxExponent : n;
index 57dd6a3e8d4a307ab483c42104c72ff0cd3f5627..2f275a11dc3891dd813cf9d43cf5a2cafe863ddd 100644 (file)
@@ -139,7 +139,7 @@ suite('ErrorStrategies.retry', { concurrency: true }, () => {
       ['header'],
       ['0']
     ])
-      .pipeThrough(new CSVTransformer(fn, { onError: retry(1, 'n/a') }))
+      .pipeThrough(new CSVTransformer(fn, { onError: retry(1, { value: 'n/a' }) }))
       .pipeTo(csvStreamEqualWritable([
         ['header'],
         ['n/a']
@@ -199,7 +199,7 @@ suite('ErrorStrategies.backoff', { concurrency: 1 }, () => { // TODO: These test
       ['header'],
       ['0']
     ])
-      .pipeThrough(new CSVTransformer(fn, { onError: backoff(10, 0) }))
+      .pipeThrough(new CSVTransformer(fn, { onError: backoff(10, { maxExponent: 0 }) }))
       .pipeTo(csvStreamEqualWritable([
         ['header'],
         [11]
@@ -214,7 +214,7 @@ suite('ErrorStrategies.backoff', { concurrency: 1 }, () => { // TODO: These test
       ['header'],
       ['0']
     ])
-      .pipeThrough(new CSVTransformer(fn, { onError: backoff(7, 4) }))
+      .pipeThrough(new CSVTransformer(fn, { onError: backoff(7, { maxExponent: 4 }) }))
       .pipeTo(csvStreamEqualWritable([
         ['header'],
         [8]
@@ -228,7 +228,7 @@ suite('ErrorStrategies.backoff', { concurrency: 1 }, () => { // TODO: These test
       ['header'],
       ['0']
     ])
-      .pipeThrough(new CSVTransformer(fn, { onError: backoff(3, Infinity, 'n/a') }))
+      .pipeThrough(new CSVTransformer(fn, { onError: backoff(3, { value: 'n/a' }) }))
       .pipeTo(csvStreamEqualWritable([
         ['header'],
         ['n/a']
This page took 0.0528 seconds and 4 git commands to generate.