@nitpicker/report-google-sheets 0.8.0 → 0.11.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.
@@ -0,0 +1,220 @@
1
+ /* eslint-disable unicorn/prefer-code-point --
2
+ * This file deliberately uses `charCodeAt` (UTF-16 code units) rather
3
+ * than `codePointAt` (Unicode code points). The natural-sort algorithm
4
+ * is byte-level by definition in Pool's reference C implementation,
5
+ * and advancing the index by one per call only works when each call
6
+ * returns one unit. Mixing in `codePointAt` would force separate
7
+ * surrogate-pair handling for negligible benefit (URLs are
8
+ * effectively ASCII at the sort layer).
9
+ */
10
+ /**
11
+ * ASCII character codes used by {@link naturalCompare}. Computed once
12
+ * via `charCodeAt` so `'Z'` reads more naturally than `0x5A`.
13
+ */
14
+ const CHAR_NUL = 0;
15
+ const CHAR_TAB = '\t'.charCodeAt(0);
16
+ const CHAR_CR = '\r'.charCodeAt(0);
17
+ const CHAR_SPACE = ' '.charCodeAt(0);
18
+ const CHAR_ZERO = '0'.charCodeAt(0);
19
+ const CHAR_NINE = '9'.charCodeAt(0);
20
+ const CHAR_UPPER_A = 'A'.charCodeAt(0);
21
+ const CHAR_UPPER_Z = 'Z'.charCodeAt(0);
22
+ const ASCII_CASE_SHIFT = 'a'.charCodeAt(0) - 'A'.charCodeAt(0);
23
+ /**
24
+ * Returns the UTF-16 code unit at `i`, or 0 (NUL) when out of range.
25
+ * Mimics how Pool's C reference treats the implicit `'\0'` terminator
26
+ * — JavaScript strings have no terminator, so we synthesize one.
27
+ * @param s - The string being scanned.
28
+ * @param i - Zero-based index.
29
+ */
30
+ function codeAtOrNul(s, i) {
31
+ return i < s.length ? s.charCodeAt(i) : CHAR_NUL;
32
+ }
33
+ /**
34
+ * Matches Pool's `nat_isspace`: ASCII whitespace (`\t`, `\n`, `\v`,
35
+ * `\f`, `\r`, ` `). Codes 9..13 are contiguous in ASCII so a single
36
+ * range covers `\t \n \v \f \r`.
37
+ * @param c - UTF-16 code unit.
38
+ */
39
+ function isAsciiWhitespace(c) {
40
+ return c === CHAR_SPACE || (c >= CHAR_TAB && c <= CHAR_CR);
41
+ }
42
+ /**
43
+ * Matches Pool's `nat_isdigit`: ASCII `'0'..'9'`.
44
+ * @param c - UTF-16 code unit.
45
+ */
46
+ function isAsciiDigit(c) {
47
+ return c >= CHAR_ZERO && c <= CHAR_NINE;
48
+ }
49
+ /**
50
+ * Pool's `compare_right` — used when no leading zero is present in
51
+ * either run. Walks both digit runs to their end. The first side to
52
+ * exhaust its digit run is the smaller number. If both runs end at
53
+ * the same time, the bias (first non-equal digit seen) decides.
54
+ *
55
+ * Returns 0 only when both runs end simultaneously with no bias —
56
+ * i.e. the two runs encode the same integer.
57
+ * @param a - First string.
58
+ * @param ai - Start index in `a` (pointing at the first digit).
59
+ * @param b - Second string.
60
+ * @param bi - Start index in `b` (pointing at the first digit).
61
+ */
62
+ function compareRight(a, ai, b, bi) {
63
+ let bias = 0;
64
+ for (;;) {
65
+ const ca = codeAtOrNul(a, ai);
66
+ const cb = codeAtOrNul(b, bi);
67
+ const aDigit = isAsciiDigit(ca);
68
+ const bDigit = isAsciiDigit(cb);
69
+ if (!aDigit && !bDigit)
70
+ return bias;
71
+ if (!aDigit)
72
+ return -1;
73
+ if (!bDigit)
74
+ return 1;
75
+ if (bias === 0) {
76
+ if (ca < cb)
77
+ bias = -1;
78
+ else if (ca > cb)
79
+ bias = 1;
80
+ }
81
+ ai++;
82
+ bi++;
83
+ }
84
+ }
85
+ /**
86
+ * Pool's `compare_left` — used when **either** run starts with a
87
+ * `'0'`, treating both runs as the fractional part of a decimal
88
+ * (e.g. `"01"` < `"1"` because `0.01 < 0.1`). Compares left-aligned
89
+ * digit-by-digit and returns at the first mismatch.
90
+ *
91
+ * Returns 0 only when both runs are identical character-by-character
92
+ * up to their end.
93
+ * @param a - First string.
94
+ * @param ai - Start index in `a` (pointing at the first digit).
95
+ * @param b - Second string.
96
+ * @param bi - Start index in `b` (pointing at the first digit).
97
+ */
98
+ function compareLeft(a, ai, b, bi) {
99
+ for (;;) {
100
+ const ca = codeAtOrNul(a, ai);
101
+ const cb = codeAtOrNul(b, bi);
102
+ const aDigit = isAsciiDigit(ca);
103
+ const bDigit = isAsciiDigit(cb);
104
+ if (!aDigit && !bDigit)
105
+ return 0;
106
+ if (!aDigit)
107
+ return -1;
108
+ if (!bDigit)
109
+ return 1;
110
+ if (ca < cb)
111
+ return -1;
112
+ if (ca > cb)
113
+ return 1;
114
+ ai++;
115
+ bi++;
116
+ }
117
+ }
118
+ /**
119
+ * Natural-order string comparator, ported from Martin Pool's
120
+ * `strnatcmp.c` (sourcefrog/natsort, originally derived from Stuart
121
+ * Cheshire's 1996 Macintosh natural sort). Faithful to the C
122
+ * reference's dispatch:
123
+ *
124
+ * - Skip leading ASCII whitespace on both sides.
125
+ * - When both sides have a digit run:
126
+ * - If **either** run starts with `'0'`, treat them as fractional
127
+ * and use {@link compareLeft} (left-aligned digit-by-digit).
128
+ * - Otherwise use {@link compareRight} (length-first, with bias for
129
+ * ties).
130
+ * - End-of-string on both sides means equal.
131
+ * - Otherwise fold ASCII letters to lower case and compare one
132
+ * UTF-16 code unit.
133
+ *
134
+ * Allocates no intermediate strings: only `charCodeAt` and integer
135
+ * arithmetic. That makes the function safe to invoke O(N log N)
136
+ * times during a 1M+ element sort without inflating the V8 heap with
137
+ * derived `SeqString` allocations.
138
+ *
139
+ * **Benchmark reference (Node 24, M-class CPU):** 100K-entry sort
140
+ * completes in ~650 ms with median heap delta ratio
141
+ * `heapDelta(100K) / heapDelta(10K) ≈ 5–15×`. If either measurement
142
+ * jumps by an order of magnitude (multi-second elapsed, ratio > 100),
143
+ * suspect a regression that reintroduced per-compare string
144
+ * allocation or `Intl.Collator` dispatch.
145
+ *
146
+ * **UTF-16 semantics:** index moves one code unit per step. ASCII
147
+ * URLs (the only kind the archive currently stores) are entirely
148
+ * BMP, so this is equivalent to per-character iteration. URLs that
149
+ * contain surrogate pairs (e.g. raw emoji in path segments) compare
150
+ * as UTF-16 code unit sequences, which is deterministic but not
151
+ * equivalent to Unicode code-point order. That matches `strnatcmp.c`
152
+ * — it also operates on bytes, not Unicode scalars.
153
+ * @param a - First URL.
154
+ * @param b - Second URL.
155
+ * @returns Negative if `a < b`, positive if `a > b`, zero if equal.
156
+ */
157
+ export function naturalCompare(a, b) {
158
+ let ai = 0;
159
+ let bi = 0;
160
+ for (;;) {
161
+ let ca = codeAtOrNul(a, ai);
162
+ let cb = codeAtOrNul(b, bi);
163
+ while (isAsciiWhitespace(ca)) {
164
+ ai++;
165
+ ca = codeAtOrNul(a, ai);
166
+ }
167
+ while (isAsciiWhitespace(cb)) {
168
+ bi++;
169
+ cb = codeAtOrNul(b, bi);
170
+ }
171
+ if (isAsciiDigit(ca) && isAsciiDigit(cb)) {
172
+ const fractional = ca === CHAR_ZERO || cb === CHAR_ZERO;
173
+ const result = fractional ? compareLeft(a, ai, b, bi) : compareRight(a, ai, b, bi);
174
+ if (result !== 0)
175
+ return result;
176
+ // Both digit-run helpers return 0 only when the digit
177
+ // runs are pointwise equal up to their joint end. Fall
178
+ // through and let the trailing byte compare advance
179
+ // past the shared first digit, exactly like Pool's C
180
+ // source.
181
+ }
182
+ if (ca === CHAR_NUL && cb === CHAR_NUL) {
183
+ return 0;
184
+ }
185
+ const fa = ca >= CHAR_UPPER_A && ca <= CHAR_UPPER_Z ? ca + ASCII_CASE_SHIFT : ca;
186
+ const fb = cb >= CHAR_UPPER_A && cb <= CHAR_UPPER_Z ? cb + ASCII_CASE_SHIFT : cb;
187
+ if (fa < fb)
188
+ return -1;
189
+ if (fa > fb)
190
+ return 1;
191
+ ai++;
192
+ bi++;
193
+ }
194
+ }
195
+ /**
196
+ * Sorts archive resources by their URL in natural order, returning a
197
+ * new array (the input is left untouched). Uses
198
+ * {@link naturalCompare} as the comparator, so embedded integers
199
+ * compare by value (`image-2.jpg` < `image-10.jpg`) and ASCII case
200
+ * is folded.
201
+ *
202
+ * The sort is stable (V8 TimSort). Each comparison allocates no
203
+ * intermediate strings, so the only extra memory is V8's TimSort
204
+ * auxiliary buffer — O(N) pointer-sized entries, not O(N × URL length).
205
+ * @param resources - The list of resources to sort. Only the `url`
206
+ * field is read.
207
+ * @returns A new array sorted by URL in natural order.
208
+ * @example
209
+ * ```ts
210
+ * sortResourcesByUrl([
211
+ * { url: 'https://x/img-10.jpg' },
212
+ * { url: 'https://x/img-2.jpg' },
213
+ * { url: 'https://x/img-1.jpg' },
214
+ * ]);
215
+ * // → [{ url: 'https://x/img-1.jpg' }, { url: 'https://x/img-2.jpg' }, { url: 'https://x/img-10.jpg' }]
216
+ * ```
217
+ */
218
+ export function sortResourcesByUrl(resources) {
219
+ return resources.toSorted((a, b) => naturalCompare(a.url, b.url));
220
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitpicker/report-google-sheets",
3
- "version": "0.8.0",
3
+ "version": "0.11.0",
4
4
  "description": "Nitpicker reporter that outputs crawl and analysis results to Google Sheets",
5
5
  "author": "D-ZERO",
6
6
  "license": "Apache-2.0",
@@ -27,12 +27,12 @@
27
27
  "clean": "tsc --build --clean"
28
28
  },
29
29
  "dependencies": {
30
- "@d-zero/dealer": "1.7.0",
30
+ "@d-zero/dealer": "1.9.0",
31
31
  "@d-zero/google-auth": "0.5.6",
32
- "@d-zero/google-sheets": "0.7.1",
32
+ "@d-zero/google-sheets": "0.9.0",
33
33
  "@d-zero/shared": "0.20.1",
34
- "@nitpicker/crawler": "0.8.0",
35
- "@nitpicker/types": "0.8.0",
34
+ "@nitpicker/crawler": "0.11.0",
35
+ "@nitpicker/types": "0.11.0",
36
36
  "ansi-colors": "4.1.3",
37
37
  "debug": "4.4.3",
38
38
  "enquirer": "2.4.1",
@@ -44,5 +44,5 @@
44
44
  "googleapis": "171.4.0",
45
45
  "vitest": "4.0.18"
46
46
  },
47
- "gitHead": "8456d02c3052b2b3438f0d32e096ed7e916c5234"
47
+ "gitHead": "b11dbe3691746b3cb980476808d1f745bc6d0e98"
48
48
  }