Aller au contenu principal

generateArrayItemsLoop()

generateArrayItemsLoop(arrayVar, ctx, itemGenerator, supportsCoercion?): object

Experimental

Generates an optimized for loop for validating array items.


Parameters

arrayVar: string

The variable name of the array

ctx: GeneratorContext

The generator context

itemGenerator: (varName, ctx) => object

Code generator for each item

supportsCoercion?: boolean

Whether to pre-allocate result array for coercions


Returns: object

Generated code lines and updated context

code: string[]

ctx: GeneratorContext


Since

2.0.0


Example

const result = generateArrayItemsLoop("value", ctx, stringGenerator);
// result.code = [
// 'var len = value.length;',
// 'for (var i = 0; i < len; i++) {',
// ' var item = value[i];',
// ' if (typeof item !== "string") return "Index " + i + ": Expected string";',
// '}'
// ]