npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tars/utils

v2.1.0

Published

TARS 框架辅助工具集合

Downloads

331

Readme

@tars/utils

TARS Framework Aids Collection

Installation

$ npm install @tars/utils

01. Configuration file parser

var Config = require ('@tars/utils'). Config;

API

parseFile(sFilePath, [encoding, callback])

Parse the specified file

  • sFilePath: filename
  • encoding: File encoding type. (Default: utf8)
  • callback: callback function, callback function format function callback (ret, config) {}, where ret is the object {code: return code, success is 0, failure is -1, message: description, exception: if success is undefined, if the failure is an event object}, config is the parser itself

parseText(sText)

Parse the string, and store the result of the parsing in the internal _data property, you can get the corresponding value through the get method

  • sText: string
  • return: true: parsing succeeded, false: parsing failed

get(key, defaultValue)

After the file is parsed, the result is stored in an object, and the specified value can be obtained through the get method. Note: If there is the same key in the configuration file / string, when get the value corresponding to the key, not all the values ​​will be obtained, but the value corresponding to the last of the key can also be understood as corresponding to the same key. The value overwrites the previous value.

  • key: The key value that needs to be valued, the format is x1.x2.x3, where x1, x2, and x3 are deep keys in order. Note: If the key value itself is in the format of x1.x2, take the corresponding key. The value needs to be written as <x1.x2>, see the example for specific usage.
  • defaultValue: Cannot get the default value of the result

getDomain(key, defaultValue)

Get the attribute array of type Object in the value corresponding to key

  • key: The key value.
  • defaultValue: Cannot get the default value of the result

getDomainValue(key, defaultValue)

Gets the array of property values ​​of type Object in the value corresponding to key

  • key: The key value.
  • defaultValue: Cannot get the default value of the result

getDomainLine(key, defaultValue)

Get all non-blank lines in the path corresponding to key

  • key: The key value.
  • defaultValue: Cannot get the default value of the result
  • return: array

data

Through this property, you can get the results of file parsing

example

var Config = require('@tars/utils').Config;

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

var data = config.data;
console.log('data: ', data);
console.log('get: tars.application.server.local: ', config.get('tars.application.server.local'));
console.log('getDomain: tars.application.server: ', config.getDomain('tars.application.server'));
console.log('getDomainValue: tars.application.server: ', config.getDomainValue('tars.application.server'));

For specific examples, see the test-config.js file in the examples directory

02. Endpoint tools

var Endpoint = require ('@tars/utils'). Endpoint;

API

Class method: parse (desc)

Parse Endpoint information from a string

  • desc: string, for example: 'tcp -h 127.0.0.1 -p 10000 -t 60000'
  • return: Returns an Endpoint instance.

toString()

Endpoint information into strings

copy()

Copy the Endpoint instance

example

var Endpoint = require ('@tars/utils').Endpoint;

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

For specific examples, see the test-endpoint.js file in the examples directory

03. timeProvider

var timeProvider = require ('@tars/utils').timeProvider;

API

nowTimestamp()

Use Date.now() to get the time. This method is the most efficient. The Date.now() method is about twice as efficient as new Date(). GetTime() and 4 times as process.hrtime().

  • return: return object
{
    hrtime: // array type, [seconds, nanoseconds],
    timestamp: // unit ms
}

diff(oTime)

Time interval of the current time relative to oTime

  • oTime: relative time, object type returned by nowTimestamp function
  • return: floating point type, time interval, in milliseconds
  • Note: nowTimestamp and diff are used in pairs

dateTimestamp()

Get the current timestamp, that is, the time from machine startup to the current time (process.hrtime)

  • return: return object
{
    hrtime: // array type, [seconds, nanoseconds],
    timestamp: // unit ms
}

dateTimestampDiff(oTime)

Time interval of the current time relative to oTime

  • oTime: relative time, object type returned by dateTimestamp function
  • return: floating point type, time interval, in milliseconds
  • Note: dateTimestamp and dateTimestampDiff are used in pairs

example

var timeProvider = require('@tars/utils').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));

For specific examples, see the test-timer.js file in the examples directory

03. Promise Library

var Promise = require ('@tars/utils').Promise;

Provide a convenient and unified Promise library for TARS applications. When developing TARS applications, we recommend that you use this library instead of choosing the Promise library yourself. When a better promise solution appears, we can directly replace the implementation in this module and take effect directly for all applications.

var Promise = require ("@tars/utils").Promise;
var promise = new Promise (function (resolve, reject) {
     setTimeout (function() {
         resolve (666)
     }, 3000);
});
promise.then (function (data) {
     console.log (data);
});

Promises in TARS are currently implemented based on the bluebird library. Bluebird has the best performance among q, bluebird, and native promises.