Socket
Socket
Sign inDemoInstall

@handy-common-utils/misc-utils

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@handy-common-utils/misc-utils


Version published
Weekly downloads
189
decreased by-1.56%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

@handy-common-utils/misc-utils

Miscellaneous utilities

Version Downloads/week CI codecov

How to use

First add it as a dependency:

npm install @handy-common-utils/misc-utils

Then you can use it in the code:

import { shortBase64UrlFromUInt32 } from '@handy-common-utils/misc-utils';

const urlSafeBase64 = shortBase64UrlFromUInt32(12345);
 // use chalk (chalk is not a dependency of this package, you need to add chalk as a dependency separately)
 import chalk from 'chalk';
 import { LineLogger } from '@handy-common-utils/misc-utils';

 // this.flags is an object with properties "debug" and "quiet"
 this.output = LineLogger.consoleWithColour(this.flags, chalk);
 this.output.warn('Configuration file not found, default configuration would be used.');  // it would be printed out in yellow

API

@handy-common-utils/misc-utils

Modules

Classes

Class: LineLogger<DEBUG_FUNC, INFO_FUNC, WARN_FUNC, ERROR_FUNC>

line-logger.LineLogger

A LineLogger logs/prints one entire line of text before advancing to another line. This class is useful for encapsulating console.log/info/warn/error functions. By having an abstraction layer, your code can switching to a different output with nearly no change.

Please note that although the name contains "Logger", this class is not intended to be used as a generic logger. It is intended for "logging for humans to read" scenario.

LineLogger.console() and LineLogger.consoleWithColour() are ready to use convenient functions. Or you can use the constructor to build your own wrappers.

example



// Just a wrapper of console.log/info/warn/error
const consoleLogger = LineLogger.console();

// Wrapper of console.log/info/warn/error but it mutes console.log
const lessVerboseConsoleLogger = LineLogger.console({debug: false});

// Wrapper of console.log/info/warn/error but it mutes console.log and console.info
const lessVerboseConsoleLogger = LineLogger.console({quiet: true});

// use chalk (chalk is not a dependency of this package, you need to add chalk as a dependency separately)
import chalk from 'chalk';
// this.flags is an object with properties "debug" and "quiet"
this.output = LineLogger.consoleWithColour(this.flags, chalk);
this.output.warn('Configuration file not found, default configuration would be used.');  // it would be printed out in yellow

Type parameters
NameType
DEBUG_FUNCextends Function
INFO_FUNCextends Function
WARN_FUNCextends Function
ERROR_FUNCextends Function
Constructors
constructor

new LineLogger<DEBUG_FUNC, INFO_FUNC, WARN_FUNC, ERROR_FUNC>(debugFunction, infoFunction, warnFunction, errorFunction, isDebug?, isQuiet?)

Constructor

Type parameters
NameType
DEBUG_FUNCextends Function
INFO_FUNCextends Function
WARN_FUNCextends Function
ERROR_FUNCextends Function
Parameters
NameTypeDefault valueDescription
debugFunctionDEBUG_FUNCundefinedfunction for outputting debug information
infoFunctionINFO_FUNCundefinedfunction for outputting info information
warnFunctionWARN_FUNCundefinedfunction for outputting warn information
errorFunctionERROR_FUNCundefinedfunction for outputting error information
isDebugbooleanfalseis debug output enabled or not, it could be overriden by isQuiet
isQuietbooleanfalseis quiet mode enabled or not. When quiet mode is enabled, both debug and info output would be discarded.
Properties
PropertyDescription
debug: DEBUG_FUNC
error: ERROR_FUNC
info: INFO_FUNC
isDebug: boolean = false
isQuiet: boolean = false
warn: WARN_FUNC
Static Protected NO_OP_FUNC: () => voidType declaration:
▸ (): void

Returns:
void
Methods
console

Static console<FLAGS>(flags?, debugFlagName?, quietFlagName?): LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an instance with console.log/info/warn/error.

Type parameters
NameType
FLAGSextends Record<string, any>
Parameters
NameTypeDefault valueDescription
flagsFLAGSundefinedThe flag object that contains fields for knowning whether debug is enabled and whether quiet mode is enabled. Values of those fields are evaluated only once within this function. They are not evaluated when debug/info/warn/error functions are called.
debugFlagNamekeyof FLAGS'debug'Name of the debug field in the flags object
quietFlagNamekeyof FLAGS'quiet'Name of the quiet field in the flags object
Returns

LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

An instance that uses console.log/info/warn/error.


consoleLike

