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

@madup-inc/utils

v0.17.0

Published

Common utils of LEVER

Downloads

149

Readme

lever-utils

Common utils of LEVER

Install

$ yarn add @madup-inc/utils

To use with node:

const { parseSearchParams } = require('@madup-inc/utils')

or to use with esm

import { parseSearchParams } from '@madup-inc/utils'

To use directly in the browser:

latest version:

<script src="https://unpkg.com/@madup-inc/utils" />
<script>
  utils.parseSearchParams('https://biz.lever.me?a=1&b=2&c=3') 
</script>

or the specific version:

<script src="https://unpkg.com/@madup-inc/[email protected]/dist/index.umd.js" />
<script>
  utils.parseSearchParams('https://biz.lever.me?a=1&b=2&c=3') 
</script>

Basic usage

parseSearchParams

parseSearchParams('https://biz.lever.me?a=1&b=2&c=3') // {a: '1', b: '2', c: '3'}
parseSearchParams('?aa=11') // { aa: '11' } 
parseSearchParams('?aa=11&bb=22') // { aa: '11', bb: '22' }
parseSearchParams('') // {}

serializeSearchParams

serializeSearchParams({ a: 1, b: 2 }) // 'a=1&b=2'
serializeSearchParams({ a: 1, b: undefined }) // 'a=1'
serializeSearchParams({ a: 1, b: '' })  //'a=1&b='
serializeSearchParams({})  // ''
serializeSearchParams()  // ''

randomStr

randomStr() // '1e82e'

isNil

isNil(null) // true
isNil(undefined) // true
isNil(0) // false
isNil('') // false

isNotNil

isNotNil(null) // false
isNotNil(undefined) // false
isNotNil(0) // true
isNotNil('') // true

validateBiznum

validateBiznum('blabla') // false
validateBiznum('1198706634')  // true
validateBiznum('')   // false
validateBiznum() // false

classNames

classNames({ a: true, b: false }) // 'a'
classNames({ a: true, b: false }, { c: true, d: true }) // 'a c d'
classNames('xx', { a: true, b: false }, 'vv') // 'xx a vv'
classNames({ a: false, b: false })  // undefined
classNames('aa', undefined, 'cc') // 'aa cc'
classNames('aa', null, 'cc')  // 'aa cc'
classNames('aa bb', 'cc') // 'aa bb cc'

clsNms

camelCase 클래스명을 사용할 경우 kebob-case 로 자동 변환되도록 classNames 기능을 확장함.

clsNms('visible', {hasContent: true}) // 'visiable has-content'
clsNms('hasContent', {visible: true}) // 'has-content visiable'
clsNms('hasContent', {visible: false}) // 'has-content'

oneOf

oneOf([[true, 2]])  // 2
oneOf([
  [false, 1],
  [false, 2],
  [true, 3],
])  // 3
oneOf([
  [false, 1],
  [true, 2],
])  // 2
oneOf([[false, 1]]) // undefined
oneOf([[false, 1]], 'zzz')  // 'zzz'

// Lazy evaluation
oneOf([() => true, 1])  // 1
oneOf([true, () => 2])  // 2
oneOf([() => true, () => 3])  // 3
oneOf([false, 1], () => 4)  // 4

toComma

toComma(100000)	// '100,000'
toComma(10.234)	// '10.234'
toComma(-10.234)	// '-10.234'
toComma(-10123.234)	// '-10,123.234'
toComma(-10123.23434)	// '-10,123.23434'
toComma(null)	// '0'
toComma(NaN)	// '0'
toComma(undefined)	// '0'
toComma('abc')	// 'NaN'

toNumber

toNumber('12,345')	// 12345
toNumber('-1,234')  // -1234
toNumber('1,900,000')	// 1900000
toNumber(1234)	// 1234
toNumber(null)	// 0
toNumber(NaN)	// 0
toNumber(undefined)	// 0
toNumber('abcd')  // NaN

