return object;
}
+ /**
+ * @typedef {Object} FieldMapExtensions
+ * @property {Array<CSVNormalizerField>} source The original CSV row object that was passed to `.toFieldMap` to
+ * create the `FieldMap` object.
+ */
+
/**
* Create a subset of the `Map` interface to get and set the `value` of a field using its `name`.
*
* - `.clear()`
* - `.size`
*
- * All other methods are unsupported. For more advanced usage, use the underlying `row` object directly.
+ * All other `Map` methods are unsupported.
*
- * The `displayName` and `emptyField` values of a field cannot be accessed from the `Map`.
+ * The `displayName` and `emptyField` values of a field cannot be accessed from the `Map`. Calling `.set()` with a
+ * `name` that is not in the row will push a new field to the row.
*
- * Calling `.set()` with a `name` that is not in the row will push a new field to the row.
+ * For more advanced usage, use the underlying `row` object directly. You can access the underlying `row` object by
+ * calling the `.source` property.
* @param {Array<CSVNormalizerField>} row A normalized CSV row.
- * @returns {Pick<Map<string,any>,'get'|'set'|'has'|'delete'|'clear'|'size'>} A `Proxy` over a `Map` object with keys
- * set to the `name` of each field in the row.
+ * @returns {Pick<Map<string,any>,'get'|'set'|'has'|'delete'|'clear'|'size'>&FieldMapExtensions}} A `Proxy`
+ * over a `Map` object with keys set to the `name` of each field in the row.
*/
static toFieldMap(row) {
const map = new Map();
target.clear();
}
}
+ if (prop === 'source') {
+ return row;
+ }
}
});
}
const map = CSVNormalizer.toFieldMap(row);
deepStrictEqual(map.size, 1);
});
+ test('has a .source property to access the underlying row', { concurrency: true }, async () => {
+ const row = [{ name: 'a', value: 1 }];
+ const map = CSVNormalizer.toFieldMap(row);
+ ok(map.source === row); // Intentionally comparing by reference, do not use deepStrictEqual
+ row.push({ name: 'b', value: 2 });
+ ok(map.source === row);
+ map.source.push({ name: 'c', value: 3 });
+ ok(map.source === row);
+ });
});
suite('CSVNormalizer.from', { concurrency: true }, () => {