]> git.ardis.lu - csvio/commitdiff
Rename `options.value` to `options.placeholder`
authorArdis Lu <ardisglu@gmail.com>
Tue, 25 Feb 2025 09:36:27 +0000 (01:36 -0800)
committerArdis Lu <ardisglu@gmail.com>
Tue, 25 Feb 2025 09:36:27 +0000 (01:36 -0800)
src/errorStrategies.js
test/errorStrategies.test.js

index 4c9d08a0fd98238ab7a6534760629070ddb433fa..508fa4d70bbbc91fed9d474b803919edb83a965f 100644 (file)
@@ -33,8 +33,8 @@ 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.
+ * @property {string} [placeholder] A placeholder value to set if the function still errors after the final iteration.
+ * If unset, the final error thrown by the function is re-thrown.
  */
 
 /**
@@ -47,14 +47,13 @@ function placeholder(value) {
  * @throws If `value` is unset and the function throws an error after the final iteration, the error is re-thrown.
  */
 function retry(iterations, options = {}) {
-  let { value } = options;
   return async (row, e, fn) => {
     while (iterations--) {
       try { return await fn(row); }
       catch { }
     }
-    if (value !== undefined) {
-      return placeholder(value)(row, e, fn);
+    if (options.placeholder !== undefined) {
+      return placeholder(options.placeholder)(row, e, fn);
     }
     throw e;
   }
@@ -65,8 +64,8 @@ function retry(iterations, options = {}) {
  * @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.
+ * @property {string} [placeholder] A placeholder value to set if the function still errors after the final iteration.
+ * If unset, the final error thrown by the function is re-thrown.
  */
 
 /**
@@ -80,19 +79,18 @@ function retry(iterations, options = {}) {
  * @throws If `value` is unset and the function throws an error after the final iteration, the error is re-thrown.
  */
 function backoff(iterations, options = {}) {
-  let { maxExponent, value } = options;
-  maxExponent ??= Infinity;
+  options.maxExponent ??= Infinity;
   return async (row, e, fn) => {
     for (let n = 0; n < iterations; n++) {
-      const exponent = n > maxExponent ? maxExponent : n;
+      const exponent = n > options.maxExponent ? options.maxExponent : n;
       const duration = (2 ** exponent) * 1000; // 1s, 2s, 4s, 8s, 16s, ...
       const jitter = Math.random() * 1000;
       await utils.sleep(duration + jitter);
       try { return await fn(row); }
       catch { }
     }
-    if (value !== undefined) {
-      return placeholder(value)(row, e, fn);
+    if (options.placeholder !== undefined) {
+      return placeholder(options.placeholder)(row, e, fn);
     }
     throw e;
   }
index 2f275a11dc3891dd813cf9d43cf5a2cafe863ddd..54eda6e8e0914069c4b1404bead662e532947872 100644 (file)
@@ -139,7 +139,7 @@ suite('ErrorStrategies.retry', { concurrency: true }, () => {
       ['header'],
       ['0']
     ])
-      .pipeThrough(new CSVTransformer(fn, { onError: retry(1, { value: 'n/a' }) }))
+      .pipeThrough(new CSVTransformer(fn, { onError: retry(1, { placeholder: 'n/a' }) }))
       .pipeTo(csvStreamEqualWritable([
         ['header'],
         ['n/a']
@@ -228,7 +228,7 @@ suite('ErrorStrategies.backoff', { concurrency: 1 }, () => { // TODO: These test
       ['header'],
       ['0']
     ])
-      .pipeThrough(new CSVTransformer(fn, { onError: backoff(3, { value: 'n/a' }) }))
+      .pipeThrough(new CSVTransformer(fn, { onError: backoff(3, { placeholder: 'n/a' }) }))
       .pipeTo(csvStreamEqualWritable([
         ['header'],
         ['n/a']
This page took 0.050578 seconds and 4 git commands to generate.