Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@handy-common-utils/misc-utils

Package Overview
Dependencies
Maintainers
0
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@handy-common-utils/misc-utils

Miscellaneous utilities

  • 1.6.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
91
decreased by-20.87%
Maintainers
0
Weekly downloads
 
Created
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
import { mask, maskAll, maskEmail, maskFullName, pathBasedReplacer } from '@handy-common-utils/misc-utils';

const masked = JSON.stringify(obj, pathBasedReplacer([
  [/(^|\.)x-api-key$/i, maskAll],
  [/(^|\.)customer\.name$/i, maskFullName],
  [/(^|\.)customer\..*[eE]mail$/i, maskEmail],
  [/(^|\.)zip$/i, (value: string) => value.slice(0, 3) + 'XX'],
  [/(^|\.)cc$/i, () => undefined],
  [/(^|\.)ssn$/i, mask],
]));

Masking

In software development, it's often necessary to hide sensitive information to protect user privacy or comply with regulations. Masking is a common technique used to replace part of a sensitive value with a different, non-sensitive value. For example, you might mask credit card numbers, passwords, or email addresses.

The mask(input, keepLeft = 1, keepRight = 0, minLength = 3, maskLengthOrMaskString = null, maskPattern = '*') function can be used to mask the content of a string, replacing a part of the input string with a mask string. It takes several optional parameters, including the number of characters to keep on the left and right sides of the string, a minimum length for the input string to have unmask characters kept, and the mask pattern to use. The maskEmail(input) and maskFullName(input) functions are specific variations of the mask function that target email addresses and full names, respectively. The maskAll(input) function masks all characters.

expect(mask(undefined)).to.be.undefined;
expect(mask(null)).to.be.null;
expect(mask('')).to.equal('');

expect(mask('abcde')).to.equal('a****');
expect(mask('abc')).to.equal('a**');
expect(mask('ab')).to.equal('**');

expect(maskEmail('james.hu@address.com')).to.equal('j****.**@address.com');
expect(maskEmail('her@here.com')).to.equal('h**@here.com');
expect(maskEmail('me@here.com')).to.equal('**@here.com');
expect(maskEmail('my.new.email.address@example.com')).to.equal('**.n**.e****.a******@example.com');

expect(maskFullName('James Hu')).to.equal('J**** **');
expect(maskFullName('John Smith')).to.equal('J*** S****');
expect(maskFullName('Mike')).to.equal('M***');
expect(maskFullName('Mia')).to.equal('M**');
expect(maskFullName('Me')).to.equal('**');
expect(maskFullName('John von Neumann')).to.equal('J*** v** N******');
expect(maskFullName('Two  Spaces')).to.equal('T**  S*****');
expect(maskFullName('张三丰')).to.equal('张**');
expect(maskFullName('张三')).to.equal('**');

Replacers for JSON.stringify(input, replacer, space)

The pathAwareReplacer(replacer, options) function allows you to build a replacer function that can be passed to JSON.stringify(input, replacer, space). Besides the key, value, and owning object, it also exposes more information to your callback function, such like the full property path as both a dot (.) separated string and as an array, and an array of ancestor objects. This can be useful when you need to replace specific values in an object, but you also want to know where those values were located in the object.

pathBasedReplacer is a function that takes an array of path-based masking rules and returns a function that can be used as the second parameter in the JSON.stringify function. This function allows you to mask sensitive information during JSON.stringify in a very flexible way.

Each element in the rules array contains two parts: a regular expression that matches the full paths to the values you want to mask or replace, and a masking or replacing function that takes the original value as input and returns the masked or replaced value.

For example, you could use pathBasedReplacer to replace all credit card numbers in an object with masked versions of the numbers:

const maskCreditCard = (value: any) => "****-****-****-" + value.slice(-4);

const replacer = pathBasedReplacer([
  [/(^|\.)billing\.cc$/i, maskCreditCard]
]);

const json = JSON.stringify({
  to: 'auditor@example.com',
  cc: 'auditing-trail@example.com',
  year: 2023,
  month: 2,
  orders: [
    {
      id: 123,
      billing: {
        address: '19 High Street',
        cc: '1234-5678-2222-3333',
      },
    },
    {
      id: 124,
      billing: {
        address: '88 Main Street',
        cc: '3435-8933-0009-2241',
      },
    },
  ],
}, replacer, 2);

substituteAll

