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

import flat from 'flat'

import { DELIMITER } from './constants.js'
import { FlattenParams } from './flat.types.js'
import { propertyKeyRegex } from './property-key-regex.js'
import { pathToParts } from './path-to-parts.js'

const isObject = (value: any): boolean => {
  // Node environment
  if (typeof File === 'undefined') {
    return typeof value === 'object' && value !== null
  }
  // Window environment
  return typeof value === 'object' && !(value instanceof File) && value !== null
}

/**
 * @load ./set.doc.md
 * @memberof module:flat
 * @param {FlattenParams} params
 * @param {string} propertyPath
 * @param {any} [value]       if not give function will only try to remove old keys
 * @returns {FlattenParams}
 */
const set = (params: FlattenParams = {}, propertyPath: string, value?: any): FlattenParams => {
  const regex = propertyKeyRegex(propertyPath)

  // remove all existing keys
  const paramsCopy = Object.keys(params)
    .filter((key) => !key.match(regex))
    .reduce((memo, key) => {
      memo[key] = params[key]

      return memo
    }, {} as FlattenParams)

  if (typeof value !== 'undefined') {
    if (isObject(value) && !(value instanceof Date)) {
      const flattened = flat.flatten(value) as any

      if (Object.keys(flattened).length) {
        Object.keys(flattened).forEach((key) => {
          paramsCopy[`${propertyPath}${DELIMITER}${key}`] = flattened[key]
        })
      } else if (Array.isArray(value)) {
        paramsCopy[propertyPath] = []
      } else {
        paramsCopy[propertyPath] = {}
      }
    } else {
      paramsCopy[propertyPath] = value
    }

    // when user gave { "nested.value": "something" } and had "nested" set to `null`, then
    // nested should be removed
    const parts = pathToParts(propertyPath).slice(0, -1)
    if (parts.length) {
      return Object.keys(paramsCopy)
        .filter((key) => !parts.includes(key))
        .reduce((memo, key) => {
          memo[key] = paramsCopy[key]

          return memo
        }, {} as FlattenParams)
    }
  }
  return paramsCopy
}

export { set }

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).