• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RauliL / laskin.js / 7023328718

28 Nov 2023 07:04PM UTC coverage: 67.583% (+0.9%) from 66.697%
7023328718

push

github

RauliL
Fix ESLint complaints

362 of 530 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

1137 of 1688 relevant lines covered (67.36%)

12.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.2
/src/api/string.ts
1
import { Context } from "../context";
2
import { RangeError } from "../exception";
8✔
3
import { isValidNumber, parseNumberValue } from "../number";
8✔
4
import { BuiltinQuoteCallback, ScriptedQuote } from "../quote";
8✔
5
import { parse } from "../parser";
8✔
6
import { newNumberValue, newStringValue } from "../value";
8✔
7

8
const w_length = (context: Context) => {
8✔
9
  context.pushNumber(context.peekString().length);
×
10
};
11

12
const w_chars = (context: Context) => {
8✔
13
  context.pushVector(context.popString().split("").map(newStringValue));
×
14
};
15

16
const w_runes = (context: Context) => {
8✔
17
  context.pushVector(
×
18
    context
19
      .popString()
20
      .split("")
21
      .map((c) => newNumberValue(c.charCodeAt(0))),
×
22
  );
23
};
24

25
const w_lines = (context: Context) => {
8✔
26
  context.pushVector(context.popString().split(/\r?\n/).map(newStringValue));
×
27
};
28

29
const w_words = (context: Context) => {
8✔
30
  context.pushVector(context.popString().split(/\s+/).map(newStringValue));
×
31
};
32

33
const w_startsWith = (context: Context) => {
8✔
34
  const string = context.popString();
×
35
  const subString = context.popString();
×
36

37
  context.pushBoolean(string.startsWith(subString));
×
38
};
39

40
const w_endsWith = (context: Context) => {
8✔
41
  const string = context.popString();
×
42
  const subString = context.popString();
×
43

44
  context.pushBoolean(string.endsWith(subString));
×
45
};
46

47
const w_includes = (context: Context) => {
8✔
48
  const string = context.popString();
×
49
  const subString = context.popString();
×
50

51
  context.pushBoolean(string.includes(subString));
×
52
};
53

54
const w_indexOf = (context: Context) => {
8✔
55
  const string = context.popString();
×
56
  const subString = context.popString();
×
57

58
  context.pushNumber(string.indexOf(subString));
×
59
};
60

61
const w_lastIndexOf = (context: Context) => {
8✔
62
  const string = context.popString();
×
63
  const subString = context.popString();
×
64

65
  context.pushNumber(string.lastIndexOf(subString));
×
66
};
67

68
const w_reverse = (context: Context) => {
8✔
69
  context.pushString(context.popString().split("").reverse().join(""));
×
70
};
71

72
const w_lowerCase = (context: Context) => {
8✔
73
  context.pushString(context.popString().toLowerCase());
×
74
};
75

76
const w_upperCase = (context: Context) => {
8✔
77
  context.pushString(context.popString().toUpperCase());
×
78
};
79

80
const w_swapCase = (context: Context) => {
8✔
81
  context.pushString(
×
82
    context
83
      .popString()
84
      .split("")
85
      .map((c) => {
86
        const upper = c.toUpperCase();
×
87

88
        return c === upper ? c.toLowerCase() : upper;
×
89
      })
90
      .join(""),
91
  );
92
};
93

94
const w_trim = (context: Context) => {
8✔
95
  context.pushString(context.popString().trim());
×
96
};
97

98
const w_trimStart = (context: Context) => {
8✔
99
  context.pushString(context.popString().trimStart());
×
100
};
101

102
const w_trimEnd = (context: Context) => {
8✔
103
  context.pushString(context.popString().trimEnd());
×
104
};
105

106
const popStringIndex = (context: Context, string: string): number => {
8✔
107
  let index = context.popNumber().value.toNumber();
×
108

109
  if (index < 0) {
×
110
    index += string.length;
×
111
  }
112
  if (index < 0 || index >= string.length) {
×
113
    throw new RangeError("String index out of bounds.");
×
114
  }
115

116
  return index;
×
117
};
118

