@rei-network/utils
Advanced tools
Comparing version 0.0.4 to 0.1.0
@@ -60,3 +60,3 @@ export declare class ChannelAbortError extends Error { | ||
*/ | ||
generator(): AsyncGenerator<Awaited<T>, void, unknown>; | ||
[Symbol.asyncIterator](): AsyncGenerator<Awaited<T>, void, unknown>; | ||
} | ||
@@ -114,3 +114,3 @@ export interface HChannelOption<T> extends ChannelOption<T> { | ||
*/ | ||
generator(): AsyncGenerator<Awaited<T>, void, unknown>; | ||
[Symbol.asyncIterator](): AsyncGenerator<Awaited<T>, void, unknown>; | ||
} | ||
@@ -173,3 +173,3 @@ /** | ||
*/ | ||
generator(): AsyncGenerator<T, void, unknown>; | ||
[Symbol.asyncIterator](): AsyncGenerator<T, void, unknown>; | ||
/** | ||
@@ -176,0 +176,0 @@ * Get all ready elements in the heap |
@@ -104,3 +104,3 @@ "use strict"; | ||
*/ | ||
async *generator() { | ||
async *[Symbol.asyncIterator]() { | ||
try { | ||
@@ -214,3 +214,3 @@ while (!this.aborted) { | ||
*/ | ||
async *generator() { | ||
async *[Symbol.asyncIterator]() { | ||
try { | ||
@@ -345,3 +345,3 @@ while (!this.aborted) { | ||
*/ | ||
async *generator() { | ||
async *[Symbol.asyncIterator]() { | ||
try { | ||
@@ -348,0 +348,0 @@ while (!this.aborted) { |
@@ -1,52 +0,7 @@ | ||
/// <reference types="node" /> | ||
/// <reference types="bn.js" /> | ||
import { BN } from 'ethereumjs-util'; | ||
import tracer from 'tracer'; | ||
interface Constructor<T = {}> { | ||
new (...args: any[]): T; | ||
} | ||
/** | ||
* This function implements multiple inheritance | ||
* @param mix1 - The first parameter to be inherited | ||
* @param mix2 - The second parameter to be inherited | ||
* @returns The result after multiple inheritance | ||
*/ | ||
export declare function mixin<T1 extends Constructor, T2 extends Constructor>(mix1: T1, mix2: T2): new (...args: any[]) => InstanceType<T1> & InstanceType<T2>; | ||
/** | ||
* Generate random integers according to the given range | ||
* @param min - Minimum limit | ||
* @param max - Maximum limit | ||
* @returns The random number | ||
*/ | ||
export declare function getRandomIntInclusive(min: number, max: number): number; | ||
/** | ||
* Run function and ignore all errors | ||
* @param fn - Function | ||
* @returns Funtion result or undefined(if something goes wrong) | ||
*/ | ||
export declare function ignoreError<T>(p?: Promise<T>): Promise<void | T> | undefined; | ||
/** | ||
* Convert hex string to buffer | ||
* @param hex - Hex string to be converted | ||
* @returns Buffer | ||
*/ | ||
export declare function hexStringToBuffer(hex: string): Buffer; | ||
/** | ||
* Convert hex string to BN | ||
* @param hex - Hex string to be converted | ||
* @returns BN | ||
*/ | ||
export declare function hexStringToBN(hex: string): BN; | ||
/** | ||
* Get current timestamp | ||
*/ | ||
export declare function nowTimestamp(): number; | ||
export declare const logger: tracer.Tracer.Logger; | ||
export { setLevel, getLevel } from 'tracer'; | ||
export * from './abort'; | ||
export * from './helper'; | ||
export * from './logger'; | ||
export * from './channel'; | ||
export * from './compress'; | ||
export * from './initializer'; | ||
export * from './functionalmap'; | ||
export * from './functionalset'; | ||
export * from './functionalMap'; | ||
export * from './functionalSet'; | ||
export * from './timeoutQueue'; | ||
export * from './abortableTimer'; |
@@ -16,112 +16,10 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getLevel = exports.setLevel = exports.logger = exports.nowTimestamp = exports.hexStringToBN = exports.hexStringToBuffer = exports.ignoreError = exports.getRandomIntInclusive = exports.mixin = void 0; | ||
const ethereumjs_util_1 = require("ethereumjs-util"); | ||
const tracer_1 = __importDefault(require("tracer")); | ||
/** | ||
* This function implements multiple inheritance | ||
* @param mix1 - The first parameter to be inherited | ||
* @param mix2 - The second parameter to be inherited | ||
* @returns The result after multiple inheritance | ||
*/ | ||
function mixin(mix1, mix2) { | ||
const mixinProps = (target, source) => { | ||
Object.getOwnPropertyNames(source).forEach((prop) => { | ||
if (/^constructor$/.test(prop)) { | ||
return; | ||
} | ||
Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop)); | ||
}); | ||
}; | ||
let ctor; | ||
if (mix1 && typeof mix1 === 'function') { | ||
ctor = class extends mix1 { | ||
constructor(...props) { | ||
super(...props); | ||
} | ||
}; | ||
mixinProps(ctor.prototype, mix2.prototype); | ||
} | ||
else { | ||
ctor = class { | ||
}; | ||
} | ||
return ctor; | ||
} | ||
exports.mixin = mixin; | ||
/** | ||
* Generate random integers according to the given range | ||
* @param min - Minimum limit | ||
* @param max - Maximum limit | ||
* @returns The random number | ||
*/ | ||
function getRandomIntInclusive(min, max) { | ||
min = Math.ceil(min); | ||
max = Math.floor(max); | ||
if (max < min) { | ||
throw new Error('The maximum value should be greater than the minimum value'); | ||
} | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
exports.getRandomIntInclusive = getRandomIntInclusive; | ||
/** | ||
* Run function and ignore all errors | ||
* @param fn - Function | ||
* @returns Funtion result or undefined(if something goes wrong) | ||
*/ | ||
function ignoreError(p) { | ||
return p && p.catch((err) => { }); | ||
} | ||
exports.ignoreError = ignoreError; | ||
/** | ||
* Convert hex string to buffer | ||
* @param hex - Hex string to be converted | ||
* @returns Buffer | ||
*/ | ||
function hexStringToBuffer(hex) { | ||
return hex.startsWith('0x') ? (0, ethereumjs_util_1.toBuffer)(hex) : (0, ethereumjs_util_1.toBuffer)('0x' + hex); | ||
} | ||
exports.hexStringToBuffer = hexStringToBuffer; | ||
/** | ||
* Convert hex string to BN | ||
* @param hex - Hex string to be converted | ||
* @returns BN | ||
*/ | ||
function hexStringToBN(hex) { | ||
return hex.startsWith('0x') ? new ethereumjs_util_1.BN(hex.substr(2), 'hex') : new ethereumjs_util_1.BN(hex, 'hex'); | ||
} | ||
exports.hexStringToBN = hexStringToBN; | ||
/** | ||
* Get current timestamp | ||
*/ | ||
function nowTimestamp() { | ||
return Math.floor(Date.now() / 1000); | ||
} | ||
exports.nowTimestamp = nowTimestamp; | ||
// tracer logger | ||
exports.logger = tracer_1.default.colorConsole({ | ||
format: '{{title}} [{{timestamp}}] {{message}}', | ||
level: 'detail', | ||
methods: ['detail', 'debug', 'info', 'warn', 'error', 'silent'], | ||
dateformat: 'mm-dd|HH:MM:ss.L', | ||
preprocess: (data) => { | ||
data.title = data.title.toUpperCase(); | ||
if (data.title.length < 5) { | ||
data.title += ' '.repeat(5 - data.title.length); | ||
} | ||
} | ||
}); | ||
var tracer_2 = require("tracer"); | ||
Object.defineProperty(exports, "setLevel", { enumerable: true, get: function () { return tracer_2.setLevel; } }); | ||
Object.defineProperty(exports, "getLevel", { enumerable: true, get: function () { return tracer_2.getLevel; } }); | ||
__exportStar(require("./abort"), exports); | ||
__exportStar(require("./helper"), exports); | ||
__exportStar(require("./logger"), exports); | ||
__exportStar(require("./channel"), exports); | ||
__exportStar(require("./compress"), exports); | ||
__exportStar(require("./initializer"), exports); | ||
__exportStar(require("./functionalmap"), exports); | ||
__exportStar(require("./functionalset"), exports); | ||
__exportStar(require("./functionalMap"), exports); | ||
__exportStar(require("./functionalSet"), exports); | ||
__exportStar(require("./timeoutQueue"), exports); | ||
__exportStar(require("./abortableTimer"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,5 @@ | ||
/** | ||
* TimeoutQueue is used to process multiple timers with the same time interval in batches, | ||
* which can save resource consumption | ||
*/ | ||
export declare class TimeoutQueue { | ||
@@ -10,4 +14,14 @@ private id; | ||
private onTimeout; | ||
/** | ||
* Add a timer callback to the queue | ||
* @param cb | ||
* @returns Callback id | ||
*/ | ||
setTimeout(cb: () => void): number; | ||
/** | ||
* Clear callback by id | ||
* @param id | ||
* @returns Is the clearing successful | ||
*/ | ||
clearTimeout(id: number): boolean; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TimeoutQueue = void 0; | ||
/** | ||
* TimeoutQueue is used to process multiple timers with the same time interval in batches, | ||
* which can save resource consumption | ||
*/ | ||
class TimeoutQueue { | ||
@@ -39,2 +43,7 @@ constructor(duration) { | ||
} | ||
/** | ||
* Add a timer callback to the queue | ||
* @param cb | ||
* @returns Callback id | ||
*/ | ||
setTimeout(cb) { | ||
@@ -50,2 +59,7 @@ const id = this.incrementId(); | ||
} | ||
/** | ||
* Clear callback by id | ||
* @param id | ||
* @returns Is the clearing successful | ||
*/ | ||
clearTimeout(id) { | ||
@@ -52,0 +66,0 @@ const index = this.queue.findIndex(({ id: _id }) => id === _id); |
{ | ||
"name": "@rei-network/utils", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"main": "dist/index.js", | ||
@@ -12,2 +12,3 @@ "types": "dist/index.d.ts", | ||
"devDependencies": { | ||
"@types/node": "^14.14.9", | ||
"typescript": "^4.1.2", | ||
@@ -14,0 +15,0 @@ "chai": "^4.3.4", |
@@ -122,3 +122,3 @@ import Heap from 'qheap'; | ||
*/ | ||
async *generator() { | ||
async *[Symbol.asyncIterator]() { | ||
try { | ||
@@ -249,3 +249,3 @@ while (!this.aborted) { | ||
*/ | ||
async *generator() { | ||
async *[Symbol.asyncIterator]() { | ||
try { | ||
@@ -391,3 +391,3 @@ while (!this.aborted) { | ||
*/ | ||
async *generator() { | ||
async *[Symbol.asyncIterator]() { | ||
try { | ||
@@ -394,0 +394,0 @@ while (!this.aborted) { |
112
src/index.ts
@@ -1,109 +0,7 @@ | ||
import { BN, toBuffer } from 'ethereumjs-util'; | ||
import tracer from 'tracer'; | ||
interface Constructor<T = {}> { | ||
new (...args: any[]): T; | ||
} | ||
/** | ||
* This function implements multiple inheritance | ||
* @param mix1 - The first parameter to be inherited | ||
* @param mix2 - The second parameter to be inherited | ||
* @returns The result after multiple inheritance | ||
*/ | ||
export function mixin<T1 extends Constructor, T2 extends Constructor>(mix1: T1, mix2: T2): new (...args: any[]) => InstanceType<T1> & InstanceType<T2> { | ||
const mixinProps = (target, source) => { | ||
Object.getOwnPropertyNames(source).forEach((prop) => { | ||
if (/^constructor$/.test(prop)) { | ||
return; | ||
} | ||
Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop)!); | ||
}); | ||
}; | ||
let ctor; | ||
if (mix1 && typeof mix1 === 'function') { | ||
ctor = class extends mix1 { | ||
constructor(...props) { | ||
super(...props); | ||
} | ||
}; | ||
mixinProps(ctor.prototype, mix2.prototype); | ||
} else { | ||
ctor = class {}; | ||
} | ||
return ctor; | ||
} | ||
/** | ||
* Generate random integers according to the given range | ||
* @param min - Minimum limit | ||
* @param max - Maximum limit | ||
* @returns The random number | ||
*/ | ||
export function getRandomIntInclusive(min: number, max: number): number { | ||
min = Math.ceil(min); | ||
max = Math.floor(max); | ||
if (max < min) { | ||
throw new Error('The maximum value should be greater than the minimum value'); | ||
} | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
/** | ||
* Run function and ignore all errors | ||
* @param fn - Function | ||
* @returns Funtion result or undefined(if something goes wrong) | ||
*/ | ||
export function ignoreError<T>(p?: Promise<T>): Promise<void | T> | undefined { | ||
return p && p.catch((err) => {}); | ||
} | ||
/** | ||
* Convert hex string to buffer | ||
* @param hex - Hex string to be converted | ||
* @returns Buffer | ||
*/ | ||
export function hexStringToBuffer(hex: string): Buffer { | ||
return hex.startsWith('0x') ? toBuffer(hex) : toBuffer('0x' + hex); | ||
} | ||
/** | ||
* Convert hex string to BN | ||
* @param hex - Hex string to be converted | ||
* @returns BN | ||
*/ | ||
export function hexStringToBN(hex: string): BN { | ||
return hex.startsWith('0x') ? new BN(hex.substr(2), 'hex') : new BN(hex, 'hex'); | ||
} | ||
/** | ||
* Get current timestamp | ||
*/ | ||
export function nowTimestamp() { | ||
return Math.floor(Date.now() / 1000); | ||
} | ||
// tracer logger | ||
export const logger = tracer.colorConsole({ | ||
format: '{{title}} [{{timestamp}}] {{message}}', | ||
level: 'detail', | ||
methods: ['detail', 'debug', 'info', 'warn', 'error', 'silent'], | ||
dateformat: 'mm-dd|HH:MM:ss.L', | ||
preprocess: (data) => { | ||
data.title = data.title.toUpperCase(); | ||
if (data.title.length < 5) { | ||
data.title += ' '.repeat(5 - data.title.length); | ||
} | ||
} | ||
}); | ||
export { setLevel, getLevel } from 'tracer'; | ||
export * from './abort'; | ||
export * from './helper'; | ||
export * from './logger'; | ||
export * from './channel'; | ||
export * from './compress'; | ||
export * from './initializer'; | ||
export * from './functionalmap'; | ||
export * from './functionalset'; | ||
export * from './functionalMap'; | ||
export * from './functionalSet'; | ||
export * from './timeoutQueue'; | ||
export * from './abortableTimer'; |
@@ -7,2 +7,6 @@ type TimeoutInfo = { | ||
/** | ||
* TimeoutQueue is used to process multiple timers with the same time interval in batches, | ||
* which can save resource consumption | ||
*/ | ||
export class TimeoutQueue { | ||
@@ -50,2 +54,7 @@ private id: number = Number.MIN_SAFE_INTEGER; | ||
/** | ||
* Add a timer callback to the queue | ||
* @param cb | ||
* @returns Callback id | ||
*/ | ||
setTimeout(cb: () => void) { | ||
@@ -62,2 +71,7 @@ const id = this.incrementId(); | ||
/** | ||
* Clear callback by id | ||
* @param id | ||
* @returns Is the clearing successful | ||
*/ | ||
clearTimeout(id: number) { | ||
@@ -64,0 +78,0 @@ const index = this.queue.findIndex(({ id: _id }) => id === _id); |
@@ -43,3 +43,3 @@ import { expect } from 'chai'; | ||
}, 100); | ||
for await (const element of numberChannel.generator()) { | ||
for await (const element of numberChannel) { | ||
expect(element, 'array member should be euqal').be.equal(testdata2[i++]); | ||
@@ -83,3 +83,3 @@ } | ||
}, 100); | ||
for await (const element of numberHChannel.generator()) { | ||
for await (const element of numberHChannel) { | ||
expect(element.data, 'heap member should be equal').equal(testdata2Sorted[i++]); | ||
@@ -130,3 +130,3 @@ } | ||
}, 100); | ||
for await (const element of numberPChannel.generator()) { | ||
for await (const element of numberPChannel) { | ||
expect(element.data, 'array member should be euqal').be.equal(dataColletion[i++]); | ||
@@ -133,0 +133,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
39
86447
4
2277
1