npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@brick-city/utils

v0.6.1

Published

A collection of utilities used by brick-city modules

Downloads

8

Readme

utils

A collection of utilities that are used by brick-city packages.

Table of Contents

Installation

npm install --save @brick-city/utils

Usage

arrayToString(arr:*[]):string

arrayToString displays an array as you would expect it to look in code. If the element is undefined, only a comma is returned, if it is a string its wrapped in quotes. Simply calls .toString on each element

Throws a type error if passed something other than an array.

import { arrayToString } from '@brick-city/util';

arrayToString([]) // []
arrayToString([,]) // [ ,]
arrayToString([,,]) // [ , ,]
arrayToString([5,,5]) // [ 5, , 5]
arrayToString([1, 'abc', 5]) // [ 1, 'abc', 5]

deepFreeze(obj:Object):Object

deepFreeze takes an object and recursively walks down the object's own properties and deepFreeze(s) any plain objects it finds, and then freezes the object. Buffers and other non-plain objects are skipped. This was a drawback of other "deepFreeze" type functions which are tripped up by Buffers. Returns a reference to the originally passed object.

import { deepFreeze } from '@brick-city/util';

deepFreeze(object)

isPlainObject(obj:any):boolean

isPlainObject returns a boolean that indicates if the object is a plain object.

import { isPlainObject } from '@brick-city/util';

isPlainObject({}) // true
isPlainObject({a:0}) // true
isPlainObject(null) // false
isPlainObject(()=>{}) // false
isPlainObject(7) // false
isPlainObject( new Object() ); //true
isPlainObject( Object.create(null) ); //true

isPlainObjectEmpty(obj:any):boolean

isPlainObjectEmpty returns a boolean that indicates if the object is a plain object and it is empty.

import { isPlainObjectEmpty } from '@brick-city/util';

isPlainObjectEmpty({}) // true
isPlainObjectEmpty({a:0}) // false
isPlainObjectEmpty(null) // false
isPlainObjectEmpty(()=>{}) // false
isPlainObjectEmpty(7) // false
isPlainObjectEmpty( new Object() ); //true
isPlainObjectEmpty( Object.create(null) ); //true

isRegex(obj:any):boolean

isRegex returns a boolean that indicates if the object is a regular expression.

import { isRegex } from '@brick-city/util';

isRegex( new RegExp('') ); //true
isRegex( new RegExp('ab+c') ); //true
isRegex( new RegExp(/.+/ ) ); //true
isRegex({}) // false
isRegex({a:0}) // false
isRegex(null) // false
isRegex(()=>{}) // false
isRegex(7) // false
isRegex( new Object() ); //false
isRegex( Object.create(null) ); //false

isUndefinedOrNull(value:any):boolean

isUndefinedOrNull returns a boolean that indicates if the value passed is either undefined or null.

import { isPlainObjectEmpty } from '@brick-city/util';

isUndefinedOrNull({}) // false
isUndefinedOrNull({a:0}) // false
isUndefinedOrNull(null) // true
isUndefinedOrNull(undefined) // true
isUndefinedOrNull(()=>{}) // false
isUndefinedOrNull(7) // false

mssqlCdcUpdateMaskToBooleanArray(updateMask:Buffer):Array<boolean>

mssqlCdcUpdateMaskToBooleanArray takes a mssql change data capture update mask, and returns a boolean array which signifies which column ordinal bits are set. The first column ordinal in mssql change data capture is 1, so the zeroth array element is empty.

import { mssqlCdcUpdateMaskToBooleanArray:updateMaskToBoolean } from '@brick-city/util';

updateMaskToBoolean(Buffer.from([0b00000010]); // [, false, true, false, false, false, false, false, false]
updateMaskToBoolean(Buffer.from([0b01000000]); // [, false, false, false, false, false, false, true, false]
updateMaskToBoolean(Buffer.from([0b00000001, 0b11111111]); // [, true, true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, true, false, true, false, false, false, false, false ]
updateMaskToBoolean(Buffer.from([0b00000101, 0b00000001, 0b11111111]); // [,
//                                                                          true, true, true, true, true, true, true, true,
//                                                                          true, false, false, false, false, false, false, false,
//                                                                          true, false, true, false, false, false, false, false,
//                                                                      ]
// Notice the bytes are reversed, the last bytes bits appear first

mssqlCdcUpdateMaskToBitArray(updateMask:Buffer):Array<boolean>

mssqlCdcUpdateMaskToBitArray takes a mssql change data capture update mask, and returns a bit array which signifies which column ordinal bits are set. The first column ordinal in mssql change data capture is 1, so the zeroth array element is empty.

import { mssqlCdcUpdateMaskTobitArray:updateMaskToBit } from '@brick-city/util';

updateMaskToBit(Buffer.from([0b00000010]); // [, 0, 1, 0, 0, 0, 0, 0, 0]
updateMaskToBit(Buffer.from([0b01000000]); // [, 0, 0, 0, 0, 0, 0, 1, 0]
updateMaskToBit(Buffer.from([0b00000001, 0b11111111]); // [, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
updateMaskToBit(Buffer.from([0b00000101, 0b00000001, 0b11111111]); // [, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0]
// Notice the bytes are reversed, the last bytes bits appear first

noopLogger

noopLogger is an object with all pino logger logging methods defined, but does nothing.

import { noopLogger } from '@brick-city/util';

// Now use it in places where a pino logger is needed, but
// none was passed.

traceLogger(logger:logger):function

Pass a pinot styled logger (needs a 'trace' method, and isLevelEnabled method) to this function and it will return a function that logs a trace message. If the logger does not have a trace method, or the log level is not 'trace', it will return a function that does nothing.

The trace message will include the file name, line number, column number, function name, and method name.

import { traceLogger } from '@brick-city/util';

trace = traceLogger(pinoInstance);

// Now drop these wherever you need to trace the logic

trace()

zeroPaddedBinary(integer:number):string

zeroPaddedBinary returns the integer as a binary string padded with zeros to a length of multiples of 8, and prefixed with '0b'. Throws with a TypeError if the value passed is not a number. The value is rounded down to an integer if it is a float.

import { zeroPaddedBinary } from '@brick-city/util';

zeroPaddedBinary({}) // throws new TypeError('Expecting a number')
zeroPaddedBinary({a:0})  // throws new TypeError('Expecting a number')
zeroPaddedBinary(null)  // throws new TypeError('Expecting a number')
zeroPaddedBinary(undefined)  // throws new TypeError('Expecting a number')
zeroPaddedBinary(262) // '0b0000000100000110'
zeroPaddedBinary(7) // '0b00000111'

Support

Please open an issue for support.

Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.

License

MIT License