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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@dataking/functions

v0.0.4

Published

A helper functions packages for our project to use internally. Packages are not supposed to be update regularly, but we try our best to run tests as much as possible.

Readme

@dataking/function

A package of helper curried functions composed with functional approach.

Usage

There isn't any otherways to do it for now. But we keep updating this package

npm install @dataking/functions

Documentation

This document and example of usage are shown below

formDataObject(formData: FormData)

Turn a FormData object to be an JavaScript object. Which should be a string only key-value pair.

Example:

form.html

<form id="form">
    <input name="foo" value="bar" />
</form>

app.js

const form = document.getElementById('form');
formDataObject(new FormData(form)).foo // will get "bar"

format(params: StringKV, format: string): string

A little string format function for composing sentence / string in your application. This function will take 2 arguments which the first one should be an object which the key should be the placeholder in the template, and the value should be the result which will replace the placeholder.

Usage:

const weatherSentance = format({'{temperature}': '30'});
weatuerSentance("It's {temperature}°C‎ today."); // "It's 30°C‎ today."

ifElseValue<Yes, No>(yes: Yes, no: No, boolCallback: any): Yes | No

Accept 3 parameters. The 1st and the 2nd ones could be anything and it's not restricted.

  • If the 3rd parameter:
    • is a callback, it will be run and check if the result is a positive value.
    • is anything else, itself will be checked if it's a positive value.

If a positive value is got, 1st parameter will be returned. Otherwise 2nd value will be returned.

This function is useful when you need to shorten the conditions in a function chain.

Note that this function is curried. Thanks ramda.

const girlFriends = me.getGirlFriendList();
const react = ifElseValue('happy', 'sad');
console.log(`I am ${react(girlFriends.length)}.`); // happy if I have at least one girlfriend. sad otherwise :(

And also the 3rd parameter could be a callback as well

const date = new Date();
const whatToDo = ifElseValue('stay home', 'go to church');
console.log(`I should ${whatToDo(date.getDay.bind(date))} today.`); // (new Date()).getDay() would return 0 if it's Sunday, then go to church ~

instanceOf(type: Function, instance: any): boolean

To check if an object is a prototype / instance of something. Note that for functional purpose, 1st parameter is the class, and the 2nd parameter should be the instance which is going to be checked.

As JavaScript is a Prototype-based language, we may apply the instanceOf(Class, object) syntax. (compare to other language like PHP, the function needs the be run like instanceOf(Class::class, object) to pass the class into the function).

const isString = instanceOf(String);
isString('hello, world'); // true
isString(123); // false

And surely this can be applied to custom class

class Animal { /** ... **/ }
class Cat extends Animal { /** ... **/ }
class Dog extends Animal { /** ... **/ }
const isCat = instanceOf(Cat);
const isDog = instanceOf(Dog);
const isAnimal = instanceOf(Animal);
const cat = new Cat();
const dog = new Dog();

// let's test
isCat(cat); // true
isDog(dog); // true
isDog(cat); // false
isCat(dog); // false
isAnimal(cat); // true
isAnimal(dog); // true

method(callback: function)

Simple call the first argument...That's it.

This is useful for functional programming especially in the situation that you don't really want to split the functional chain.

So you can do something like:

R.pipe(
    // ...... (some operation)
    method,
    // ...... (some other operation)
);

Instead of:

R.pipe(
    // ...... (some operation)
    prop => prop(),
    // ...... (some other operation)
);

notNilThen(callback: Function, instance: any): any

This function will return the result by the callback (1st parameter) injected the 2nd parameter. But only if the 2nd parameter is not nil (either undefined or null). If the 2nd parameter is a nil value, itself will be returned (so null return if it's null, undefined returned if it's undefined).

Note that this function is curried.

Example:

const thanosSnap = item => Math.random() > .5 ? item : null;
const date = thanosSnap(new Date()); // so the variable date will have 50% chance to be null
// applying `notNilThen` function here to avoid the error: "Cannot read property 'getTime' of undefined"
const result = notNilThen(date => date.getTime())(date);

safelyApply(params: ListOfAny, callback: any)

The purpose for this function is for the case when the user isn't so sure the function is actually stored correctly. Accepting the list of params for the 1st argument and the callback as the 2nd argument. Note that this function is curried.

Example:

safelyApply([1,2,3,4,5,6,7], Math.max); // output >>> 7
safelyApply([1,2,3,4,5,6,7], NaN); // output >>> null
safelyApply([1,2,3,4,5,6,7])(undefined); // output >>> null
safelyApply([])(Array) // output >>> []

submitEventData(event: Event)

Fetch the form data from a form submitting event

Example in React:

function SubmitForm(props) {

    const onSubmit = event => console.log(submitEventData(event)); // would log an object >>> { foo: "bar" }

    return (
        <form onSubmit={onSubmit}>
            <input name="foo" value="bar" />
        </form>
    );
}

Or usage with ramda in a functional approach:

const onSubmit = R.pipe(
    submitEventData,
    R.tap(console.log) // log the object out
);

validateEmail(email: string)

Validate a email, return true if the input is a valid email, false otherwise.

validateEmail('[email protected]'); // true
validateEmail('winghim@dataking'); // false
validateEmail('foo.bar'); // false