/
githubmirror
/
wp-calypso
Обзор
Документация
Войти
/
githubmirror
/
wp-calypso
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/js-utils/src/defaults.ts
51 строка
2 KB
Griffith Chen
Trim redundant lodash references from js-utils comments (#112267)
03 июл 2026, 17:11
Не верифицирован
03 июл 2026, 17:11
f3399af
Код
Авторство
О чём код?
function defaults< T extends object, S extends object >( object: T, source: S ): T & S; function defaults< T extends object, S1 extends object, S2 extends object >( object: T, source1: S1, source2: S2 ): T & S1 & S2; function defaults< T extends object >( object: T, ...sources: Array< object | null | undefined > ): T; /** * Assigns own enumerable properties of the source objects to `object` for every * destination property that is `undefined`. Earlier sources take precedence, * `object` always wins over sources, and `object` is mutated and returned. * @param object The destination object (mutated). * @param sources The source objects. * @returns `object`. */ function defaults( object: Record< string, unknown > | null | undefined, ...sources: Array< object | null | undefined > ): object { const objectProto = Object.prototype as Record< string, unknown >; const hasOwn = Object.prototype.hasOwnProperty; // `Object()` coerces a nullish destination to `{}`. const target = Object( object ) as Record< string, unknown >; for ( const source of sources ) { if ( source == null ) { continue; } // Iterate own and inherited enumerable keys. // eslint-disable-next-line guard-for-in for ( const key in source ) { const value = target[ key ]; // Assign when the destination resolves to `undefined`, or to an // inherited `Object.prototype` value that isn't an own property (e.g. // `constructor`, `toString`). if ( value === undefined || ( value === objectProto[ key ] && ! hasOwn.call( target, key ) ) ) { target[ key ] = ( source as Record< string, unknown > )[ key ]; } } } return target; } export default defaults;