-
Notifications
You must be signed in to change notification settings - Fork 638
/
find-sub-property.ts
29 lines (28 loc) · 1.13 KB
/
find-sub-property.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import PropertyDecorator from '../../property/property-decorator'
import { PathParts } from '../../../../utils/flat/path-parts.type'
/**
* @private
*
* @param {PathParts} pathParts parts returned by `pathToParts` method
* @param {PropertyDecorator} rootProperty where function should recursively search for
* a subProperty matching one of the pathParts
*
* @return {PropertyDecorator | null} found subProperty
*/
export const findSubProperty = (
pathParts: PathParts,
rootProperty: PropertyDecorator,
): PropertyDecorator | null => {
const subProperties = rootProperty.subProperties()
const foundPath = pathParts.find(path => (
subProperties.find(supProperty => supProperty.propertyPath === path)))
if (foundPath) {
const subProperty = subProperties.find(supProperty => supProperty.propertyPath === foundPath)
if (subProperty && foundPath !== pathParts[pathParts.length - 1]) {
// if foundPath is not the last (full) path - checkout recursively all subProperties
return findSubProperty(pathParts, subProperty)
}
return subProperty || null
}
return null
}