mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
chore: Use ES6 template strings in CSS and code generators (#5902)
* Unindent CSS, save 3 kb of code. * Convert generator functions to template strings. This resolves #5761.
This commit is contained in:
@@ -42,7 +42,7 @@ Dart['math_arithmetic'] = function(block) {
|
||||
'MINUS': [' - ', Dart.ORDER_ADDITIVE],
|
||||
'MULTIPLY': [' * ', Dart.ORDER_MULTIPLICATIVE],
|
||||
'DIVIDE': [' / ', Dart.ORDER_MULTIPLICATIVE],
|
||||
'POWER': [null, Dart.ORDER_NONE] // Handle power separately.
|
||||
'POWER': [null, Dart.ORDER_NONE], // Handle power separately.
|
||||
};
|
||||
const tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
const operator = tuple[0];
|
||||
@@ -152,7 +152,7 @@ Dart['math_constant'] = function(block) {
|
||||
'GOLDEN_RATIO': ['(1 + Math.sqrt(5)) / 2', Dart.ORDER_MULTIPLICATIVE],
|
||||
'SQRT2': ['Math.sqrt2', Dart.ORDER_UNARY_POSTFIX],
|
||||
'SQRT1_2': ['Math.sqrt1_2', Dart.ORDER_UNARY_POSTFIX],
|
||||
'INFINITY': ['double.infinity', Dart.ORDER_ATOMIC]
|
||||
'INFINITY': ['double.infinity', Dart.ORDER_ATOMIC],
|
||||
};
|
||||
const constant = block.getFieldValue('CONSTANT');
|
||||
if (constant !== 'INFINITY') {
|
||||
@@ -165,20 +165,13 @@ Dart['math_number_property'] = function(block) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
const PROPERTIES = {
|
||||
'EVEN': [' % 2 == 0', Dart.ORDER_MULTIPLICATIVE,
|
||||
Dart.ORDER_EQUALITY],
|
||||
'ODD': [' % 2 == 1', Dart.ORDER_MULTIPLICATIVE,
|
||||
Dart.ORDER_EQUALITY],
|
||||
'WHOLE': [' % 1 == 0', Dart.ORDER_MULTIPLICATIVE,
|
||||
Dart.ORDER_EQUALITY],
|
||||
'POSITIVE': [' > 0', Dart.ORDER_RELATIONAL,
|
||||
Dart.ORDER_RELATIONAL],
|
||||
'NEGATIVE': [' < 0', Dart.ORDER_RELATIONAL,
|
||||
Dart.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, Dart.ORDER_MULTIPLICATIVE,
|
||||
Dart.ORDER_EQUALITY],
|
||||
'PRIME': [null, Dart.ORDER_NONE,
|
||||
Dart.ORDER_UNARY_POSTFIX]
|
||||
'EVEN': [' % 2 == 0', Dart.ORDER_MULTIPLICATIVE, Dart.ORDER_EQUALITY],
|
||||
'ODD': [' % 2 == 1', Dart.ORDER_MULTIPLICATIVE, Dart.ORDER_EQUALITY],
|
||||
'WHOLE': [' % 1 == 0', Dart.ORDER_MULTIPLICATIVE, Dart.ORDER_EQUALITY],
|
||||
'POSITIVE': [' > 0', Dart.ORDER_RELATIONAL, Dart.ORDER_RELATIONAL],
|
||||
'NEGATIVE': [' < 0', Dart.ORDER_RELATIONAL, Dart.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, Dart.ORDER_MULTIPLICATIVE, Dart.ORDER_EQUALITY],
|
||||
'PRIME': [null, Dart.ORDER_NONE, Dart.ORDER_UNARY_POSTFIX],
|
||||
};
|
||||
const dropdownProperty = block.getFieldValue('PROPERTY');
|
||||
const [suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
|
||||
@@ -189,27 +182,26 @@ Dart['math_number_property'] = function(block) {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
const functionName = Dart.provideFunction_(
|
||||
'math_isPrime',
|
||||
['bool ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if (n == 2 || n == 3) {',
|
||||
' return true;',
|
||||
' }',
|
||||
' // False if n is null, negative, is 1, or not whole.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' +
|
||||
' n % 3 == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {',
|
||||
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
'}']);
|
||||
const functionName = Dart.provideFunction_('math_isPrime', `
|
||||
bool ${Dart.FUNCTION_NAME_PLACEHOLDER_}(n) {
|
||||
// https://en.wikipedia.org/wiki/Primality_test#Naive_methods
|
||||
if (n == 2 || n == 3) {
|
||||
return true;
|
||||
}
|
||||
// False if n is null, negative, is 1, or not whole.
|
||||
// And false if n is divisible by 2 or 3.
|
||||
if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 || n % 3 == 0) {
|
||||
return false;
|
||||
}
|
||||
// Check all the numbers of form 6k +/- 1, up to sqrt(n).
|
||||
for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {
|
||||
if (n % (x - 1) == 0 || n % (x + 1) == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + numberToCheck + ')';
|
||||
} else if (dropdownProperty === 'DIVISIBLE_BY') {
|
||||
const divisor = Dart.valueToCode(block, 'DIVISOR',
|
||||
@@ -246,70 +238,76 @@ Dart['math_on_list'] = function(block) {
|
||||
let code;
|
||||
switch (func) {
|
||||
case 'SUM': {
|
||||
const functionName = Dart.provideFunction_('math_sum', [
|
||||
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List<num> myList) {',
|
||||
' num sumVal = 0;',
|
||||
' myList.forEach((num entry) {sumVal += entry;});', ' return sumVal;',
|
||||
'}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_sum', `
|
||||
num ${Dart.FUNCTION_NAME_PLACEHOLDER_}(List<num> myList) {
|
||||
num sumVal = 0;
|
||||
myList.forEach((num entry) {sumVal += entry;});
|
||||
return sumVal;
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'MIN': {
|
||||
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||
const functionName = Dart.provideFunction_('math_min', [
|
||||
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List<num> myList) {',
|
||||
' if (myList.isEmpty) return null;', ' num minVal = myList[0];',
|
||||
' myList.forEach((num entry) ' +
|
||||
'{minVal = Math.min(minVal, entry);});',
|
||||
' return minVal;', '}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_min', `
|
||||
num ${Dart.FUNCTION_NAME_PLACEHOLDER_}(List<num> myList) {
|
||||
if (myList.isEmpty) return null;
|
||||
num minVal = myList[0];
|
||||
myList.forEach((num entry) {minVal = Math.min(minVal, entry);});
|
||||
return minVal;
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'MAX': {
|
||||
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||
const functionName = Dart.provideFunction_('math_max', [
|
||||
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List<num> myList) {',
|
||||
' if (myList.isEmpty) return null;', ' num maxVal = myList[0];',
|
||||
' myList.forEach((num entry) ' +
|
||||
'{maxVal = Math.max(maxVal, entry);});',
|
||||
' return maxVal;', '}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_max', `
|
||||
num ${Dart.FUNCTION_NAME_PLACEHOLDER_}(List<num> myList) {
|
||||
if (myList.isEmpty) return null;
|
||||
num maxVal = myList[0];
|
||||
myList.forEach((num entry) {maxVal = Math.max(maxVal, entry);});
|
||||
return maxVal;
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'AVERAGE': {
|
||||
// This operation exclude null and values that are not int or float:
|
||||
// math_mean([null,null,"aString",1,9]) -> 5.0
|
||||
const functionName = Dart.provideFunction_('math_mean', [
|
||||
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {',
|
||||
' // First filter list for numbers only.',
|
||||
' List localList = new List.from(myList);',
|
||||
' localList.removeWhere((a) => a is! num);',
|
||||
' if (localList.isEmpty) return null;', ' num sumVal = 0;',
|
||||
' localList.forEach((var entry) {sumVal += entry;});',
|
||||
' return sumVal / localList.length;', '}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_mean', `
|
||||
num ${Dart.FUNCTION_NAME_PLACEHOLDER_}(List myList) {
|
||||
// First filter list for numbers only.
|
||||
List localList = new List.from(myList);
|
||||
localList.removeWhere((a) => a is! num);
|
||||
if (localList.isEmpty) return null;
|
||||
num sumVal = 0;
|
||||
localList.forEach((var entry) {sumVal += entry;});
|
||||
return sumVal / localList.length;
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'MEDIAN': {
|
||||
const functionName = Dart.provideFunction_('math_median', [
|
||||
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {',
|
||||
' // First filter list for numbers only, then sort, ' +
|
||||
'then return middle value',
|
||||
' // or the average of two middle values if list has an ' +
|
||||
'even number of elements.',
|
||||
' List localList = new List.from(myList);',
|
||||
' localList.removeWhere((a) => a is! num);',
|
||||
' if (localList.isEmpty) return null;',
|
||||
' localList.sort((a, b) => (a - b));',
|
||||
' int index = localList.length ~/ 2;',
|
||||
' if (localList.length % 2 == 1) {', ' return localList[index];',
|
||||
' } else {',
|
||||
' return (localList[index - 1] + localList[index]) / 2;', ' }', '}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_median', `
|
||||
num ${Dart.FUNCTION_NAME_PLACEHOLDER_}(List myList) {
|
||||
// First filter list for numbers only, then sort, then return middle value
|
||||
// or the average of two middle values if list has an even number of elements.
|
||||
List localList = new List.from(myList);
|
||||
localList.removeWhere((a) => a is! num);
|
||||
if (localList.isEmpty) return null;
|
||||
localList.sort((a, b) => (a - b));
|
||||
int index = localList.length ~/ 2;
|
||||
if (localList.length % 2 == 1) {
|
||||
return localList[index];
|
||||
} else {
|
||||
return (localList[index - 1] + localList[index]) / 2;
|
||||
}
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
@@ -318,63 +316,67 @@ Dart['math_on_list'] = function(block) {
|
||||
// As a list of numbers can contain more than one mode,
|
||||
// the returned result is provided as an array.
|
||||
// Mode of [3, 'x', 'x', 1, 1, 2, '3'] -> ['x', 1]
|
||||
const functionName = Dart.provideFunction_('math_modes', [
|
||||
'List ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List values) {',
|
||||
' List modes = [];',
|
||||
' List counts = [];',
|
||||
' int maxCount = 0;',
|
||||
' for (int i = 0; i < values.length; i++) {',
|
||||
' var value = values[i];',
|
||||
' bool found = false;',
|
||||
' int thisCount;',
|
||||
' for (int j = 0; j < counts.length; j++) {',
|
||||
' if (counts[j][0] == value) {',
|
||||
' thisCount = ++counts[j][1];',
|
||||
' found = true;',
|
||||
' break;',
|
||||
' }',
|
||||
' }',
|
||||
' if (!found) {',
|
||||
' counts.add([value, 1]);',
|
||||
' thisCount = 1;',
|
||||
' }',
|
||||
' maxCount = Math.max(thisCount, maxCount);',
|
||||
' }',
|
||||
' for (int j = 0; j < counts.length; j++) {',
|
||||
' if (counts[j][1] == maxCount) {',
|
||||
' modes.add(counts[j][0]);',
|
||||
' }',
|
||||
' }',
|
||||
' return modes;',
|
||||
'}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_modes', `
|
||||
List ${Dart.FUNCTION_NAME_PLACEHOLDER_}(List values) {
|
||||
List modes = [];
|
||||
List counts = [];
|
||||
int maxCount = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
var value = values[i];
|
||||
bool found = false;
|
||||
int thisCount;
|
||||
for (int j = 0; j < counts.length; j++) {
|
||||
if (counts[j][0] == value) {
|
||||
thisCount = ++counts[j][1];
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
counts.add([value, 1]);
|
||||
thisCount = 1;
|
||||
}
|
||||
maxCount = Math.max(thisCount, maxCount);
|
||||
}
|
||||
for (int j = 0; j < counts.length; j++) {
|
||||
if (counts[j][1] == maxCount) {
|
||||
modes.add(counts[j][0]);
|
||||
}
|
||||
}
|
||||
return modes;
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'STD_DEV': {
|
||||
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||
const functionName = Dart.provideFunction_('math_standard_deviation', [
|
||||
'num ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {',
|
||||
' // First filter list for numbers only.',
|
||||
' List numbers = new List.from(myList);',
|
||||
' numbers.removeWhere((a) => a is! num);',
|
||||
' if (numbers.isEmpty) return null;', ' num n = numbers.length;',
|
||||
' num sum = 0;', ' numbers.forEach((x) => sum += x);',
|
||||
' num mean = sum / n;', ' num sumSquare = 0;',
|
||||
' numbers.forEach((x) => sumSquare += ' +
|
||||
'Math.pow(x - mean, 2));',
|
||||
' return Math.sqrt(sumSquare / n);', '}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_standard_deviation', `
|
||||
num ${Dart.FUNCTION_NAME_PLACEHOLDER_}(List myList) {
|
||||
// First filter list for numbers only.
|
||||
List numbers = new List.from(myList);
|
||||
numbers.removeWhere((a) => a is! num);
|
||||
if (numbers.isEmpty) return null;
|
||||
num n = numbers.length;
|
||||
num sum = 0;
|
||||
numbers.forEach((x) => sum += x);
|
||||
num mean = sum / n;
|
||||
num sumSquare = 0;
|
||||
numbers.forEach((x) => sumSquare += Math.pow(x - mean, 2));
|
||||
return Math.sqrt(sumSquare / n);
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'RANDOM': {
|
||||
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||
const functionName = Dart.provideFunction_('math_random_item', [
|
||||
'dynamic ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(List myList) {',
|
||||
' int x = new Math.Random().nextInt(myList.length);',
|
||||
' return myList[x];', '}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_random_item', `
|
||||
dynamic ${Dart.FUNCTION_NAME_PLACEHOLDER_}(List myList) {
|
||||
int x = new Math.Random().nextInt(myList.length);
|
||||
return myList[x];
|
||||
}
|
||||
`);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
@@ -411,12 +413,17 @@ Dart['math_random_int'] = function(block) {
|
||||
Dart.definitions_['import_dart_math'] = 'import \'dart:math\' as Math;';
|
||||
const argument0 = Dart.valueToCode(block, 'FROM', Dart.ORDER_NONE) || '0';
|
||||
const argument1 = Dart.valueToCode(block, 'TO', Dart.ORDER_NONE) || '0';
|
||||
const functionName = Dart.provideFunction_('math_random_int', [
|
||||
'int ' + Dart.FUNCTION_NAME_PLACEHOLDER_ + '(num a, num b) {',
|
||||
' if (a > b) {', ' // Swap a and b to ensure a is smaller.',
|
||||
' num c = a;', ' a = b;', ' b = c;', ' }',
|
||||
' return new Math.Random().nextInt(b - a + 1) + a;', '}'
|
||||
]);
|
||||
const functionName = Dart.provideFunction_('math_random_int', `
|
||||
int ${Dart.FUNCTION_NAME_PLACEHOLDER_}(num a, num b) {
|
||||
if (a > b) {
|
||||
// Swap a and b to ensure a is smaller.
|
||||
num c = a;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return new Math.Random().nextInt(b - a + 1) + a;
|
||||
}
|
||||
`);
|
||||
const code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user