@mark1russell7/text 0.0.16 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,6 +26,20 @@ next")}`); // "::warning title=a%3A b::50%25%0Anext"
26
26
  escapeMarkdown("a | b *c*"); // "a \\| b \\*c\\*"
27
27
  ```
28
28
 
29
+ ### One word of a POSIX shell
30
+
31
+ `escapeShell(text)` gives one word that a POSIX shell reads as the text, with no expansion and no split into words. A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is. Each other text goes in single quotes, and each `'` becomes `'\''`. The empty text becomes `''`.
32
+
33
+ The entry `@mark1russell7/text/shell` has `escapeShell`. The root entry does not have it, to keep its size.
34
+
35
+ ```ts
36
+ import { escapeShell } from "@mark1russell7/text/shell";
37
+
38
+ escapeShell("notes.txt"); // "notes.txt"
39
+ escapeShell("my file's name"); // "'my file'\\''s name'"
40
+ escapeShell("$HOME"); // "'$HOME'"
41
+ ```
42
+
29
43
  ## English word forms
30
44
 
31
45
  `countOf(count, noun, plural?)` gives a count with a noun. Only the count 1 takes the singular noun. The default plural noun is the singular noun with "s" at the end.
@@ -320,6 +334,7 @@ The tests also examine 18 copies of a cut with a mark, from a later list of the
320
334
  - `escapeHtml` does not escape quotes. For an attribute value, use `escapeAttribute`.
321
335
  - `escapeRegExp` gives a pattern for use outside a character class. In a class, `-` also has a meaning.
322
336
  - `escapeMarkdown` does not change spaces. Four spaces at the start of a line make a code block.
337
+ - `escapeShell` gives one word for a POSIX shell, not for `cmd.exe` or PowerShell. It throws a `RangeError` for a text with NUL.
323
338
  - `wordSearch` keeps accents, so "cafe" does not find "café".
324
339
  - `countOf` knows no irregular plural. Give the plural noun for each noun that does not add "s".
325
340
  - `listOf` does not change the parts. A part with a comma makes a list that a reader cannot split again.
package/build/index.d.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  /**
2
2
  * The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes, regular
3
3
  * expressions, Markdown and GitHub Actions workflow commands. The other groups are the English word forms, a word
4
- * search and the text of a thrown value. The entry `@mark1russell7/text/redact` removes secrets from a text, and the entry
4
+ * search and the text of a thrown value.
5
+ *
6
+ * The entry `@mark1russell7/text/redact` removes secrets from a text, and the entry
5
7
  * `@mark1russell7/text/order` has two orders of texts. The entry `@mark1russell7/text/english` adds the list join `listOf`.
6
- * The entry `@mark1russell7/text/search` adds a ranked search with TF-IDF.
8
+ * The entry `@mark1russell7/text/search` adds a ranked search with TF-IDF. The entry `@mark1russell7/text/shell` has the
9
+ * escape of a POSIX shell word.
7
10
  */
8
11
  import { countOf } from "./english.js";
9
12
  import { searchKey, wordSearch } from "./search.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/error.ts","../src/escape.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;wBAkBgB,aAAa;;;;;;;;wBCHb,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe;;;;;;;;wBAWf,mBAAmB;;;;;;;;wBAWnB,uBAAuB"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/error.ts","../src/escape.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAkBgB,aAAa;;;;;;;;wBCHb,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe;;;;;;;;wBAWf,mBAAmB;;;;;;;;wBAWnB,uBAAuB"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * This function escapes a text for one word of a POSIX shell command, for example an argument of a command in a script.
3
+ * The shell reads the word as the text, with no expansion and no split into words.
4
+ *
5
+ * - A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is.
6
+ * - Each other text goes in single quotes. Each single quote of the text becomes `'\''`.
7
+ * - The empty text becomes `''`.
8
+ *
9
+ * A shell word cannot hold the NUL character. Thus the function throws a `RangeError` for a text with NUL.
10
+ *
11
+ * @param text - The plain text.
12
+ * @returns One word that a POSIX shell reads as the text.
13
+ */
14
+ export declare function escapeShell(text: string): string;
15
+ //# sourceMappingURL=shell.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell.d.ts","names":[],"sources":["../src/shell.ts"],"mappings":";;;;;;;;;;;;;wBAqBgB,YAAY"}
package/build/shell.js ADDED
@@ -0,0 +1,27 @@
1
+ // src/shell.ts
2
+ /**
3
+ * The entry `@mark1russell7/text/shell`: the escape of a text for one word of a POSIX shell command. It is a separate
4
+ * entry, so the root entry keeps its size.
5
+ */
6
+ const SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;
7
+ /**
8
+ * This function escapes a text for one word of a POSIX shell command, for example an argument of a command in a script.
9
+ * The shell reads the word as the text, with no expansion and no split into words.
10
+ *
11
+ * - A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is.
12
+ * - Each other text goes in single quotes. Each single quote of the text becomes `'\''`.
13
+ * - The empty text becomes `''`.
14
+ *
15
+ * A shell word cannot hold the NUL character. Thus the function throws a `RangeError` for a text with NUL.
16
+ *
17
+ * @param text - The plain text.
18
+ * @returns One word that a POSIX shell reads as the text.
19
+ */
20
+ function escapeShell(text) {
21
+ if (text.includes("\0")) throw new RangeError("A shell word cannot hold the NUL character.");
22
+ if (SAFE.test(text)) return text;
23
+ return `'${text.replaceAll("'", "'\\''")}'`;
24
+ }
25
+ export { escapeShell };
26
+
27
+ //# sourceMappingURL=shell.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell.js","names":[],"sources":["../src/shell.ts"],"sourcesContent":["/**\n * The entry `@mark1russell7/text/shell`: the escape of a text for one word of a POSIX shell command. It is a separate\n * entry, so the root entry keeps its size.\n */\n\n// The characters that a POSIX shell reads as plain word characters at each place of a word. It is the safe set of `shlex.quote` of Python.\nconst SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;\n\n/**\n * This function escapes a text for one word of a POSIX shell command, for example an argument of a command in a script.\n * The shell reads the word as the text, with no expansion and no split into words.\n *\n * - A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is.\n * - Each other text goes in single quotes. Each single quote of the text becomes `'\\''`.\n * - The empty text becomes `''`.\n *\n * A shell word cannot hold the NUL character. Thus the function throws a `RangeError` for a text with NUL.\n *\n * @param text - The plain text.\n * @returns One word that a POSIX shell reads as the text.\n */\nexport function escapeShell(text: string): string {\n if (text.includes(\"\\0\")) throw new RangeError(\"A shell word cannot hold the NUL character.\");\n if (SAFE.test(text)) return text;\n return `'${text.replaceAll(\"'\", \"'\\\\''\")}'`;\n}\n"],"mappings":";;;;;AAMA,MAAM,OAAO;;;;;;;;;;;;;;AAeb,SAAgB,YAAY,MAAsB;CAChD,IAAI,KAAK,SAAS,IAAI,GAAG,MAAM,IAAI,WAAW,6CAA6C;CAC3F,IAAI,KAAK,KAAK,IAAI,GAAG,OAAO;CAC5B,OAAO,IAAI,KAAK,WAAW,KAAK,OAAO,EAAE;AAC3C"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/text",
4
- "version": "0.0.16",
4
+ "version": "0.0.17",
5
5
  "description": "Plain text helpers: the escapes for HTML, attributes, patterns, Markdown and workflow commands, English word forms (countOf, listOf), a word search (wordSearch), the text of a thrown value (errorMessage), the orders of texts by code units and by code points (compareCodeUnits, compareCodePoints), the removal of secrets (redactSecrets) and the shapes of tokens and of secrets by their context (findSecretShapes, redactSecretShapes, findSecretShapesInValue, CONTEXT_SHAPES, isSecretName), and lengths in code units, code points and graphemes with truncation and padding (textLength, truncate, pad), the marked parts of a text (partsAt, partsOf), and a scan of HTML for its text and its start tags (scanHtml) with the decode of its character references (decodeReferences).",
6
6
  "keywords": [
7
7
  "text",
@@ -73,6 +73,10 @@
73
73
  "types": "./build/search.d.ts",
74
74
  "default": "./build/search.js"
75
75
  },
76
+ "./shell": {
77
+ "types": "./build/shell.d.ts",
78
+ "default": "./build/shell.js"
79
+ },
76
80
  "./package.json": "./package.json"
77
81
  },
78
82
  "files": [
package/src/index.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  /**
2
2
  * The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes, regular
3
3
  * expressions, Markdown and GitHub Actions workflow commands. The other groups are the English word forms, a word
4
- * search and the text of a thrown value. The entry `@mark1russell7/text/redact` removes secrets from a text, and the entry
4
+ * search and the text of a thrown value.
5
+ *
6
+ * The entry `@mark1russell7/text/redact` removes secrets from a text, and the entry
5
7
  * `@mark1russell7/text/order` has two orders of texts. The entry `@mark1russell7/text/english` adds the list join `listOf`.
6
- * The entry `@mark1russell7/text/search` adds a ranked search with TF-IDF.
8
+ * The entry `@mark1russell7/text/search` adds a ranked search with TF-IDF. The entry `@mark1russell7/text/shell` has the
9
+ * escape of a POSIX shell word.
7
10
  */
8
11
  export { countOf } from "./english.ts";
9
12
  export { errorMessage } from "./error.ts";
package/src/shell.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The entry `@mark1russell7/text/shell`: the escape of a text for one word of a POSIX shell command. It is a separate
3
+ * entry, so the root entry keeps its size.
4
+ */
5
+
6
+ // The characters that a POSIX shell reads as plain word characters at each place of a word. It is the safe set of `shlex.quote` of Python.
7
+ const SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;
8
+
9
+ /**
10
+ * This function escapes a text for one word of a POSIX shell command, for example an argument of a command in a script.
11
+ * The shell reads the word as the text, with no expansion and no split into words.
12
+ *
13
+ * - A text of safe characters only (letters, digits and `_ @ % + = : , . / -`) stays as it is.
14
+ * - Each other text goes in single quotes. Each single quote of the text becomes `'\''`.
15
+ * - The empty text becomes `''`.
16
+ *
17
+ * A shell word cannot hold the NUL character. Thus the function throws a `RangeError` for a text with NUL.
18
+ *
19
+ * @param text - The plain text.
20
+ * @returns One word that a POSIX shell reads as the text.
21
+ */
22
+ export function escapeShell(text: string): string {
23
+ if (text.includes("\0")) throw new RangeError("A shell word cannot hold the NUL character.");
24
+ if (SAFE.test(text)) return text;
25
+ return `'${text.replaceAll("'", "'\\''")}'`;
26
+ }