@tmsfe/tms-core 0.0.47 → 0.0.48

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.47",
3
+ "version": "0.0.48",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 经纬度距离计算
3
3
  */
4
-
4
+ import { roundStr } from './stringUtils';
5
5
 
6
6
  // 将角度换算为弧度
7
7
  function convertDegreesToRadians(degrees: number): number {
@@ -32,6 +32,34 @@ function calCoordinateDistance(
32
32
  return distance * 1000;
33
33
  }
34
34
 
35
+ /**
36
+ * 对距离进行格式化
37
+ * @param {Number} distance 距离,单位米
38
+ * @param {Number} keep 保留几位小数
39
+ * @param {Boolean} removeTrailingZero 是否移除字符串末尾的无效数字0
40
+ * @param {String} unit 单位风格类型:en-英文-m/km,EN-英文大写-M/KM,zh-中文-米/千米,ZH-中文-米/公里
41
+ * @returns 格式化后的字符串;distance非法时返回空字符串
42
+ */
43
+ function formatDistance(distance: number|string, keep = 2, removeTrailingZero = false, unit = 'en'): string {
44
+ const distanceNum = parseFloat(String(distance));
45
+ if (isNaN(distanceNum)) return '';
46
+
47
+ // 数字部分格式化
48
+ const num = distanceNum >= 1000 ? distanceNum / 1000 : distanceNum;
49
+ const numStr = roundStr(num, keep, removeTrailingZero);
50
+ // 单位部分格式化
51
+ let units;
52
+ switch (unit) {
53
+ case 'zh': units = ['米', '千米']; break;
54
+ case 'ZH': units = ['米', '公里']; break;
55
+ case 'EN': units = ['M', 'KM']; break;
56
+ default: units = ['m', 'km']; break;
57
+ }
58
+ const unitStr = units[distanceNum >= 1000 ? 1 : 0];
59
+ return `${numStr}${unitStr}`;
60
+ }
61
+
35
62
  export {
36
63
  calCoordinateDistance,
64
+ formatDistance,
37
65
  };
@@ -10,7 +10,7 @@ import AutoReport from './report/proxy/index';
10
10
  import md5 from './md5';
11
11
  import { rpxToPx } from './rpx';
12
12
  import { serialize } from './objUtils';
13
- import { calCoordinateDistance } from './distanceUtils';
13
+ import { calCoordinateDistance, formatDistance } from './distanceUtils';
14
14
  import * as stringUtils from './stringUtils';
15
15
  import * as timeUtils from './timeUtils';
16
16
  import * as ipxHelper from './ipxHelper';
@@ -212,6 +212,7 @@ const api = {
212
212
  rpxToPx,
213
213
  serialize,
214
214
  calCoordinateDistance,
215
+ formatDistance,
215
216
  createRequest,
216
217
  getEnvInfo,
217
218
  isAppPageExist,
package/src/index.js CHANGED
@@ -10,7 +10,7 @@ import md5 from './md5';
10
10
  import { callCloudFunc } from './cloudService';
11
11
  import EventDispatcher from './eventDispatcher';
12
12
  import { serialize } from './objUtils';
13
- import { calCoordinateDistance } from './distanceUtils';
13
+ import { calCoordinateDistance, formatDistance } from './distanceUtils';
14
14
  import { rpxToPx } from './rpx';
15
15
  import {
16
16
  formatPlate,
@@ -170,7 +170,9 @@ const api = {
170
170
  /* 处理对象方法 */
171
171
  serialize,
172
172
 
173
+ /* 距离方法 */
173
174
  calCoordinateDistance,
175
+ formatDistance,
174
176
 
175
177
  /* 获取外部合作商openid */
176
178
  getMpOpenId, // 变更为 getOuterOpenId