Path: blob/master/node_modules/@hapi/hoek/lib/reach.js
1126 views
'use strict';12const Assert = require('./assert');345const internals = {};678module.exports = function (obj, chain, options) {910if (chain === false ||11chain === null ||12chain === undefined) {1314return obj;15}1617options = options || {};18if (typeof options === 'string') {19options = { separator: options };20}2122const isChainArray = Array.isArray(chain);2324Assert(!isChainArray || !options.separator, 'Separator option is not valid for array-based chain');2526const path = isChainArray ? chain : chain.split(options.separator || '.');27let ref = obj;28for (let i = 0; i < path.length; ++i) {29let key = path[i];30const type = options.iterables && internals.iterables(ref);3132if (Array.isArray(ref) ||33type === 'set') {3435const number = Number(key);36if (Number.isInteger(number)) {37key = number < 0 ? ref.length + number : number;38}39}4041if (!ref ||42typeof ref === 'function' && options.functions === false || // Defaults to true43!type && ref[key] === undefined) {4445Assert(!options.strict || i + 1 === path.length, 'Missing segment', key, 'in reach path ', chain);46Assert(typeof ref === 'object' || options.functions === true || typeof ref !== 'function', 'Invalid segment', key, 'in reach path ', chain);47ref = options.default;48break;49}5051if (!type) {52ref = ref[key];53}54else if (type === 'set') {55ref = [...ref][key];56}57else { // type === 'map'58ref = ref.get(key);59}60}6162return ref;63};646566internals.iterables = function (ref) {6768if (ref instanceof Set) {69return 'set';70}7172if (ref instanceof Map) {73return 'map';74}75};767778