Skip to content

Commit 3eed88e

Browse files
author
linzhijun
committed
增加 DAYS、UNICHAR、UNICODE、COTH、CSCH、SECH、ACOT、ACOTH方法
1 parent ae12155 commit 3eed88e

12 files changed

Lines changed: 382 additions & 0 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Function_2 } from '../Function_2.js';
2+
import { Operand } from '../../../Operand.js';
3+
4+
class Function_DAYS extends Function_2 {
5+
get Name() {
6+
return "Days";
7+
}
8+
9+
constructor(a, b) {
10+
super(a, b);
11+
}
12+
13+
evaluate(engine, tempParameter) {
14+
let args1 = this.getDate_1(engine, tempParameter);
15+
if (args1.IsError) { return args1; }
16+
let args2 = this.getDate_2(engine, tempParameter);
17+
if (args2.IsError) { return args2; }
18+
19+
let endDate = args1.DateValue.ToDateTime();
20+
let startDate = args2.DateValue.ToDateTime();
21+
22+
let diffTime = endDate.getTime() - startDate.getTime();
23+
let diffDays = Math.round(diffTime / (1000 * 60 * 60 * 24));
24+
25+
return Operand.Create(diffDays);
26+
}
27+
}
28+
29+
export { Function_DAYS };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Function_1 } from '../Function_1.js';
2+
import { Operand } from '../../../Operand.js';
3+
4+
class Function_ACOT extends Function_1 {
5+
get Name() {
6+
return "Acot";
7+
}
8+
9+
constructor(a) {
10+
super(a);
11+
}
12+
13+
evaluate(work, tempParameter) {
14+
let args1 = this.getNumber_1(work, tempParameter);
15+
if (args1.IsError) { return args1; }
16+
return Operand.Create(Math.PI / 2 - Math.atan(args1.DoubleValue));
17+
}
18+
}
19+
20+
export { Function_ACOT };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Function_1 } from '../Function_1.js';
2+
import { Operand } from '../../../Operand.js';
3+
4+
class Function_ACOTH extends Function_1 {
5+
get Name() {
6+
return "Acoth";
7+
}
8+
9+
constructor(a) {
10+
super(a);
11+
}
12+
13+
evaluate(work, tempParameter) {
14+
let args1 = this.getNumber_1(work, tempParameter);
15+
if (args1.IsError) { return args1; }
16+
let d = args1.DoubleValue;
17+
if (Math.abs(d) <= 1) {
18+
return Operand.Error(this.functionError, 'ACOTH');
19+
}
20+
return Operand.Create(0.5 * Math.log((d + 1) / (d - 1)));
21+
}
22+
}
23+
24+
export { Function_ACOTH };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Function_1 } from '../Function_1.js';
2+
import { Operand } from '../../../Operand.js';
3+
4+
class Function_COTH extends Function_1 {
5+
get Name() {
6+
return "Coth";
7+
}
8+
9+
constructor(a) {
10+
super(a);
11+
}
12+
13+
evaluate(work, tempParameter) {
14+
let args1 = this.getNumber_1(work, tempParameter);
15+
if (args1.IsError) { return args1; }
16+
let d = Math.tanh(args1.DoubleValue);
17+
if (d === 0) {
18+
return this.div0Error();
19+
}
20+
return Operand.Create(1.0 / d);
21+
}
22+
}
23+
24+
export { Function_COTH };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Function_1 } from '../Function_1.js';
2+
import { Operand } from '../../../Operand.js';
3+
4+
class Function_CSCH extends Function_1 {
5+
get Name() {
6+
return "Csch";
7+
}
8+
9+
constructor(a) {
10+
super(a);
11+
}
12+
13+
evaluate(work, tempParameter) {
14+
let args1 = this.getNumber_1(work, tempParameter);
15+
if (args1.IsError) { return args1; }
16+
let d = Math.sinh(args1.DoubleValue);
17+
if (d === 0) {
18+
return this.div0Error();
19+
}
20+
return Operand.Create(1.0 / d);
21+
}
22+
}
23+
24+
export { Function_CSCH };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Function_1 } from '../Function_1.js';
2+
import { Operand } from '../../../Operand.js';
3+
4+
class Function_SECH extends Function_1 {
5+
get Name() {
6+
return "Sech";
7+
}
8+
9+
constructor(a) {
10+
super(a);
11+
}
12+
13+
evaluate(work, tempParameter) {
14+
let args1 = this.getNumber_1(work, tempParameter);
15+
if (args1.IsError) { return args1; }
16+
let d = Math.cosh(args1.DoubleValue);
17+
if (d === 0) {
18+
return this.div0Error();
19+
}
20+
return Operand.Create(1.0 / d);
21+
}
22+
}
23+
24+
export { Function_SECH };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Function_1 } from '../Function_1.js';
2+
import { Operand } from '../../../Operand.js';
3+
4+
class Function_UNICHAR extends Function_1 {
5+
get Name() {
6+
return "UniChar";
7+
}
8+
9+
constructor(a) {
10+
super(a);
11+
}
12+
13+
evaluate(work, tempParameter) {
14+
let args1 = this.getNumber_1(work, tempParameter);
15+
if (args1.IsError) { return args1; }
16+
try {
17+
return Operand.Create(String.fromCodePoint(args1.IntValue));
18+
} catch (e) {
19+
return Operand.Error(this.functionError, 'UNICHAR');
20+
}
21+
}
22+
}
23+
24+
export { Function_UNICHAR };
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Function_1 } from '../Function_1.js';
2+
import { Operand } from '../../../Operand.js';
3+
4+
class Function_UNICODE extends Function_1 {
5+
get Name() {
6+
return "Unicode";
7+
}
8+
9+
constructor(a) {
10+
super(a);
11+
}
12+
13+
evaluate(work, tempParameter) {
14+
let args1 = this.getText_1(work, tempParameter);
15+
if (args1.IsError) { return args1; }
16+
if (!args1.TextValue || args1.TextValue.length === 0) {
17+
return Operand.Error(this.functionError, 'UNICODE');
18+
}
19+
return Operand.Create(args1.TextValue.codePointAt(0));
20+
}
21+
}
22+
23+
export { Function_UNICODE };

