aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.js
blob: 89908decf51e3530b90c05bfd40f1f65cf1fbbf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const Nil = undefined;

function to_list(array) {
  let list = [];
  for (let item of array.reverse()) {
    list = [item, list];
  }
  return list;
}

function Ok(x) {
  return { type: "Ok", 0: x };
}

function Error(x) {
  return { type: "Error", 0: x };
}

export function identity(x) {
  return x;
}

export function parse_int(value) {
  if (/^[-+]?(\d+)$/.test(value)) {
    return Ok(Number(value));
  } else {
    return Error(Nil);
  }
}

export function to_string(int) {
  return int.toString();
}

export function int_to_base_string(int, base) {
  return int.toString(base);
}

export function string_replace(string, target, substitute) {
  return string.replaceAll(target, substitute);
}

export function string_reverse(string) {
  return [...string].reverse().join("");
}

export function string_length(string) {
  let iterator = graphemes_iterator(string);
  if (iterator) {
    let i = 0;
    for (let _ of iterator) {
      i++;
    }
    return i;
  } else {
    return string.match(/./gu).length;
  }
}

function graphemes_iterator(string) {
  if (Intl && Intl.Segmenter) {
    return new Intl.Segmenter("en-gb").segment(string)[Symbol.iterator]();
  }
}

export function pop_grapheme(string) {
  let first;
  let iterator = graphemes_iterator(string);
  if (iterator) {
    first = iterator.next().value?.segment;
  } else {
    first = string.match(/./u)?.[0];
  }
  if (first) {
    return Ok([first, string.slice(first.length)]);
  } else {
    return Error(Nil);
  }
}

export function lowercase(string) {
  return string.toLowerCase();
}

export function uppercase(string) {
  return string.toUpperCase();
}

export function less_than(a, b) {
  return a < b;
}

export function add(a, b) {
  return a + b;
}

export function equal(a, b) {
  return a === b;
}

export function split(xs, pattern) {
  return to_list(xs.split(pattern));
}

export function join(xs) {
  return xs.flat(Infinity).join("");
}

export function byte_size(data) {
  if (typeof Blob === "function") {
    return new Blob([data]).size;
  } else if (typeof Buffer === "function") {
    return Buffer.byteLength(data);
  } else {
    return data.length;
  }
}

export function slice_string(string, from, length) {
  return string.slice(from, from + length);
}

export function crop_string(string, substring) {
  return string.substring(string.indexOf(substring));
}

export function index_of(haystack, needle) {
  return haystack.indexOf(needle) | 0;
}

export function starts_with(haystack, needle) {
  return haystack.startsWith(needle);
}

export function ends_with(haystack, needle) {
  return haystack.endsWith(needle);
}

export function split_once(haystack, needle) {
  let index = haystack.indexOf(needle);
  if (index >= 0) {
    let before = haystack.slice(0, index);
    let after = haystack.slice(index + needle.length);
    return Ok([before, after]);
  } else {
    return Error(Nil);
  }
}

export function trim(string) {
  return string.trim();
}

export function trim_left(string) {
  return string.trimLeft();
}

export function trim_right(string) {
  return string.trimRight();
}