Skip to content

Commit c65bc5c

Browse files
author
Esben Sparre Andreasen
committed
JS: add Util::pluralize, also add tests for Util::capitalize
1 parent 3af91d5 commit c65bc5c

File tree

6 files changed

+22
-0
lines changed

6 files changed

+22
-0
lines changed

javascript/ql/src/semmle/javascript/Util.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,16 @@ bindingset[s]
1111
string capitalize(string s) {
1212
result = s.charAt(0).toUpperCase() + s.suffix(1)
1313
}
14+
15+
/**
16+
* Gets the pluralization for `n` occurrences of `noun`.
17+
*
18+
* For example, the pluralization of `"function"` for `n = 2` is `"functions"`.
19+
*/
20+
bindingset[noun, n]
21+
string pluralize(string noun, int n) {
22+
if n = 1 then
23+
result = noun
24+
else
25+
result = noun + "s"
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| X | X | Xx | XX | Xx | XX |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import semmle.javascript.Util
2+
3+
select capitalize("x"), capitalize("X"), capitalize("xx"), capitalize("XX"), capitalize("Xx"), capitalize("xX")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| xs | x | xs | xs |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import semmle.javascript.Util
2+
3+
select pluralize("x", 0), pluralize("x", 1), pluralize("x", 2), pluralize("x", -1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// used by qltest to identify the language

0 commit comments

Comments
 (0)