Skip to content

Commit 855c111

Browse files
fix(thicktoken): rm slice ordering (#672)
1 parent e54476c commit 855c111

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

thicktoken/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bpinternal/thicktoken",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Tiktoken but thicker; A bundled tokenizer with added helper functions",
55
"repository": {
66
"url": "https://github.com/botpress/packages"

thicktoken/src/smart-slice.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ test('SmartSlice handles slices of zero elements', () => {
3131
const indices1 = [...slice1]
3232
expect(indices1).toEqual([])
3333

34-
const slice2 = new SmartSlice([[-5, 5]], 10)
35-
const indices2 = [...slice2]
34+
const slices2 = new SmartSlice([[9, 1]], 10)
35+
const indices2 = [...slices2]
3636
expect(indices2).toEqual([])
37+
38+
const slice3 = new SmartSlice([[-5, 5]], 10)
39+
const indices3 = [...slice3]
40+
expect(indices3).toEqual([])
3741
})
3842

3943
test('SmartSlice handles overlapping slices', () => {

thicktoken/src/smart-slice.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export class SmartSlice {
55

66
public constructor(slices: Slice[], max: number) {
77
const clampedSlices: Slice[] = slices.map((s) => this._clampSlice(s, max))
8-
const sortedSlices: Slice[] = clampedSlices.sort((a, b) => a[0] - b[0])
8+
const nonEmptySlices: Slice[] = clampedSlices.filter(([start, end]) => start < end)
9+
const sortedSlices: Slice[] = nonEmptySlices.sort((a, b) => a[0] - b[0])
910

1011
const merged: Slice[] = []
1112
for (const [start, end] of sortedSlices) {
@@ -29,13 +30,6 @@ export class SmartSlice {
2930
start = start < 0 ? max + start : start
3031
end = end < 0 ? max + end : end
3132

32-
// order slice
33-
if (end < start) {
34-
const tmp = start
35-
start = end
36-
end = tmp
37-
}
38-
3933
// clamp slice
4034
const clampedStart = Math.max(0, Math.min(start, max))
4135
const clampedEnd = Math.max(0, Math.min(end, max))

0 commit comments

Comments
 (0)