@bagubagu/utils
Bagubagu Utilities
Last updated a year ago by louislarry .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @bagubagu/utils 
SYNC missed versions from official npm registry.

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("thor@avenger.com")); // 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 = "monyet@kambing.com";
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)); // monyet@kambing.com

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": [...] }

Current Tags

  • 1.13.0                                ...           latest (a year ago)

17 Versions

  • 1.13.0                                ...           a year ago
  • 1.12.0                                ...           a year ago
  • 1.11.1                                ...           a year ago
  • 1.11.0                                ...           a year ago
  • 1.9.0                                ...           a year ago
  • 1.8.0                                ...           2 years ago
  • 1.7.0                                ...           2 years ago
  • 1.6.1                                ...           2 years ago
  • 1.5.0                                ...           3 years ago
  • 1.4.0                                ...           3 years ago
  • 1.3.0                                ...           3 years ago
  • 1.2.0                                ...           3 years ago
  • 1.1.3                                ...           3 years ago
  • 1.1.2                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Downloads
Today 0
This Week 1
This Month 1
Last Day 1
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (10)

Copyright 2014 - 2016 © taobao.org |