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

bme-utils

v0.1.7

Published

Utilities for Node.JS, JavaScript and TypeScript projects

Downloads

27

Readme

bme-utils

bme-utils is a TypeScript library for utilities required for your next daily projects.

https://nodei.co/npm/bme-utils.png?downloads=true&downloadRank=true&stars=true

Installation

Use the package manager npm or yarn to install bme-utils.

npm i bme-utils
yarn add bme-utis

Utilities

Clamp

Returns trimmed value from the provided range. If the minimum value will be larger than the maximum value function will throw an error.

Usage

import { clamp } from 'bme-utils';

clamp(8, 21, 66); // 21 — min
clamp(88, 21, 66); // 88 — max
clamp(37, 21, 66); // 37 — value

Where:

  • value is value to be clamped
  • min is minimum allowed value
  • max is maximum allowed value

Compare objects

Returns boolean results of comparing two objects. It simply compares JSONed objects. Values can be in any format that can be transformed to JSON string.

Usage

import { compareObjects } from 'bme-utils';

const objectA = { manufacturer: 'Mercedes-Benz', model: 'A-Class', engine: 'A45s' };
const objectB = { manufacturer: 'Mercedes-Benz', model: 'A-Class', engine: 'A200' };

compareObjects(objectA, objectB); // false
compareObjects(objectA, objectA); // true
compareObjects(objectA, { manufacturer: 'Mercedes-Benz', model: 'A-Class', engine: 'A45s' }); // true

Where:

  • objectA is any value (Preferred: object)
  • objectB is any value (Preferred: object)

Debounce

WORK IN PROGRESS, DEFAULT VALUES MIGHT CHANGE IN FUTURE Debounce function allows to "group" calls in time sequences to execute it only once. Imagine that you are in the elevator where doors are closing. When someone from the outside press the button it will open doors and the elevator would not go up until doors are closed.

Usage

import { debounce } from 'bme-utils';

const callback = () => console.debug('Hello world');
const wait = 450;

debounce(callback, wait); // 'Hello world

Where:

  • callback is function
  • wait is value in millisecond to wait until execute code (or start elevator) (Default: 450)

Detect device

Returns device type. It can be used to detect if the user is using mobile device or desktop.

Usage

import { detectDevice } from 'bme-utils';

detectDevice();

Response types

export type DeviceType = 'desktop' | 'mobile' | 'tablet';
export type Browser = 'Chrome' | 'Firefox' | 'Safari' | 'Edge' | 'IE' | 'unknown';
export type OS = 'iOS' | 'Android' | 'Windows' | 'macOS' | 'Linux' | 'unknown';

interface Response { 
  type: DeviceType; 
  browser: Browser; 
  os: OS 
}

First element

Returns the first element of an array or null value. Might be useful if you enforce the no magic numbers rule.

Usage

import { firstElement } from 'bme-utils';

const list = ['First', 'Second', 'Third'];
const listB = [];

firstElement(list); // 'First'
firstElement(listB); // null

Last element

Returns the last element of an array or null value. Might be useful if you enforce the no magic numbers rule.

Usage

import { lastElement } from 'bme-utils';

const list = ['First', 'Second', 'Third'];
const listB = [];

lastElement(list); // 'Third'
lastElement(listB); // null

Where:

  • list is array of any

Is empty

Returns a boolean value. The input value can be an array of any, string, number, object, null or undefined. For array and object, it checks if there's more than or equal to 1 entry. For string, it checks if there's more than or equal to 1 character. For number, it checks if it's not equal to NaN or 0. For null and undefined it always returns false.

Usage

import { isEmpty } from 'bme-utils';

const value = ['First', 'Second', 'Third'];
const valueB = [];
const valueC = 0;
const valueD = 100;
const valueE = NaN;
const valueF = 'First';
const valueG = '';
const valueH = {};
const valueI = { manufacturer: 'Mercedes-Benz' };

isEmpty(value); // false
isEmpty(valueB); // true
isEmpty(valueC); // true
isEmpty(valueD); // false
isEmpty(valueE); // true
isEmpty(valueF); // false
isEmpty(valueG); // true
isEmpty(valueH); // true
isEmpty(valueI); // false

Where:

  • value is array of any, string, number, object, null or undefined

Max index

Returns a maximum index value. The input value can be an array of any or string. For array it will return last element index and for string it will return last letter index. If input will be empty it will return null.

Usage

import { maxIndex } from 'bme-utils';

const elements = ['First', 'Second', 'Third'];
const elementsB = [];
const elementsC = 'First';
const elementsD = '';

maxIndex(elements); // 2
maxIndex(elementsB); // null
maxIndex(elementsC); // 4
maxIndex(elementsD); // null

Where:

  • elements is array of any or string.

Random boolean

Returns a random boolean value. Optionally you can set trueChance.

Usage

import { randomBoolean } from 'bme-utils';

const trueChance = 0.66;

randomBoolean(trueChance); // true | false

Where:

  • trueChance is an optional number value in the range of 0 to 1. (Default: 0.5)

Random element

Returns a random element of array. The input value can be an array of any. If array will be empty it will return null.

Usage

import { randomElement } from 'bme-utils';

const elements = ['First', 'Second', 'Third'];
const elementsB = [];

maxIndex(elements); // 2
maxIndex(elementsB); // null

Where:

  • elements is array of any or string.

Random number

Returns a random number value from provided range values. The minimum value is set to 0 by default.

Usage

import { randomNumber } from 'bme-utils';

const max = 100;
const min = 66;

randomNumber(max, min); // number

Where:

  • max is a maximum value
  • min is an optional minimum value. (Default: 0)

To rgba

EXPERIMENTAL Transform RGB or hex value to RGBA value.

Usage

import { toRgba } from 'bme-utils';

const colour = '#300032';
const colourB = 'rgb(48, 0, 50)';
const opacity = 0.33;

toRgba(colour, opacity); // rgba(48, 0, 50, 0.33)
toRgba(colourB, opacity); // rgba(48, 0, 50, 0.33)

Where:

  • colour is a string value with HEX or RGB colour
  • opacity is a number value of demanded opacity

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT