( block: Block, generator: DartGenerator, )
| 95 | } |
| 96 | |
| 97 | export function text_charAt( |
| 98 | block: Block, |
| 99 | generator: DartGenerator, |
| 100 | ): [string, Order] { |
| 101 | // Get letter at index. |
| 102 | // Note: Until January 2013 this block did not have the WHERE input. |
| 103 | const where = block.getFieldValue('WHERE') || 'FROM_START'; |
| 104 | const textOrder = |
| 105 | where === 'FIRST' || where === 'FROM_START' |
| 106 | ? Order.UNARY_POSTFIX |
| 107 | : Order.NONE; |
| 108 | const text = generator.valueToCode(block, 'VALUE', textOrder) || "''"; |
| 109 | let at; |
| 110 | switch (where) { |
| 111 | case 'FIRST': { |
| 112 | const code = text + '[0]'; |
| 113 | return [code, Order.UNARY_POSTFIX]; |
| 114 | } |
| 115 | case 'FROM_START': { |
| 116 | at = generator.getAdjusted(block, 'AT'); |
| 117 | const code = text + '[' + at + ']'; |
| 118 | return [code, Order.UNARY_POSTFIX]; |
| 119 | } |
| 120 | case 'LAST': |
| 121 | case 'FROM_END': { |
| 122 | if (where === 'LAST') { |
| 123 | at = 1; |
| 124 | } else { |
| 125 | at = generator.getAdjusted(block, 'AT', 1); |
| 126 | } |
| 127 | const functionName = generator.provideFunction_( |
| 128 | 'text_get_from_end', |
| 129 | ` |
| 130 | String ${generator.FUNCTION_NAME_PLACEHOLDER_}(String text, num x) { |
| 131 | return text[text.length - x]; |
| 132 | } |
| 133 | `, |
| 134 | ); |
| 135 | const code = `${functionName}(${text}, ${at})`; |
| 136 | return [code, Order.UNARY_POSTFIX]; |
| 137 | } |
| 138 | case 'RANDOM': { |
| 139 | // TODO(#7600): find better approach than casting to any to override |
| 140 | // CodeGenerator declaring .definitions protected. |
| 141 | (generator as AnyDuringMigration).definitions_['import_dart_math'] = |
| 142 | "import 'dart:math' as Math;"; |
| 143 | const functionName = generator.provideFunction_( |
| 144 | 'text_random_letter', |
| 145 | ` |
| 146 | String ${generator.FUNCTION_NAME_PLACEHOLDER_}(String text) { |
| 147 | int x = new Math.Random().nextInt(text.length); |
| 148 | return text[x]; |
| 149 | } |
| 150 | `, |
| 151 | ); |
| 152 | const code = functionName + '(' + text + ')'; |
| 153 | return [code, Order.UNARY_POSTFIX]; |
| 154 | } |
nothing calls this directly
no test coverage detected