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

use-postal-jp

v2.0.2

Published

郵便番号を住所に変換するReactカスタムフックです。住所データを内部に持たず、APIで住所変換するため軽量なパッケージになっています。

Downloads

3,787

Readme

use-postal-jp

郵便番号を住所に変換するReactカスタムフックです。住所データを内部に持たず、APIで住所変換するため軽量なパッケージになっています。

NPM codecov

Install

// npm
npm install --save use-postal-jp

// yarn
yarn add use-postal-jp

DEMO

https://use-postal-jp.vercel.app/

Usage

import React, { useEffect, useState } from 'react'
import { usePostalJp } from 'use-postal-jp'

const App = () => {
  const [value, setValue] = useState('')
  const [address, loading, error] = usePostalJp(value, value.length >= 7)

  return (
    <div>
      <input type='text' onChange={(e) => setValue(e.target.value)} />
      {!loading &&
        <>
          <p>prefectureCode: {address.prefectureCode && address.prefectureCode}</p>
          <p>prefecture: {address.prefecture && address.prefecture}</p>
          <p>address1: {address.address1 && address.address1}</p>
          <p>address2: {address.address2 && address.address2}</p>
          <p>address3: {address.address3 && address.address3}</p>
          <p>address4: {address.address4 && address.address4}</p>
          <p>error: {error && error.message}</p>
        </>
      }
    </div>
  )
}

usePostalJp

const [address, loading, error] = usePostalJp(postalCode, ready, { url, parse })

Args

usePostalJp(postalCode, ready, option)

  • postalCode: string (必須)
    • 郵便番号の文字列
    • ハイフンあり('100-0001')、全角('100ー0001') でもOK
  • ready: boolean (必須)
    • true をわたすと、APIリクエストが行われる
  • option: object (任意)

Returns

const [address, loading, error] = usePostalJp(...)

  • address: Address | null
type Adress = {
  prefectureCode: string; // 都道府県コード
  prefecture: string; // 都道府県
  address1: string; // 市区町村
  address2: string; // 市区町村配下
  address3: stging; // 更に詳細情報
  address4: string; // 建物名など
}
  • loading: boolean
    • API通信中は true になる
  • error: Error | null
    • 郵便番号の桁不足や、存在しない郵便番号でAPIが叩かれるなどしたときに、エラーオブジェクトが返却される
    • 正常であれば null

カスタムエンドポイントを使用する場合

// エンドポイントを作成する関数: 前方3桁、後方4桁の半角郵便番号数字が配列で引数に渡される
const url = ([code1, code2]: [string, string]) => `https://your.api.com/api?postal_code=${code1}-${code1}`
// エンドポイントからのレスポンスをパースする関数: 返り値が直接 address となる
const parse = (res: YourResBodyObject) => ({
  prefecture: res[0].prefecture_name,
  city: res[0].city_name,
  town: res[0].town_name
})

// address: { prefecture: string; city: string; town: string } | null
const [address, loading, error] = usePostalJp(postalCode, ready, { url, parse })

住所検索API

本プロジェクトでは madefor/postal-code-api の住所検索APIを使用させていただいております。 オーナー様及びコントリビューターの方々に感謝申し上げます。

License

MIT © aiji42