just-shorten
v1.0.2
Published
A tiny utility to shorten strings and Ethereum addresses in a readable way.
Maintainers
Readme
Just Shorten
A tiny utility to shorten strings and Ethereum addresses in a readable way.
Installation
npm install just-shorten
# or
yarn add just-shortenUsage
shortenString
Shortens a string by keeping the first and last few characters, inserting a joiner in the middle.
import { shortenString } from "just-shorten";
shortenString("qwertyuiop"); // "qwe...iop"
shortenString("abcdef", 2, "-"); // "ab---ef"
shortenString("hi"); // "hi" (too short, returned as is)shortenAddress
Shortens an Ethereum address (or any string). If the string starts with 0x, it keeps the 0x prefix.
import { shortenAddress } from "just-shorten";
shortenAddress("0x123456789abcdef"); // "0x123...def"
shortenAddress("abcdef"); // "abc...def"
shortenAddress("0x12"); // "0x12" (too short, returned as is)
shortenAddress("0x"); // "0x"Parameters:
- value: string – the string to shorten.
- charsCount: number = 3 – how many characters to keep on each side.
- joiner: string = "." – the string to insert in the middle (repeated 3 times by default).
Edge Cases
- Empty strings are returned as-is.
- Strings shorter than charsCount * 2 are returned as-is.
- Negative or non-finite charsCount will return the original string.
