Skip to content

Commit 3aca300

Browse files
committed
refactor: rename try basic latin functions to be consistent
1 parent 1214643 commit 3aca300

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

src/utils/latin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
* @param end - The index to stop searching the uint8array
1414
* @returns string if all bytes are within the basic latin range, otherwise null
1515
*/
16-
export function tryLatin(uint8array: Uint8Array, start: number, end: number): string | null {
16+
export function tryReadBasicLatin(
17+
uint8array: Uint8Array,
18+
start: number,
19+
end: number
20+
): string | null {
1721
if (uint8array.length === 0) {
1822
return '';
1923
}
@@ -74,7 +78,7 @@ export function tryLatin(uint8array: Uint8Array, start: number, end: number): st
7478
* @param offset - The position in the destination to begin writing bytes to
7579
* @returns the number of bytes written to destination if all code units are below 128, otherwise null
7680
*/
77-
export function tryWriteLatin(
81+
export function tryWriteBasicLatin(
7882
destination: Uint8Array,
7983
source: string,
8084
offset: number

src/utils/node_byte_utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BSONError } from '../error';
22
import { validateUtf8 } from '../validate_utf8';
3-
import { tryLatin, tryWriteLatin } from './latin';
3+
import { tryReadBasicLatin, tryWriteBasicLatin } from './latin';
44

55
type NodeJsEncoding = 'base64' | 'hex' | 'utf8' | 'binary';
66
type NodeJsBuffer = ArrayBufferView &
@@ -124,7 +124,7 @@ export const nodeJsByteUtils = {
124124
},
125125

126126
toUTF8(buffer: Uint8Array, start: number, end: number, fatal: boolean): string {
127-
const basicLatin = end - start <= 20 ? tryLatin(buffer, start, end) : null;
127+
const basicLatin = end - start <= 20 ? tryReadBasicLatin(buffer, start, end) : null;
128128
if (basicLatin != null) {
129129
return basicLatin;
130130
}
@@ -149,7 +149,7 @@ export const nodeJsByteUtils = {
149149
},
150150

151151
encodeUTF8Into(buffer: Uint8Array, source: string, byteOffset: number): number {
152-
const latinBytesWritten = tryWriteLatin(buffer, source, byteOffset);
152+
const latinBytesWritten = tryWriteBasicLatin(buffer, source, byteOffset);
153153
if (latinBytesWritten != null) {
154154
return latinBytesWritten;
155155
}

src/utils/web_byte_utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BSONError } from '../error';
2-
import { tryLatin } from './latin';
2+
import { tryReadBasicLatin } from './latin';
33

44
type TextDecoder = {
55
readonly encoding: string;
@@ -170,7 +170,7 @@ export const webByteUtils = {
170170
},
171171

172172
toUTF8(uint8array: Uint8Array, start: number, end: number, fatal: boolean): string {
173-
const basicLatin = end - start <= 20 ? tryLatin(uint8array, start, end) : null;
173+
const basicLatin = end - start <= 20 ? tryReadBasicLatin(uint8array, start, end) : null;
174174
if (basicLatin != null) {
175175
return basicLatin;
176176
}

test/node/utils/latin.test.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { expect } from 'chai';
2-
import { tryLatin, tryWriteLatin } from '../../../src/utils/latin';
2+
import { tryReadBasicLatin, tryWriteBasicLatin } from '../../../src/utils/latin';
33
import * as sinon from 'sinon';
44

5-
describe('tryLatin()', () => {
5+
describe('tryReadBasicLatin()', () => {
66
context('when given a buffer of length 0', () => {
77
it('returns an empty string', () => {
8-
expect(tryLatin(new Uint8Array(), 0, 10)).to.equal('');
8+
expect(tryReadBasicLatin(new Uint8Array(), 0, 10)).to.equal('');
99
});
1010
});
1111

1212
context('when the distance between end and start is 0', () => {
1313
it('returns an empty string', () => {
14-
expect(tryLatin(new Uint8Array([1, 2, 3]), 0, 0)).to.equal('');
14+
expect(tryReadBasicLatin(new Uint8Array([1, 2, 3]), 0, 0)).to.equal('');
1515
});
1616
});
1717

@@ -30,61 +30,61 @@ describe('tryLatin()', () => {
3030
context('when there is 1 byte', () => {
3131
context('that exceed 127', () => {
3232
it('returns null', () => {
33-
expect(tryLatin(new Uint8Array([128]), 0, 1)).be.null;
33+
expect(tryReadBasicLatin(new Uint8Array([128]), 0, 1)).be.null;
3434
});
3535
});
3636

3737
it('calls fromCharCode once', () => {
38-
tryLatin(new Uint8Array([95]), 0, 1);
38+
tryReadBasicLatin(new Uint8Array([95]), 0, 1);
3939
expect(fromCharCodeSpy).to.have.been.calledOnce;
4040
});
4141

4242
it('never calls array.push', () => {
43-
tryLatin(new Uint8Array([95]), 0, 1);
43+
tryReadBasicLatin(new Uint8Array([95]), 0, 1);
4444
expect(pushSpy).to.have.not.been.called;
4545
});
4646
});
4747

4848
context('when there is 2 bytes', () => {
4949
context('that exceed 127', () => {
5050
it('returns null', () => {
51-
expect(tryLatin(new Uint8Array([0, 128]), 0, 2)).be.null;
52-
expect(tryLatin(new Uint8Array([128, 0]), 0, 2)).be.null;
53-
expect(tryLatin(new Uint8Array([128, 128]), 0, 2)).be.null;
51+
expect(tryReadBasicLatin(new Uint8Array([0, 128]), 0, 2)).be.null;
52+
expect(tryReadBasicLatin(new Uint8Array([128, 0]), 0, 2)).be.null;
53+
expect(tryReadBasicLatin(new Uint8Array([128, 128]), 0, 2)).be.null;
5454
});
5555
});
5656

5757
it('calls fromCharCode twice', () => {
58-
tryLatin(new Uint8Array([95, 105]), 0, 2);
58+
tryReadBasicLatin(new Uint8Array([95, 105]), 0, 2);
5959
expect(fromCharCodeSpy).to.have.been.calledTwice;
6060
});
6161

6262
it('never calls array.push', () => {
63-
tryLatin(new Uint8Array([95, 105]), 0, 2);
63+
tryReadBasicLatin(new Uint8Array([95, 105]), 0, 2);
6464
expect(pushSpy).to.have.not.been.called;
6565
});
6666
});
6767

6868
context('when there is 3 bytes', () => {
6969
context('that exceed 127', () => {
7070
it('returns null', () => {
71-
expect(tryLatin(new Uint8Array([0, 0, 128]), 0, 3)).be.null;
72-
expect(tryLatin(new Uint8Array([0, 128, 0]), 0, 3)).be.null;
73-
expect(tryLatin(new Uint8Array([128, 0, 0]), 0, 3)).be.null;
74-
expect(tryLatin(new Uint8Array([128, 128, 128]), 0, 3)).be.null;
75-
expect(tryLatin(new Uint8Array([128, 128, 0]), 0, 3)).be.null;
76-
expect(tryLatin(new Uint8Array([128, 0, 128]), 0, 3)).be.null;
77-
expect(tryLatin(new Uint8Array([0, 128, 128]), 0, 3)).be.null;
71+
expect(tryReadBasicLatin(new Uint8Array([0, 0, 128]), 0, 3)).be.null;
72+
expect(tryReadBasicLatin(new Uint8Array([0, 128, 0]), 0, 3)).be.null;
73+
expect(tryReadBasicLatin(new Uint8Array([128, 0, 0]), 0, 3)).be.null;
74+
expect(tryReadBasicLatin(new Uint8Array([128, 128, 128]), 0, 3)).be.null;
75+
expect(tryReadBasicLatin(new Uint8Array([128, 128, 0]), 0, 3)).be.null;
76+
expect(tryReadBasicLatin(new Uint8Array([128, 0, 128]), 0, 3)).be.null;
77+
expect(tryReadBasicLatin(new Uint8Array([0, 128, 128]), 0, 3)).be.null;
7878
});
7979
});
8080

8181
it('calls fromCharCode thrice', () => {
82-
tryLatin(new Uint8Array([95, 105, 100]), 0, 3);
82+
tryReadBasicLatin(new Uint8Array([95, 105, 100]), 0, 3);
8383
expect(fromCharCodeSpy).to.have.been.calledThrice;
8484
});
8585

8686
it('never calls array.push', () => {
87-
tryLatin(new Uint8Array([95, 105, 100]), 0, 3);
87+
tryReadBasicLatin(new Uint8Array([95, 105, 100]), 0, 3);
8888
expect(pushSpy).to.have.not.been.called;
8989
});
9090
});
@@ -93,44 +93,45 @@ describe('tryLatin()', () => {
9393
context(`when there is ${stringLength} bytes`, () => {
9494
context('that exceed 127', () => {
9595
it('returns null', () => {
96-
expect(tryLatin(new Uint8Array(stringLength).fill(128), 0, stringLength)).be.null;
96+
expect(tryReadBasicLatin(new Uint8Array(stringLength).fill(128), 0, stringLength)).be
97+
.null;
9798
});
9899
});
99100

100101
it('calls fromCharCode once', () => {
101-
tryLatin(new Uint8Array(stringLength).fill(95), 0, stringLength);
102+
tryReadBasicLatin(new Uint8Array(stringLength).fill(95), 0, stringLength);
102103
expect(fromCharCodeSpy).to.have.been.calledOnce;
103104
});
104105

105106
it(`calls array.push ${stringLength}`, () => {
106-
tryLatin(new Uint8Array(stringLength).fill(95), 0, stringLength);
107+
tryReadBasicLatin(new Uint8Array(stringLength).fill(95), 0, stringLength);
107108
expect(pushSpy).to.have.callCount(stringLength);
108109
});
109110
});
110111
}
111112

112113
context('when there is >21 bytes', () => {
113114
it('returns null', () => {
114-
expect(tryLatin(new Uint8Array(21).fill(95), 0, 21)).be.null;
115-
expect(tryLatin(new Uint8Array(201).fill(95), 0, 201)).be.null;
115+
expect(tryReadBasicLatin(new Uint8Array(21).fill(95), 0, 21)).be.null;
116+
expect(tryReadBasicLatin(new Uint8Array(201).fill(95), 0, 201)).be.null;
116117
});
117118
});
118119
});
119120

120-
describe('tryWriteLatin()', () => {
121+
describe('tryWriteBasicLatin()', () => {
121122
context('when given a string of length 0', () => {
122123
it('returns 0 and does not modify the destination', () => {
123124
const input = Uint8Array.from({ length: 10 }, () => 1);
124-
expect(tryWriteLatin(input, '', 2)).to.equal(0);
125+
expect(tryWriteBasicLatin(input, '', 2)).to.equal(0);
125126
expect(input).to.deep.equal(Uint8Array.from({ length: 10 }, () => 1));
126127
});
127128
});
128129

129130
context('when given a string with a length larger than the buffer', () => {
130131
it('returns null', () => {
131132
const input = Uint8Array.from({ length: 10 }, () => 1);
132-
expect(tryWriteLatin(input, 'a'.repeat(11), 0)).to.be.null;
133-
expect(tryWriteLatin(input, 'a'.repeat(13), 2)).to.be.null;
133+
expect(tryWriteBasicLatin(input, 'a'.repeat(11), 0)).to.be.null;
134+
expect(tryWriteBasicLatin(input, 'a'.repeat(13), 2)).to.be.null;
134135
});
135136
});
136137

@@ -149,7 +150,7 @@ describe('tryWriteLatin()', () => {
149150
context('that exceed 127', () => {
150151
it('returns null', () => {
151152
expect(
152-
tryWriteLatin(
153+
tryWriteBasicLatin(
153154
new Uint8Array(stringLength * 3),
154155
'a'.repeat(stringLength - 1) + '\x80',
155156
0
@@ -159,7 +160,7 @@ describe('tryWriteLatin()', () => {
159160
});
160161

161162
it(`calls charCodeAt ${stringLength}`, () => {
162-
tryWriteLatin(
163+
tryWriteBasicLatin(
163164
new Uint8Array(stringLength * 3),
164165
String.fromCharCode(127).repeat(stringLength),
165166
stringLength
@@ -171,7 +172,7 @@ describe('tryWriteLatin()', () => {
171172

172173
context('when there is >25 characters', () => {
173174
it('returns null', () => {
174-
expect(tryWriteLatin(new Uint8Array(75), 'a'.repeat(26), 0)).be.null;
175+
expect(tryWriteBasicLatin(new Uint8Array(75), 'a'.repeat(26), 0)).be.null;
175176
});
176177
});
177178
});

0 commit comments

Comments
 (0)