Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 90a5b6b

Browse files
committed
Convert simple class to function+object
1 parent b1c3826 commit 90a5b6b

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/fast-string-array.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
export interface FastStringArray {
2+
indexes: { [key: string]: number };
3+
array: readonly string[];
4+
}
5+
16
/**
27
* FastStringArray acts like a `Set` (allowing only one occurrence of a string
38
* `key`), but provides the index of the `key` in the backing array.
@@ -6,9 +11,11 @@
611
* the backing array, like how `sourcesContent[i]` is the source content
712
* associated with `source[i]`, and there are never duplicates.
813
*/
9-
export class FastStringArray {
10-
indexes = Object.create(null) as { [key: string]: number };
11-
array = [] as ReadonlyArray<string>;
14+
export function FastStringArray(): FastStringArray {
15+
return {
16+
indexes: { __proto__: null } as any,
17+
array: [],
18+
};
1219
}
1320

1421
/**
@@ -29,4 +36,3 @@ export function put(strarr: FastStringArray, key: string): number {
2936

3037
return index;
3138
}
32-

src/source-map-tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ type MappingSource = SourceMapSegmentObject | typeof INVALID_MAPPING | typeof SO
1717
*/
1818
export function traceMappings(tree: SourceMapTree): TraceMap {
1919
const mappings: SourceMapSegment[][] = [];
20-
const names = new FastStringArray();
21-
const sources = new FastStringArray();
20+
const names = FastStringArray();
21+
const sources = FastStringArray();
2222
const sourcesContent: (string | null)[] = [];
2323
const { sources: rootSources, map } = tree;
2424
const rootNames = map.names;

test/unit/fast-array-string.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { FastStringArray, put } from '../../src/fast-string-array';
22

33
describe('FastStringArray', () => {
4-
let array: FastStringArray;
5-
6-
beforeEach(() => {
7-
array = new FastStringArray();
8-
});
9-
104
describe('put()', () => {
115
test('puts string in if not present', () => {
6+
const array = FastStringArray();
7+
128
put(array, 'test');
139
expect(array.array).toEqual(['test']);
1410
put(array, 'test');
@@ -26,12 +22,16 @@ describe('FastStringArray', () => {
2622
});
2723

2824
test('returns index of string in array', () => {
25+
const array = FastStringArray();
26+
2927
expect(put(array, 'test')).toBe(0);
3028
expect(put(array, 'foo')).toBe(1);
3129
expect(put(array, 'bar')).toBe(2);
3230
});
3331

3432
test('returns original index of string in array', () => {
33+
const array = FastStringArray();
34+
3535
put(array, 'test');
3636
put(array, 'foo');
3737
put(array, 'bar');
@@ -42,6 +42,8 @@ describe('FastStringArray', () => {
4242
});
4343

4444
test('handles empty string', () => {
45+
const array = FastStringArray();
46+
4547
expect(put(array, '')).toBe(0);
4648
expect(put(array, '')).toBe(0);
4749
expect(array.array).toEqual(['']);

0 commit comments

Comments
 (0)