UNPKG

@coolgk/utils

Version:

javascript, typescript utility and wrapper functions and classes: array, string, base64, ampq, bcrypt, cache, captcha, csv, email, jwt, number, pdf, tmp, token, unit conversion, url params, session, form data, google sign in, facebook sign in

47 lines (45 loc) 1.32 kB
/*! * @package @coolgk/utils * @version 3.1.4 * @link https://github.com/coolgk/node-utils * @license MIT * @author Daniel Gong <daniel.k.gong@gmail.com> * * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ "use strict"; /*! * Copyright (c) 2017 Daniel Gong <daniel.k.gong@gmail.com>. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); const number_1 = require("./number"); function bytesToString(value) { const units = ['B', 'KB', 'MB', 'GB', 'TB']; while (units.length > 1 && value > 1023) { units.shift(); value = value / 1024; } return number_1.round(value, 2) + units[0]; } exports.bytesToString = bytesToString; function millisecondsToString(value) { const units = [ ['second', 60], ['minute', 60], ['hour', 24], ['day', 30], ['month', 12], ['year', 1000] ]; value = value / 1000; let [[unit, maxValue]] = units; while (units.length > 1 && value >= maxValue) { units.shift(); value = value / maxValue; [[unit, maxValue]] = units; } return `${Math.floor(value)} ${unit}`; } exports.millisecondsToString = millisecondsToString;