The substituteAll(input, searchPattern, substitute) function allows you to perform substitutions on an input string by matching a specified pattern and replacing the matches with substitution strings built by a function. It provides flexibility in handling complex substitution scenarios through the substitute callback function.

Example Usage Scenarios:

  • Templating: Replace placeholder variables in a template string with dynamic values. For example, transforming the template "Hello, {name}! How are you, {name}? I am {me}." into "Hello, John! How are you, John? I am James." by substituting {name} with the value "John" and {me} with value "James".
const input = 'Hello, {name}! How are you, {name}? I am {me}.';
const searchPattern = /{([^{}]+)}/g;
const dict: Record<string, string> = {
  name: 'John',
  me: 'James',
};
const substitute: Parameters<typeof substituteAll>[2] = (_match, result) => {
  const key = result[1];
  return dict[key] ?? `{NOT FOUND: ${key}}`;
};
const result = substituteAll(input, searchPattern, substitute);

  • Text Transformation: Modify specific segments of a string based on predefined patterns. For instance, converting dates written in a non-standard format, such as "MM/DD/YY", to a standardized format, like "YYYY-MM-DD", using a suitable regular expression pattern and substitution logic.
const input = 'Event date: 12/31/21';
const searchPattern = / ((\d{2})\/(\d{2})\/(\d{2}))/g;
const substitute = (_: string, result: any) => {
  const [match, date, month, day, year] = result;
  const formattedDate = `20${year}-${month}-${day}`;
  return match.replace(date, formattedDate);
};

const result = substituteAll(input, searchPattern, substitute);

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 = falseis debug output enabled or not, it could be overriden by isQuiet
isQuiet: boolean = falseis quiet mode enabled or not. When quiet mode is enabled, both debug and info output would be discarded.
warn: WARN_FUNC
Static Protected NO_OP_FUNC: () => 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
logMedikooLoggerinstance 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.

Enums

Enumeration: HttpStatusCode

http-status.HttpStatusCode

Some (not all) well known HTTP status codes

Enumeration Members
ACCEPTED202

ACCEPTED202 = 202

The request has been received but not yet acted upon. It is non-committal, meaning that there is no way in HTTP to later send an asynchronous response indicating the outcome of processing the request. It is intended for cases where another process or server handles the request, or for batch processing.


BAD_GATEWAY502

BAD_GATEWAY502 = 502

This error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response.


BAD_REQUEST400

BAD_REQUEST400 = 400

This response means that server could not understand the request due to invalid syntax.


CONFLICT409

CONFLICT409 = 409

This response is sent when a request conflicts with the current state of the server.


CREATED201

CREATED201 = 201

The request has succeeded and a new resource has been created as a result of it. This is typically the response sent after a PUT request.


FORBIDDEN403

FORBIDDEN403 = 403

The client does not have access rights to the content, i.e. they are unauthorized, so server is rejecting to give proper response. Unlike 401, the client's identity is known to the server.


GATEWAY_TIMEOUT504

GATEWAY_TIMEOUT504 = 504

This error response is given when the server is acting as a gateway and cannot get a response in time.


INTERNAL_SERVER_ERROR500

INTERNAL_SERVER_ERROR500 = 500

The server encountered an unexpected condition that prevented it from fulfilling the request.


METHOD_NOT_ALLOWED405

METHOD_NOT_ALLOWED405 = 405

The request method is known by the server but has been disabled and cannot be used. For example, an API may forbid DELETE-ing a resource. The two mandatory methods, GET and HEAD, must never be disabled and should not return this error code.


MOVED_PERMANENTLY301

MOVED_PERMANENTLY301 = 301

This response code means that URI of requested resource has been changed. Probably, new URI would be given in the response.


MOVED_TEMPORARILY302

MOVED_TEMPORARILY302 = 302

This response code means that URI of requested resource has been changed temporarily. New changes in the URI might be made in the future. Therefore, this same URI should be used by the client in future requests.


NOT_FOUND404

NOT_FOUND404 = 404

The server can not find requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 to hide the existence of a resource from an unauthorized client. This response code is probably the most famous one due to its frequent occurence on the web.


NOT_IMPLEMENTED501

NOT_IMPLEMENTED501 = 501

The request method is not supported by the server and cannot be handled. The only methods that servers are required to support (and therefore that must not return this code) are GET and HEAD.


NO_CONTENT204

NO_CONTENT204 = 204

There is no content to send for this request, but the headers may be useful. The user-agent may update its cached headers for this resource with the new ones.


OK200

OK200 = 200

