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

helper-fns

v2.9.2

Published

Some common utilities functions for everyday backend usage with zero dependencies

Downloads

1,688

Readme

helper-fns

JavaScript Utilities

INSTALLAION

npm i helper-fns
yarn add helper-fns

Common JavaScript packages and utilities used across my projects.

isEmpty

Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.

isEmpty([]) // true
isEmpty({}) // true
isEmpty('') // true
isEmpty([1, 2]) // false
isEmpty({ a: 1, b: 2 }) // false
isEmpty('text') // false
isEmpty(123) // false - type is not considered a collection
isEmpty(true) // false

orderedToken

Generates token based on user defined format

orderedToken('RU-XXXXX') // RU-16891

pick

Picks the key-value pairs corresponding to the given keys from an object.

pick({ a: 1, b: '2', c: 3 }, ['a', 'c']) // { 'a': 1, 'c': 3 }

omit

Omits the key-value pairs corresponding to the given keys from an object.

omit({ a: 1, b: '2', c: 3 }, ['b']) // { 'a': 1, 'c': 3 }

sumOfAnArray

Calculates the sum of two or more numbers/arrays.

sumOfAnArray(1, 2, 3, 4) // 10

pipeFunctions

Performs left-to-right function composition.

const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = pipeFunctions(multiply, add5)
multiplyAndAdd5(5, 2) // 15

renameKeys

Replaces the names of multiple object keys with the values provided.

const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 }
renameKeys({ name: 'firstName', job: 'passion' }, obj)
// { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }

objectArrayToArray

Creates an array of key-value pair arrays from an object.

objectToEntries({ a: 1, b: 2 }) // [ ['a', 1], ['b', 2] ]

clone

Creates a shallow clone of value.

const objects = [{ a: 1 }, { b: 2 }]

const shallow = clone(objects)
console.log(shallow[0] === objects[0])

difference

Calculates the difference between two arrays, without filtering duplicate values.

difference([1, 2, 3, 3], [1, 2, 4]) // [3, 3]

union

Returns every element that exists in any of the two arrays at least once.

union([1, 2, 3], [4, 3, 2]) // [1, 2, 3, 4]

isDate

Checks if gicen string is date

console.log(isDate('not-date'))
// false

console.log(isDate('2019-01-10'))
// true

groupBy

Groups the elements of an array based on the given function.

groupBy([6.1, 4.2, 6.3], Math.floor) // {4: [4.2], 6: [6.1, 6.3]}
groupBy(['one', 'two', 'three'], 'length') // {3: ['one', 'two'], 5: ['three']}

orderBy

Sorts an array of objects, ordered by properties and orders.

const users = [
  { name: 'fred', age: 48 },
  { name: 'barney', age: 36 },
  { name: 'fred', age: 40 },
]
orderBy(users, ['name', 'age'], ['asc', 'desc'])
// [{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
orderBy(users, ['name', 'age'])
// [{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]

randomNumber

Generates random number of between a min number and a max number.

console.log(randomNumber(0, 6)) // integer between 0 and 6
// 5

enumToString

Generates random number of between a min number and a max number.

export enum AppRoles {
  AUTHOR = 'AUTHOR',
  ADMIN = 'ADMIN'
}

console.log(enumToString(AppRoles))
// ["AUTHOR", "ADMIN"]

randomString

Generates random string of giben length

console.log(randomString(6))
// a1t4ry

strAfter

Get string after a substring

strAfter('pineapple', 'pine') // apple

strBefore

Get string before a substring

strBefore('pineapple', 'apple') // pine

isObject

Checks if the passed value is an object or not.

isObject([1, 2, 3, 4]) // true
isObject([]) // true
isObject(['Hello!']) // true
isObject({ a: 1 }) // true
isObject({}) // true
isObject(true) // false

fixedDecimal

Get a number after truncating it from the decimal point. No round off is done

fixedDecimal(3.141525, 3) // 3.141

generateRandomString

Get a random string of defined length

generateRandomString(6) // fd84bg

slugify

Generate a slug from a string

slugify('i love javascript') // i-love-javascript

capitalizeEveryWord

capitalizeEveryWord('hello world!') // 'Hello World!'

throttle

window.addEventListener(
  'resize',
  throttle((evt) => {
    console.log(window.innerWidth)
    console.log(window.innerHeight)
  }, 250),
) // Will log the window dimensions at most every 250ms

unescapeHTML

unescapeHTML('<a href="#">Me & you</a>')
// '<a href="#">Me & you</a>'

timeTaken

timeTaken(() => 2 ** 10) // 1024, (logged): timeTaken: 0.02099609375ms

formatDuration

formatDuration(1001) // '1 second, 1 millisecond'
formatDuration(34325055574)
// '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'

template

template('Hello, {{name}}!', { name: 'world' })
// => Hello, world!

template('Howdy, {{0}}! {{1}}', ['partner', '🤠'])
// => Howdy, partner! 🤠

template('foo: "{{foo}}"; bar: "{{bar}}";', { foo: 123 })
// => foo: "123"; bar: "";

template(
	`
  Name: {{name.last}}, {{name.first}}
  Location: {{address.city}} ({{address.country}})
  Hobbies: {{hobbies.0}}, {{hobbies.1}}, {{hobbies.2}}
`,
	{
	  name: {
	    first: 'Luke',
	    last: 'Edwards',
	  },
	  address: {
	    city: 'Los Angeles',
	    country: 'USA',
	  },
	  hobbies: ['eat', 'sleep', 'repeat'],
	},
)

encrypt and decrypt

  console.log(encrypt('hello','32 bytes hex key','16 bytes hex iv'))
  // p5HX3eMlroLYJPhXr2zARg==

 console.log(decrypt('p5HX3eMlroLYJPhXr2zARg==','32 bytes hex key','16 bytes hex iv'))
// hello

Contributing

Any types of contributions are welcome. Feel free to send pull requests or create issues.

License

Licensed under The MIT License.