@arki/string for agents
For @arki/[email protected] · source docs/agent-guide.md
The long form of llms.txt. When they disagree, src/index.ts wins.
The model
- Each concern has its own subpath:
/case,/format,/slug,/base64,/readability, and more. The root@arki/stringre-exports all of them. - Functions are plain and pure.
str(value)from/fluentwraps the same functions in an immutable chain. - Lengths and positions count graphemes (what a reader sees). Native
.lengthcounts UTF-16 units. - Encoding failures are canonical values from
@arki/failure; narrow withisFailure. Other static misuse usesStringError. Wrong types are not checked. - The package depends on
@arki/failureand runs on Node, Bun, Deno, browsers and Hermes.
Task map
Make a URL slug from a title
- Import
slugifyfrom@arki/string/slug. - Pass
{ locale }when the language is not German. The default mapsätoae. - For Chinese or Korean, pass
{ replacements: chinese }from@arki/string/slug-cjk.
import { slugify } from '@arki/string/slug';
if (slugify('Café Guide & Notes') !== 'cafe-guide-and-notes') throw new Error('slug');
if (slugify('ä ö', { locale: 'sv' }) !== 'a-o') throw new Error('locale');Cut text to a budget without breaking it
- Import
truncatefrom@arki/string/format. - The budget includes the ellipsis. Pass
boundary: 'word-before'to back up to a word.
import { truncate } from '@arki/string/format';
const cut = truncate('The quick brown fox', 10, { boundary: 'word-before' });
if (cut !== 'The quick…') throw new Error(cut);Decode base64 from a user
- Call
fromBase64and narrow withisFailurebefore using the text. InvalidBase64(string.invalid_base64) identifies malformed base64;InvalidUtf8(string.invalid_utf8) identifies invalid text bytes.- Breaking:
tryFromBase64aliasesfromBase64; replace null checks and catches with failure narrowing. Fluent codecs also return failures orStringValue.
Fill a template
interpolate('Hi {{ name }}', { name: 'Ada' }).- Choose
missing: 'keep'or'empty'when a value may be absent. The default throwsARKI_STRING_E005.
Score how hard a text is to read
- Call
analyze(text)once from/metrics. - Pass the result to each readability or vocabulary function.
Rules that bite
length('👨👩👧👦')is1. Native.lengthis11. Do not mix them in one calculation.slugifyuses German rules by default:übecomesue.escapeHtmlis not a sanitizer.hashCodeis not cryptographic.interpolatekeys are flat identifiers.{{ a.b }}isARKI_STRING_E004.Boolean(str(''))istrue. Use.isEmpty().- Chinese and Korean slugs need
@arki/string/slug-cjk. - Encoding functions return failures. Static misuse in other APIs still throws
StringError.
Errors
| Code | Meaning | What to do |
|---|---|---|
ARKI_STRING_E001 | An argument is out of range: empty needle, negative length, bad glyph. | Check the argument against the reference. |
string.invalid_utf8 | Text or bytes are not valid UTF-8. | Pass well-formed text, and handle the returned failure. |
string.invalid_base64 | Input is not strict base64. | Fix padding and alphabet, and handle the returned failure. |
ARKI_STRING_E004 | A placeholder is malformed. | Write {{ name }} and close every {{. |
ARKI_STRING_E005 | A placeholder has no value and missing is 'throw'. | Pass the value, or set missing. |
Verify
bun run test
bun run test:docs
bun ../../tooling/library/src/llms.ts --check