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

js-fast-utils

v1.2.1

Published

Utility javascript functions.

Downloads

1

Readme

js-fast-utils

Javascript utility functions

npm

Javascript type verification functions

isDefined

Check if is not undefined.

isObject

Check if is an Object.

isString

Check if is a String.

isFunction

Check if is a Function.

isArray

Check if is a Array.

isNumber

Check if is a Number.

isRegex

Check if is a RegExp.

isNull

Check if is null.

isAsyncFunction

Check if is an async function.

isEmptyObject

Check if is an Object and has no properties.

isEmptyString

Check if is equal to an empty string.

isEmptyArray

Check if is an Array and has zero items.

isEmpty

Check if is null, not defined or a type with no elements/properties/items even if it is defined.

Date functions

isDate

Test a string if can be parsed to Date

Other utility functions

objectMap

Loop into object properties assigning to a new one using predicate function and get a new mapped object.

It's useful for rearranging objects:

const daysOfWeek = {
  sun: { id: 0, name: 'Sunday', abbreviated: 'SUN' },
  mon: { id: 1, name: 'Monday', abbreviated: 'MON' },
  tue: { id: 2, name: 'Tuesday', abbreviated: 'TUE' },
  wed: { id: 3, name: 'Wednesday', abbreviated: 'WED' },
  thu: { id: 4, name: 'Thursday', abbreviated: 'THU' },
  fri: { id: 5, name: 'Friday', abbreviated: 'FRI' },
  sat: { id: 6, name: 'Saturday', abbreviated: 'SAT' }
}

const indexedDaysOfWeek = objectMap(daysOfWeek, ({ id, ...day }) => ({ [id]: day }))
/* indexedDaysOfWeek:
{
  0: {name: "Sunday", abbreviated: "SUN"}
  1: {name: "Monday", abbreviated: "MON"}
  2: {name: "Tuesday", abbreviated: "TUE"}
  3: {name: "Wednesday", abbreviated: "WED"}
  4: {name: "Thursday", abbreviated: "THU"}
  5: {name: "Friday", abbreviated: "FRI"}
  6: {name: "Saturday", abbreviated: "SAT"}
}
*/

objectForeach

Loop into object properties sending key-value pairs to the predicate function.

const groupedByDayOfWeek = {
  monday: {
    detectedPeople: 754,
    inDeniedList: 0
  },
  tuesday: {
    detectedPeople: 548,
    inDeniedList: 2
  }
}

objectForeach(groupedByDayOfWeek, (detectionInfo, dayOfWeek) => {
  if (detectionInfo.inDeniedList > 0) {
    // Do something
  }
})

objectToArrayMap

Convert object properties into array items using the predicate function.

const groupedByDayOfWeek = {
  monday: {
    detectedPeople: 754,
    inDeniedList: 0
  },
  tuesday: {
    detectedPeople: 548,
    inDeniedList: 2
  }
}

const dataArray = objectToArrayMap(groupedByDayOfWeek, (detectionInfo, dayOfWeek) => ({detectionInfo, dayOfWeek}))
/* dataArray:
[
  {
    "detectionInfo":{
      "detectedPeople":754,
      "inDeniedList":0
    },
    "dayOfWeek":"monday"
  },
  {
    "detectionInfo":{
      "detectedPeople":548,
      "inDeniedList":2
    },
    "dayOfWeek":"tuesday"
  }
]
*/

canBeNumber

Verify if can be converted to a Number.

console.log(canBeNumber('1')) // true
console.log(canBeNumber(''), Number('')) // true, 0
console.log(canBeNumber('Not a number')) // false

deleteKeys

Iterate object properties and delete them if they match the predicate.

// For example, an object from a user page edit form that you want to delete the empty properties to not send them to backend api
const userForm = {
  username: '[email protected]',
  password: undefined, // the user did not change the password and the backend do not accept an empty value for this field

  // Other examples
  emptyStringPassword: '',
  emptyObject: {},
  nullValueProp: null
}

deleteKeys(userForm, value => value === undefined)
console.log(userForm) // { username: '[email protected]', emptyStringPassword: '', emptyObject: {}, nullValueProp: null }

// You can use isEmpty to reach undefined, null, empty strings '' and empty objects {}:
deleteKeys(userForm, value => isEmpty(value))
console.log(userForm) // { username: '[email protected]' }

// And them send the put request
const response = await axios.put(userFormApiUrl, { data: userForm })

deleteKeysRecursive

Iterate object properties and delete them recursively if they match the predicate.

// Using the same example in deleteKeys(). Take a look at it first.

const userForm = {
  username: '[email protected]',
  password: undefined, // the user did not change the password and the backend do not accept an empty value for this field

  // Will also delete this entire prop (unlike deleteEmptyKeys())
  objectWithEmptyProps: {
    emptyProp: ''
  }
}

deleteKeysRecursive(userForm, value => isEmpty(value))

console.log(userForm) // { username: '[email protected]' }

mapAsync

Generate a new array by iterate through items and using a async predicate function modify them. The return will be a Promise.

Changes

Version 1.0.0

  • Initial release functions

Version 1.1.0

  • Add deleteKeys function

  • Add deleteKeysRecursive function

Breaking changes!

  1. Fix deleteEmptyKeys logic to delete undefined, null, empty strings '' and empty objects {} (before the function was just undefined and empty objects {}).

  2. Fix deleteEmptyKeysRecursive logic to delete undefined, null, empty strings '' and empty objects {} (before the function was just undefined and empty objects {}).

Version 1.2.0

Breaking changes!

  1. Removed deprecated deleteEmptyKeys function (deleteKeys can be used instead)

  2. Removed deprecated deleteEmptyKeysRecursive function (deleteKeysRecursive can be used instead)

TODOS:

  • Fix some typescript typings

  • Add unity tests