react-code-dataset / admin-bro /src /utils /flat /property-key-regex.ts
Devendra174's picture
Upload folder using huggingface_hub
1e92f2d verified
raw
history blame contribute delete
890 Bytes
import { DELIMITER } from './constants.js'
import { GetOptions } from './flat.types.js'
// this is the regex used to find all existing properties starting with a key
export const propertyKeyRegex = (propertyPath: string, options?: GetOptions): RegExp => {
const delimiter = new RegExp(`\\${DELIMITER}`, 'g')
const escapedDelimiter = `\\${DELIMITER}`
// but for `nested.1.property.0` it will produce `nested(\.|\.\d+\.)1(\.|\.\d+\.)property.0`
// and this is intentional because user can give an one index in property path for with deeply
// nested arrays
const escapedDelimiterOrIndex = `(${escapedDelimiter}|${escapedDelimiter}\\d+${escapedDelimiter})`
const path = options?.includeAllSiblings
? propertyPath.replace(delimiter, escapedDelimiterOrIndex)
: propertyPath.replace(delimiter, escapedDelimiter)
return new RegExp(`^${path}($|${escapedDelimiter})`, '')
}