addOrdinalSuffix (cardinal → ordinal) and removeOrdinalSuffix (ordinal → cardinal). Both operate on already-Persian word forms; for numeric → words conversion, use Number to Words which can apply the ordinal automatically.
Functions
addOrdinalSuffix(number)
Rules (applied in order)
- Ends with
ی→ appendاُم(leading space + standaloneاُم). - Ends with
سه→ strip last 2 chars, appendسوم. - Otherwise → append
م.
Input
- Must be a string. Anything else throws
TypeError("PersianTools: addOrdinalSuffix - The input must be string"). ""does NOT throw — it falls through rule 3 and returns"م". Validate non-emptiness upstream if needed.
removeOrdinalSuffix(word)
Behaviour
- Falsy input is returned as-is (no throw, no
""coercion). - Generic suffixes are removed:
مین,ام,اُم. - A table of irregular forms is applied (longest matches first) — e.g.
سوم → سه,یازدهم → یازده,دوازدهم → دوازده.
Pitfalls
addOrdinalSuffix("")returns"م"(a single character), not"". Validate upstream.addOrdinalSuffix(null)throws TypeError, doesn’t return"".removeOrdinalSuffix(null)returnsnullunchanged (falsy input passes through).اولis not handled byremoveOrdinalSuffix— it returns"اول"unchanged (no built-in mapping toیک).- Rule coverage is conservative.
addOrdinalSuffix("یک")returns"یک اُم"(rule 1: ends withی), which is grammatical but uncommon; the colloquial"یکم"is not produced. Confirm against your style guide.
Composition with Number to Words
Source
src/modules/addOrdinalSuffix/addOrdinalSuffix.ts, src/modules/removeOrdinalSuffix/index.ts · Tests: test/addOrdinalSuffix.spec.ts, test/removeOrdinalSuffix.spec.ts