UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

41 lines (40 loc) 1.14 kB
//#region src/compat/_internal/isDeepKey.ts /** Matches any deep property path. Examples: `a.b`, `a[0]`, `a["b"]` */ const regexIsDeepProp = /\.|(\[(?:[^[\]]*|(["'])(?:(?!\2)[^\\]|\\.)*?\2)\])/; /** * Checks if a given key is a deep key. * * A deep key is a string that contains a dot (.) or square brackets with a property accessor. * * @param {PropertyKey} key - The key to check. * @returns {boolean} - Returns true if the key is a deep key, otherwise false. * * Examples: * * isDeepKey('a.b') // true * isDeepKey('a[b]') // true * isDeepKey('a') // false * isDeepKey(123) // false * isDeepKey('a.b.c') // true * isDeepKey('a[b][c]') // true * isDeepKey('a.') // false * isDeepKey('.a') // false * isDeepKey('a[b') // false * isDeepKey('a]b]') // false * isDeepKey('a][b') // false * isDeepKey('') // false * isDeepKey('a[0]') // true * */ function isDeepKey(key) { switch (typeof key) { case "number": case "symbol": return false; case "string": if (key === "" || key.startsWith(".") || key.endsWith(".")) return false; return regexIsDeepProp.test(key); default: return false; } } //#endregion export { isDeepKey };