dayjs-core
v0.1.0
Published
A tiny, learning-project clone of Day.js — chainable date manipulation with a plugin system.
Readme
dayjs-clone
A tiny learning-project clone of Day.js: a chainable, mostly-immutable date library with a plugin system for extending core functionality.
Why this structure?
src/core.ts— theMyDateclass. Every manipulation method (add,subtract,startOf) returns a new instance instead of mutatingthis. This is the single biggest design decision in a date library — get it wrong and every consumer of your library will eventually get bitten by a shared-reference bug.src/plugin-system.ts— theextend()function. Plugins receive theMyDateclass itself and patch its prototype. This keepscore.tssmall while letting you ship optional features (relative time, timezones, custom parsing) as separate, tree-shakeable modules — exactly how Day.js keeps its core bundle at ~2KB.src/plugins/relativeTime.ts— an example plugin implementing.fromNow()(e.g. "3 hours ago"), showing the plugin pattern end to end, including how to use TypeScript'sdeclare moduleto type-augment the core class from a plugin file.
Usage
import dayjsClone, { extend } from 'dayjs-clone';
import relativeTime from 'dayjs-clone/plugins/relativeTime';
const now = dayjsClone();
const nextWeek = now.add(7, 'day');
console.log(nextWeek.format('YYYY-MM-DD'));
extend(relativeTime);
console.log(now.subtract(2, 'hour').fromNow()); // "2 hours ago"Scripts
| Command | What it does |
|-----------------------|------------------------------------------------|
| npm install | Install dependencies |
| npm run test | Run the test suite once |
| npm run test:watch | Run tests in watch mode |
| npm run build | Bundle to dist/ (CJS + ESM + .d.ts) |
| npm run typecheck | Type-check without emitting files |
| npm run dev | Watch mode build |
Where to go next
- Add more units to
format()— day-of-week names (ddd), 12-hour clock (hh A), millisecond padding. - Add a
diff()method — compute the difference between two dates in a given unit. - Add a locale plugin — swap month/weekday names based on a locale
object, like Day.js's
dayjs/locale/*files. - Add a UTC plugin — right now all getters use local time. A
utc()plugin that swaps the internal getters is a good exercise in how much timezone handling complicates a "simple" date library. - Set up CI — GitHub Actions workflow running
npm testandnpm run typecheckon every push. - Publish to npm — once you're happy with the API,
npm publish.
