Skip to content
Myr

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 file

Take it

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 1–4why, one step back
1import 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 back
5// Use spread (which only copies own properties) to safely read from boundOptions without prototype pollution

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

line 6why, one step back
6export const mergeOptions = (boundOptions, options) => {

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

line 7why, one step back
7 const safeBoundOptions = {__proto__: null, ...boundOptions};

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 8–11why, one step back
8 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 back
12 return {...safeBoundOptions, ...mergedOptions};

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 13–23why, one step back
13};
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]);