Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@easy-ds-bot/timeparser

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@easy-ds-bot/timeparser - npm Package Compare versions

Comparing version
1.0.2
to
2.0.0
+59
-18
dist/cjs/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeParser = exports.limits_map = exports.time_RegExp = exports.limit_ms = void 0;
exports.timeParser = exports.deparseTime = exports.parseTime = exports.limitsMap = exports.timeNamesMap = exports.timeRegExpNames = exports.timeRegExp = exports.limitms = void 0;
/** 316 centuries or 31600 years */
exports.limit_ms = 999_999_999_999_999;
exports.time_RegExp = {
exports.limitms = 999_999_999_999_999;
exports.timeRegExp = {
1: /([0-9])+(ms|msec(|s)|milli|millisecond(|s)|мс|мсек|миллисекунд(|а|ы))/gi,

@@ -16,13 +16,34 @@ 1_000: /([0-9])+(s|sec(|s)|second(|s)|с|сек|секунд(|а|ы))/gi,

};
exports.limits_map = {
1: 997_198_860_799_837,
1_000: 997_198_860_800,
60_000: 16_619_981_013,
3_600_000: 276_999_684,
86_400_000: 11_534_000,
604_800_000: 1_647_714,
2_592_000_000: 379_200,
22_118_400_000: 31_600,
exports.timeRegExpNames = {
"ms": /([0-9])+(ms|msec(|s)|milli|millisecond(|s)|мс|мсек|миллисекунд(|а|ы))/gi,
"s": /([0-9])+(s|sec(|s)|second(|s)|с|сек|секунд(|а|ы))/gi,
"m": /([0-9])+(m|min(|s)|minute(|s)|м|мин|минут(|а|ы))/gi,
"h": /([0-9])+(h|hr(|s)|hour(|s)|ч|чс|час(|а|ов))/gi,
"d": /([0-9])+(d|day(|s)|д|день|дн(|и|ей))/gi,
"w": /([0-9])+(w|wk(|s)|week(|s)|н|нд|нед|недел(ь|и|ей))/gi,
"mon": /([0-9])+(mon|mth(|s)|mnth(|s)|month(|s)|мес|месяц(|ы|ев))/gi,
"y": /([0-9])+(y|yr(|s)|year(|s)|г|гд|год(|ы|ов)|лет)/gi,
};
const time_RegExp_entries = Object.entries(exports.time_RegExp);
exports.timeNamesMap = {
1: "ms",
1_000: "s",
60_000: "m",
3_600_000: "h",
86_400_000: "d",
604_800_000: "w",
2_592_000_000: "mon",
22_118_400_000: "y",
};
//Ziggurat!!!
exports.limitsMap = {
1: Math.floor(exports.limitms / 1),
1_000: Math.floor(exports.limitms / 1_000),
60_000: Math.floor(exports.limitms / 60_000),
3_600_000: Math.floor(exports.limitms / 3_600_000),
86_400_000: Math.floor(exports.limitms / 86_400_000),
604_800_000: Math.floor(exports.limitms / 604_800_000),
2_592_000_000: Math.floor(exports.limitms / 2_592_000_000),
22_118_400_000: Math.floor(exports.limitms / 22_118_400_000),
};
const entriesRegExp = Object.entries(exports.timeRegExp);
/**

@@ -35,6 +56,6 @@ * Converts string time (e.g. `1d3h`) into milliseconds (`97_200_000`)

*/
function timeParser(time) {
function parseTime(time) {
let result = 0;
let result_matches = [];
for (const [ms, rx] of time_RegExp_entries) {
for (const [ms, rx] of entriesRegExp) {
let matches = time.match(rx);

@@ -47,3 +68,3 @@ if (matches === null)

let matches_nums = matches.map($ => Number($.match(/([0-9])/g)?.join('') ?? 0)).reduce((a, b) => a + b);
if (matches_nums > exports.limits_map[Number(ms)])
if (matches_nums > exports.limitsMap[Number(ms)])
return null;

@@ -53,2 +74,3 @@ result += matches_nums * Number(ms);

return {
get _deparsed() { return deparseTime(result); },
_matches: result_matches,

@@ -65,3 +87,22 @@ ms: result,

}
exports.timeParser = timeParser;
exports.default = timeParser;
exports.parseTime = parseTime;
/**
* Converts milliseconds into string time
*
* One month = 30 days, one year = 365 days
*/
function deparseTime(ms, separator = '') {
let result = [];
for (let remainder = ms; remainder > 0;)
for (const [time, name] of Object.entries(exports.timeNamesMap).reverse()) {
const count = Math.floor(remainder / Number(time));
if (count) {
remainder -= count * Number(time);
result.push(`${count}${name}`);
}
}
return result.join(separator);
}
exports.deparseTime = deparseTime;
exports.timeParser = parseTime;
exports.default = exports.timeParser;
/** 316 centuries or 31600 years */
export const limit_ms = 999_999_999_999_999;
export const time_RegExp = {
export const limitms = 999_999_999_999_999;
export const timeRegExp = {
1: /([0-9])+(ms|msec(|s)|milli|millisecond(|s)|мс|мсек|миллисекунд(|а|ы))/gi,

@@ -13,13 +13,34 @@ 1_000: /([0-9])+(s|sec(|s)|second(|s)|с|сек|секунд(|а|ы))/gi,

};
export const limits_map = {
1: 997_198_860_799_837,
1_000: 997_198_860_800,
60_000: 16_619_981_013,
3_600_000: 276_999_684,
86_400_000: 11_534_000,
604_800_000: 1_647_714,
2_592_000_000: 379_200,
22_118_400_000: 31_600,
export const timeRegExpNames = {
"ms": /([0-9])+(ms|msec(|s)|milli|millisecond(|s)|мс|мсек|миллисекунд(|а|ы))/gi,
"s": /([0-9])+(s|sec(|s)|second(|s)|с|сек|секунд(|а|ы))/gi,
"m": /([0-9])+(m|min(|s)|minute(|s)|м|мин|минут(|а|ы))/gi,
"h": /([0-9])+(h|hr(|s)|hour(|s)|ч|чс|час(|а|ов))/gi,
"d": /([0-9])+(d|day(|s)|д|день|дн(|и|ей))/gi,
"w": /([0-9])+(w|wk(|s)|week(|s)|н|нд|нед|недел(ь|и|ей))/gi,
"mon": /([0-9])+(mon|mth(|s)|mnth(|s)|month(|s)|мес|месяц(|ы|ев))/gi,
"y": /([0-9])+(y|yr(|s)|year(|s)|г|гд|год(|ы|ов)|лет)/gi,
};
const time_RegExp_entries = Object.entries(time_RegExp);
export const timeNamesMap = {
1: "ms",
1_000: "s",
60_000: "m",
3_600_000: "h",
86_400_000: "d",
604_800_000: "w",
2_592_000_000: "mon",
22_118_400_000: "y",
};
//Ziggurat!!!
export const limitsMap = {
1: Math.floor(limitms / 1),
1_000: Math.floor(limitms / 1_000),
60_000: Math.floor(limitms / 60_000),
3_600_000: Math.floor(limitms / 3_600_000),
86_400_000: Math.floor(limitms / 86_400_000),
604_800_000: Math.floor(limitms / 604_800_000),
2_592_000_000: Math.floor(limitms / 2_592_000_000),
22_118_400_000: Math.floor(limitms / 22_118_400_000),
};
const entriesRegExp = Object.entries(timeRegExp);
/**

@@ -32,6 +53,6 @@ * Converts string time (e.g. `1d3h`) into milliseconds (`97_200_000`)

*/
export function timeParser(time) {
export function parseTime(time) {
let result = 0;
let result_matches = [];
for (const [ms, rx] of time_RegExp_entries) {
for (const [ms, rx] of entriesRegExp) {
let matches = time.match(rx);

@@ -44,3 +65,3 @@ if (matches === null)

let matches_nums = matches.map($ => Number($.match(/([0-9])/g)?.join('') ?? 0)).reduce((a, b) => a + b);
if (matches_nums > limits_map[Number(ms)])
if (matches_nums > limitsMap[Number(ms)])
return null;

@@ -50,2 +71,3 @@ result += matches_nums * Number(ms);

return {
get _deparsed() { return deparseTime(result); },
_matches: result_matches,

@@ -62,2 +84,20 @@ ms: result,

}
/**
* Converts milliseconds into string time
*
* One month = 30 days, one year = 365 days
*/
export function deparseTime(ms, separator = '') {
let result = [];
for (let remainder = ms; remainder > 0;)
for (const [time, name] of Object.entries(timeNamesMap).reverse()) {
const count = Math.floor(remainder / Number(time));
if (count) {
remainder -= count * Number(time);
result.push(`${count}${name}`);
}
}
return result.join(separator);
}
export const timeParser = parseTime;
export default timeParser;
export interface ParsedTime {
ms: number;
seconds: number;
minutes: number;
hours: number;
days: number;
weeks: number;
months: number;
years: number;
get ms(): number;
get seconds(): number;
get minutes(): number;
get hours(): number;
get days(): number;
get weeks(): number;
get months(): number;
get years(): number;
_matches: string[];
get _deparsed(): string;
}
/** 316 centuries or 31600 years */
export declare const limit_ms = 999999999999999;
export declare const time_RegExp: {
export declare const limitms = 999999999999999;
export declare const timeRegExp: {
1: RegExp;

@@ -24,3 +25,23 @@ 1000: RegExp;

};
export declare const limits_map: {
export declare const timeRegExpNames: {
ms: RegExp;
s: RegExp;
m: RegExp;
h: RegExp;
d: RegExp;
w: RegExp;
mon: RegExp;
y: RegExp;
};
export declare const timeNamesMap: {
1: string;
1000: string;
60000: string;
3600000: string;
86400000: string;
604800000: string;
2592000000: string;
22118400000: string;
};
export declare const limitsMap: {
1: number;

@@ -42,3 +63,10 @@ 1000: number;

*/
export declare function timeParser(time: string): ParsedTime | null;
export declare function parseTime(time: string): ParsedTime | null;
/**
* Converts milliseconds into string time
*
* One month = 30 days, one year = 365 days
*/
export declare function deparseTime(ms: number, separator?: string): string;
export declare const timeParser: typeof parseTime;
export default timeParser;
{
"name": "@easy-ds-bot/timeparser",
"version": "1.0.2",
"version": "2.0.0",
"description": "Simply string time converter (en/ru). From \"5d2h\" to 439200000 ms",

@@ -5,0 +5,0 @@ "main": "index",

+21
-16

@@ -23,5 +23,5 @@ <p align="center">

// file.js, type: CommonJS
const { timeParser } = require("@easy-ds-bot/timeparser");
const { parseTime } = require("@easy-ds-bot/timeparser");
let unparsed = "1h5week";
let parsed = timeParser(unparsed);
let parsed = parseTime(unparsed);
console.log(parsed.ms); //3_027_600_000 ms

@@ -33,5 +33,5 @@ console.log(parsed.seconds); //3_027_600 sec

// file.js, type: ESM
import { timeParser } from "@easy-ds-bot/timeparser";
import { parseTime } from "@easy-ds-bot/timeparser";
let unparsed = "1h5week";
let parsed = timeParser(unparsed);
let parsed = parseTime(unparsed);
console.log(parsed.ms); //3_027_600_000 ms

@@ -43,14 +43,19 @@ console.log(parsed.seconds); //3_027_600 sec

```
(cjs) timeParser("1s") x 371,492 ops/sec ±0.97% (88 runs sampled)
(cjs) timeParser("1s").ms x 369,225 ops/sec ±0.86% (92 runs sampled)
(cjs) timeParser("1s").years x 372,001 ops/sec ±0.61% (95 runs sampled)
(cjs) timeParser("1y2mth3w4d5h6s7ms") x 116,745 ops/sec ±1.86% (89 runs sampled)
(cjs) timeParser("1y2mth3w4d5h6s7ms").ms x 102,950 ops/sec ±5.63% (79 runs sampled)
(cjs) timeParser("1y2mth3w4d5h6s7ms").years x 109,983 ops/sec ±2.64% (86 runs sampled)
(esm) timeParser("1s") x 370,134 ops/sec ±1.45% (91 runs sampled)
(esm) timeParser("1s").ms x 377,191 ops/sec ±0.60% (94 runs sampled)
(esm) timeParser("1s").years x 372,549 ops/sec ±0.58% (94 runs sampled)
(esm) timeParser("1y2mth3w4d5h6s7ms") x 122,565 ops/sec ±0.22% (95 runs sampled)
(esm) timeParser("1y2mth3w4d5h6s7ms").ms x 123,687 ops/sec ±0.53% (97 runs sampled)
(esm) timeParser("1y2mth3w4d5h6s7ms").years x 114,201 ops/sec ±1.89% (87 runs sampled)
(cjs) parseTime("1s") x 311,011 ops/sec ±3.58% (79 runs sampled)
(cjs) parseTime("1s").ms x 302,129 ops/sec ±1.53% (83 runs sampled)
(cjs) parseTime("1s").years x 322,713 ops/sec ±1.16% (88 runs sampled)
(cjs) parseTime("1s")._deparsed x 177,589 ops/sec ±1.03% (80 runs sampled)
(cjs) parseTime("1y2mth3w4d5h6s7ms") x 112,658 ops/sec ±0.88% (93 runs sampled)
(cjs) parseTime("1y2mth3w4d5h6s7ms").ms x 113,132 ops/sec ±0.44% (94 runs sampled)
(cjs) parseTime("1y2mth3w4d5h6s7ms").years x 110,092 ops/sec ±0.65% (91 runs sampled)
(cjs) parseTime("1y2mth3w4d5h6s7ms")._deparsed x 84,229 ops/sec ±0.53% (95 runs sampled)
(esm) parseTime("1s") x 297,430 ops/sec ±3.12% (79 runs sampled)
(esm) parseTime("1s").ms x 319,093 ops/sec ±0.82% (85 runs sampled)
(esm) parseTime("1s").years x 327,453 ops/sec ±0.95% (89 runs sampled)
(esm) parseTime("1s")._deparsed x 180,199 ops/sec ±0.97% (94 runs sampled)
(esm) parseTime("1y2mth3w4d5h6s7ms") x 114,544 ops/sec ±0.50% (94 runs sampled)
(esm) parseTime("1y2mth3w4d5h6s7ms").ms x 112,932 ops/sec ±0.55% (94 runs sampled)
(esm) parseTime("1y2mth3w4d5h6s7ms").years x 109,972 ops/sec ±0.48% (89 runs sampled)
(esm) parseTime("1y2mth3w4d5h6s7ms")._deparsed x 85,896 ops/sec ±0.64% (94 runs sampled)
```