()
| 8 | import { Transformer } from "./index.js"; |
| 9 | |
| 10 | const transform = function () { |
| 11 | // Import arguments normalization |
| 12 | let handler, records; |
| 13 | let options = {}; |
| 14 | for (const i in arguments) { |
| 15 | const argument = arguments[i]; |
| 16 | let type = typeof argument; |
| 17 | if (argument === null) { |
| 18 | type = "null"; |
| 19 | } else if (type === "object" && Array.isArray(argument)) { |
| 20 | type = "array"; |
| 21 | } |
| 22 | if (type === "array") { |
| 23 | records = argument; |
| 24 | } else if (type === "object") { |
| 25 | options = { ...argument }; |
| 26 | } else if (type === "function") { |
| 27 | handler = argument; |
| 28 | } else if (type !== "null") { |
| 29 | throw new Error( |
| 30 | `Invalid Arguments: got ${JSON.stringify(argument)} at position ${i}`, |
| 31 | ); |
| 32 | } |
| 33 | } |
| 34 | // Validate arguments |
| 35 | let expected_handler_length = 1; |
| 36 | if (options.params) { |
| 37 | expected_handler_length++; |
| 38 | } |
| 39 | if (handler.length > expected_handler_length) { |
| 40 | throw Error("Invalid Handler: only synchronous handlers are supported"); |
| 41 | } |
| 42 | // Start transformation |
| 43 | const chunks = []; |
| 44 | const transformer = new Transformer(options, handler); |
| 45 | transformer.push = function (chunk) { |
| 46 | chunks.push(chunk); |
| 47 | }; |
| 48 | for (const record of records) { |
| 49 | transformer._transform(record, null, function () {}); |
| 50 | } |
| 51 | return chunks; |
| 52 | }; |
| 53 | |
| 54 | export { transform }; |
no test coverage detected