‎JavaScript/src/Internals/Visitors/MathFunctionVisitor.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ import { Function_ASINH } from '../Functions/MathTrigonometric/Function_ASINH.js
4444
import { Function_ATAN } from '../Functions/MathTrigonometric/Function_ATAN.js';
4545
import { Function_ATANH } from '../Functions/MathTrigonometric/Function_ATANH.js';
4646
import { Function_ATAN2 } from '../Functions/MathTrigonometric/Function_ATAN2.js';
47+
import { Function_COT } from '../Functions/MathTrigonometric/Function_COT.js';
48+
import { Function_COTH } from '../Functions/MathTrigonometric/Function_COTH.js';
49+
import { Function_CSC } from '../Functions/MathTrigonometric/Function_CSC.js';
50+
import { Function_CSCH } from '../Functions/MathTrigonometric/Function_CSCH.js';
51+
import { Function_SEC } from '../Functions/MathTrigonometric/Function_SEC.js';
52+
import { Function_SECH } from '../Functions/MathTrigonometric/Function_SECH.js';
53+
import { Function_ACOT } from '../Functions/MathTrigonometric/Function_ACOT.js';
54+
import { Function_ACOTH } from '../Functions/MathTrigonometric/Function_ACOTH.js';
4755
import { Function_FIXED } from '../Functions/MathBase/Function_FIXED.js';
4856
import { Function_BIN2OCT } from '../Functions/MathTransformation/Function_BIN2OCT.js';
4957
import { Function_BIN2DEC } from '../Functions/MathTransformation/Function_BIN2DEC.js';
@@ -103,6 +111,8 @@ import { Function_T } from '../Functions/String/Function_T.js';
103111
import { Function_TEXT } from '../Functions/String/Function_TEXT.js';
104112
import { Function_TRIM } from '../Functions/String/Function_TRIM.js';
105113
import { Function_UPPER } from '../Functions/String/Function_UPPER.js';
114+
import { Function_UNICHAR } from '../Functions/String/Function_UNICHAR.js';
115+
import { Function_UNICODE } from '../Functions/String/Function_UNICODE.js';
106116
import { Function_DATEVALUE } from '../Functions/DateTimes/Function_DATEVALUE.js';
107117
import { Function_TIMESTAMP } from '../Functions/DateTimes/Function_TIMESTAMP.js';
108118
import { Function_TIMEVALUE } from '../Functions/DateTimes/Function_TIMEVALUE.js';
@@ -124,6 +134,7 @@ import { Function_EOMONTH } from '../Functions/DateTimes/Function_EOMONTH.js';
124134
import { Function_NETWORKDAYS } from '../Functions/DateTimes/Function_NETWORKDAYS.js';
125135
import { Function_WORKDAY } from '../Functions/DateTimes/Function_WORKDAY.js';
126136
import { Function_WEEKNUM } from '../Functions/DateTimes/Function_WEEKNUM.js';
137+
import { Function_DAYS } from '../Functions/DateTimes/Function_DAYS.js';
127138
import { Function_ADDMONTHS } from '../Functions/DateTimes/Function_ADDMONTHS.js';
128139
import { Function_ADDYEARS } from '../Functions/DateTimes/Function_ADDYEARS.js';
129140
import { Function_ADDSECONDS } from '../Functions/DateTimes/Function_ADDSECONDS.js';
@@ -290,6 +301,14 @@ const funcDict = {
290301
"ATAN": (args) => new Function_ATAN(args),
291302
"ATANH": (args) => new Function_ATANH(args),
292303
"ATAN2": (args) => new Function_ATAN2(args),
304+
"COT": (args) => new Function_COT(args),
305+
"COTH": (args) => new Function_COTH(args),
306+
"CSC": (args) => new Function_CSC(args),
307+
"CSCH": (args) => new Function_CSCH(args),
308+
"SEC": (args) => new Function_SEC(args),
309+
"SECH": (args) => new Function_SECH(args),
310+
"ACOT": (args) => new Function_ACOT(args),
311+
"ACOTH": (args) => new Function_ACOTH(args),
293312
"ROUND": (args) => new Function_ROUND(args),
294313
"ROUNDDOWN": (args) => new Function_ROUNDDOWN(args),
295314
"ROUNDUP": (args) => new Function_ROUNDUP(args),
@@ -338,6 +357,8 @@ const funcDict = {
338357
"UPPER": (args) => new Function_UPPER(args),
339358
"TOUPPER": (args) => new Function_UPPER(args),
340359
"VALUE": (args) => new Function_VALUE(args),
360+
"UNICHAR": (args) => new Function_UNICHAR(args),
361+
"UNICODE": (args) => new Function_UNICODE(args),
341362
"DATEVALUE": (args) => new Function_DATEVALUE(args),
342363
"TIMEVALUE": (args) => new Function_TIMEVALUE(args),
343364
"DATE": (args) => new Function_DATE(args),
@@ -356,6 +377,7 @@ const funcDict = {
356377
"NETWORKDAYS": (args) => new Function_NETWORKDAYS(args),
357378
"WORKDAY": (args) => new Function_WORKDAY(args),
358379
"WEEKNUM": (args) => new Function_WEEKNUM(args),
380+
"DAYS": (args) => new Function_DAYS(args),
359381
"MAX": (args) => new Function_MAX(args),
360382
"MEDIAN": (args) => new Function_MEDIAN(args),
361383
"MIN": (args) => new Function_MIN(args),

‎JavaScript/tests/AlgorithmEngineTest_dateTime.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,20 @@ function testWEEKNUM() {
314314
console.log('WEEKNUM 测试通过!');
315315
}
316316

317+
function testDAYS() {
318+
console.log('开始测试 DAYS...');
319+
320+
const engine = new AlgorithmEngineWithTryEvaluate();
321+
322+
let days = engine.TryEvaluate("DAYS('2017-1-7', '2017-1-1')", 0);
323+
assert.strictEqual(days, 6, "DAYS('2017-1-7', '2017-1-1') 应该是 6");
324+
325+
days = engine.TryEvaluate("DAYS('2017-1-1', '2017-1-7')", 0);
326+
assert.strictEqual(days, -6, "DAYS('2017-1-1', '2017-1-7') 应该是 -6");
327+
328+
console.log('DAYS 测试通过!');
329+
}
330+
317331
function testAdd() {
318332
console.log('开始测试 Add...');
319333

@@ -358,6 +372,7 @@ function runAllTests() {
358372
testNETWORKDAYS();
359373
testWORKDAY();
360374
testWEEKNUM();
375+
testDAYS();
361376
testAdd();
362377
console.log('所有测试通过!');
363378
} catch (error) {
@@ -394,6 +409,7 @@ export {
394409
testNETWORKDAYS,
395410
testWORKDAY,
396411
testWEEKNUM,
412+
testDAYS,
397413
testAdd,
398414
runAllTests
399415
};

0 commit comments

Comments
 (0)