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

@xlou/webtools

v1.1.9

Published

Frontend Development Tools

Downloads

60

Readme

Languages

Introduction

  • Commonly used tools in frontend development.
  • Examples: Base64 encoding/decoding, deep copying of data, extracting URL parameters, etc.

Usage

Using in Traditional Projects

<script src="https://unpkg.com/@xlou/[email protected]/dist/umd/webtools.min.js"></script>
<!-- It's recommended to download and use the file locally -->
<script>
  /* After including this JS file, the tools object will be available on the window */
  let query = tools.getQuery()
</script>

Using in Vue, React, Angular, and Other Node Projects

Installation

npm i @xlou/webtools -S

In main.js or main.ts

/* Using specific functions */
import { getQuery } from '@xlou/webtools'

let query = getQuery()

/* Using the entire package */
import tools from '@xlou/webtools'

let query = tools.getQuery()

API

deepCopy   Deep Copy for Reference Types

Parameter Details

function deepCopy(obj: any, set?: Set<any>): any;

Usage Example

let objA = { m: "hello", n: [1, 2, 3] }
let objB = deepCopy(a) // objB => { m: "hello", n: [1, 2, 3] }
objA.m = "hi"
objB.n[0] = 4 // objB => { m: "hi", n: [4, 2, 3] }
console.log(objA) // objA => { m: "hello", n: [1, 2, 3] }

getQuery   Get URL Parameters

Parameter Details

interface GeneralObject {
  [prop: string]: string | number | boolean | null | undefined;
}
function getQuery(href?: string): GeneralObject;
/* If href is not provided, it gets parameters from the current page's URL */

Usage Example

/* If the current page's URL is www.xxx.com?name=tom&id=1 */
let q0 = getQuery() // q0 => { name: "tom", id: 1 }
let q1 = getQuery("www.xxx.com?type=1") // q1 => { type: 1 }

queryString   Convert Object to Query String

Parameter Details

function queryString(obj: GeneralObject, bol?: boolean): string;

Usage Example

let a = { name: "tom", id: 1 }
let m = queryString(a) // m => "name=tom&id=1"
let n = queryString(a, true) // n => "?name=tom&id=1"

filterObject   Filter an Object

Parameter Details

function filterObject(obj: Object, str?: string, bol?: boolean): Object;

Usage Example

let a = { m: 123, n: "hello", p: 456, q: 789 }
let b = filterObject(a, "p, q") // b => { p: 456, q: 789 }
let c = filterObject(a, "p, q", false) // b => { m: 123, n: "hello" }

toFixed   Format Decimal Places

Parameter Details

function toFixed(num?: number | string, s?: number | string): string | undefined;

Usage Example

let a = 1.335
let m = a.toFixed(2) // m => 1.33 Using the default toFixed method might lead to unexpected results
let n = toFixed(a, 2) // n => 1.34
let p = toFixed(a) // p => 1

formSubmit   Simulate Form Submission for File Downloads

Parameter Details

function formSubmit(obj: FormOptions): void;

Usage Example

formSubmit({
  method: "post", // Request type
  action: "./hello", // Request URL
  /* ... Other form parameters */
  data: { name: "tom" } // Request data
})

readText   Read Text Files

Parameter Details

function readText(url: string): Promise<string>;

Usage Example

readText("./hello.txt")
.then(res => {
  console.log(res)
})

readJSON   Read JSON Files

Parameter Details

function readJSON(url: string): Promise<any>;

Usage Example

readJSON("./hello.json")
.then(res => {
  console.log(res)
})

getStore   Read from localStorage, Parsing JSON if Applicable

Parameter Details

function getStore(str: string): any;

Usage Example

/* key: a, value: { "m": "hello" } */
let b = getStore("a") // b => { m: "hello" }

setStore   Write to localStorage, Parsing Objects to JSON if Applicable

Parameter Details

function setStore(str: string, data: any): void;

Usage Example

let a = { m: "hello" }
let b = "tom"
setStore('p', a) // key: p, value: { "m": "hello" }
setStore('q', b) // key: q, value: tom

Base64   Encode and Decode Strings using Base64

Parameter Details

class Base64 {
  constructor(key?: string);
  private key;
  encode(input: string): string;
  decode(input: string): string;
  private utf8_encode;
  private utf8_decode;
}

Usage Example

const base64 = new Base64()
let a = base64.encode("Hello, World!") // a => 'SGVsbG8sIFdvcmxkIQ=='
let b = base64.decode('SGVsbG8sIFdvcmxkIQ==') // b => "Hello, World!"

unid   Generate a Unique ID String

Parameter Details

function unid(): string;

Usage Example

let a = unid() // a => 'xenj1qoj5lbei4nh2'

colorRGB   Get RGB Values from a Color

Parameter Details

function colorRGB(str: string): Array<number> | undefined;

Usage Example

colorRGB("#f00") // [255, 0, 0]
colorRGB("#ff7300") // [255, 115, 0]
colorRGB("rgb(128, 55, 255)") // [128, 55, 255]

clipboardWrite   Copy the specified content to the clipboard.

Parameter Details

function clipboardWrite(content: any, type?: string): Promise<void>;

Usage Example

function copyText() {
  clipboardWrite("Hello, World!") // If the 'type' parameter is not specified, it defaults to 'text/plain'.
  .then(() => {
    console.log("Copy successful")
  })
}
async function copyImage() {
  const res = await fetch("./flower.png")
  const blob = await res.blob()
  clipboardWrite(blob, blob.type)
  .then(() => {
    console.log("Copy successful")
  })
}