Skip to content

Commit 2c40b57

Browse files
author
xueren.dt
committed
fix(ava): fix unit test
1 parent ffd0dc4 commit 2c40b57

1 file changed

Lines changed: 14 additions & 33 deletions

File tree

  • packages/ava/src/extract/features

packages/ava/src/extract/features/plain.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,42 +70,23 @@ export function isOrdinal(info: ColumnFeature): boolean {
7070
const { rawData, recommendation } = info;
7171
if (recommendation !== COLUMN_TYPE.string) return false;
7272
if (isConst(info)) return false;
73-
const list = rawData.filter((item) => !isNil(item) && isBasicType(item));
73+
const list = rawData.filter((item) => !isNil(item) && isBasicType(item)).map((item) => `${item}`);
7474
if (list.length === 0) return false;
75-
let start: null | string = null;
76-
let end: null | string = null;
75+
// Compute common prefix and suffix lengths safely to avoid infinite loops
76+
const minLen = Math.min(...list.map((s) => s.length));
7777
let startIndex = -1;
78-
let endIndex = -1;
79-
80-
let through = true;
81-
while (through) {
82-
let through = true;
83-
for (let i = 0; i < list.length; i += 1) {
84-
const item = list[i];
85-
const char = item[startIndex + 1];
86-
if (start === null || i === 0) start = char;
87-
if (char !== start) {
88-
through = false;
89-
break;
90-
}
91-
}
92-
if (!through) break;
93-
startIndex += 1;
78+
for (let idx = 0; idx < minLen; idx += 1) {
79+
const c0 = list[0][idx];
80+
const allSame = list.every((s) => s[idx] === c0);
81+
if (!allSame) break;
82+
startIndex = idx;
9483
}
95-
through = true;
96-
while (through) {
97-
let through = true;
98-
for (let i = 0; i < list.length; i += 1) {
99-
const item = list[i];
100-
const char = item[item.length - 1 - (endIndex + 1)];
101-
if (end === null || i === 0) end = char;
102-
if (char !== end) {
103-
through = false;
104-
break;
105-
}
106-
}
107-
if (!through) break;
108-
endIndex += 1;
84+
let endIndex = -1;
85+
for (let idx = 0; idx < minLen; idx += 1) {
86+
const c0 = list[0][list[0].length - 1 - idx];
87+
const allSame = list.every((s) => s[s.length - 1 - idx] === c0);
88+
if (!allSame) break;
89+
endIndex = idx;
10990
}
11091
const patterns = [/\d+/, /(||||||||||)+/, /(||||||)/, /^[a-z]$/, /^[A-Z]$/];
11192
if (startIndex === -1 && endIndex === -1) return false;

0 commit comments

Comments
 (0)