parseCookie

parseCookie('aa=11; bb=22')  // {aa:11, bb:22}

serializeCookie

serializeCookie('user', 'John', { secure: true, 'max-age': 3600 })  // 'user=John; path=/; secure; max-age=3600'
serializeCookie('user', 'John', { samesite: true }) // 'user=John; path=/; samesite'
serializeCookie('user', 'John', { samesite: false }) // 'user=John; path=/'
serializeCookie('user', 'John', { samesite: 'lax' })  // 'user=John; path=/; samesite=lax'

strMatched

should be true when given array matches with target string

strMatched(['aa', 'bb', 'cc'], 'cc')  // true
strMatched(['aa', 'bb', 'cc'], 'dd')  // false
strMatched(['aa', 'bb', 'cc', /dd/], 'dd')  // true
strMatched(['aa', value => value.startsWith('hello')], 'hello world') // true

matched

curried function for general type matching

matched(['aa', 'bb', 'cc'], 'cc')  // true
matched(['aa', 'bb', 'cc'])('cc')  // true
matched(['aa', 'bb', undefind])(undefind)  // true
matched(['aa', 'bb', undefind, null])(null)  // true
matched([NaN, null, undefined], NaN) // true

parseFilename

parseFilename('aa.jpg') // { name: 'aa', ext: 'jpg' }
parseFilename('aa.bb.cc') // { name: 'aa.bb', ext: 'cc' }
parseFilename('aa') // { name: 'aa', ext: '' }

fileSizeUnit

fileSizeUnit(1) // 1B
fileSizeUnit(1024) // 1KB
fileSizeUnit(1024 * 1024) // 1MB
fileSizeUnit(1024 * 1024 * 1024) // 1GB

fileSizeUnit(Math.pow(1024, 2) / 3 + Math.pow(1024, 2)) // 1.33MB

// when seconde argument false return-value use lowercase
fileSizeUnit(1024, false) // 1kb
fileSizeUnit(100, false) // 100b

snakeToCamel

snakeToCamel('hello_world') // helloWolrd
snakeToCamel('snake2_Camel') // snake2Camel
snakeToCamel('camel') // camel

camelToKabab

camelToKabab('helloWolrd') //hello-world
camelToKabab('camel2Kabab') //camel2-kabab
camelToKabab('kabab') //kabab

Browser only

isIE

isIE(window.navigator)  // true on Internet Explorer

getCookie

// if document.cookie is 'name=keating; id=1234'
getCookie('name') // 'keating'

setCookie

should use exactly the same path and domain options as when we set it.

setCookie('user', 'John', { secure: true, 'max-age': 3600 })  // the cookie is set

deleteCookie

should use exactly the same path and domain options as when we set it.

deleteCookie('user')
deleteCookie('user', {domain: '.lever.me'})

pathMatched

should be true when given array matches with window.location.pathname

// when window.location.pathname is `/ads/create`
pathMatched(['/ads/manage', '/ads/update']) // false
pathMatched(['/ads/manage', '/ads/update', '/ads/create']) // true

// Regexp is usable as a condition
pathMatched(['/ads/manage', '/ads/update', /\/ads\/.+/]) // true

// function is also usable
pathMatched(['/creative', value => value.startsWith('/ads')]) // true

downloadFile

downloadFile('https://guide.pdf', 'lever-guide.pdf') // download file with name `lever-guide.pdf`

downloadData

// download file with name 'photo.jpg' in fileData Blob object
axios({
  url: 'https://images.lever.me/photo.jpg',
  method: 'get',
  responseType: 'blob',
}).then(res => downloadData(res.data, 'photo.jpg'))

loadJs

// <script src='https://accounts.google.com/gsi/client' type="text/javascript"></script> should be appended to head tag
loadJs('https://accounts.google.com/gsi/client')
  .then(() => {
    console.log('js loaded')
  })