Skip to content

Commit

Permalink
Add path parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent 4633841 commit de3b743
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/config/normalize/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// TODO: validate when invalid `name`
export const parsePropName = function (name) {
const propPath = [...`.${name}`.matchAll(PROP_NAME_REGEXP)].map(normalizeProp)
return propPath
}

const PROP_NAME_REGEXP =
/(?<loose>\?)?((\.(?<propName>[^.?[\]]+))|(\[(?<propIndex>[\d*]+)\]))/guy

const normalizeProp = function ({ groups: { propName, propIndex, loose } }) {
const value = getPropValue(propName, propIndex)
const isArray = propIndex !== undefined
const looseBool = loose !== undefined
return { value, isArray, loose: looseBool }
}

const getPropValue = function (propName, propIndex) {
if (propName !== undefined) {
return propName
}

return propIndex === '*' ? propIndex : Number(propIndex)
}

0 comments on commit de3b743

Please sign in to comment.