@handijitcom/tr 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ All notable changes to this package are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and the package follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [1.0.0] - 2026-11-27
11
+
12
+ First release. Written from scratch; it does not share code or API with the
13
+ unpublished `@handijit/tr`.
14
+
15
+ ### Added
16
+
17
+ - Text: `slugify`, `foldTurkish`, `toLowerTr`, `toUpperTr`, `compareTr`.
18
+ - Money in integer kuruş: `parseLira`, `formatLira`, `liraInWords`.
19
+ - VAT: `addVat`, `removeVat`, `vatOf` (bigint arithmetic, half away from zero).
20
+ - Validators and formatters: `isTckn`, `isVkn`, `isIban`, `formatIban`,
21
+ `normalizePhone`, `isMobilePhone`, `formatPhone`.
22
+ - Dates: `formatDateTr`, `parseDateTr` (`dd.MM.yyyy`, Türkiye time).
23
+ - Provinces: `PROVINCES` (81 entries with plate codes), `provinceByPlate`.
24
+ - Types: `Kurus`, `Province`, `SlugifyOptions`, `FormatLiraOptions`,
25
+ `VatOfOptions`.
26
+
27
+ [Unreleased]: https://github.com/handijit/tr/compare/v1.0.0...HEAD
28
+ [1.0.0]: https://github.com/handijit/tr/releases/tag/v1.0.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ahmet İlhan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,419 @@
1
- # Temporary Holding Version
1
+ # @handijitcom/tr
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ <p align="center">
4
+ <a href="https://www.npmjs.com/package/@handijitcom/tr"><img alt="npm version" src="https://img.shields.io/npm/v/@handijitcom/tr.svg" /></a>
5
+ <a href="https://www.npmjs.com/package/@handijitcom/tr"><img alt="npm downloads" src="https://img.shields.io/npm/dy/@handijitcom/tr" /></a>
6
+ <a href="./LICENSE"><img alt="MIT License" src="https://img.shields.io/badge/license-MIT-blue" /></a>
7
+ </p>
8
+
9
+ Dependency-free helpers for software built for Türkiye: Turkish-aware slugs,
10
+ search folding, casing and sorting; lira amounts kept in integer kuruş (parse,
11
+ format, spell out, VAT); TCKN, VKN, IBAN and phone validation; `dd.MM.yyyy`
12
+ dates; and the 81 provinces with their plate codes.
13
+
14
+ - **21 functions + 1 data table, 0 runtime dependencies.**
15
+ - **3.58 kB gzip** for the whole minified browser bundle; tree-shakeable
16
+ (`"sideEffects": false`).
17
+ - **No floats in money.** Amounts are integer kuruş; arithmetic runs on
18
+ `bigint`, and every function accepts a `bigint` for amounts beyond
19
+ `Number.MAX_SAFE_INTEGER`.
20
+ - **Locale-independent.** `I → ı` and `İ → i` are handled by the package, not by
21
+ the runtime's default locale.
22
+ - **ESM, CommonJS and a `<script>` build** (global `handijitTr`), each with its
23
+ own type declarations.
24
+ - **320 tests, 100% coverage** of statements, branches, functions and lines,
25
+ enforced in CI.
26
+
27
+ ---
28
+
29
+ ## Table of contents
30
+
31
+ - [Requirements](#requirements)
32
+ - [Installation](#installation)
33
+ - [Quick start](#quick-start)
34
+ - [When to use it](#when-to-use-it)
35
+ - [API reference](#api-reference)
36
+ - [Text](#text) — `slugify`, `foldTurkish`, `toLowerTr`, `toUpperTr`, `compareTr`
37
+ - [Money](#money) — `parseLira`, `formatLira`, `liraInWords`
38
+ - [VAT](#vat) — `addVat`, `removeVat`, `vatOf`
39
+ - [Validation](#validation) — `isTckn`, `isVkn`, `isIban`, `formatIban`, `normalizePhone`, `isMobilePhone`, `formatPhone`
40
+ - [Dates](#dates) — `formatDateTr`, `parseDateTr`
41
+ - [Provinces](#provinces) — `PROVINCES`, `provinceByPlate`
42
+ - [Semantics](#semantics)
43
+ - [Performance](#performance)
44
+ - [Bundle size](#bundle-size)
45
+ - [Contributing](#contributing)
46
+ - [Releasing](#releasing)
47
+ - [License](#license)
48
+
49
+ ## Requirements
50
+
51
+ - Node.js 24 or newer, or any ES2022 browser.
52
+ - `compareTr` uses `Intl.Collator("tr")`; Node.js ships the Turkish locale data
53
+ by default, as do all evergreen browsers.
54
+
55
+ ## Installation
56
+
57
+ ```bash
58
+ npm install @handijitcom/tr
59
+ # or
60
+ yarn add @handijitcom/tr
61
+ # or
62
+ pnpm add @handijitcom/tr
63
+ ```
64
+
65
+ ## Quick start
66
+
67
+ ```ts
68
+ import {
69
+ addVat,
70
+ formatLira,
71
+ isTckn,
72
+ normalizePhone,
73
+ parseLira,
74
+ slugify,
75
+ } from "@handijitcom/tr";
76
+
77
+ slugify("Ön Fren Balatası"); // "on-fren-balatasi" → /parca/on-fren-balatasi
78
+
79
+ const net = parseLira("1.234,56"); // 123456 (kuruş) or undefined
80
+ if (net !== undefined) formatLira(addVat(net, 20)); // "₺1.481,47"
81
+
82
+ isTckn("10000000146"); // true
83
+ normalizePhone("0 (532) 123 45 67"); // "+905321234567"
84
+ ```
85
+
86
+ ## When to use it
87
+
88
+ Use it when a product targets Turkish users and you need the small,
89
+ error-prone pieces every such project rewrites: URLs from Turkish titles, a
90
+ search that matches "ışık" with "ISIK", prices that never drift by a kuruş,
91
+ and form validation for identity, tax and bank numbers.
92
+
93
+ Not a fit for: currencies other than the lira, other locales, or full
94
+ date/time handling (time zones, durations). Districts (ilçe) are out of scope.
95
+
96
+ ## API reference
97
+
98
+ Two input policies run through the whole package:
99
+
100
+ - **Validators, parsers and phone/IBAN formatters accept `unknown` and never
101
+ throw.** Anything they cannot read gives `false` or `undefined`.
102
+ - **Text, money and date functions throw on programmer errors**: a non-string
103
+ text is a `TypeError`; a fractional, `NaN`, infinite or unsafe kuruş amount, a
104
+ negative rate or an invalid date is a `RangeError`. Messages name the
105
+ function, e.g. `formatLira: expected a safe integer amount of kuruş, received 1.5`.
106
+
107
+ ### Text
108
+
109
+ #### `slugify(text, options?)`
110
+
111
+ ```ts
112
+ slugify(text: string, options?: SlugifyOptions): string
113
+ ```
114
+
115
+ Transliterates Turkish letters (`ç ğ ı İ ö ş ü` and `â î û`; other accents
116
+ such as `é` too), lower-cases, turns every run of other characters into a single
117
+ `-` and trims dashes at both ends.
118
+
119
+ | Option | Type | Default | Description |
120
+ | ----------- | -------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
121
+ | `maxLength` | `number` | no limit | Longest slug; a longer one is cut at the last word boundary that fits. A single word longer than the limit is cut hard. `NaN`, non-numbers and values below 1 mean no limit; fractions are floored. |
122
+
123
+ ```ts
124
+ slugify("Kâğıt Havlu (12'li)"); // "kagit-havlu-12-li"
125
+ slugify("Ön Fren Balatası", { maxLength: 10 }); // "on-fren"
126
+ slugify("!!!"); // ""
127
+ ```
128
+
129
+ #### `foldTurkish(text)`
130
+
131
+ ```ts
132
+ foldTurkish(text: string): string
133
+ ```
134
+
135
+ Search normalization: Turkish lower-casing, then ASCII folding, then every
136
+ whitespace run collapsed to one space and trimmed. Punctuation is kept.
137
+
138
+ ```ts
139
+ foldTurkish(" IŞIK Çağrı "); // "isik cagri"
140
+ foldTurkish("İSTANBUL") === foldTurkish("istanbul"); // true
141
+ ```
142
+
143
+ #### `toLowerTr(text)` / `toUpperTr(text)`
144
+
145
+ ```ts
146
+ toLowerTr(text: string): string
147
+ toUpperTr(text: string): string
148
+ ```
149
+
150
+ Case conversion with Turkish rules in any runtime locale.
151
+
152
+ ```ts
153
+ toLowerTr("IĞDIR"); // "ığdır"
154
+ toLowerTr("İZMİR"); // "izmir"
155
+ toUpperTr("izmir"); // "İZMİR"
156
+ ```
157
+
158
+ #### `compareTr(a, b)`
159
+
160
+ ```ts
161
+ compareTr(a: string, b: string): number
162
+ ```
163
+
164
+ Turkish alphabetical order through `Intl.Collator("tr")` (`c < ç < d`,
165
+ `h < ı < i`). Returns a negative number, zero or a positive number; pass it to
166
+ `sort`.
167
+
168
+ ```ts
169
+ ["Zeynep", "Çiğdem", "Ceren"].sort(compareTr); // ["Ceren", "Çiğdem", "Zeynep"]
170
+ ```
171
+
172
+ ### Money
173
+
174
+ All amounts are **integer kuruş** (`1 lira = 100 kuruş`), typed `Kurus =
175
+ number | bigint`. Numbers must be safe integers; pass a `bigint` for larger
176
+ amounts.
177
+
178
+ #### `parseLira(text)`
179
+
180
+ ```ts
181
+ parseLira(text: unknown): number | undefined
182
+ ```
183
+
184
+ Reads a Turkish-formatted amount: dots group thousands, a comma starts one or
185
+ two kuruş digits. An optional leading `-`, a leading `₺` and a trailing `₺`,
186
+ `TL` or `TRY` are accepted.
187
+
188
+ ```ts
189
+ parseLira("1.234,56"); // 123456
190
+ parseLira("1234,5"); // 123450
191
+ parseLira("₺1.234"); // 123400
192
+ parseLira("12.5"); // undefined (a dot never starts a fraction)
193
+ parseLira("abc"); // undefined
194
+ ```
195
+
196
+ Returns `undefined` for non-strings, malformed text and amounts above
197
+ `Number.MAX_SAFE_INTEGER` kuruş.
198
+
199
+ #### `formatLira(kurus, options?)`
200
+
201
+ ```ts
202
+ formatLira(kurus: Kurus, options?: FormatLiraOptions): string
203
+ ```
204
+
205
+ | Option | Type | Default | Description |
206
+ | ---------- | --------- | ------- | ------------------------------------------------------------------------ |
207
+ | `symbol` | `boolean` | `true` | Prefix `₺`. Only `false` turns it off. |
208
+ | `fraction` | `boolean` | `true` | Show two kuruş digits. `false` rounds half away from zero to whole lira. |
209
+
210
+ ```ts
211
+ formatLira(123456); // "₺1.234,56"
212
+ formatLira(-5); // "-₺0,05"
213
+ formatLira(123456, { symbol: false }); // "1.234,56"
214
+ formatLira(123456, { fraction: false }); // "₺1.235"
215
+ ```
216
+
217
+ #### `liraInWords(kurus)`
218
+
219
+ ```ts
220
+ liraInWords(kurus: Kurus): string
221
+ ```
222
+
223
+ Spells an amount out in Turkish, as on cheques and invoices. Words are
224
+ separated by spaces; negatives start with `eksi`. Up to 999 kentilyon
225
+ (10²¹ − 1) lira; larger amounts throw a `RangeError`.
226
+
227
+ ```ts
228
+ liraInWords(123456); // "bin iki yüz otuz dört lira elli altı kuruş"
229
+ liraInWords(0); // "sıfır lira"
230
+ liraInWords(5); // "beş kuruş"
231
+ ```
232
+
233
+ ### VAT
234
+
235
+ ```ts
236
+ addVat(netKurus: number, ratePercent: number): number // bigint in → bigint out
237
+ removeVat(grossKurus: number, ratePercent: number): number // bigint in → bigint out
238
+ vatOf(kurus: number, ratePercent: number, options?: VatOfOptions): number
239
+ ```
240
+
241
+ `ratePercent` is the rate in percent (`20`, `10`, `1`, `0.5`) with at most two
242
+ decimals; negative, `NaN` or finer rates throw a `RangeError`.
243
+
244
+ | Option (`vatOf`) | Type | Default | Description |
245
+ | ---------------- | ------------------ | ------- | ------------------------------------------------------------------------------------ |
246
+ | `from` | `"net" \| "gross"` | `"net"` | `"net"`: the VAT on top of the amount. `"gross"`: the VAT already inside the amount. |
247
+
248
+ **Rounding.** Every result is rounded to the kuruş **half away from zero**
249
+ (0.5 kuruş → 1, −0.5 → −1), so a refund mirrors its sale.
250
+
251
+ - `addVat(net) = net + round(net × rate)`
252
+ - `removeVat(gross) = round(gross ÷ (1 + rate))`
253
+ - `vatOf(gross, rate, { from: "gross" }) = gross − removeVat(gross, rate)`, so
254
+ net + VAT always equals the gross exactly.
255
+ - Rounding means `addVat(removeVat(g))` can differ from `g` by a kuruş.
256
+
257
+ ```ts
258
+ addVat(10000, 20); // 12000
259
+ addVat(999, 20); // 1199 (199.8 → 200)
260
+ removeVat(1199, 20); // 999 (999.17 → 999)
261
+ vatOf(1199, 20, { from: "gross" }); // 200
262
+ ```
263
+
264
+ ### Validation
265
+
266
+ | Function | Returns | Accepts |
267
+ | ----------------------- | --------------------- | -------------------------------------------------------------- |
268
+ | `isTckn(value)` | `boolean` | 11 digits, first not `0`, both check digits valid. |
269
+ | `isVkn(value)` | `boolean` | 10 digits with a valid check digit. |
270
+ | `isIban(value)` | `boolean` | `TR` + 24 digits, mod-97 valid. Spaces and lower case allowed. |
271
+ | `formatIban(value)` | `string \| undefined` | A valid IBAN, grouped by four. |
272
+ | `normalizePhone(value)` | `string \| undefined` | A Turkish number in E.164 form. |
273
+ | `isMobilePhone(value)` | `boolean` | A normalizable number whose national part starts with `5`. |
274
+ | `formatPhone(value)` | `string \| undefined` | A normalizable number as `0532 123 45 67`. |
275
+
276
+ All take `unknown`; non-strings give `false` / `undefined`. TCKN and VKN are
277
+ strict: no spaces, strings only (a number would lose leading zeros).
278
+
279
+ ```ts
280
+ isTckn("10000000146"); // true
281
+ isVkn("1234567890"); // true
282
+ isIban("TR33 0006 1005 1978 6457 8413 26"); // true
283
+ formatIban("tr330006100519786457841326"); // "TR33 0006 1005 1978 6457 8413 26"
284
+ normalizePhone("0532 123 45 67"); // "+905321234567"
285
+ formatPhone("+905321234567"); // "0532 123 45 67"
286
+ ```
287
+
288
+ `normalizePhone` ignores spaces, dashes, dots and parentheses, then accepts an
289
+ optional `+90`, `0090`, `90` or `0` prefix followed by a 10-digit national
290
+ number starting with `2`–`5` or `8` (landlines, mobiles, `850` numbers).
291
+
292
+ ### Dates
293
+
294
+ ```ts
295
+ formatDateTr(date: Date | number): string // "dd.MM.yyyy"
296
+ parseDateTr(text: unknown): Date | undefined
297
+ ```
298
+
299
+ Both work in **Türkiye time, a fixed UTC+03:00** (the country's offset all
300
+ year since September 2016), so a server in UTC and a browser anywhere print the
301
+ same day. `parseDateTr` returns the start of that day in Türkiye.
302
+
303
+ ```ts
304
+ formatDateTr(new Date("2026-11-26T21:00:00Z")); // "27.11.2026"
305
+ parseDateTr("27.11.2026"); // 2026-11-26T21:00:00.000Z
306
+ parseDateTr("7/3/2026"); // 7 March 2026
307
+ parseDateTr("31.04.2026"); // undefined
308
+ ```
309
+
310
+ `parseDateTr` accepts `d.M.yyyy` or `dd.MM.yyyy` with `.` or `/` (the same
311
+ separator twice) and rejects impossible days. `formatDateTr` throws a
312
+ `TypeError` for anything but a `Date` or a number and a `RangeError` for an
313
+ invalid date or a year outside 0–9999.
314
+
315
+ Known limit: for instants before September 2016 that fall in a winter
316
+ (UTC+02:00) evening between 21:00 and 22:00 UTC, the fixed offset reads the next
317
+ day.
318
+
319
+ ### Provinces
320
+
321
+ ```ts
322
+ PROVINCES: readonly Province[] // { plate: number; name: string }, ordered by plate
323
+ provinceByPlate(plate: number | string): Province | undefined
324
+ ```
325
+
326
+ ```ts
327
+ PROVINCES.length; // 81
328
+ provinceByPlate(34); // { plate: 34, name: "İstanbul" }
329
+ provinceByPlate("06"); // { plate: 6, name: "Ankara" }
330
+ provinceByPlate(82); // undefined
331
+ ```
332
+
333
+ The table and its entries are frozen. Sort names with `compareTr`.
334
+
335
+ ## Semantics
336
+
337
+ | Input | Behaviour |
338
+ | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
339
+ | `""` | Text functions return `""`; parsers return `undefined`; validators `false`. |
340
+ | Non-string text | `slugify`, `foldTurkish`, `toLowerTr`, `toUpperTr`, `compareTr` throw `TypeError`; parsers / validators return `undefined` / `false`. |
341
+ | `NaN`, `±Infinity`, `1.5` kuruş | `RangeError` (a kuruş amount must be an integer). |
342
+ | `-0` kuruş | Treated as `0`: `formatLira(-0)` → `"₺0,00"`. |
343
+ | Amount above `Number.MAX_SAFE_INTEGER` | Pass a `bigint`; a number result that would exceed it throws `RangeError`; `parseLira` returns `undefined`. |
344
+ | `"1.23"` / `"12.5"` to `parseLira` | `undefined`: in Turkish a dot only groups thousands. |
345
+ | Invalid `Date` | `formatDateTr` throws `RangeError`. |
346
+
347
+ ## Performance
348
+
349
+ `npm run bench` on the built output (Node v26.11.0 · Apple M5 · darwin/arm64):
350
+
351
+ | Scenario | µs/op |
352
+ | ----------------------------------- | -----: |
353
+ | slugify: product title | 0.774 |
354
+ | slugify: 2.5 kB text, maxLength 80 | 23.680 |
355
+ | foldTurkish: product title | 0.771 |
356
+ | parseLira: "1.234.567,89 TL" | 0.109 |
357
+ | formatLira: 123456789 | 0.056 |
358
+ | liraInWords: 123456789 | 0.344 |
359
+ | addVat: 20% | 0.008 |
360
+ | isTckn | 0.020 |
361
+ | isIban: spaced | 0.233 |
362
+ | normalizePhone: "0 (532) 123 45 67" | 0.097 |
363
+
364
+ ## Bundle size
365
+
366
+ | File | Raw | Gzip | Brotli |
367
+ | -------------------- | -------- | ------- | ------- |
368
+ | `build/index.mjs` | 22.95 kB | 7.75 kB | 6.63 kB |
369
+ | `build/index.cjs` | 23.41 kB | 7.82 kB | 6.69 kB |
370
+ | `build/index.umd.js` | 8.54 kB | 3.58 kB | 3.16 kB |
371
+ | `build/index.d.ts` | 5.40 kB | 2.01 kB | 1.72 kB |
372
+
373
+ The minified browser bundle has a 4 kB gzip budget, enforced by `npm run size`.
374
+
375
+ | Entry | File |
376
+ | -------------------- | ------------------------------------------ |
377
+ | ESM (`import`) | `build/index.mjs` + `build/index.d.mts` |
378
+ | CommonJS (`require`) | `build/index.cjs` + `build/index.d.ts` |
379
+ | `<script>` | `build/index.umd.js` (global `handijitTr`) |
380
+
381
+ ## Contributing
382
+
383
+ ```bash
384
+ npm install
385
+ npm run verify # lint, typecheck, test with coverage, build, size budget, package lint
386
+ npm run bench # measure the built output
387
+ ```
388
+
389
+ 320 tests, enforced at 100% for statements, branches, functions and lines. A
390
+ change to a public function comes with its tests, a README update and a
391
+ CHANGELOG entry.
392
+
393
+ ## Releasing
394
+
395
+ Releases are automatic and happen only from CI:
396
+
397
+ 1. Move the entries under `Unreleased` in [CHANGELOG.md](./CHANGELOG.md) to a
398
+ new version heading.
399
+ 2. Bump the version: `npm version patch` (or `minor` / `major`).
400
+ 3. Merge to `master`.
401
+
402
+ The publish workflow then runs `npm run verify`, smoke-tests the packed tarball
403
+ through both `import` and `require`, publishes to npm with provenance (token in
404
+ the `NPM_ACCESS_TOKEN` repository secret), pushes a `v<version>` tag and creates
405
+ a GitHub Release from the CHANGELOG section. A commit that does not change the
406
+ version is skipped.
407
+
408
+ <!-- handijit:about:start -->
409
+
410
+ ## About Han Dijit
411
+
412
+ `@handijitcom/tr` is built and maintained by **[Han Dijit](https://www.handijit.com)**, a software company serving businesses across Türkiye: web, e-commerce, mobile, artificial intelligence, cyber security and enterprise software projects.
413
+
414
+ [www.handijit.com](https://www.handijit.com) · [All Han Dijit packages on npm](https://www.npmjs.com/org/handijitcom)
415
+ <!-- handijit:about:end -->
416
+
417
+ ## License
418
+
419
+ MIT © [Han Dijit](https://github.com/handijit)