mirror of
https://github.com/google/blockly.git
synced 2026-01-08 17:40:09 +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:
@@ -31,7 +31,7 @@ JavaScript['math_arithmetic'] = function(block) {
|
||||
'MINUS': [' - ', JavaScript.ORDER_SUBTRACTION],
|
||||
'MULTIPLY': [' * ', JavaScript.ORDER_MULTIPLICATION],
|
||||
'DIVIDE': [' / ', JavaScript.ORDER_DIVISION],
|
||||
'POWER': [null, JavaScript.ORDER_NONE] // Handle power separately.
|
||||
'POWER': [null, JavaScript.ORDER_NONE], // Handle power separately.
|
||||
};
|
||||
const tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
const operator = tuple[0];
|
||||
@@ -137,11 +137,10 @@ JavaScript['math_constant'] = function(block) {
|
||||
const CONSTANTS = {
|
||||
'PI': ['Math.PI', JavaScript.ORDER_MEMBER],
|
||||
'E': ['Math.E', JavaScript.ORDER_MEMBER],
|
||||
'GOLDEN_RATIO':
|
||||
['(1 + Math.sqrt(5)) / 2', JavaScript.ORDER_DIVISION],
|
||||
'GOLDEN_RATIO': ['(1 + Math.sqrt(5)) / 2', JavaScript.ORDER_DIVISION],
|
||||
'SQRT2': ['Math.SQRT2', JavaScript.ORDER_MEMBER],
|
||||
'SQRT1_2': ['Math.SQRT1_2', JavaScript.ORDER_MEMBER],
|
||||
'INFINITY': ['Infinity', JavaScript.ORDER_ATOMIC]
|
||||
'INFINITY': ['Infinity', JavaScript.ORDER_ATOMIC],
|
||||
};
|
||||
return CONSTANTS[block.getFieldValue('CONSTANT')];
|
||||
};
|
||||
@@ -150,20 +149,16 @@ JavaScript['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', JavaScript.ORDER_MODULUS,
|
||||
JavaScript.ORDER_EQUALITY],
|
||||
'ODD': [' % 2 === 1', JavaScript.ORDER_MODULUS,
|
||||
JavaScript.ORDER_EQUALITY],
|
||||
'EVEN': [' % 2 === 0', JavaScript.ORDER_MODULUS, JavaScript.ORDER_EQUALITY],
|
||||
'ODD': [' % 2 === 1', JavaScript.ORDER_MODULUS, JavaScript.ORDER_EQUALITY],
|
||||
'WHOLE': [' % 1 === 0', JavaScript.ORDER_MODULUS,
|
||||
JavaScript.ORDER_EQUALITY],
|
||||
'POSITIVE': [' > 0', JavaScript.ORDER_RELATIONAL,
|
||||
JavaScript.ORDER_RELATIONAL],
|
||||
'NEGATIVE': [' < 0', JavaScript.ORDER_RELATIONAL,
|
||||
JavaScript.ORDER_RELATIONAL],
|
||||
'DIVISIBLE_BY': [null, JavaScript.ORDER_MODULUS,
|
||||
JavaScript.ORDER_EQUALITY],
|
||||
'PRIME': [null, JavaScript.ORDER_NONE,
|
||||
JavaScript.ORDER_FUNCTION_CALL]
|
||||
'DIVISIBLE_BY': [null, JavaScript.ORDER_MODULUS, JavaScript.ORDER_EQUALITY],
|
||||
'PRIME': [null, JavaScript.ORDER_NONE, JavaScript.ORDER_FUNCTION_CALL],
|
||||
};
|
||||
const dropdownProperty = block.getFieldValue('PROPERTY');
|
||||
const [suffix, inputOrder, outputOrder] = PROPERTIES[dropdownProperty];
|
||||
@@ -172,27 +167,26 @@ JavaScript['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 = JavaScript.provideFunction_(
|
||||
'mathIsPrime',
|
||||
['function ' + JavaScript.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 (isNaN(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 (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 = JavaScript.provideFunction_('mathIsPrime', `
|
||||
function ${JavaScript.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 (isNaN(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 (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 = JavaScript.valueToCode(block, 'DIVISOR',
|
||||
@@ -242,13 +236,11 @@ JavaScript['math_on_list'] = function(block) {
|
||||
break;
|
||||
case 'AVERAGE': {
|
||||
// mathMean([null,null,1,3]) === 2.0.
|
||||
const functionName = JavaScript.provideFunction_(
|
||||
'mathMean',
|
||||
['function ' + JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(myList) {',
|
||||
' return myList.reduce(function(x, y) {return x + y;}) / ' +
|
||||
'myList.length;',
|
||||
'}']);
|
||||
const functionName = JavaScript.provideFunction_('mathMean', `
|
||||
function ${JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) {
|
||||
return myList.reduce(function(x, y) {return x + y;}) / myList.length;
|
||||
}
|
||||
`);
|
||||
list = JavaScript.valueToCode(block, 'LIST',
|
||||
JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
@@ -256,21 +248,18 @@ JavaScript['math_on_list'] = function(block) {
|
||||
}
|
||||
case 'MEDIAN': {
|
||||
// mathMedian([null,null,1,3]) === 2.0.
|
||||
const functionName = JavaScript.provideFunction_(
|
||||
'mathMedian',
|
||||
['function ' + JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(myList) {',
|
||||
' var localList = myList.filter(function (x) ' +
|
||||
'{return typeof x === \'number\';});',
|
||||
' if (!localList.length) return null;',
|
||||
' localList.sort(function(a, b) {return b - a;});',
|
||||
' if (localList.length % 2 === 0) {',
|
||||
' return (localList[localList.length / 2 - 1] + ' +
|
||||
'localList[localList.length / 2]) / 2;',
|
||||
' } else {',
|
||||
' return localList[(localList.length - 1) / 2];',
|
||||
' }',
|
||||
'}']);
|
||||
const functionName = JavaScript.provideFunction_('mathMedian', `
|
||||
function ${JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) {
|
||||
var localList = myList.filter(function (x) {return typeof x === \'number\';});
|
||||
if (!localList.length) return null;
|
||||
localList.sort(function(a, b) {return b - a;});
|
||||
if (localList.length % 2 === 0) {
|
||||
return (localList[localList.length / 2 - 1] + localList[localList.length / 2]) / 2;
|
||||
} else {
|
||||
return localList[(localList.length - 1) / 2];
|
||||
}
|
||||
}
|
||||
`);
|
||||
list = JavaScript.valueToCode(block, 'LIST',
|
||||
JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
@@ -280,70 +269,67 @@ JavaScript['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 = JavaScript.provideFunction_(
|
||||
'mathModes',
|
||||
['function ' + JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(values) {',
|
||||
' var modes = [];',
|
||||
' var counts = [];',
|
||||
' var maxCount = 0;',
|
||||
' for (var i = 0; i < values.length; i++) {',
|
||||
' var value = values[i];',
|
||||
' var found = false;',
|
||||
' var thisCount;',
|
||||
' for (var j = 0; j < counts.length; j++) {',
|
||||
' if (counts[j][0] === value) {',
|
||||
' thisCount = ++counts[j][1];',
|
||||
' found = true;',
|
||||
' break;',
|
||||
' }',
|
||||
' }',
|
||||
' if (!found) {',
|
||||
' counts.push([value, 1]);',
|
||||
' thisCount = 1;',
|
||||
' }',
|
||||
' maxCount = Math.max(thisCount, maxCount);',
|
||||
' }',
|
||||
' for (var j = 0; j < counts.length; j++) {',
|
||||
' if (counts[j][1] === maxCount) {',
|
||||
' modes.push(counts[j][0]);',
|
||||
' }',
|
||||
' }',
|
||||
' return modes;',
|
||||
'}']);
|
||||
const functionName = JavaScript.provideFunction_('mathModes', `
|
||||
function ${JavaScript.FUNCTION_NAME_PLACEHOLDER_}(values) {
|
||||
var modes = [];
|
||||
var counts = [];
|
||||
var maxCount = 0;
|
||||
for (var i = 0; i < values.length; i++) {
|
||||
var value = values[i];
|
||||
var found = false;
|
||||
var thisCount;
|
||||
for (var j = 0; j < counts.length; j++) {
|
||||
if (counts[j][0] === value) {
|
||||
thisCount = ++counts[j][1];
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
counts.push([value, 1]);
|
||||
thisCount = 1;
|
||||
}
|
||||
maxCount = Math.max(thisCount, maxCount);
|
||||
}
|
||||
for (var j = 0; j < counts.length; j++) {
|
||||
if (counts[j][1] === maxCount) {
|
||||
modes.push(counts[j][0]);
|
||||
}
|
||||
}
|
||||
return modes;
|
||||
}
|
||||
`);
|
||||
list = JavaScript.valueToCode(block, 'LIST',
|
||||
JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'STD_DEV': {
|
||||
const functionName = JavaScript.provideFunction_(
|
||||
'mathStandardDeviation',
|
||||
['function ' + JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(numbers) {',
|
||||
' var n = numbers.length;',
|
||||
' if (!n) return null;',
|
||||
' var mean = numbers.reduce(function(x, y) {return x + y;}) / n;',
|
||||
' var variance = 0;',
|
||||
' for (var j = 0; j < n; j++) {',
|
||||
' variance += Math.pow(numbers[j] - mean, 2);',
|
||||
' }',
|
||||
' variance = variance / n;',
|
||||
' return Math.sqrt(variance);',
|
||||
'}']);
|
||||
const functionName = JavaScript.provideFunction_('mathStandardDeviation', `
|
||||
function ${JavaScript.FUNCTION_NAME_PLACEHOLDER_}(numbers) {
|
||||
var n = numbers.length;
|
||||
if (!n) return null;
|
||||
var mean = numbers.reduce(function(x, y) {return x + y;}) / n;
|
||||
var variance = 0;
|
||||
for (var j = 0; j < n; j++) {
|
||||
variance += Math.pow(numbers[j] - mean, 2);
|
||||
}
|
||||
variance = variance / n;
|
||||
return Math.sqrt(variance);
|
||||
}
|
||||
`);
|
||||
list = JavaScript.valueToCode(block, 'LIST',
|
||||
JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
}
|
||||
case 'RANDOM': {
|
||||
const functionName = JavaScript.provideFunction_(
|
||||
'mathRandomList',
|
||||
['function ' + JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(list) {',
|
||||
' var x = Math.floor(Math.random() * list.length);',
|
||||
' return list[x];',
|
||||
'}']);
|
||||
const functionName = JavaScript.provideFunction_('mathRandomList', `
|
||||
function ${JavaScript.FUNCTION_NAME_PLACEHOLDER_}(list) {
|
||||
var x = Math.floor(Math.random() * list.length);
|
||||
return list[x];
|
||||
}
|
||||
`);
|
||||
list = JavaScript.valueToCode(block, 'LIST',
|
||||
JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
@@ -384,18 +370,17 @@ JavaScript['math_random_int'] = function(block) {
|
||||
JavaScript.ORDER_NONE) || '0';
|
||||
const argument1 = JavaScript.valueToCode(block, 'TO',
|
||||
JavaScript.ORDER_NONE) || '0';
|
||||
const functionName = JavaScript.provideFunction_(
|
||||
'mathRandomInt',
|
||||
['function ' + JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(a, b) {',
|
||||
' if (a > b) {',
|
||||
' // Swap a and b to ensure a is smaller.',
|
||||
' var c = a;',
|
||||
' a = b;',
|
||||
' b = c;',
|
||||
' }',
|
||||
' return Math.floor(Math.random() * (b - a + 1) + a);',
|
||||
'}']);
|
||||
const functionName = JavaScript.provideFunction_('mathRandomInt', `
|
||||
function ${JavaScript.FUNCTION_NAME_PLACEHOLDER_}(a, b) {
|
||||
if (a > b) {
|
||||
// Swap a and b to ensure a is smaller.
|
||||
var c = a;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return Math.floor(Math.random() * (b - a + 1) + a);
|
||||
}
|
||||
`);
|
||||
const code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user