mm_maths 1.1.7 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -26,7 +26,21 @@ class Maths {
26
26
  * 常量(集合)
27
27
  */
28
28
  this.const = {};
29
-
29
+
30
+ /**
31
+ * 变量(集合)用于替换代码中的文字,赋予值。等同于读取缓存值
32
+ */
33
+ this.vars = {
34
+ // "ma5": "MA(CLOSE, 5)"
35
+ };
36
+
37
+ /**
38
+ * 变量值(集合)用于替换代码中的文字,赋予值。等同于读取缓存值
39
+ */
40
+ this.vals = {
41
+ // "ma5": []
42
+ };
43
+
30
44
  /**
31
45
  * 声明(集合)
32
46
  */
@@ -228,6 +242,25 @@ Maths.prototype.set_const = function(constant) {
228
242
  $.push(this.const, constant, true);
229
243
  };
230
244
 
245
+
246
+ /**
247
+ * 重置变量
248
+ */
249
+ Maths.prototype.reset_val = function(){
250
+ for(var k in this.vars){
251
+ this.vals[k] = this.first_run_code("return " + this.vars[k]);
252
+ }
253
+ }
254
+
255
+ /**
256
+ * 设置变量
257
+ * @param {Object} vars 变量集合,采用键值对的方式 例如:{ a: null, b: 15, d: [1,-1] }
258
+ */
259
+ Maths.prototype.set_var = function(vars) {
260
+ $.push(this.vars, vars, true);
261
+ this.reset_val();
262
+ };
263
+
231
264
  /**
232
265
  * 下定义
233
266
  * @param {Object} def 定义集合, 例如:
@@ -248,17 +281,20 @@ Maths.prototype.set_know = function(know) {
248
281
  * 设置函数头
249
282
  */
250
283
  Maths.prototype.head = function() {
251
- var head = "";
252
- for (var k in this.math) {
253
- if (!this.const[k]) {
254
- head += "var " + k + " = math." + k + ";\r\n"
284
+ if(!this.head_value){
285
+ var head = "";
286
+ for (var k in this.math) {
287
+ if (!this.const[k]) {
288
+ head += "var " + k + " = math." + k + ";\r\n"
289
+ }
255
290
  }
291
+ for (var k in this.const) {
292
+ var value = this.const[k];
293
+ head += "var " + k + " = constant." + k + ";\r\n"
294
+ }
295
+ this.head_value = head;
256
296
  }
257
- for (var k in this.const) {
258
- var value = this.const[k];
259
- head += "var " + k + " = constant." + k + ";\r\n"
260
- }
261
- return head;
297
+ return this.head_value;
262
298
  }
263
299
 
264
300
  /**
@@ -268,7 +304,6 @@ Maths.prototype.head = function() {
268
304
  */
269
305
  Maths.prototype.convertTo = function(express){
270
306
  var express = this.convert_equal(express);
271
-
272
307
  // 先进行符号转换
273
308
  var dt = this.symbol;
274
309
  for(var k in dt){
@@ -278,7 +313,6 @@ Maths.prototype.convertTo = function(express){
278
313
  express = func(express);
279
314
  }
280
315
  }
281
-
282
316
  // 再进行式子转换
283
317
  dt = this.convert;
284
318
  for(var k in dt){
@@ -323,12 +357,23 @@ Maths.prototype.derivation = function(express) {
323
357
  // 符号转换
324
358
  };
325
359
 
360
+ /**
361
+ * 更新标签
362
+ * @param {String} express 表达式
363
+ */
364
+ Maths.prototype.update_tag = function(express) {
365
+ for(var k in this.vars){
366
+ express = express.replaceAll(this.vars[k], "vals." + k);
367
+ }
368
+ return express;
369
+ }
370
+
326
371
  /**
327
372
  * 运行代码
328
373
  * @param {String} express 表达式
329
374
  * @return {Object} 执行结果
330
375
  */
331
- Maths.prototype.run_code = function(express) {
376
+ Maths.prototype.first_run_code = function(express) {
332
377
  var head = this.head();
333
378
  var exp = head + express;
334
379
  let func = new Function('constant', 'math', exp);
@@ -336,6 +381,25 @@ Maths.prototype.run_code = function(express) {
336
381
  var ret;
337
382
  try {
338
383
  ret = func(this.const, this);
384
+ } catch (e) {
385
+ console.log(e);
386
+ }
387
+ return ret;
388
+ };
389
+
390
+ /**
391
+ * 运行代码
392
+ * @param {String} express 表达式
393
+ * @return {Object} 执行结果
394
+ */
395
+ Maths.prototype.run_code = function(express) {
396
+ var head = this.head();
397
+ var exp = head + this.update_tag(express);
398
+ let func = new Function('constant', 'math', 'vals', exp);
399
+
400
+ var ret;
401
+ try {
402
+ ret = func(this.const, this, this.vals);
339
403
  } catch (e) {
340
404
  console.log(e);
341
405
  //TODO handle the exception
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
- {
2
- "name": "mm_maths",
3
- "version": "1.1.7",
4
- "description": "这是一个可以将数学公式推导变形,并转成js和计算的模块。",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/qiuwenwu/mm_maths.git"
12
- },
13
- "keywords": [
14
- "math",
15
- "array",
16
- "number",
17
- "identities",
18
- "expression",
19
- "set",
20
- "convert",
21
- "symbol",
22
- "$"
23
- ],
24
- "author": "邱文武",
25
- "license": "ISC",
26
- "bugs": {
27
- "url": "https://github.com/qiuwenwu/mm_maths/issues"
28
- },
29
- "homepage": "https://github.com/qiuwenwu/mm_maths#readme",
30
- "dependencies": {
31
- "mm_expand": "^1.4.5"
32
- }
1
+ {
2
+ "name": "mm_maths",
3
+ "version": "1.1.9",
4
+ "description": "这是一个可以将数学公式推导变形,并转成js和计算的模块。",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/qiuwenwu/mm_maths.git"
12
+ },
13
+ "keywords": [
14
+ "math",
15
+ "array",
16
+ "number",
17
+ "identities",
18
+ "expression",
19
+ "set",
20
+ "convert",
21
+ "symbol",
22
+ "$"
23
+ ],
24
+ "author": "邱文武",
25
+ "license": "ISC",
26
+ "bugs": {
27
+ "url": "https://github.com/qiuwenwu/mm_maths/issues"
28
+ },
29
+ "homepage": "https://github.com/qiuwenwu/mm_maths#readme",
30
+ "dependencies": {
31
+ "mm_expand": "^1.4.7"
32
+ }
33
33
  }
package/test.js CHANGED
@@ -12,7 +12,7 @@ var consts = {
12
12
  };
13
13
  maths.set_const(consts);
14
14
 
15
- // // 第三步,转换公式
15
+ // 第三步,转换公式
16
16
  var expression = "y = abc * ab * 3c * 2";
17
17
  var express = maths.convertTo(expression);
18
18
  console.log('打印公式: ', express);
package/test2.js CHANGED
@@ -1,3 +1,36 @@
1
+ var Maths = require('./index');
2
+
3
+ var maths = new Maths(__dirname);
4
+
5
+ // 第一步,载入公式及相关函数
6
+ maths.update(__dirname, "stock");
7
+
8
+ maths.const = {
9
+ CLOSE: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
10
+ }
11
+
12
+ maths.set_var({
13
+ ma5: "MA(CLOSE, 5)"
14
+ });
15
+
16
+ console.log(new Date());
17
+ var ret = maths.run_code(`
18
+ var a = MA(CLOSE, 5);
19
+ return a;
20
+ `);
21
+
22
+ console.log(new Date());
23
+ var ret = maths.run_code(`
24
+ var b = MA(CLOSE, 5);
25
+ return REF(b, 0);
26
+ `);
27
+
28
+ console.log(new Date());
29
+ console.log(ret);
30
+
31
+
32
+
33
+
1
34
 
2
35
  // console.log(Math.cos(0));
3
36
  // var fd = (2*Math.PI / 360) * 30;