PHP 8.2.30
Preview: applyToDefaults.js Size: 2.61 KB
/proc/thread-self/root/home/byroehnu/.trash/node_modules11/@hapi/hoek/lib/applyToDefaults.js

'use strict';

const Assert = require('./assert');
const Clone = require('./clone');
const Merge = require('./merge');
const Reach = require('./reach');


const internals = {};


module.exports = function (defaults, source, options = {}) {

    Assert(defaults && typeof defaults === 'object', 'Invalid defaults value: must be an object');
    Assert(!source || source === true || typeof source === 'object', 'Invalid source value: must be true, falsy or an object');
    Assert(typeof options === 'object', 'Invalid options: must be an object');

    if (!source) {                                                  // If no source, return null
        return null;
    }

    if (options.shallow) {
        return internals.applyToDefaultsWithShallow(defaults, source, options);
    }

    const copy = Clone(defaults);

    if (source === true) {                                          // If source is set to true, use defaults
        return copy;
    }

    const nullOverride = options.nullOverride !== undefined ? options.nullOverride : false;
    return Merge(copy, source, { nullOverride, mergeArrays: false });
};


internals.applyToDefaultsWithShallow = function (defaults, source, options) {

    const keys = options.shallow;
    Assert(Array.isArray(keys), 'Invalid keys');

    const seen = new Map();
    const merge = source === true ? null : new Set();

    for (let key of keys) {
        key = Array.isArray(key) ? key : key.split('.');            // Pre-split optimization

        const ref = Reach(defaults, key);
        if (ref &&
            typeof ref === 'object') {

            seen.set(ref, merge && Reach(source, key) || ref);
        }
        else if (merge) {
            merge.add(key);
        }
    }

    const copy = Clone(defaults, {}, seen);

    if (!merge) {
        return copy;
    }

    for (const key of merge) {
        internals.reachCopy(copy, source, key);
    }

    const nullOverride = options.nullOverride !== undefined ? options.nullOverride : false;
    return Merge(copy, source, { nullOverride, mergeArrays: false });
};


internals.reachCopy = function (dst, src, path) {

    for (const segment of path) {
        if (!(segment in src)) {
            return;
        }

        const val = src[segment];

        if (typeof val !== 'object' || val === null) {
            return;
        }

        src = val;
    }

    const value = src;
    let ref = dst;
    for (let i = 0; i < path.length - 1; ++i) {
        const segment = path[i];
        if (typeof ref[segment] !== 'object') {
            ref[segment] = {};
        }

        ref = ref[segment];
    }

    ref[path[path.length - 1]] = value;
};

Directory Contents

Dirs: 0 × Files: 26

Name Size Perms Modified Actions
2.61 KB lrw-r--r-- 2026-02-21 00:53:22
Edit Download
307 B lrw-r--r-- 2026-02-21 00:53:22
Edit Download
401 B lrw-r--r-- 2026-02-21 00:53:24
Edit Download
144 B lrw-r--r-- 2026-02-21 00:53:24
Edit Download
4.07 KB lrw-r--r-- 2026-02-21 00:53:28
Edit Download
6.39 KB lrw-r--r-- 2026-02-21 00:53:28
Edit Download
7.48 KB lrw-r--r-- 2026-02-21 00:53:30
Edit Download
595 B lrw-r--r-- 2026-02-21 00:53:32
Edit Download
474 B lrw-r--r-- 2026-02-21 00:53:34
Edit Download
1.69 KB lrw-r--r-- 2026-02-21 00:53:34
Edit Download
437 B lrw-r--r-- 2026-02-21 00:53:34
Edit Download
198 B lrw-r--r-- 2026-02-21 00:53:36
Edit Download
349 B lrw-r--r-- 2026-02-21 00:53:36
Edit Download
73 B lrw-r--r-- 2026-02-21 00:53:36
Edit Download
12.42 KB lrw-r--r-- 2026-02-21 00:53:50
Edit Download
968 B lrw-r--r-- 2026-02-21 00:53:38
Edit Download
780 B lrw-r--r-- 2026-02-21 00:53:38
Edit Download
141 B lrw-r--r-- 2026-02-21 00:53:38
Edit Download
2.36 KB lrw-r--r-- 2026-02-21 00:53:40
Edit Download
401 B lrw-r--r-- 2026-02-21 00:53:40
Edit Download
1.84 KB lrw-r--r-- 2026-02-21 00:53:42
Edit Download
320 B lrw-r--r-- 2026-02-21 00:53:42
Edit Download
224 B lrw-r--r-- 2026-02-21 00:53:44
Edit Download
1.23 KB lrw-r--r-- 2026-02-21 00:53:44
Edit Download
205 B lrw-r--r-- 2026-02-21 00:53:46
Edit Download
901 B lrw-r--r-- 2026-02-21 00:53:46
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).