119
const w_substring = (context: Context) => {
8✔
120
  const string = context.popString();
×
121
  const begin = popStringIndex(context, string);
×
122
  const end = popStringIndex(context, string);
×
123

124
  if (end < begin) {
×
125
    throw new RangeError("String index out of bounds.");
×
126
  }
127

128
  context.pushString(string.substring(begin, end));
×
129
};
130

131
const w_split = (context: Context) => {
8✔
132
  const string = context.popString();
×
133
  const pattern = context.popString();
×
134

135
  context.pushVector(string.split(pattern).map(newStringValue));
×
136
};
137

138
const w_repeat = (context: Context) => {
8✔
139
  const string = context.popString();
×
140
  let { value: count } = context.popNumber();
×
141
  let result = "";
×
142

143
  while (count.comparedTo(0) > 0) {
×
144
    result += string;
×
145
    count = count.sub(1);
×
146
  }
147
  context.pushString(result);
×
148
};
149

150
const w_replace = (context: Context) => {
8✔
151
  const string = context.popString();
×
152
  const replacement = context.popString();
×
153
  const needle = context.popString();
×
154

155
  context.pushString(string.replace(needle, replacement));
×
156
};
157

158
const w_padStart = (context: Context) => {
8✔
159
  const string = context.popString();
×
160
  const padString = context.popString();
×
161
  const { value: targetLength } = context.popNumber();
×
162

163
  context.pushString(string.padStart(targetLength.toNumber(), padString));
×
164
};
165

166
const w_padEnd = (context: Context) => {
8✔
167
  const string = context.popString();
×
168
  const padString = context.popString();
×
169
  const { value: targetLength } = context.popNumber();
×
170

171
  context.pushString(string.padEnd(targetLength.toNumber(), padString));
×
172
};
173

174
const w_at = (context: Context) => {
8✔
175
  const string = context.popString();
×
176
  const index = popStringIndex(context, string);
×
177

178
  context.pushString(string[index]);
×
179
};
180

181
const w_toQuote = (context: Context) => {
8✔
182
  context.pushQuote(new ScriptedQuote(parse(context.popString())));
×
183
};
184

185
const w_toNumber = (context: Context) => {
8✔
186
  const string = context.popString();
×
187

UNCOV
188
  if (!isValidNumber(string)) {
×
UNCOV
189
    throw new RangeError("Cannot convert given string into number.");
×
190
  }
191

192
  context.push(parseNumberValue(string));
×
193
};
194

195
export const string: [string, BuiltinQuoteCallback][] = [
8✔
196
  ["string:length", w_length],
197
  ["string:chars", w_chars],
198
  ["string:runes", w_runes],
199
  ["string:lines", w_lines],
200
  ["string:words", w_words],
201

202
  // Comparison.
203
  ["string:starts-with?", w_startsWith],
204
  ["string:ends-with?", w_endsWith],
205
  ["string:includes?", w_includes],
206
  ["string:index-of", w_indexOf],
207
  ["string:last-index-of", w_lastIndexOf],
208

209
  // Modifications.
210
  ["string:reverse", w_reverse],
211
  ["string:lower-case", w_lowerCase],
212
  ["string:upper-case", w_upperCase],
213
  ["string:swap-case", w_swapCase],
214
  ["string:trim", w_trim],
215
  ["string:trim-start", w_trimStart],
216
  ["string:trim-end", w_trimEnd],
217
  ["string:substring", w_substring],
218
  ["string:split", w_split],
219
  ["string:repeat", w_repeat],
220
  ["string:replace", w_replace],
221
  ["string:pad-start", w_padStart],
222
  ["string:pad-end", w_padEnd],
223

224
  ["string:@", w_at],
225

226
  // Conversions.
227
  ["string:>quote", w_toQuote],
228
  ["string:>number", w_toNumber],
229
];
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc