Skip to content

Commit 5d7c866

Browse files
committed
Avoid React Native crash
1 parent 15d571a commit 5d7c866

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

src/gleam_stdlib.mjs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function to_string(term) {
4545
}
4646

4747
export function float_to_string(float) {
48-
let string = float.toString();
48+
const string = float.toString();
4949
if (string.indexOf(".") >= 0) {
5050
return string;
5151
} else {
@@ -133,10 +133,10 @@ export function string_length(string) {
133133
if (string === "") {
134134
return 0;
135135
}
136-
let iterator = graphemes_iterator(string);
136+
const iterator = graphemes_iterator(string);
137137
if (iterator) {
138138
let i = 0;
139-
for (let _ of iterator) {
139+
for (const _ of iterator) {
140140
i++;
141141
}
142142
return i;
@@ -159,7 +159,7 @@ function graphemes_iterator(string) {
159159

160160
export function pop_grapheme(string) {
161161
let first;
162-
let iterator = graphemes_iterator(string);
162+
const iterator = graphemes_iterator(string);
163163
if (iterator) {
164164
first = iterator.next().value?.segment;
165165
} else {
@@ -221,10 +221,10 @@ export function ends_with(haystack, needle) {
221221
}
222222

223223
export function split_once(haystack, needle) {
224-
let index = haystack.indexOf(needle);
224+
const index = haystack.indexOf(needle);
225225
if (index >= 0) {
226-
let before = haystack.slice(0, index);
227-
let after = haystack.slice(index + needle.length);
226+
const before = haystack.slice(0, index);
227+
const after = haystack.slice(index + needle.length);
228228
return new Ok([before, after]);
229229
} else {
230230
return new Error(Nil);
@@ -265,7 +265,7 @@ export function crash(message) {
265265

266266
export function bit_string_to_string(bit_string) {
267267
try {
268-
let decoder = new TextDecoder("utf-8", { fatal: true });
268+
const decoder = new TextDecoder("utf-8", { fatal: true });
269269
return new Ok(decoder.decode(bit_string.buffer));
270270
} catch (_error) {
271271
return new Error(Nil);
@@ -283,7 +283,7 @@ export function print(string) {
283283
}
284284

285285
export function print_error(string) {
286-
if (typeof process === "object") {
286+
if (typeof process === "object" && process.stderr?.write) {
287287
process.stderr.write(string); // We can write without a trailing newline
288288
} else if (typeof Deno === "object") {
289289
Deno.stderr.writeSync(new TextEncoder().encode(string)); // We can write without a trailing newline
@@ -293,7 +293,7 @@ export function print_error(string) {
293293
}
294294

295295
export function print_debug(string) {
296-
if (typeof process === "object") {
296+
if (typeof process === "object" && process.stderr?.write) {
297297
process.stderr.write(string + "\n"); // If we're in Node.js, use `stderr`
298298
} else if (typeof Deno === "object") {
299299
Deno.stderr.writeSync(new TextEncoder().encode(string + "\n")); // If we're in Deno, use `stderr`
@@ -329,7 +329,7 @@ export function power(base, exponent) {
329329
}
330330

331331
export function random_uniform() {
332-
let random_uniform_result = Math.random();
332+
const random_uniform_result = Math.random();
333333
// With round-to-nearest-even behavior, the ranges claimed for the functions below
334334
// (excluding the one for Math.random() itself) aren't exact.
335335
// If extremely large bounds are chosen (2^53 or higher),
@@ -344,10 +344,10 @@ export function random_uniform() {
344344
}
345345

346346
export function bit_string_slice(bits, position, length) {
347-
let start = Math.min(position, position + length);
348-
let end = Math.max(position, position + length);
347+
const start = Math.min(position, position + length);
348+
const end = Math.max(position, position + length);
349349
if (start < 0 || end > bits.length) return new Error(Nil);
350-
let buffer = new Uint8Array(bits.buffer.buffer, start, Math.abs(length));
350+
const buffer = new Uint8Array(bits.buffer.buffer, start, Math.abs(length));
351351
return new Ok(new BitString(buffer));
352352
}
353353

@@ -381,13 +381,13 @@ export function compile_regex(pattern, options) {
381381
if (options.multi_line) flags += "m";
382382
return new Ok(new RegExp(pattern, flags));
383383
} catch (error) {
384-
let number = (error.columnNumber || 0) | 0;
384+
const number = (error.columnNumber || 0) | 0;
385385
return new Error(new RegexCompileError(error.message, number));
386386
}
387387
}
388388

389389
export function regex_scan(regex, string) {
390-
let matches = Array.from(string.matchAll(regex)).map((match) => {
390+
const matches = Array.from(string.matchAll(regex)).map((match) => {
391391
const content = match[0];
392392
const submatches = [];
393393
for (let n = match.length - 1; n > 0; n--) {
@@ -439,7 +439,7 @@ function unsafe_percent_decode(string) {
439439
export function percent_decode(string) {
440440
try {
441441
return new Ok(unsafe_percent_decode(string));
442-
} catch (error) {
442+
} catch (_error) {
443443
return new Error(Nil);
444444
}
445445
}
@@ -450,23 +450,23 @@ export function percent_encode(string) {
450450

451451
export function parse_query(query) {
452452
try {
453-
let pairs = [];
454-
for (let section of query.split("&")) {
455-
let [key, value] = section.split("=");
453+
const pairs = [];
454+
for (const section of query.split("&")) {
455+
const [key, value] = section.split("=");
456456
if (!key) continue;
457457
pairs.push([unsafe_percent_decode(key), unsafe_percent_decode(value)]);
458458
}
459459
return new Ok(List.fromArray(pairs));
460-
} catch (error) {
460+
} catch (_error) {
461461
return new Error(Nil);
462462
}
463463
}
464464

465465
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
466466
export function encode64(bit_string) {
467-
let aBytes = bit_string.buffer;
468-
let nMod3 = 2,
469-
sB64Enc = "";
467+
const aBytes = bit_string.buffer;
468+
let nMod3 = 2;
469+
let sB64Enc = "";
470470

471471
for (let nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
472472
nMod3 = nIdx % 3;
@@ -524,10 +524,10 @@ function b64ToUint6(nChr) {
524524
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64#Solution_2_%E2%80%93_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8
525525
export function decode64(sBase64) {
526526
if (sBase64.match(/[^A-Za-z0-9\+\/=]/g)) return new Error(Nil);
527-
let sB64Enc = sBase64.replace(/=/g, "");
528-
let nInLen = sB64Enc.length;
529-
let nOutLen = (nInLen * 3 + 1) >> 2;
530-
let taBytes = new Uint8Array(nOutLen);
527+
const sB64Enc = sBase64.replace(/=/g, "");
528+
const nInLen = sB64Enc.length;
529+
const nOutLen = (nInLen * 3 + 1) >> 2;
530+
const taBytes = new Uint8Array(nOutLen);
531531

532532
for (
533533
let nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0;
@@ -569,7 +569,7 @@ export function classify_dynamic(data) {
569569
} else if (data === undefined) {
570570
return "Nil";
571571
} else {
572-
let type = typeof data;
572+
const type = typeof data;
573573
return type.charAt(0).toUpperCase() + type.slice(1);
574574
}
575575
}
@@ -654,7 +654,7 @@ export function decode_option(data, decoder) {
654654
if (data === null || data === undefined || data instanceof None)
655655
return new Ok(new None());
656656
if (data instanceof Some) data = data[0];
657-
let result = decoder(data);
657+
const result = decoder(data);
658658
if (result.isOk()) {
659659
return new Ok(new Some(result[0]));
660660
} else {
@@ -663,10 +663,14 @@ export function decode_option(data, decoder) {
663663
}
664664

665665
export function decode_field(value, name) {
666-
let not_a_map_error = () => decoder_error("Map", value);
666+
const not_a_map_error = () => decoder_error("Map", value);
667667

668-
if (value instanceof PMap || value instanceof WeakMap || value instanceof Map) {
669-
let entry = map_get(value, name);
668+
if (
669+
value instanceof PMap ||
670+
value instanceof WeakMap ||
671+
value instanceof Map
672+
) {
673+
const entry = map_get(value, name);
670674
return new Ok(entry.isOk() ? new Some(entry[0]) : new None());
671675
} else if (Object.getPrototypeOf(value) == Object.prototype) {
672676
return try_get_field(value, name, () => new Ok(new None()));

0 commit comments

Comments
 (0)