@hyunbinseo/tools
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

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/tools@0.3/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 to Object with Types

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

formDataToObject(formData, {
  // All field names should be in kebab-case.
  get: ['event-name'],
  getAll: [
    [
      'day-index', // field name in kebab-case
      'day-indexes', // and its plural version
    ],
  ],
});

// { eventName: 'Touch Grass', dayIndexes: ['0', '6'] };
type ReturnType = //
  Record<'eventName', FormDataEntryValue | null> &
    Record<'dayIndexes', FormDataEntryValue[] | null>;

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 Map | Set | Record

ReadonlyMap and ReadonlySet types restrict write methods. (e.g. set, add)

// ReadonlyMap<number, number>
const readonlyMap = toReadonly(new Map([[1, 30]]));
readonlyMap.set(1, 31); // 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<{ year: number }>;
const readonlyRecord = toReadonly({ year: 2017 });
// Cannot assign to 'year' because it is a read-only property.
readonlyRecord['year'] = 2024;

Dependents (0)

Package Sidebar

Install

npm i @hyunbinseo/tools

Weekly Downloads

8

Version

0.3.2

License

MIT

Unpacked Size

15.3 kB

Total Files

7

Last publish

Collaborators

  • hyunbinseo