The request has succeeded. The meaning of a success varies depending on the HTTP method: GET: The resource has been fetched and is transmitted in the message body. HEAD: The entity headers are in the message body. POST: The resource describing the result of the action is transmitted in the message body. TRACE: The message body contains the request message as received by the server


PERMANENT_REDIRECT308

PERMANENT_REDIRECT308 = 308

This means that the resource is now permanently located at another URI, specified by the Location: HTTP Response header. This has the same semantics as the 301 Moved Permanently HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request.


REQUEST_TIMEOUT408

REQUEST_TIMEOUT408 = 408

This response is sent on an idle connection by some servers, even without any previous request by the client. It means that the server would like to shut down this unused connection. This response is used much more since some browsers, like Chrome, Firefox 27+, or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also note that some servers merely shut down the connection without sending this message.


SEE_OTHER303

SEE_OTHER303 = 303

Server sent this response to directing client to get requested resource to another URI with an GET request.


SERVICE_UNAVAILABLE503

SERVICE_UNAVAILABLE503 = 503

The server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded. Note that together with this response, a user-friendly page explaining the problem should be sent. This responses should be used for temporary conditions and the Retry-After: HTTP header should, if possible, contain the estimated time before the recovery of the service. The webmaster must also take care about the caching-related headers that are sent along with this response, as these temporary condition responses should usually not be cached.


TEMPORARY_REDIRECT307

TEMPORARY_REDIRECT307 = 307

Server sent this response to directing client to get requested resource to another URI with same method that used prior request. This has the same semantic than the 302 Found HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request.


TOO_MANY_REQUESTS429

TOO_MANY_REQUESTS429 = 429

The user has sent too many requests in a given amount of time ("rate limiting").


UNAUTHORIZED401

UNAUTHORIZED401 = 401

Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response.

Modules

Module: array

Functions
distributeRoundRobin

distributeRoundRobin<T>(array, groups): T[][]

Distributes an array into a number of groups in a round robin fashion. This function has been tuned for performance.

Type parameters
Name
T
Parameters
NameTypeDescription
arrayT[]The input array
groupsnumberNumber of groups the elements in the input array need to be distributed into.
Returns

T[][]

The result as an array of arrays which each represents a group

Module: codec

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 the value is 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 the value is 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.


escapeForRegExp

escapeForRegExp(text): string

Escape a string literal for using it inside of RegExp. (From: https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex)

Parameters
NameTypeDescription
textundefined | null | stringthe string literal to be escaped
Returns

string

escaped string that can be used inside of RegExp, or an empty string if the input is null or undefined


escapeForRegExpReplacement

escapeForRegExpReplacement(text): string

Escape replacement string for using it inside of RegExp replacement parameter. (From: https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex)

Parameters
NameTypeDescription
textundefined | null | stringthe replacement string to be escaped, or an empty string if the input is null or undefined
Returns

string

escaped replacement string that can be used inside of RegExp replacement parameter


generateRandomString

generateRandomString(len): string

Generate a strong (using crypto.randomFillSync(...)) random string that is URL/path safe. In the generated string, approximately every 6 characters represent randomly generated 32 bits. For example, if you need 128 bits of randomness, you just need to generate a string containing 24 characters.

Parameters
NameTypeDescription
lennumberlength of the string to be generated
Returns

string

the random string


generateRandomStringQuickly

generateRandomStringQuickly(len): string

Generate a weak (using Math.random()) random string that is URL/path safe. In the generated string, approximately every 6 characters represent randomly generated 32 bits. For example, if you need 128 bits of randomness, you just need to generate a string containing 24 characters.

Parameters
NameTypeDescription
lennumberlength of the string to be generated
Returns

string

the random string


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 the value is 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 trailing '='.

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 the value is 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: http-status

Enumerations
Variables
HttpStatusMessage

Const HttpStatusMessage: Object

Some (not all) HTTP status messages matching their codes

Type declaration
NameType
200string
201string
202string
204string
301string
302string
303string
307string
308string
400string
401string
403string
404string
405string
408string
409string
429string
500string
501string
502string
503string
504string

Module: index

References
ConsoleLineLogger

Re-exports ConsoleLineLogger


HttpStatusCode

Re-exports HttpStatusCode


HttpStatusMessage

Re-exports HttpStatusMessage


JsonStringifyReplacer

Re-exports JsonStringifyReplacer


JsonStringifyReplacerFromPathBasedRules

Re-exports JsonStringifyReplacerFromPathBasedRules


LineLogger

