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

@hyunbinseo/tools

v0.3.5

Published

Fully typed JavaScript utilities with ESM and CJS support

Downloads

36

Readme

Tools by Hyunbin

Fully typed JavaScript utilities with ESM and CJS support. Module List

Usage

Node.js

npm i @hyunbinseo/tools
pnpm i @hyunbinseo/tools
// Reference the following section for the full module list.
import { dateToSafeISOString, generatePINString } from '@hyunbinseo/tools';

Browser

<script type="module">
  // Reference the following section for the full module list.
  // The major version number MUST be specified in the pathname.
  import {} from 'https://cdn.jsdelivr.net/npm/@hyunbinseo/[email protected]/dist/index.js';
</script>

Modules

Date to ISO String with Timezone

Returns a YYYY-MM-DDThh:mm:ss+hh:mm ISO 8601 string. (date and time with the offset)

const date = new Date('2024-05-26T00:00:00.000Z');

// 2024-05-26T08:45:00+08:45
dateToISOStringWithOffset(date, '+08:45');
dateToISOStringWithOffset(date, -525);

Date to Day of the Week with Timezone

Returns a number where 0 represents Sunday.

const date = new Date('2024-05-26T11:00:00Z');
dateToDayWithOffset(date, '-12:00'); // 6 — 5/25, Saturday
dateToDayWithOffset(date, '+00:00'); // 0 — 5/26, Sunday
dateToDayWithOffset(date, '+14:00'); // 1 — 5/27, Monday

Date to Safe ISO String

Returns a timestamp string that can be safely used in filename, directory name, etc.

dateToSafeISOString(); // Uses the current time (e.g. 20240402T020408.248Z)
dateToSafeISOString(new Date('2024-05-26T00:00:00+09:00')); // 20240525T150000.000Z

// The outputted string CANNOT be used in JavaScript.
new Date('20240525T150000.000Z'); // Invalid Date

FormData / URLSearchParams to Object

  • Converts kebab-case field names to camelCase.
  • Outputs a typed object with camelCase keys.
const formData = new FormData(); // new URLSearchParams()
formData.append('event-name', 'Touch Grass');
formData.append('day-index', '0');
formData.append('day-index', '6');

formDataToObject(formData, {
  // kebab-case field names are converted to camelCase.
  get: [
    'event-name', // becomes `eventName` in the object.
  ],
  getAll: [
    // field name and its plural version.
    // becomes `dayIndexes` in the object.
    ['day-index', 'day-indexes'],
  ],
});
{ "eventName": "Touch Grass", "dayIndexes": ["0", "6"] }
type ReturnType = //
  Record<'eventName', FormDataEntryValue | null> &
    Record<'dayIndexes', FormDataEntryValue[] | null>;

The output type can be narrowed using Valibot or other schema libraries.

// { eventName: string; dayIndexes: number[] };
const formObject = parse(fSchema, fObject);
import { formDataToObject } from '@hyunbinseo/tools';
import type { GenericSchema } from 'valibot';
import { array, integer, object, parse, pipe, string, transform } from 'valibot';

const formData = new FormData();
formData.append('day-index', '0');
formData.append('day-index', '6');

// { dayIndexes: FormDataEntryValue[] | null }
const fObject = formDataToObject(formData, {
  getAll: [['day-index', 'day-indexes']],
});

const fSchema = object({
  dayIndexes: array(pipe(string(), transform(Number), integer())),
}) satisfies GenericSchema<typeof fObject, unknown>;
// Ensures that the `dayIndexes` key exists in the object schema.

// { dayIndexes: number[] };
const formObject = parse(fSchema, fObject);

Generate PIN String

Returns a truly random number string using the Crypto.getRandomValues() method.

generatePINString(); // e.g. 270136
generatePINString(8); // e.g. 39534786

To Readonly Array / Map / Set

ReadonlyArray, ReadonlyMap and ReadonlySet types restrict write methods.

// ReadonlyMap<number, number>
const readonlyMap = toReadonly(new Map([[3, 26]]));
readonlyMap.set(3, 27); // Property 'set' does not exist

// ReadonlySet<number | boolean>
const readonlySet = toReadonly(new Set([5, 26, true]));
readonlySet.add(false); // Property 'add' does not exist

// readonly number[]
const readonlyArray = toReadonly([3, 5, 26]);
readonlyArray.push(27); // Property 'push' does not exist

// Readonly<{ year: number }>
const readonlyRecord = toReadonly({ year: 2017 });
// Cannot assign to 'year' because it is a read-only property.
readonlyRecord['year'] = 2024;

Deep-NonNullable Record

const review: { rating?: number } = {};

if (!review.rating) throw new Error();
review; // { rating?: number }
review.rating; // number

if (!hasNonNullableValues(review, ['rating'])) throw new Error();
review; // { rating: number }