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

@charliefan/fe-toolbox

v1.0.4

Published

tool box of util functions

Downloads

4

Readme

Front End Development Toolbox

Description

fe-toolbox is a collection of utility functions that might be useful in Front End development

Install

npm i --save @charliefan/fe-toolbox

or

yarn add @charliefan/fe-toolbox

import into js file:

import * as feToolbox from '@charliefan/fe-toolbox';

Contents

Usage

Cookie Tool

Get cookie value from cookie:

import { getCookieItem } from '@charliefan/fe-toolbox';

// if there is 'mykey=123' in cookie, this will return 123 from cookie
const myKeyValue = getCookieItem('mykey');

localStorage Tool

  1. set item in localStorage:

storageTool.setItem(key: string, data: any, expiredIn: number, startAt?: number):void

expiredIn and startAt are unix time in miliseconds

import { storageTool } from '@charliefan/fe-toolbox';

// 1. set item in localStorage without expiration
storageTool.setItem('myData', { a: '1', b: '2'});

// 2. set item in localStorage expired in 1 minutes
storageTool.setItem('myData', { a: '1', b: '2'}, 60000);


// 3. set item in localStorage expired in 1 minutes at given start
storageTool.setItem('myData', { a: '1', b: '2'}, 60000, new Date().getTime();
  1. get item from localStorage:

storageTool.getItem(key: string): any

return null if data does not exists or data is expired

import { storageTool } from '@charliefan/fe-toolbox';

const retrievedData = storageTool.getItem('my-key');
  1. remove item from localStorage:

storageTool.removeItem(key: string): void

exact same with localStorage.removeItem

  1. remove all items from localStorage:

storageTool.removeAll():void

exact same with localStorage.clear()

String Tool

  1. stringhighlighter is the function that can match string in an origin string and highlight it with <b> tag

stringhighlighter(keywords: string, orgin: string,):string

import { stringhighlighter } from '@charliefan/fe-toolbox';

const highlighted = stringhighlighter('777 Bay', '777 Bay street, Toronto, ON');

// will return '<b>777 Bay</b> street, Toronto, ON'
  1. numberStringFormat is a function to format number into a string as required. For example, to format a number into a currency number string

numberStringFormat(numberSrc: string | number, bit = 0, sign = ',', gapNum = 3): string

digits (optional) The number of digits to appear after the decimal point

sign (optional) default is ',' seperator symbol e.g thousand seperator

gapnum (optioal) default is 3, the number of digits where the sign appear after.

import { numberStringFormat } from '@charliefan/fe-toolbox';

const price = numberStringFormat(64999);

// price -> '64,999'

const productNum = numberStringFormat(123123123433, 0, '-', 4);

// productNum -> '1231-2312-3433'

numberFormat(numberSrc: string | number, bit = 0, sign = ',', gapNum = 3): string

Array Tool

listItemSelector is a function that get selected items list by given target and current selected list;

listItemSelector(
    target: string | number,
    selectedList: Array<string | number>,
    defaultValue: string | number = '',
    isDefaultValueExcludable = false
): Array<string | number>

For example:

arrayItemPicker(1, [1,2,3]) will return [2,3] arrayItemPicker(3, [1,2]) will return [1,2,3]

import { listItemSelector } from '@charliefan/fe-toolbox';

const checkedItems = listItemSelector(3, [1,2]);
// checkedItems will be [1,2,3]


// if default value is provided, then it will return an array that contains a default value

const checkedItems2 = listItemSelector(1, [1], 1);
// checkedItems2 will be [1]


// if isDefaultValueExcludable is true, then if the target selected default value, then the array will exclude other values except default value:


const checkedItems3 = listItemSelector(1, [2,3,4], 1, true);
// checkedItems3 will be [1]

flatDeep is a function to flat an array

flatDeep(arr: Array<T>, depth: number): Array<T>

depth is the depth of the array, it can also be infinite:

import { flatDeep } from '@charliefan/fe-toolbox';

const flattenArray = flatDeep([1,[2,3], 4], 1);

// flattenArray will be [1,2,3,4]

const flattenArray2 = flatDeep([1,[2,3], 4, [5, [6,7]]], 1);
// flattenArray2 will be [1,2,3,4,5,6,7]