nl2time 0.1.1 → 0.3.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.
@@ -8,12 +8,18 @@ const HOUR_MERIDIEM_RE = /^(\d{1,2})(am|pm|a|p)$/;
8
8
  const ORDINAL_RE = /^(\d{1,2})(st|nd|rd|th)$/;
9
9
  const NUMDATE_RE = /^(\d{1,4})([/\-.])(\d{1,2})(?:\2(\d{1,4}))?$/;
10
10
  const NUMBER_RE = /^\d{1,4}$/;
11
- const RAW_TOKEN_RE = /[a-z0-9:/\-.'~\u00bc\u00bd\u00be]+/gi;
11
+ // Latin letters incl. accents (\u00c0-\u024f), CJK ideographs, kana, and
12
+ // fullwidth forms. CJK runs are split into per-character word tokens below.
13
+ const RAW_TOKEN_RE = /[a-z0-9:/\-.'~\u00bc\u00bd\u00be\u00c0-\u024f\u3040-\u30ff\u3400-\u9fff\u3007\uff10-\uff19\uff1a]+/gi;
14
+ const CJK_RE = /[\u3040-\u30ff\u3400-\u9fff\u3007]/;
12
15
  export function tokenize(text) {
13
16
  const tokens = [];
14
17
  for (const m of text.matchAll(RAW_TOKEN_RE)) {
15
18
  const raw = m[0]
16
19
  .toLowerCase()
20
+ // Fullwidth digits/colon → ASCII (Japanese/Chinese text).
21
+ .replace(/[\uff10-\uff19]/g, (c) => String.fromCharCode(c.charCodeAt(0) - 0xfee0))
22
+ .replace(/\uff1a/g, ':')
17
23
  .replace(/^-(?=\d)/, '') // "(Tue)-11:30" → clock token after paren split
18
24
  .replace(/[.]+$/, '')
19
25
  .replace(/[''`]s$/, '') // "fortnight's" → fortnight
@@ -25,6 +31,29 @@ export function tokenize(text) {
25
31
  continue;
26
32
  const start = m.index;
27
33
  const end = start + m[0].length;
34
+ // CJK text has no spaces: emit each CJK character as its own word token,
35
+ // with ASCII runs between them tokenized normally ("昨日の3時" →
36
+ // 昨, 日, の?, 3, 時 — の is outside the token class and acts as a gap).
37
+ if (CJK_RE.test(raw)) {
38
+ let asciiRun = '';
39
+ let cursor = start;
40
+ for (const ch of raw) {
41
+ if (CJK_RE.test(ch)) {
42
+ if (asciiRun) {
43
+ pushToken(tokens, asciiRun, cursor - asciiRun.length, cursor);
44
+ asciiRun = '';
45
+ }
46
+ tokens.push({ type: 'word', value: ch, start: cursor, end: cursor + ch.length });
47
+ }
48
+ else {
49
+ asciiRun += ch;
50
+ }
51
+ cursor += ch.length;
52
+ }
53
+ if (asciiRun)
54
+ pushToken(tokens, asciiRun, cursor - asciiRun.length, cursor);
55
+ continue;
56
+ }
28
57
  // Split dash-joined mixed tokens ("18-dec", "nov-feb", "friday-jun-15")
29
58
  // into parts with explicit "-" connectors so the range machinery sees
30
59
  // them. Pure-numeric joins (11-4, 2014-2018) and "mid-…" stay intact.
@@ -0,0 +1,6 @@
1
+ import type { Rule } from './en.js';
2
+ export declare const ZH_RULE_ENTRIES: readonly {
3
+ name: string;
4
+ rule: Rule;
5
+ }[];
6
+ export declare const ZH_CONNECTORS: string[];