Re-exports LineLogger


PathAwareReplacer

Re-exports PathAwareReplacer


base64FromUInt32

Re-exports base64FromUInt32


base64UrlFromUInt32

Re-exports base64UrlFromUInt32


consoleLike

Re-exports consoleLike


consoleWithColour

Re-exports consoleWithColour


consoleWithoutColour

Re-exports consoleWithoutColour


distributeRoundRobin

Re-exports distributeRoundRobin


escapeForRegExp

Re-exports escapeForRegExp


escapeForRegExpReplacement

Re-exports escapeForRegExpReplacement


generateRandomString

Re-exports generateRandomString


generateRandomStringQuickly

Re-exports generateRandomStringQuickly


mask

Re-exports mask


maskAll

Re-exports maskAll


maskCreditCard

Re-exports maskCreditCard


maskEmail

Re-exports maskEmail


maskFullName

Re-exports maskFullName


masker

Re-exports masker


pathAwareReplacer

Re-exports pathAwareReplacer


pathBasedReplacer

Re-exports pathBasedReplacer


shortBase64FromUInt32

Re-exports shortBase64FromUInt32


shortBase64UrlFromUInt32

Re-exports shortBase64UrlFromUInt32


substituteAll

Re-exports substituteAll


urlSafe

Re-exports urlSafe

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
logMedikooLoggerinstance 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.

Module: mask

Functions
mask

mask<T>(input, keepLeft?, keepRight?, minLength?, maskLengthOrMaskString?, maskPattern?): T

Mask the content of a string

Type parameters
NameType
Textends undefined | null | string
Parameters
NameTypeDefault valueDescription
inputTundefinedThe input which could also be null or undefined
keepLeftnumber1Number of characters on the left to be kept in the output without masking. Default value is 1.
keepRightnumber0Number of characters on the right to be kept in the output without masking. Default value is 0.
minLengthnumber3Minimal length of the string for keepLeft and keepRight to be effective. If the input string is shorter than this length, the whole string would be masked. Default value is 3.
maskLengthOrMaskStringundefined | null | string | numbernullThe string to be used for replacing the part in the input that needs to be masked, or the length of the mask string if a fixed length is desired, or null/undefined if the mask string should have the same length as the part to be masked. Default value is null.
maskPatternstring'*'The pattern to be repeated as the mask. Default value is '*'.
Returns

T

String with masked content


maskAll

maskAll<T>(input): T

Replace each character of the input with '*'

Type parameters
NameType
Textends undefined | null | string
Parameters
NameTypeDescription
inputTa string or null or undefined
Returns

T

masked string or null or undefined


maskCreditCard

maskCreditCard<T>(input): T

Mask credit card number string

Type parameters
NameType
Textends undefined | null | string
Parameters
NameTypeDescription
inputTcredit card number string which could also be null or undefined
Returns

T

Something like --****-1234, or null/undefined if the input is null/undefined


maskEmail

maskEmail<T>(email): T

Mask sensitive information in an email address while keeping some information for troubleshooting

Type parameters
NameType
Textends undefined | null | string
Parameters
NameTypeDescription
emailTthe email address which could also be null or undefined
Returns

T

masked email address


maskFullName

maskFullName<T>(name): T

Mask sensitive information in the full name while keeping useful information for troubleshooting

Type parameters
NameType
Textends undefined | null | string
Parameters
NameTypeDescription
nameTthe full name which could also be null or undefined
Returns

T

masked full name


masker

masker<T>(keepLeft?, keepRight?, minLength?, maskLengthOrMaskString?, maskPattern?): (input: T) => T

Create a mask function with pre-set parameters.

Example

const maskApiKey = masker(2, 2, 10);
  const maskedString = maskApiKey(myApiKey);
Type parameters
NameType
Textends undefined | null | string = string
Parameters
NameTypeDescription
keepLeft?numberNumber of characters on the left to be kept in the output without masking. Default value is 1.
keepRight?numberNumber of characters on the right to be kept in the output without masking. Default value is 0.
minLength?numberMinimal length of the string for keepLeft and keepRight to be effective. If the input string is shorter than this length, the whole string would be masked. Default value is 3.
maskLengthOrMaskString?null | string | numberThe string to be used for replacing the part in the input that needs to be masked, or the length of the mask string if a fixed length is desired, or null/undefined if the mask string should have the same length as the part to be masked. Default value is null.
maskPattern?stringThe pattern to be repeated as the mask. Default value is '*'.
Returns