Static consoleLike(log): LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an instance from 'log' (https://github.com/medikoo/log). info of the LineLogger is mapped to notice of the medikoo log.

Parameters
NameTypeDescription
logLoggerinstance of the logger
Returns

LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

instance of LineLogger that is actually ConsoleLineLogger type


consoleWithColour

Static consoleWithColour<FLAGS, COLOURER>(flags, colourer, debugColourFuncName?, infoColourFuncName?, warnColourFuncName?, errorColourFuncName?, debugFlagName?, quietFlagName?): LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an instance with console.log/info/warn/error and chalk/colors/cli-color. This package does not depend on chalk or colors or cli-color, you need to add them as dependencies separately.

Type parameters
NameType
FLAGSextends Record<string, any>
COLOURERextends Record<string, any>
Parameters
NameTypeDefault valueDescription
flagsFLAGSundefinedThe flag object that contains fields for knowning whether debug is enabled and whether quiet mode is enabled. Values of those fields are evaluated only once within this function. They are not evaluated when debug/info/warn/error functions are called.
colourerCOLOURERundefinedSupplier of the colouring function, such as chalk or colors or cli-color
debugColourFuncNamekeyof COLOURER'grey'Name of the function within colourer that will be used to add colour to debug messages, or null if colouring is not desired.
infoColourFuncName?keyof COLOURERundefinedName of the function within colourer that will be used to add colour to info messages, or null if colouring is not desired.
warnColourFuncNamekeyof COLOURER'yellow'Name of the function within colourer that will be used to add colour to warn messages, or null if colouring is not desired.
errorColourFuncNamekeyof COLOURER'red'Name of the function within colourer that will be used to add colour to error messages, or null if colouring is not desired.
debugFlagNamekeyof FLAGS'debug'Name of the debug field in the flags object
quietFlagNamekeyof FLAGS'quiet'Name of the quiet field in the flags object
Returns

LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

An instance that uses console.log/info/warn/error and also adds colour to the messages using chalk/colors/cli-color.

Modules

Module: index

References
ConsoleLineLogger

Re-exports ConsoleLineLogger


LineLogger

Re-exports LineLogger


consoleLike

Re-exports consoleLike


consoleWithColour

Re-exports consoleWithColour


consoleWithoutColour

Re-exports consoleWithoutColour

Functions
base64FromUInt32

base64FromUInt32<T>(ui32): Exclude<T, number> | string

Encode an unsigned 32-bit integer into BASE64 string.

Type parameters
NameType
Textends undefined | null | number
Parameters
NameTypeDescription
ui32TA 32-bit integer number which could also be null or undefined. It must be a valid unsigned 32-bit integer. Behavior is undefined when valueis anything other than an unsigned 32-bit integer. If you don't care about loosing precision, you can convert a number by doing n >>> 0 (See https://stackoverflow.com/questions/22335853/hack-to-convert-javascript-number-to-uint32)
Returns

Exclude<T, number> | string

BASE64 string representing the integer input, or the original input if it is null or undefined.


base64UrlFromUInt32

base64UrlFromUInt32<T>(ui32, replacements?): Exclude<T, number> | string

Encode an unsigned 32-bit integer into URL/path safe BASE64 string.

Type parameters
NameType
Textends undefined | null | number
Parameters
NameTypeDefault valueDescription
ui32TundefinedA 32-bit integer number which could also be null or undefined. It must be a valid unsigned 32-bit integer. Behavior is undefined when valueis anything other than an unsigned 32-bit integer. If you don't care about loosing precision, you can convert a number by doing n >>> 0 (See https://stackoverflow.com/questions/22335853/hack-to-convert-javascript-number-to-uint32)
replacementsstring'_-='A string containing replacement characters for "/", "+", and "=". If omitted, default value of '_-=' would be used.
Returns

Exclude<T, number> | string

URL/path safe BASE64 string representing the integer input, or the original input if it is null or undefined.


shortBase64FromUInt32

shortBase64FromUInt32<T>(ui32): Exclude<T, number> | string

Encode an unsigned 32-bit integer into BASE64 string without trailing '='.

Type parameters
NameType
Textends undefined | null | number
Parameters
NameTypeDescription
ui32TA 32-bit integer number which could also be null or undefined. It must be a valid unsigned 32-bit integer. Behavior is undefined when valueis anything other than an unsigned 32-bit integer. If you don't care about loosing precision, you can convert a number by doing n >>> 0 (See https://stackoverflow.com/questions/22335853/hack-to-convert-javascript-number-to-uint32)
Returns

Exclude<T, number> | string

BASE64 string without trailing '=' representing the integer input, or the original input if it is null or undefined.


shortBase64UrlFromUInt32

shortBase64UrlFromUInt32<T>(ui32, replacements?): Exclude<T, number> | string

Encode an unsigned 32-bit integer into URL/path safe BASE64 string without trailling '='.

Type parameters
NameType
Textends undefined | null | number
Parameters
NameTypeDefault valueDescription
ui32TundefinedA 32-bit integer number which could also be null or undefined. It must be a valid unsigned 32-bit integer. Behavior is undefined when valueis anything other than an unsigned 32-bit integer. If you don't care about loosing precision, you can convert a number by doing n >>> 0 (See https://stackoverflow.com/questions/22335853/hack-to-convert-javascript-number-to-uint32)
replacementsstring'_-'A string containing replacement characters for "/" and "+". If omitted, default value of '_-' would be used.
Returns

Exclude<T, number> | string

URL/path safe BASE64 string without trailing '=' representing the integer input, or the original input if it is null or undefined.


urlSafe

urlSafe<T>(base64Input, replacements?): T

Make a "normal" (BASE64) string URL/path safe.

Type parameters
NameType
Textends undefined | null | string
Parameters
NameTypeDefault valueDescription
base64InputTundefinedA (BASE64) string which could be null or undefined.
replacementsstring'_-='A string containing replacement characters for "/", "+", and "=". If omitted, default value of '_-=' would be used.
Returns

T

URL/path safe version of the (BASE64) input string, or the original input if it is null or undefined.

Module: line-logger

Classes
Type aliases
ConsoleLineLogger

Ƭ ConsoleLineLogger: ReturnType<typeof console>

Type of the object returned by LineLogger.console() or LineLogger.consoleWithColour(). It has the same function signatures as console.log/info/warn/error.

Functions
consoleLike

consoleLike(log): LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an instance from 'log' (https://github.com/medikoo/log). info of the LineLogger is mapped to notice of the medikoo log.

Parameters
NameTypeDescription
logLoggerinstance of the logger
Returns

LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

instance of LineLogger that is actually ConsoleLineLogger type


consoleWithColour

consoleWithColour<FLAGS, COLOURER>(flags, colourer, debugColourFuncName?, infoColourFuncName?, warnColourFuncName?, errorColourFuncName?, debugFlagName?, quietFlagName?): LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an encapsulation of console output functions with console.log/info/warn/error and chalk/colors/cli-color.

Type parameters
NameType
FLAGSextends Record<string, any>
COLOURERextends Record<string, any>
Parameters
NameTypeDefault valueDescription
flagsFLAGSundefinedThe flag object that contains fields for knowning whether debug is enabled and whether quiet mode is enabled. Values of those fields are evaluated only once within this function. They are not evaluated when debug/info/warn/error functions are called.
colourerCOLOURERundefinedSupplier of the colouring function, such as chalk or colors or cli-color
debugColourFuncNamekeyof COLOURER'grey'Name of the function within colourer that will be used to add colour to debug messages, or null if colouring is not desired.
infoColourFuncName?keyof COLOURERundefinedName of the function within colourer that will be used to add colour to info messages, or null if colouring is not desired.
warnColourFuncNamekeyof COLOURER'yellow'Name of the function within colourer that will be used to add colour to warn messages, or null if colouring is not desired.
errorColourFuncNamekeyof COLOURER'red'Name of the function within colourer that will be used to add colour to error messages, or null if colouring is not desired.
debugFlagNamekeyof FLAGS'debug'Name of the debug field in the flags object
quietFlagNamekeyof FLAGS'quiet'Name of the quiet field in the flags object. Quiet flag can override debug flag.
Returns

LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

An LineLogger instance that uses console.log/info/warn/error and also adds colour to the messages using chalk/colors/cli-color.


consoleWithoutColour

consoleWithoutColour<FLAGS>(flags?, debugFlagName?, quietFlagName?): LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

Build an encapsulation of console output functions with console.log/info/warn/error.

Type parameters
NameType
FLAGSextends Record<string, any>
Parameters
NameTypeDefault valueDescription
flagsFLAGSundefinedThe flag object that contains fields for knowning whether debug is enabled and whether quiet mode is enabled. Values of those fields are evaluated only once within this function. They are not evaluated when debug/info/warn/error functions are called.
debugFlagNamekeyof FLAGS'debug'Name of the debug field in the flags object
quietFlagNamekeyof FLAGS'quiet'Name of the quiet field in the flags object. Quiet flag can override debug flag.
Returns

LineLogger<(message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void, (message?: any, ...optionalParams: any[]) => void>

An LineLogger instance that uses console.log/info/warn/error.

Keywords

FAQs

Last updated on 26 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc