Add More cxjs_*() Functions#113
Conversation
Add cxjs__wordify(). Add cxjs__parsedate(). Add cxjs_datepart(). Add cxjs_datediff(). Add cxjs_dateformat().
Greptile SummaryThis PR adds five JavaScript helper functions to
Confidence Score: 3/5Safe to merge for most practical date ranges; the year 0–99 bug will produce wrong output for any date string whose year is two digits or fewer, and the comma inconsistency in wordify produces subtly wrong English for numbers like 100,001. The shared cxjs__parsedate helper is called by every date function added in this PR. Passing a year in the range 0–99 causes a silent wrong result (e.g., year 50 → 1950) that propagates through cxjs_datepart, cxjs_datediff, and cxjs_dateformat without any error signal. The cxjs_wordify comma-separator issue is a logic inconsistency rather than a crash, but it makes the function output inconsistently formatted depending on whether the hundreds digit is exactly 1. centrallix-os/sys/js/ht_render.js — specifically cxjs__parsedate (lines 586–594) and the separator condition in cxjs_wordify (line 566).
|
| Filename | Overview |
|---|---|
| centrallix-os/sys/js/ht_render.js | Adds five new cxjs_*() helper functions for date/number formatting; the shared cxjs__parsedate helper mishandles years 0–99 (JavaScript Date constructor quirk), and cxjs_wordify has an inconsistent comma-separator condition for chunk === 100. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[cxjs_wordify] -->|returns string| OUT1[English words]
B[cxjs_datepart] --> P[cxjs__parsedate]
C[cxjs_datediff] --> P
D[cxjs_dateformat] --> P
P -->|new Date year,month-1,day| E{year 0-99?}
E -->|yes| WRONG[Wrong: 1900+year]
E -->|no| OK[Correct Date]
OK --> B
OK --> C
OK --> D
Reviews (1): Last reviewed commit: "Add functions." | Re-trigger Greptile
| return new Date( | ||
| parseInt(match[3], 10), | ||
| parseInt(match[1], 10) - 1, | ||
| parseInt(match[2], 10), | ||
| (match[4]) ? parseInt(match[4], 10) : 0, | ||
| (match[5]) ? parseInt(match[5], 10) : 0, | ||
| (match[6]) ? parseInt(match[6], 10) : 0, | ||
| 0 | ||
| ); |
There was a problem hiding this comment.
Year 0–99 interpreted as 1900+year
JavaScript's Date constructor maps a two-argument-style year (0–99) to 1900+year. Any date string with a year in that range — e.g., "1/1/0050" — will be silently parsed as 1950 instead of 50 CE, causing wrong results from every caller (cxjs_datepart, cxjs_datediff, cxjs_dateformat). Use setFullYear after construction to override this behavior: const d = new Date(0, month-1, day, h, m, s, 0); d.setFullYear(year);
| else if (rem > 0) result += digits[rem] + " "; | ||
|
|
||
| result += multiples[multiple]; | ||
| if (chunk > 10 && (chunk%10 !== 0 || chunk > 100) && multiple !== 0) |
There was a problem hiding this comment.
Inconsistent comma separator for chunk === 100
The condition chunk%10 !== 0 || chunk > 100 treats an exact-hundred chunk (e.g., 100) as a "round" group (gets a space) while 200, 300, … (also multiples of 100 with no ones/tens) are considered "complex" and get a comma. This means cxjs_wordify(100001) → "One Hundred Thousand One " (no comma) whereas cxjs_wordify(200001) → "Two Hundred Thousand, One " (with comma), despite both having the same structural complexity. Changing > 100 to >= 100 makes the rule consistent.
| if (chunk > 10 && (chunk%10 !== 0 || chunk > 100) && multiple !== 0) | |
| if (chunk > 10 && (chunk%10 !== 0 || chunk >= 100) && multiple !== 0) |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
This isn't high priority right now. I'll come back to it after I clean up |
cxjs_*() functionscxjs_*() Functions
Several of the
cxjs_*()functions that I added in #112 turned out more complex than I expected. Note that #112 is higher priority than this PR.This PR adds the following additional functions:
cxjs_wordify()cxjs_parsedate()cxjs_datepart()cxjs_datediff()cxjs_dateformat()