UNPKG

1.33 kBJavaScriptView Raw
1/**
2EXPERIMENTAL
3
4function* generate(field, values, path = '') {
5 const [ , key, rest ] = /([^.]+)\.?(.+)?/.exec(field)
6
7 if (/.+\[\]/.test(key)) {
8 // is array key
9 const noBrackets = key.substring(0, key.length - 2)
10 const array = values && values[ noBrackets ]
11 if (array && !Array.isArray(array)) {
12 throw new Error(`Expected array value for ${path}${field}, but found ${typeof array}: ${JSON.stringify(array)}`)
13 }
14 if (array) {
15 if (rest) {
16 for (let index in array) {
17 yield * generate(rest, array[ index ], `${path}${noBrackets}[${index}].`)
18 }
19 } else {
20 for (let index in array) {
21 yield `${path}${noBrackets}[${index}]`
22 }
23 }
24 } else {
25 yield `${path}${key}`
26 }
27 } else if (rest) {
28 // need to recurse
29 yield * generate(rest, values && values[ key ], `${path}${key}.`)
30 } else {
31 // value key
32 yield `${path}${key}`
33 }
34}
35*/
36
37/**
38 * Iterates over all the fields specified by a fields array and store values.
39 *
40 * @param fields The fields array given to redux-form
41 * @param values The current values of the form in the Redux store
42 */
43
44/**
45 EXPERIMENTAL
46
47 export default function* fieldKeys(fields = [], values = {}) {
48 for (let field of fields) {
49 yield * generate(field, values)
50 }
51}
52*/
53"use strict";
\No newline at end of file