fn

A mask function that has specified parameters as pre-set

▸ (input): T

####### Parameters

NameType
inputT

####### Returns

T

Module: stringify-replacer

Type Aliases
JsonStringifyReplacer

Ƭ JsonStringifyReplacer: (this: any, key: string, value: any) => any

Type declaration

▸ (this, key, value): any

The original replacer expected by JSON.stringify(...)

####### Parameters

NameType
thisany
keystring
valueany

####### Returns

any


JsonStringifyReplacerFromPathBasedRules

Ƭ JsonStringifyReplacerFromPathBasedRules: JsonStringifyReplacer & { rules: [RegExp, (input: any) => any][] }

A JsonStringifyReplacer that was created from path based rules. Those rules are stored in the rules property in case of need.


PathAwareReplacer

Ƭ PathAwareReplacer: (key: string, value: any, path: string, parent: Parent, pathArray: string[], ancestors: Parent[]) => any

Type declaration

▸ (key, value, path, parent, pathArray, ancestors): any

The replacer that can potentially utilise the full path of the property in the object.

####### Parameters

NameTypeDescription
keystringName of the property, or the index in the parent array.
valueanyValue of the property or the object in the parent array.
pathstringThe full path of the property in the object, such like "access.visitor.location" or "request.x-forwarded-for.0". Please note that the special characters (including ".") in property names are not escaped, for example, "order.billing address.first line".
parentParentThe object that the property or the element belongs to. It could be { '': <the original object> } when this replacer function is called the first time.
pathArraystring[]The full path as an array. It is more useful than path in case special characters exist in property names. When this replacer function is called the first time, pathArray array would have a zero length.
ancestorsParent[]All the ancestor objects/arrays of the property. When this replacer function is called the first time, ancestors array would have a zero length.

####### Returns

any

Functions
pathAwareReplacer

pathAwareReplacer(replacer, options?): JsonStringifyReplacer

Build a replacer function that can be passed to JSON.stringify(...).

Parameters
NameTypeDescription
replacerPathAwareReplacerThe actual replacer function which could utilise additional information.
options?ObjectOptions to control whether the pathArray and ancestors parameters would have values populated. By default all information available would be populated. There is no need to specify options unless you are extremely concerned about performance, for example if you need to frequently stringify 500MB objects.
options.ancestors?boolean-
options.pathArray?boolean-
Returns

JsonStringifyReplacer

The replacer function that can be passed to JSON.stringify(...).


pathBasedReplacer

pathBasedReplacer(rules): JsonStringifyReplacerFromPathBasedRules

Create a replacer function for JSON.stringify(...) from an array of path based rules. This function is useful for creating masking replacers which can apply masking based on the path of the property.

Example

import { mask, maskAll, maskEmail, maskFullName, pathBasedReplacer } from '@handy-common-utils/misc-utils';
console.log(JSON.stringify(obj, pathBasedReplacer([
 [/.*\.x-api-key$/, maskAll],
 [/.*customer\.name$/, maskFullName],
 [/.*customer\..*[eE]mail$/, maskEmail],
 [/.*\.zip$/, (value: string) => value.slice(0, 3) + 'XX'],
 [/.*\.cc$/, () => undefined],
 [/.*\.ssn$/, mask],
])));
Parameters
NameTypeDescription
rules[RegExp, (input: any) => any][]Array of rules: if the regular expression tests true with the property path, the replacer function will be applied on the value
Returns

JsonStringifyReplacerFromPathBasedRules

the replacer function built from those path based rules

Module: substitute

Functions
substituteAll

substituteAll<T>(input, searchPattern, substitute): T

Substitute all occurrences of a pattern in a string.

Type parameters
NameType
Textends undefined | null | string
Parameters
NameTypeDescription
inputTThe input string on which the substitutions will be performed.
searchPatternRegExpThe regular expression pattern used to search for segments that should be substituted. It must have the g flag set. If the beginning part of the input should be skipped, set the lastIndex of the searchPattern before calling this function. After all the substitution are done, the lastIndex of the searchPattern will be reset to zero. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex
substitute(match: string, matchResult: RegExpExecArray) => null | stringTThe function that builds the substitution string. It is called with the matched substring and the result of RegExp.exec(). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#examples. The function can return null to indicate that no further substitution is desired. In such case, the lastIndex of the searchPattern will not be reset to zero.
Returns

T

The resulting string after performing all substitutions.

Keywords

FAQs

Package last updated on 14 Aug 2024

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc