| 1 | test("countNewlines", () => { |
| 2 | assert.equal(string.countNewlines("line 1\nline 2\nline 3"), 2); |
| 3 | assert.equal(string.countNewlines(""), 0); |
| 4 | }); |
| 5 | test("escapeRegExp", () => { |
| 6 | // the exact escape encoding differs between the native `RegExp.escape` |
| 7 | // (hex escapes) and the fallback (backslashes); assert the behavior |
| 8 | const input = "delete * from users where id = $id"; |
| 9 | const re = new RegExp(`^${string.escapeRegExp(input)}$`); |
| 10 | assert.ok(re.test(input)); |
| 11 | assert.ok(!re.test("delete x from users where id = yid")); |
| 12 | }); |
| 13 | test("escapeShellArgument", () => { |
| 14 | // escapeShellArgument leaves it alone when the input is trivial |
| 15 | assert.equal(string.escapeShellArgument("test"), "test"); |
| 16 | assert.equal(string.escapeShellArgument("abc/file.@.txt"), "abc/file.@.txt"); |
| 17 | |
| 18 | // it adds quotes when there are spaces and globs |
| 19 | assert.equal( |
| 20 | string.escapeShellArgument("hello world"), |
| 21 | "'hello world'", |
| 22 | ); |
| 23 | |
| 24 | // handles other cases |
| 25 | assert.equal( |
| 26 | string.escapeShellArgument( |
| 27 | "delete * from \"users\" where \"username\" = 'clover'", |
| 28 | ), |
| 29 | "'delete * from \"users\" where \"username\" = '\\''clover'\\'", |
| 30 | ); |
| 31 | assert.equal(string.escapeShellArgument("'string"), "\"'string\""); |
| 32 | assert.equal(string.escapeShellArgument("'str\"ing"), "\\''str\"ing'"); |
| 33 | }); |
| 34 | test("formatByteSize", () => { |
| 35 | assert.equal(string.formatByteSize(0), "0B"); |
| 36 | assert.equal(string.formatByteSize(1), "1B"); |
| 37 | assert.equal(string.formatByteSize(1000), "1kB"); |
| 38 | assert.equal(string.formatByteSize(-1000), "-1kB"); |
| 39 | assert.equal(string.formatByteSize(1500), "1.5kB"); |
| 40 | assert.equal(string.formatByteSize(1578), "1.58kB"); |
| 41 | assert.equal(string.formatByteSize(5_100_000), "5.10MB"); |
| 42 | assert.equal(string.formatByteSize(-7_231_692_881), "-7.23GB"); |
| 43 | assert.equal(string.formatByteSize(-4_294_967_296), "-4.29GB"); |
| 44 | |
| 45 | assert.equal(string.formatByteSize(53.251), "53.25B"); |
| 46 | assert.equal(string.formatByteSize(Infinity), "Infinity"); |
| 47 | assert.equal(string.formatByteSize(-Infinity), "-Infinity"); |
| 48 | assert.equal(string.formatByteSize(NaN), "NaN"); |
| 49 | }); |
| 50 | test("formatDurationLetters", () => { |
| 51 | assert.equal(string.formatDurationLetters(1), "1s"); |
| 52 | assert.equal(string.formatDurationLetters(1.542), "1.54s"); |
| 53 | assert.equal(string.formatDurationLetters(1.5), "1.5s"); |
| 54 | assert.equal(string.formatDurationLetters(60), "1m"); |
| 55 | assert.equal(string.formatDurationLetters(65), "1m5s"); |
| 56 | assert.equal(string.formatDurationLetters(10000), "2h46m"); |
| 57 | assert.equal(string.formatDurationLetters(421321378), "13y131d"); |
| 58 | |
| 59 | assert.equal(string.formatDurationLetters(0), "now"); |
| 60 | assert.equal(string.formatDurationLetters(-3000), "50m ago"); |
| 61 | assert.equal(string.formatDurationLetters(Infinity), "forever"); |
| 62 | assert.equal(string.formatDurationLetters(-Infinity), "forever ago"); |
| 63 | assert.equal(string.formatDurationLetters(NaN), "NaN"); |
| 64 | }); |
| 65 | |
| 66 | import assert from "node:assert"; |
| 67 | import { test } from "vitest"; |
| 68 | import * as string from "./string.ts"; |