@eonae/common
v0.10.5
Published
All sorts of common functions and modules
Downloads
51
Readme
Полезные утилиты
Конвертация
Нэймспейс Convert предоставляет статические методы приведения к int, float и boolean.
import { Convert } from '@eonae/common';
const n = Convert.int(stringThatShouldBeAnInteger);
Конфигурация
Абстрактный класс ConfigurationReader и его реализация - класс ProcessEnv предоставляет удобный механизм создания объектов конфигурации со встроенной простой валидацией:
import dotenv from 'dotenv';
import { ProcessEnv } from '@eonae/common';
dotenv.config();
const env = new ProcessEnv(); // Реализует ConfigurationReader
export const config = {
// Последний необязательный аргумент - значение по умолчанию
port: env.getNumber('PORT', 8080),
host: env.getString('HOST'),
disableAuth: env.getBoolean('DISABLE_AUTH'),
logFormat: env.getEnumValue('LOG_FORMAT', LogFormat, LogFormat.warn)
}
В случае отсутствия нужного параметра в источнике конфигурации (если не указано значение по умолчанию) будет выброшен ConfigurationError. Также ошибка вылетит, если не удастся привести значение к нужному формату.
Можно реализовывать ConfigurationReader иначе, переопределяя метод get(key: string), например, используя в качестве источника конфигурации json, yaml файлы и т. п.
Экстракция
Функция readObj (и readObjSync) даёт возможность в одну строчку читать json и yaml и .env файлы, сразу предоставляя вызывающему коду информацию в виду объекта.
import { readObjSync } from '@eonae/common';
import { join } from 'path';
var filepath = join(__dirname, './urls.json')
const urls = readObjSync(filepath);
// use urls.getDrivers and other urls in code
Таймеры
Класс Stopwatch помогает удобно и с точностью до миллисекунды измерять время выполнения кода.
import { Stopwatch } from '@eonae/common';
const sw = new Stopwatch();
sw.Start();
// Do something
console.log(sw.elapsedMilliseconds); // Распечатает количество прошедших миллисекунд
Дата и время
Комбинирует библиотеки moment и moment-range (удобная работа с периодами времени)
import { xmoment } from '@eonae/common';
const period = xmoment.range(new Date(...), new Date(...))
См. документацию пакета moment-range: https://github.com/rotaready/moment-range
Http service wrapper
В разработке...
Прочее
await delay(1000); // will wait for 1 second