[Pas2js] Pas2js 1.4.26
warleyalex
warleyalex at yahoo.com.br
Thu Jun 18 03:32:17 CEST 2020
a) Can you confirm when we use the Web.pas unit, the code completion stop
worlking :(
I suspect is new keyword "async"
b) Look at the following function:
=========================================
function hexColour(c: Integer): String;
begin
if (c < 256) then
Result := abs(c).toString(16)
else
Result := '0';
end;
console.log(hexColour(233)); // outputs "16" but expected output: "e9"
console.log(hexColour(11)); // output "16" but expected output: "b"
=========================================
generates this JS:
this.IntToStr = function (Value) {
var Result = "";
Result = "" + Value;
return Result;
};
rtl.createHelper($mod,"TIntegerHelper",null,function () {
this.ToString = function (AValue) {
var Result = "";
Result = $mod.IntToStr(AValue);
return Result;
};
});
this.hexColour = function (c) {
var Result = "";
if (c < 256) {
Result = pas.SysUtils.TIntegerHelper.ToString(16)}
else Result = "0";
return Result;
};
...the correct is:
function hexColour(c: Integer): String;
function abs(const A: integer): TNumber; external name 'Math.abs';
begin
if (c < 256) then
Result := abs(c).toString(16)
else
Result := '0';
end;
generates
this.hexColour = function (c) {
var Result = "";
if (c < 256) {
Result = Math.abs(c).toString(16)}
else Result = "0";
return Result;
};
c) It would be nice if we have straigthforward standard built-in functions
for the type double, for instance:
var
x: Double;
begin
x:= 0.25564545;
console.log( x.toFixed(2) );
d) These classes TNumber and TJSMath are useful for me.
---------------------------------------------------------
type
TNumber = class external name 'Number'
public
class var MAX_VALUE: Double; external name 'MAX_VALUE';
class var MIN_VALUE: Double; external name 'MIN_VALUE';
class var NEGATIVE_INFINITY: Double; external name 'NEGATIVE_INFINITY';
class var NaN: String; external name 'NaN';
class var POSITIVE_INFINITY: Double; external name 'POSITIVE_INFINITY';
class var MAX_SAFE_INTEGER: Integer; external name 'MAX_SAFE_INTEGER';
class var MIN_SAFE_INTEGER: Integer; external name 'MIN_SAFE_INTEGER';
function toString(radix: Integer): String; overload; external name
'toString';
function toFixed(DecimalPlaces: Integer): String {string | Double};
external name 'toFixed'; overload;
function toFixed(): Double; external name 'toFixed'; overload;
function toExponential(ExponencialPlaces: Integer): String {string |
Double}; external name 'toExponential'; overload;
function toExponential(): Double; external name 'toExponential';
overload;
function toPrecision(Precision: Integer): String {string | Double};
external name 'toPrecision'; overload;
function toPrecision(): Double; external name 'toPrecision'; overload;
class function parseFloat(n: JSValue): TNumber; external name
'parseFloat'; overload;
class function isNaN(n: JSValue): boolean; external name 'isNaN';
overload;
class function isFinite(n: double): boolean; external name 'isFinite';
overload;
class function isInteger(num: double): boolean; external name
'isInteger'; overload;
class function isSafeInteger(num: double): boolean; external name
'isSafeInteger'; overload;
class function parseInt(str: string; radix: Integer): Integer; overload;
external name 'parseInt';
end;
type
TJSMath = class external name 'Math'
public
class var E: Double; external name 'E'; // 2.718281828459045;
class var PI: Double; external name 'PI'; // 3.141592653589793;
class var LN10: Double; external name 'LN10'; // 2.302585092994046;
class var LN2: Double; external name 'LN2'; // 0.6931471805599453;
class var LOG10E: Double; external name 'LOG10E'; // 0.4342944819032518;
class var LOG2E: Double; external name 'LOG2E'; // 1.4426950408889634;
class var SQRT1_2: Double; external name 'SQRT1_2'; //
0.7071067811865476;
class var SQRT2: Double; external name 'SQRT2'; // 1.4142135623730951;
class function abs(const A: integer): integer; overload; external name
'abs';
class function abs(const A: NativeInt): integer; overload; external name
'abs';
class function abs(const A: Double): Double; overload; external name
'abs';
class function acos(x: Double): Double; external name 'acos';
class function acosh(x: Double): Double; external name 'acosh';
class function asin(x: Double): Double; external name 'asin';
class function asinh(x: Double): Double; external name 'asinh';
class function atan(const A: Double): Double; external name 'atan';
class function atanh(const A: Double): Double; external name 'atanh';
class function atan2(const A,B: Double): Double; external name 'atan2';
class function cbrt(const A,B: Double): Double; external name 'cbrt';
class function ceil(x: Double): Integer; external name 'ceil';
class function clz32(x: Double): Integer; external name 'clz32';
class function cos(const A: Double): Double; external name 'cos';
class function cosh(const A: Double): Double; external name 'cosh';
class function exp(const A: Double): Double; external name 'exp';
class function expm1(const A: Double): Double; external name 'expm1';
class function floor(x: Double): Integer; external name 'floor';
class function fround(x: Double): Integer; external name 'fround';
class function hypot(x: Double): Integer; external name 'hypot';
class function imul(x, y: Double): Integer; external name 'imul';
class function log(x: Double): Double; external name 'log';
class function log1p(x: Double): Double; external name 'log1p';
class function log10(x: Double): Double; external name 'log10';
class function log2(x: Double): Double; external name 'log2';
class function max(x, max: Double): Double; overload; external name
'max';
class function max_int(x, max: Integer): Integer; overload; external
name 'max';
class function min(x, max: Double): Double; overload; external name
'min';
class function pow(x: Double): Double; external name 'pow'; overload;
class function pow(base: integer; exponent: double): Double; external
name 'pow'; overload;
class function random: Double; overload; external name 'random';
class function round(const A: Double): NativeInt; external name 'round';
class function sign(const A: Double): NativeInt; external name 'sign';
class function sin(const A: Double): Double; external name 'sin';
class function sinh(const A: Double): Double; external name 'sinh';
class function sqrt(const A: Double): Double; external name 'sqrt';
class function tan(const A: Double): Double; external name 'tan';
class function tanh(const A: Double): Double; external name 'tanh';
class function trunc(const A: Double): Double; external name 'trunc';
end;
function circumference(r: String): Double;
begin
if (TNumber.isNaN( TNumber.parseFloat(r) )) then
Result := 0
else
Result := (TNumber.parseFloat(r).toExponential()) * 2.0 * PI ;
end;
function typeOfNaN(x :String): String;
begin
if (TNumber.isNaN(x)) then
result := 'Number NaN'
end;
function hexColour(c: Integer): String;
function abs(const A: integer): TNumber; external name 'Math.abs';
begin
if (c < 256) then
Result := abs(c).toString(16)
else
Result := '0';
end;
function fits(x, y: Double): String;
begin
if (TNumber.isInteger(y / x)) then
result := 'Fits!'
else
result := 'Does NOT fit!';
end;
function warn(x: Double): String;
begin
if (TNumber.isSafeInteger(x)) then
result := 'Precision safe.'
else
result :='Precision may be lost!';
end;
function roughScale(x: string; base: integer): Integer;
var
parsed: Integer;
begin
parsed := TNumber.parseInt(x, base);
if (TNumber.isNaN(parsed)) then
result := 0 else
result := parsed * 100;
end;
function multiply(x, y: double): JSValue;
begin
if (x * y > TNumber.MAX_VALUE) then
result := 'Process as Infinity'
else
result := (x * y);
end;
function multiply2(x, y: double): JSValue;
begin
if (x * y < TNumber.MIN_VALUE) then
result := 'Process as -Infinity'
else
result := (x * y);
end;
var
x: string;
numObj: double;
begin
// Your code here
console.log( TNumber.parseFloat(123.456).toFixed(2) ); // 123.46
console.log( TNumber.parseFloat(0.004).toFixed(2) ); // 0.00
console.log( TNumber.parseFloat('1.23e+5').toFixed(2) ); // "123000.00"
// toExponential
console.log( TNumber.parseFloat(123456).toExponential(2) ); // expected
output: "1.23e+5"
console.log( TNumber.parseFloat('123456').toExponential()); // expected
output: "1.23456e+5" "
console.log( TNumber.parseFloat('oink').toExponential()); // expected
output: "NaN"
numObj := 77.1234;
console.log( TNumber.parseFloat(numObj).toExponential() ); // logs
7.71234e+1
console.log( TNumber.parseFloat(numObj).toExponential(4) ); // logs
7.7123e+1
console.log( TNumber.parseFloat(numObj).toExponential(2) ); // logs 7.71e+1
// Number.parseFloat()
console.log(circumference('4.567abcdefgh')); // expected output:
28.695307297889173
console.log(circumference('abcdefgh')); // expected output: 0
// Number.isNaN()
console.log(typeOfNaN('100F')); // expected output: "NaN"
console.log(typeOfNaN(TNumber.NaN)); // expected output: "Number NaN"
console.log('==========');
console.log( TNumber.parseFloat(123.456).toPrecision(4)); // "123.5"
console.log( TNumber.parseFloat(0.004).toPrecision(4)); // "0.004000"
console.log( TNumber.parseFloat('1.23e+5').toPrecision(4)); // "1.230e+5"
// Number.toPrecision()
numObj := 5.123456;
console.log(TNumber.parseFloat(numObj).toPrecision()); // logs '5.123456'
console.log(TNumber.parseFloat(numObj).toPrecision(5)); // logs '5.1235'
console.log(TNumber.parseFloat(numObj).toPrecision(2)); // logs '5.1'
console.log(TNumber.parseFloat(numObj).toPrecision(1)); // logs '5'
//Number.toString()
console.log(hexColour(233)); // expected output: "e9"
console.log(hexColour(11)); // expected output: "b"
//Number.isFinite()
console.log(TNumber.isFinite(1 / 0)); // expected output: false
console.log(TNumber.isFinite(10 / 5)); // expected output: true
console.log(TNumber.isFinite(0 / 0)); // expected output: false
//Number.isInteger()
console.log(fits(5, 10)); // expected output: "Fits!"
console.log(fits(5, 11)); // expected output: "Does NOT fit!"
//Number.isSafeInteger()
console.log(warn(TJSMath.pow(2, 53))); // expected output: "Precision may be
lost!"
console.log(warn(TJSMath.pow(2, 53) - 1)); // expected output: "Precision
safe."
//Number.parseInt()
console.log(roughScale(' 0xF', 16)); // expected output: 1500
console.log(roughScale('321', 2)); // expected output: 0
// Number.MAX_VALUE
console.log(multiply(1.7976931348623157e+308, 1)); // expected output:
1.7976931348623157e+308
console.log(multiply(1.7976931348623157e+308, 2)); // expected output:
"Process as Infinity"
// Number.MIN_VALUE
console.log(multiply2(5e-324, 1)); // expected output: 5e-324
console.log(multiply2(-1.7976931348623157e+308, 2)); // expected output:
Process as -Infinity
--
Sent from: http://pas2js.38893.n8.nabble.com/
More information about the Pas2js
mailing list