1.2.2 • Published 6 months ago

@lml_taf/taf-utils v1.2.2

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
6 months ago

taf-utils

taf相关的工具模块

Installation

$ npm install taf-utils

01. 配置文件解析器

var Config = require('taf-utils').Config;

API

parseFile(sFilePath, encoding, callback)

解析指定文件

  • sFilePath: 文件名
  • encoding: 文件编码类型。 (默认值: utf8)
  • callback: 回调函数,回调函数的格式 function callback(ret, config){}, 其中ret为对象{code: 返回码,成功为0, 失败为-1, message: 描述, exception:如果成功为undefined,如果失败为事件对象}, config为解析器本身

parseText(sText)

解析字符串,并将解析的结果存于内部的_data属性中,可以通过get方法获取相应的值

  • sText: 字符串
  • return: true:解析成功, false: 解析失败

get(key, defaultValue)

文件被解析之后,会将结果存储到一个对象中,通过get方法可以获取制定的值。注:如果配置文件/字符串中有相同的key,则get获取key对应的值时,不会获取所有的值,而是获取该key最后对应的那个值,也可以理解为对应相同的key后面的值覆盖前面的值。

  • key: 需要取值的key值,格式为x1.x2.x3,其中x1,x2,x3依次为深层次的key,注:如果key值本身为x1.x2格式,取该key对应的值时需要写成< x1.x2>,具体使用参见例子。
  • defaultValue: 取不到结果的默认值

getDomain(key, defaultValue)

获取key对应的值中类型为Object的属性数组

  • key: key值。
  • defaultValue: 取不到结果的默认值

getDomainValue(key, defaultValue)

获取key对应的值中类型为Object的属性值数组

  • key: key值。
  • defaultValue: 取不到结果的默认值

getDomainLine(key, defaultValue)

获取key对应路径下的所有非空行

  • key: key值。
  • defaultValue: 取不到结果的默认值
  • return: 数组

data

通过该属性,可以获取文件解析的结果

example

var Config = require('taf-utils').Config;

var config = new Config();
config.parseFile('./config.conf', 'utf8');

var data = config.data;
console.log('data: ', data);
console.log('get: taf.application.server.<TRom.ThemeStoreServer.ThemeStore1ObjAdapter>: ', config.get("taf.application.server.<TRom.ThemeStoreServer.ThemeStore1ObjAdapter>"));
console.log('get: taf.application.server.local: ', config.get('taf.application.server.local'));
console.log('getDomain: taf.application.server: ', config.getDomain('taf.application.server'));
console.log('getDomainValue: taf.application.server: ', config.getDomainValue('taf.application.server'));

具体例子参见examples目录下的test-config.js文件

02. Endpoint工具

var Endpoint = require('taf-utils').Endpoint;

API

Class方法:parse(desc)

从字符串中解析出Endpoint信息

  • desc: 字符串,例如:'tcp -h 10.136.167.31 -p 19386 -t 60000'
  • return: 返回Endpoint实例。

toString()

Endpoint信息转化成字符串

copy()

拷贝Endpoint实例

example

var Endpoint = require('taf-utils').Endpoint;

var endpoint = Endpoint.parse('tcp -h 10.136.167.31 -p 19386 -t 60000');
console.log('endpoint: ' + endpoint.toString());
console.log('endpoint.copy: ' + endpoint.copy().toString());

具体例子参见examples目录下的test-endpoint.js文件

03. timeProvider工具

var timeProvider = require('taf-utils').timeProvider;

API

nowTimestamp()

采用Date.now()的方式获取时间,此种方式效率最高,Date.now()的方式的效率大概是new Date().getTime()的2倍,是process.hrtime() 方式的4倍。

  • return: 返回对象
{
    hrtime: // 数组类型,[秒, 纳秒],
    timestamp: // 单位ms
}

diff(oTime)

当前时间相对于oTime的时间间隔

  • oTime: 相对时间,nowTimestamp函数返回的对象类型
  • return: 浮点类型,时间间隔,单位毫秒
  • 注:nowTimestamp和diff配对使用

dateTimestamp()

获取当前的时间戳, 即机器从启动到当前的时间(process.hrtime)

  • return: 返回对象
{
    hrtime: // 数组类型,[秒, 纳秒],
    timestamp: // 单位ms
}

dateTimestampDiff(oTime)

当前时间相对于oTime的时间间隔

  • oTime: 相对时间,dateTimestamp函数返回的对象类型
  • return: 浮点类型,时间间隔,单位毫秒
  • 注:dateTimestamp和dateTimestampDiff配对使用

example

var timeProvider = require('../index').timeProvider;

var i = 0, count = 10000000;
var tt1, tt2, interval = 0;
var t1 = new Date().getTime();
var t2 = t1;

tt1 = timeProvider.nowTimestamp();
for(i = 0; i < count; i++)
{
    tt2 = timeProvider.diff(tt1);
}
t2 = new Date().getTime();
console.log('【hrTime】interval: ' + (t2 - t1));

t1 = new Date().getTime();
tt1 = timeProvider.dateTimestamp();
for(i = 0; i < count; i++)
{
    tt2 = timeProvider.dateTimestampDiff(tt1);
}
t2 = new Date().getTime();
console.log('【hrTime】interval: ' + (t2 - t1));

具体例子参见examples目录下的test-timer.js文件

1.2.2

6 months ago

1.1.1

6 months ago