Introducing `divider`: A Handy Utility to Split Strings and Arrays ✂️
Hey folks!
Lately I spend more time arguing with AI than with humans… and honestly, AI wins most of the time 🤖😂
Anyway, I’m @nyaomaru, a frontend engineer!
We all split strings daily, right?
Most of the time, split()
is enough. But what about those cases where things get tricky?
- Multiple delimiters in the same string
- Cutting by fixed positions
- Directly handling
string[]
without extra loops
That’s when split
+ map
+ regex
+ substring
turns into a mess.
So I built @nyaomaru/divider: a lightweight utility that lets you divide by delimiters or indexes, and it works on arrays too.
TL;DR
-
split
is fine for simple cases, but becomes verbose with multiple delimiters, fixed chunks, or arrays. -
divider
gives you one-liners:divider(input, ...separatorsOrIndexes, options?)
. - Comes with helpers (
dividerFirst
,dividerLast
,dividerLoop
) and presets (emailDivider
,csvDivider
,pathDivider
).
Example:
divider('a b,c', ' ', ',');
// ['a', 'b', 'c']
divider(['TOK12340', 'OSA98761'], 3, 7);
// [["TOK","1234","0"], ["OSA","9876","1"]]
🚀 Install
pnpm add @nyaomaru/divider
# or npm i / yarn add
import { divider } from '@nyaomaru/divider';
Use Case 1: Fixed-length Substrings Without Boilerplate
Say you want to format ISBN codes.
Normally you’d end up chaining substring()
calls:
const toISBN = (code: string): string => {
if (code.length !== 10 && code.length !== 13) return '';
if (code.length === 10) {
const countryCode = code.substring(0, 1);
const publisherCode = code.substring(1, 3);
const bookCode = code.substring(3, 9);
const checkDigit = code.substring(9);
return `ISBN${countryCode}-${publisherCode}-${bookCode}-${checkDigit}`;
}
const isbnPrefix = code.substring(0, 3);
const countryCode = code.substring(3, 4);
const publisherCode = code.substring(4, 6);
const bookCode = code.substring(6, 12);
const checkDigit = code.substring(12);
return `ISBN${isbnPrefix}-${countryCode}-${publisherCode}-${bookCode}-${checkDigit}`;
};
With divider
, the intent is much clearer:
import { divider } from '@nyaomaru/divider';
const toISBN = (code: string): string => {
if (code.length === 10) {
const [countryCode, publisherCode, bookCode, checkDigit] = divider(
code,
1,
3,
9
);
return `ISBN${countryCode}-${publisherCode}-${bookCode}-${checkDigit}`;
}
const [isbnPrefix, countryCode, publisherCode, bookCode, checkDigit] =
divider(code, 3, 4, 6, 12);
return `ISBN${isbnPrefix}-${countryCode}-${publisherCode}-${bookCode}-${checkDigit}`;
};
👉 Easy to review at a glance.
Use Case 2: Mixed Delimiters in Logs
Logs often contain spaces, commas, and tabs together:
const log = 'ERRORt2025-09-11, UserID:1234';
// regex split
log.split(/[s,]+/);
// ["ERROR", "2025-09-11", "UserID:1234"]
Readable? Not really.
With divider
:
import { divider } from '@nyaomaru/divider';
divider(log, ' ', ',', 't');
// ["ERROR", "2025-09-11", "UserID:1234"]
Much simpler.
Use Case 3: Fixed-length Records in Arrays
Financial/banking systems often rely on fixed-length codes:
const records = ['TOK12340', 'OSA98761'];
const parsed1 = records.map((r) => [
r.substring(0, 3),
r.substring(3, 7),
r.substring(7),
]);
// [["TOK","1234","0"], ["OSA","9876","1"]]
With divider
:
import { divider } from '@nyaomaru/divider';
const parsed2 = divider(records, 3, 7);
// [["TOK","1234","0"], ["OSA","9876","1"]]
One-liner, done.
Use Case 4: Splitting Numbers and Letters
Parsing alphanumeric strings:
const str = 'abc123def456';
// regex gets messy
str.split(/(d+)/).filter(Boolean);
// ["abc","123","def","456"]
With divider
:
import { dividerNumberString } from '@nyaomaru/divider';
dividerNumberString(str);
// ["abc","123","def","456"]
📝 Summary
If split
is a cutter, divider
is a multi-tool.
Especially for slightly complex string manipulations, it helps you keep code clean, readable, and maintainable.
It also works directly on string[]
, supports presets, and stays lightweight.
📋 Copy-paste Recipes
CSV
import { divider, csvDivider } from '@nyaomaru/divider';
const lines = ['Alice,24,Engineer,Tokyo', 'Bob,30,Designer,Osaka'];
divider(lines, ',');
// [["Alice","24","Engineer","Tokyo"], ["Bob","30","Designer","Osaka"]]
csvDivider(lines);
// [["Alice","24","Engineer","Tokyo"], ["Bob","30","Designer","Osaka"]]
Email
import { emailDivider } from '@nyaomaru/divider';
const [local, domain] = emailDivider('nyao@example.com');
// ["nyao", "example.com"]
emailDivider('nyao@example.com', { splitTLD: true });
// ['nyao', 'example', 'com']
Path
import { pathDivider } from '@nyaomaru/divider';
pathDivider('/usr/local/bin');
// ['usr', 'local', 'bin']
pathDivider('foo|bar/baz');
// ['foo', 'bar', 'baz']
Other utilities
import { dividerLast, dividerLoop } from '@nyaomaru/divider';
dividerLast('hello world', ' ');
// 'world'
dividerLoop('abcdefghij', 3);
// ['abc', 'def', 'ghi', 'j']
dividerLoop(['hello', 'world'], 2, { flatten: true });
// ['he', 'll', 'ow', 'or', 'ld']
✨ Links & Contribute
PRs and Issues welcome — in English or Japanese or Dutch 😺
Next Up
Next, I’ll switch gears a bit.
Instead of another utility, I want to talk about technical debt — that pile of “we’ll fix it later” decisions that always comes back to bite us.
The article will cover:
- What “technical debt” actually means in real-world projects
- Why those little “temporary hacks” keep compounding interest
- How teams can repay debt incrementally (without betting on a risky full rewrite)
It’ll be a practical, down-to-earth deep dive, written from everyday development experience rather than just theory.
Stay tuned — I hope it helps your team reduce those “just for now” moments! 🧹
✂️ That’s the gist!
divider
is small but handy — especially when split()
alone starts to feel clunky.
I’d love to hear how you use it in your projects, or what edge cases you run into.
Drop a comment with your thoughts, or open an Issue/PR on GitHub if you’ve got ideas.
Feedback is always welcome! 🙌