bind.js
This file
Path
lib/methods/bind.js
23 lines · 964 bytes · JavaScript
Last changed by
Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
History of this fileTake it
Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
lines 1–4why, one step back1import isPlainObject from 'is-plain-obj';2import {FD_SPECIFIC_OPTIONS} from '../arguments/specific.js';34// Deep merge specific options like `env`. Shallow merge the other ones.Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 5why, one step back5// Use spread (which only copies own properties) to safely read from boundOptions without prototype pollutionRefactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 6why, one step back6export const mergeOptions = (boundOptions, options) => {Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 7why, one step back7 const safeBoundOptions = {__proto__: null, ...boundOptions};Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 8–11why, one step back8 const mergedOptions = Object.fromEntries(Object.entries(options).map(([optionName, optionValue]) => [9 optionName,10 mergeOption(optionName, safeBoundOptions[optionName], optionValue),11 ]));Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 12why, one step back12 return {...safeBoundOptions, ...mergedOptions};Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
lines 13–23why, one step back13};1415const mergeOption = (optionName, boundOptionValue, optionValue) => {16 if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) {17 return {...boundOptionValue, ...optionValue};18 }1920 return optionValue;21};2223const DEEP_OPTIONS = new Set(['env', ...FD_SPECIFIC_OPTIONS]);