(object, options)
| 603 | } |
| 604 | |
| 605 | export function stringify(object, options) { |
| 606 | if (!object) { |
| 607 | return ''; |
| 608 | } |
| 609 | |
| 610 | options = { |
| 611 | encode: true, |
| 612 | strict: true, |
| 613 | arrayFormat: 'none', |
| 614 | arrayFormatSeparator: ',', |
| 615 | ...definedOptions(options), |
| 616 | }; |
| 617 | |
| 618 | validateArrayFormatSeparator(options.arrayFormatSeparator); |
| 619 | |
| 620 | const formatter = encoderForArrayFormat(options); |
| 621 | |
| 622 | // Copying into a plain object intentionally drops a key named `__proto__`, since assigning it sets the prototype instead of a property. |
| 623 | const objectCopy = {}; |
| 624 | |
| 625 | // `Object.fromEntries()` would define the key as a real own property instead, |
| 626 | // which would keep a `__proto__` key that is meant to be dropped here. |
| 627 | // eslint-disable-next-line unicorn/prefer-object-from-entries |
| 628 | for (const [key, value] of Object.entries(object)) { |
| 629 | objectCopy[key] = value; |
| 630 | } |
| 631 | |
| 632 | const keys = Object.keys(objectCopy); |
| 633 | |
| 634 | if (options.sort !== false) { |
| 635 | keys.sort(typeof options.sort === 'function' ? options.sort : undefined); |
| 636 | } |
| 637 | |
| 638 | return keys.map(key => { |
| 639 | let value = objectCopy[key]; |
| 640 | |
| 641 | // Apply replacer function if provided |
| 642 | if (options.replacer) { |
| 643 | value = options.replacer(key, value); |
| 644 | } |
| 645 | |
| 646 | // `skipNull` and `skipEmptyString` look at the replaced value, like they do for array items. A replacer that returns `undefined` skips the key. |
| 647 | if (isSkipped(value, options)) { |
| 648 | return ''; |
| 649 | } |
| 650 | |
| 651 | if (value === null) { |
| 652 | return encode(key, options); |
| 653 | } |
| 654 | |
| 655 | if (Array.isArray(value)) { |
| 656 | // Apply replacer to array elements if provided |
| 657 | // Note: The replacer was already applied to the array itself above. Here it is applied to each element. |
| 658 | let processedArray = value; |
| 659 | if (options.replacer) { |
| 660 | processedArray = value.map((item, index) => |
| 661 | options.replacer(`${key}[${index}]`, item), |
| 662 | ).filter(item => item !== undefined); |
no test coverage detected