(opts)
| 4 | import { is_object } from "../utils/is_object.js"; |
| 5 | |
| 6 | const normalize_options = function (opts) { |
| 7 | const options = {}; |
| 8 | // Merge with user options |
| 9 | for (const opt in opts) { |
| 10 | options[underscore(opt)] = opts[opt]; |
| 11 | } |
| 12 | // Normalize option `encoding` |
| 13 | // Note: defined first because other options depends on it |
| 14 | // to convert chars/strings into buffers. |
| 15 | if (options.encoding === undefined || options.encoding === true) { |
| 16 | options.encoding = "utf8"; |
| 17 | } else if (options.encoding === null || options.encoding === false) { |
| 18 | options.encoding = null; |
| 19 | } else if ( |
| 20 | typeof options.encoding !== "string" && |
| 21 | options.encoding !== null |
| 22 | ) { |
| 23 | throw new CsvError( |
| 24 | "CSV_INVALID_OPTION_ENCODING", |
| 25 | [ |
| 26 | "Invalid option encoding:", |
| 27 | "encoding must be a string or null to return a buffer,", |
| 28 | `got ${JSON.stringify(options.encoding)}`, |
| 29 | ], |
| 30 | options, |
| 31 | ); |
| 32 | } |
| 33 | // Normalize option `bom` |
| 34 | if ( |
| 35 | options.bom === undefined || |
| 36 | options.bom === null || |
| 37 | options.bom === false |
| 38 | ) { |
| 39 | options.bom = false; |
| 40 | } else if (options.bom !== true) { |
| 41 | throw new CsvError( |
| 42 | "CSV_INVALID_OPTION_BOM", |
| 43 | [ |
| 44 | "Invalid option bom:", |
| 45 | "bom must be true,", |
| 46 | `got ${JSON.stringify(options.bom)}`, |
| 47 | ], |
| 48 | options, |
| 49 | ); |
| 50 | } |
| 51 | // Normalize option `cast` |
| 52 | options.cast_function = null; |
| 53 | if ( |
| 54 | options.cast === undefined || |
| 55 | options.cast === null || |
| 56 | options.cast === false || |
| 57 | options.cast === "" |
| 58 | ) { |
| 59 | options.cast = undefined; |
| 60 | } else if (typeof options.cast === "function") { |
| 61 | options.cast_function = options.cast; |
| 62 | options.cast = true; |
| 63 | } else if (options.cast !== true) { |
no test coverage detected