PHP 8.2.30
Preview: get.ts Size: 2.51 KB
//proc/thread-self/root/home/byroehnu/.trash/node_modules11/adminjs/src/utils/flat/get.ts

import flat from 'flat'

import { DELIMITER } from './constants.js'
import { selectParams } from './select-params.js'
import { FlattenParams, GetOptions } from './flat.types.js'
import { propertyKeyRegex } from './property-key-regex.js'

const TEMP_HOLDING_KEY = 'TEMP_HOLDING_KEY'

/**
 * @load ./get.doc.md
 * @memberof module:flat
 * @param {FlattenParams}   params      flatten params from which property has to be taken
 * @param {string}          [propertyPath]  name of the property
 * @param {GetOptions}      options     options
 * @returns {any}                       when property key exists directly it returns what is inside,
 *                                      otherwise it tries to find any nested objects and returns
 *                                      them
 */
const get = (params: FlattenParams = {}, propertyPath?: string, options?: GetOptions): any => {
  if (!propertyPath) {
    return flat.unflatten(params)
  }

  // when object has this key - simply return it
  // we cannot rely on typeof params[propertyPath !== 'undefined' because params can actually be
  // undefined and in such case if would pass and function would return [undefined]
  if (Object.keys(params).find((key) => (key === propertyPath))) {
    return params[propertyPath]
  }

  const regex = propertyKeyRegex(propertyPath, options)
  const selectedParams = selectParams(params, propertyPath, options)

  const nestedProperties = Object.keys(selectedParams).reduce((memo, key, index) => {
    let newKey = key.replace(regex, `${TEMP_HOLDING_KEY}${DELIMITER}`)

    // when user wants to take allSiblings we have to fix the indexes so nested items from
    // different siblings don't overlap
    //
    // Example for key `nested.1.el`:
    //  'nested.0.el.0.value': 'val0.0',
    //  'nested.0.el.1.value': 'val0.1',
    //  'nested.1.el.0.value': 'val1',
    //  'nested.1.el.1.value': 'val2',
    //
    // has to be changed to:
    //  'TEMP_HOLDING_KEY.0.value': 'val0.0',
    //  'TEMP_HOLDING_KEY.1.value': 'val0.1',
    //  'TEMP_HOLDING_KEY.2.value': 'val1',
    //  'TEMP_HOLDING_KEY.3.value': 'val2',
    if (options?.includeAllSiblings) {
      newKey = newKey.replace(
        new RegExp(`${TEMP_HOLDING_KEY}\\${DELIMITER}(\\d*)`),
        `${TEMP_HOLDING_KEY}${DELIMITER}${index}`,
      )
    }

    memo[newKey] = selectedParams[key]

    return memo
  }, {} as FlattenParams)

  if (Object.keys(nestedProperties).length) {
    return (flat.unflatten(nestedProperties) as Record<string, unknown>)[TEMP_HOLDING_KEY]
  }
  return undefined
}

export { get }

Directory Contents

Dirs: 0 × Files: 26

Name Size Perms Modified Actions
44 B lrw-r--r-- 2026-02-28 00:32:02
Edit Download
448 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
1.42 KB lrw-r--r-- 2026-02-28 00:32:10
Edit Download
906 B lrw-r--r-- 2026-02-28 00:32:10
Edit Download
1.24 KB lrw-r--r-- 2026-02-28 00:32:14
Edit Download
2.74 KB lrw-r--r-- 2026-02-28 00:31:56
Edit Download
965 B lrw-r--r-- 2026-02-28 00:32:14
Edit Download
587 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
2.84 KB lrw-r--r-- 2026-02-28 00:32:20
Edit Download
2.51 KB lrw-r--r-- 2026-02-28 00:32:20
Edit Download
65 B lrw-r--r-- 2026-02-28 00:32:28
Edit Download
2.01 KB lrw-r--r-- 2026-02-28 00:32:34
Edit Download
686 B lrw-r--r-- 2026-02-28 00:32:36
Edit Download
38 B lrw-r--r-- 2026-02-28 00:32:40
Edit Download
730 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
976 B lrw-r--r-- 2026-02-28 00:32:40
Edit Download
890 B lrw-r--r-- 2026-02-28 00:32:40
Edit Download
1.21 KB lrw-r--r-- 2026-02-28 00:31:56
Edit Download
2.47 KB lrw-r--r-- 2026-02-28 00:32:40
Edit Download
1.41 KB lrw-r--r-- 2026-02-28 00:32:40
Edit Download
504 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
1.49 KB lrw-r--r-- 2026-02-28 00:32:44
Edit Download
1.11 KB lrw-r--r-- 2026-02-28 00:32:44
Edit Download
639 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
4.70 KB lrw-r--r-- 2026-02-28 00:32:44
Edit Download
2.03 KB lrw-r--r-- 2026-02-28 00:32:44
Edit Download

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