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

@bagubagu/utils

v1.13.0

Published

Bagubagu Utilities

Downloads

70

Readme

Bagubagu Utilities Build Status

Common Typescript/Javascript utilities that we use at Bagubagu Studio.

Install

npm install @bagubagu/utils --save

Usage

with Typescript

import { generatePushId, encodeFirebaseKey } from "@bagubagu/utils";

With Node

const { generatePushId, encodeFirebaseKey } = require("@bagubagu/utils");

API

isEmail

Return true if parameter is a valid email, false otherwise

import { isEmail } from "@bagubagu/utils";

console.log(isEmail("[email protected]")); // true
console.log(isEmail("captain marvel")); // false

isPhone

Return true if parameter is a valid phone number, false otherwise

import { isPhone } from "@bagubagu/utils";

console.log(isPhone("75245")); // false
console.log(isEmail("hello world")); // false
console.log(isEmail("+62811888888999")); // true

toE164

Convert to E164 formatted phone number. Return original string if parameter is not a valid phone number.

import { toE164 } from "@bagubagu/utils";

console.log(toE164("08188889999")); // +62811888889999
console.log(toE164("+6281188889999")); // +6281188889999
console.log(toE164("hello world")); // hello world
console.log(toE164("01234")); // 01234

getHostedZoneByName

Return Hosted Zone Id by providing its hosted zone name

import { config, SharedIniFileCredentials } from "aws-sdk";
import { getHostedZoneByName } from "@bagubagu/utils";

const credentials = new SharedIniFileCredentials();
const hostedZoneName = "cokodidi.com";

const result = await getHostedZoneByName({ hostedZoneName, credentials });
console.log(result); // { hostedZoneId: 'Z2U8QZRMLEMY1M' }

generatePushId

Generate a firebase like push Id.

import { generatePushId } from "@bagubagu/utils";

const pushId = generatePushId();
console.log(pushId); // -KiA4eelV_DYbflp0YcW

generateReadableId

Generate human readable unique Id.

import { generateReadableId } from "@bagubagu/utils";

console.log(generateReadableId()); // black-velvet-48
console.log(generateReadableId({ language: "id" })); // kursi-cantik-39

encodeFirebaseKey

Firebase does not allow database key to contain following characters: '.', '#', '$', '/', '[', ']'. Consequently we are unable to use email address as key. Use encodeFirebaseKey to encode email address then use it as key.

import { encodeFirebaseKey } from "@bagubagu/utils";

const email = "[email protected]";
const key = encodeFirebaseKey(email); // monyet%40kambing%2Ecom

decodeFirebaseKey

Decode an encoded firebase key.

import { decodeFirebaseKey } from "@bagubagu/utils";

const encodedKey = "monyet%40kambing%2Ecom";
console.log(decodeFirebaseKey(encodeKey)); // [email protected]

objectEntries

Make object keys and values available through for of loop. Requires tsconfig.json to set compilerOptions.target: "es2015" and compilerOptions.lib: [ "es2015" ]

import { objectEntries } from "@bagubagu/utils";

const weapons = {
  thor: "mjolnir",
  spiderman: "web",
  wonderwoman: "rope",
  hulk: "rage"
};

for (const [key, value] of objectEntries(weapons)) {
  if (key === "hulk") {
    console.log(key + ": " + value); // hulk: rage
  }
}

getRandom

Get Random number between floor and ceiling

import { getRandom } from "@bagubagu/utils";

console.log(getRandom(1, 10)); // output a number between 1 and 10

date2String

Change date value to string value with semantic format

import { date2String } from "@bagubagu/utils";

const dateValue = new Date();
console.log(date2String(dateValue)); // output a string with 'yyyy-mm-dd' format

removeEmptyValues

Remove empty values of an object

import { removeEmptyValues } from "@bagubagu/utils";

const weapons = {
  thor: "mjolnir",
  spiderman: "",
  wonderwoman: null,
  hulk: undefined
};
console.log(removeEmptyValues(weapons)); // output { thor: 'mjolnir' }

groupBy

Group array of object by key name

import { groupBy } from "@bagubagu/utils";

const heroes = [
  { name: "Spiderman", age: 15 },
  { name: "Thor", age: 35 },
  { name: "Loki", age: 35 }
];
console.log(groupBy(heroes, "age")); // output { "35": [...], "15": [...] }