fuji-moto
v1.0.0
Published
A lightweight utility toolkit for everyday developer problems
Downloads
15
Maintainers
Readme
fuji-moto
A lightweight utility toolkit for everyday developer problems. Built with TypeScript, zero-config, and no heavy dependencies.
Installation
npm install fuji-motoUsage
import {
// Safe Utilities
safeGet,
isDefined,
// Async Utilities
tryOrDefault,
retry,
// Formatting Utilities
formatNGN,
normalizePhone,
// Utility Helpers
chunk,
debounce,
throttle,
} from "fuji-moto";API Reference
Safe Utilities
safeGet(obj, path, defaultValue)
Safely gets a nested property from an object using a dot-notation path.
const user = { profile: { name: "John", age: 30 } };
safeGet(user, "profile.name", "Unknown"); // "John"
safeGet(user, "profile.email", "N/A"); // "N/A"
safeGet(user, "invalid.path", null); // nullisDefined(value)
Checks if a value is defined (not null and not undefined).
isDefined(42); // true
isDefined("hello"); // true
isDefined(null); // false
isDefined(undefined); // falseAsync Utilities
tryOrDefault(asyncFn, fallback)
Executes an async function and returns its result, or a fallback value if it throws.
const result = await tryOrDefault(
async () => {
const data = await fetch("/api/data");
return data.json();
},
{ error: true }
);
// Returns the fetched data or { error: true } if fetch failsretry(asyncFn, options)
Retries an async function a specified number of times with a delay between attempts.
const result = await retry(
async () => {
return await fetchData();
},
{ retries: 5, delay: 2000 } // Retry 5 times with 2 second delay
);Options:
retries(default:3) - Number of retry attemptsdelay(default:1000) - Delay in milliseconds between retries
Formatting Utilities
formatNGN(amount)
Formats a number as Nigerian Naira currency.
formatNGN(1234.56); // "₦1,234.56"
formatNGN(1000000); // "₦1,000,000.00"
formatNGN("500.5"); // "₦500.50"normalizePhone(phone)
Normalizes a Nigerian phone number to the +234 format.
normalizePhone("08012345678"); // "+2348012345678"
normalizePhone("2348012345678"); // "+2348012345678"
normalizePhone("+2348012345678"); // "+2348012345678"
normalizePhone("8012345678"); // "+2348012345678"
normalizePhone("080 1234 5678"); // "+2348012345678" (handles spaces)Utility Helpers
chunk(array, size)
Splits an array into chunks of a specified size.
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4], 2); // [[1, 2], [3, 4]]
chunk([1, 2, 3], 1); // [[1], [2], [3]]debounce(fn, delay)
Creates a debounced function that delays invoking the function until after the specified delay has passed since the last invocation.
const debouncedSearch = debounce((query: string) => {
console.log("Searching:", query);
}, 300);
// Multiple rapid calls will only execute once after 300ms of inactivity
debouncedSearch("hello");
debouncedSearch("world"); // Only this will execute after 300msthrottle(fn, delay)
Creates a throttled function that invokes the function at most once per specified delay period.
const throttledScroll = throttle(() => {
console.log("Scrolled");
}, 100);
// Multiple rapid calls will execute at most once per 100ms
throttledScroll();
throttledScroll();
throttledScroll();Development
# Build the project
npm run build
# Run tests
npm testLicense
MIT
Publishing Guide
Step 1: Check Package Name Availability
Before publishing, verify that the package name fuji-moto is available on npm:
npm view fuji-motoIf the package doesn't exist, you'll see a 404 error, which means the name is available. If it exists, you'll need to choose a different name and update it in package.json.
Step 2: Log in to npm
If you don't have an npm account, create one at npmjs.com.
Then log in via the command line:
npm loginYou'll be prompted for:
- Username
- Password
- Email address
- One-time password (if you have 2FA enabled)
Verify you're logged in:
npm whoamiStep 3: Publish the Package
Make sure you've built the project:
npm run buildThen publish the package publicly:
npm publish --access publicNote: If this is your first time publishing a scoped package (packages starting with @), you'll need to use --access public. Since fuji-moto is unscoped, you can simply use:
npm publishThe prepublishOnly script in package.json will automatically run npm run build before publishing, ensuring the latest code is compiled.
Step 4: Verify Installation
After publishing, verify the package can be installed:
# In a different directory or project
npm install fuji-motoThen test the import:
import { safeGet, formatNGN } from "fuji-moto";
console.log(formatNGN(1000)); // Should output: ₦1,000.00Updating the Package
When you make changes and want to publish a new version:
- Update the version in
package.json(or usenpm version patch|minor|major) - Run
npm publishagain
# Automatically bump version and publish
npm version patch # 1.0.0 -> 1.0.1
npm publishImportant Notes
- The
filesfield inpackage.jsonensures only thedistdirectory is published - The
prepublishOnlyscript ensures the package is built before publishing - Make sure all tests pass before publishing
- Consider adding a
.npmignorefile if you need to exclude additional files
