* A wrapper around a single value (usually a property of an object/array) * that makes it easy to iterate over properties of the value, and extract * them.
| 103 | * them. |
| 104 | */ |
| 105 | class ObjectParserProp<P> implements IObjectParserProp<P> { |
| 106 | /** The value this is wrapping. */ |
| 107 | private readonly _property: P; |
| 108 | /** The context of this object parser tree. */ |
| 109 | private readonly _context: Context; |
| 110 | /** Stack of property names that lead to this value. */ |
| 111 | private readonly _stack: string[]; |
| 112 | |
| 113 | constructor(property: P, context: Context, stack: string[]) { |
| 114 | this._property = property; |
| 115 | this._context = context; |
| 116 | this._stack = stack; |
| 117 | } |
| 118 | |
| 119 | map(func: ObjectParserMapFunc<P>): this { |
| 120 | const prop = this._property; |
| 121 | if (typeof prop === 'object' && prop !== null && !Array.isArray(prop)) { |
| 122 | for (const label in prop) { |
| 123 | const item = new ObjectParserProp(prop[label], this._context, createStack(this._stack, label)); |
| 124 | func(item as any, label, prop); |
| 125 | } |
| 126 | } else { |
| 127 | this._context.onError(new ObjectParserError(this._stack, 'Property is not a non-array object.')); |
| 128 | } |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | mapRaw(func: ObjectParserMapRawFunc<P>): this { |
| 133 | const prop = this._property; |
| 134 | if (typeof prop === 'object' && prop !== null && !Array.isArray(prop)) { |
| 135 | for (const label in prop) { |
| 136 | func(prop[label], label, prop); |
| 137 | } |
| 138 | } else { |
| 139 | this._context.onError(new ObjectParserError(this._stack, 'Property is not a non-array object.')); |
| 140 | } |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | array(func: ObjectParserArrayFunc<P>): this { |
| 145 | const prop = this._property; |
| 146 | if (Array.isArray(prop)) { |
| 147 | for (let i = 0; i < prop.length; i++) { |
| 148 | const item = new ObjectParserProp(prop[i], this._context, createStack(this._stack, i)); |
| 149 | func(item as any, i, prop); |
| 150 | } |
| 151 | } else { |
| 152 | this._context.onError(new ObjectParserError(this._stack, 'Property is not an array.')); |
| 153 | } |
| 154 | return this; |
| 155 | } |
| 156 | |
| 157 | arrayRaw(func: ObjectParserArrayRawFunc<P>): this { |
| 158 | const prop = this._property; |
| 159 | if (Array.isArray(prop)) { |
| 160 | for (let i = 0; i < prop.length; i++) { |
| 161 | func(prop[i], i, prop); |
| 162 | } |
nothing calls this directly
no outgoing calls
no test coverage detected