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:
Neil Fraser
2022-01-28 17:58:43 -08:00
committed by GitHub
parent a31003fab9
commit 1f6a1bd8d9
39 changed files with 2194 additions and 2062 deletions

View File

@@ -34,7 +34,7 @@ PHP['math_arithmetic'] = function(block) {
'MINUS': [' - ', PHP.ORDER_SUBTRACTION],
'MULTIPLY': [' * ', PHP.ORDER_MULTIPLICATION],
'DIVIDE': [' / ', PHP.ORDER_DIVISION],
'POWER': [' ** ', PHP.ORDER_POWER]
'POWER': [' ** ', PHP.ORDER_POWER],
};
const tuple = OPERATORS[block.getFieldValue('OP')];
const operator = tuple[0];
@@ -134,7 +134,7 @@ PHP['math_constant'] = function(block) {
'GOLDEN_RATIO': ['(1 + sqrt(5)) / 2', PHP.ORDER_DIVISION],
'SQRT2': ['M_SQRT2', PHP.ORDER_ATOMIC],
'SQRT1_2': ['M_SQRT1_2', PHP.ORDER_ATOMIC],
'INFINITY': ['INF', PHP.ORDER_ATOMIC]
'INFINITY': ['INF', PHP.ORDER_ATOMIC],
};
return CONSTANTS[block.getFieldValue('CONSTANT')];
};
@@ -143,20 +143,13 @@ PHP['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', PHP.ORDER_MODULUS,
PHP.ORDER_EQUALITY],
'ODD': ['', ' % 2 == 1', PHP.ORDER_MODULUS,
PHP.ORDER_EQUALITY],
'WHOLE': ['is_int(', ')', PHP.ORDER_NONE,
PHP.ORDER_FUNCTION_CALL],
'POSITIVE': ['', ' > 0', PHP.ORDER_RELATIONAL,
PHP.ORDER_RELATIONAL],
'NEGATIVE': ['', ' < 0', PHP.ORDER_RELATIONAL,
PHP.ORDER_RELATIONAL],
'DIVISIBLE_BY': [null, null, PHP.ORDER_MODULUS,
PHP.ORDER_EQUALITY],
'PRIME': [null, null, PHP.ORDER_NONE,
PHP.ORDER_FUNCTION_CALL]
'EVEN': ['', ' % 2 == 0', PHP.ORDER_MODULUS, PHP.ORDER_EQUALITY],
'ODD': ['', ' % 2 == 1', PHP.ORDER_MODULUS, PHP.ORDER_EQUALITY],
'WHOLE': ['is_int(', ')', PHP.ORDER_NONE, PHP.ORDER_FUNCTION_CALL],
'POSITIVE': ['', ' > 0', PHP.ORDER_RELATIONAL, PHP.ORDER_RELATIONAL],
'NEGATIVE': ['', ' < 0', PHP.ORDER_RELATIONAL, PHP.ORDER_RELATIONAL],
'DIVISIBLE_BY': [null, null, PHP.ORDER_MODULUS, PHP.ORDER_EQUALITY],
'PRIME': [null, null, PHP.ORDER_NONE, PHP.ORDER_FUNCTION_CALL],
};
const dropdownProperty = block.getFieldValue('PROPERTY');
const [prefix, suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
@@ -165,27 +158,26 @@ PHP['math_number_property'] = function(block) {
let code;
if (dropdownProperty === 'PRIME') {
// Prime is a special case as it is not a one-liner test.
const functionName = PHP.provideFunction_(
'math_isPrime',
['function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($n) {',
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
' if ($n == 2 || $n == 3) {',
' return true;',
' }',
' // False if n is NaN, negative, is 1, or not whole.',
' // And false if n is divisible by 2 or 3.',
' if (!is_numeric($n) || $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 ($x = 6; $x <= sqrt($n) + 1; $x += 6) {',
' if ($n % ($x - 1) == 0 || $n % ($x + 1) == 0) {',
' return false;',
' }',
' }',
' return true;',
'}']);
const functionName = PHP.provideFunction_('math_isPrime', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($n) {
// https://en.wikipedia.org/wiki/Primality_test#Naive_methods
if ($n == 2 || $n == 3) {
return true;
}
// False if n is NaN, negative, is 1, or not whole.
// And false if n is divisible by 2 or 3.
if (!is_numeric($n) || $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 ($x = 6; $x <= 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 = PHP.valueToCode(block, 'DIVISOR',
@@ -236,23 +228,23 @@ PHP['math_on_list'] = function(block) {
code = 'max(' + list + ')';
break;
case 'AVERAGE': {
const functionName = PHP.provideFunction_('math_mean', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($myList) {',
' return array_sum($myList) / count($myList);', '}'
]);
const functionName = PHP.provideFunction_('math_mean', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($myList) {
return array_sum($myList) / count($myList);
}
`);
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || 'array()';
code = functionName + '(' + list + ')';
break;
}
case 'MEDIAN': {
const functionName = PHP.provideFunction_('math_median', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($arr) {',
' sort($arr,SORT_NUMERIC);',
' return (count($arr) % 2) ? $arr[floor(count($arr)/2)] : ',
' ($arr[floor(count($arr)/2)] + $arr[floor(count($arr)/2)' +
' - 1]) / 2;',
'}'
]);
const functionName = PHP.provideFunction_('math_median', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($arr) {
sort($arr,SORT_NUMERIC);
return (count($arr) % 2) ? $arr[floor(count($arr) / 2)] :
($arr[floor(count($arr) / 2)] + $arr[floor(count($arr) / 2) - 1]) / 2;
}
`);
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
code = functionName + '(' + list + ')';
break;
@@ -261,36 +253,40 @@ PHP['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 = PHP.provideFunction_('math_modes', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($values) {',
' if (empty($values)) return array();',
' $counts = array_count_values($values);',
' arsort($counts); // Sort counts in descending order',
' $modes = array_keys($counts, current($counts), true);',
' return $modes;', '}'
]);
const functionName = PHP.provideFunction_('math_modes', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($values) {
if (empty($values)) return array();
$counts = array_count_values($values);
arsort($counts); // Sort counts in descending order
$modes = array_keys($counts, current($counts), true);
return $modes;
}
`);
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
code = functionName + '(' + list + ')';
break;
}
case 'STD_DEV': {
const functionName = PHP.provideFunction_('math_standard_deviation', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($numbers) {',
' $n = count($numbers);', ' if (!$n) return null;',
' $mean = array_sum($numbers) / count($numbers);',
' foreach($numbers as $key => $num) $devs[$key] = ' +
'pow($num - $mean, 2);',
' return sqrt(array_sum($devs) / (count($devs) - 1));', '}'
]);
const functionName = PHP.provideFunction_('math_standard_deviation', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($numbers) {
$n = count($numbers);
if (!$n) return null;
$mean = array_sum($numbers) / count($numbers);
foreach($numbers as $key => $num) $devs[$key] = pow($num - $mean, 2);
return sqrt(array_sum($devs) / (count($devs) - 1));
}
`);
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
code = functionName + '(' + list + ')';
break;
}
case 'RANDOM': {
const functionName = PHP.provideFunction_('math_random_list', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($list) {',
' $x = rand(0, count($list)-1);', ' return $list[$x];', '}'
]);
const functionName = PHP.provideFunction_('math_random_list', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($list) {
$x = rand(0, count($list)-1);
return $list[$x];
}
`);
list = PHP.valueToCode(block, 'LIST', PHP.ORDER_NONE) || '[]';
code = functionName + '(' + list + ')';
break;
@@ -325,11 +321,14 @@ PHP['math_random_int'] = function(block) {
// Random integer between [X] and [Y].
const argument0 = PHP.valueToCode(block, 'FROM', PHP.ORDER_NONE) || '0';
const argument1 = PHP.valueToCode(block, 'TO', PHP.ORDER_NONE) || '0';
const functionName = PHP.provideFunction_('math_random_int', [
'function ' + PHP.FUNCTION_NAME_PLACEHOLDER_ + '($a, $b) {',
' if ($a > $b) {', ' return rand($b, $a);', ' }',
' return rand($a, $b);', '}'
]);
const functionName = PHP.provideFunction_('math_random_int', `
function ${PHP.FUNCTION_NAME_PLACEHOLDER_}($a, $b) {
if ($a > $b) {
return rand($b, $a);
}
return rand($a, $b);
}
`);
const code = functionName + '(' + argument0 + ', ' + argument1 + ')';
return [code, PHP.ORDER_FUNCTION_CALL];
};