@mark1russell7/text 0.0.4 → 0.0.5

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
@@ -1,6 +1,6 @@
1
1
  # @mark1russell7/text
2
2
 
3
- Plain text helpers in four groups. The groups are the escapes of a text for five targets, the English word forms, a word search and the removal of secrets.
3
+ Plain text helpers in five groups: the escapes of a text for five targets, the English word forms and a word search. The other two groups are the text of a thrown value and the removal of secrets.
4
4
 
5
5
  ```sh
6
6
  pnpm add @mark1russell7/text
@@ -53,6 +53,26 @@ wordSearch("istanbul")("İSTANBUL"); // false: "İ" keeps its dot as U+0307
53
53
  searchKey("file"); // "file"
54
54
  ```
55
55
 
56
+ ## The text of a thrown value
57
+
58
+ `errorMessage(error)` gives the text of a thrown value, for a log line or a message on a screen. It does not throw.
59
+
60
+ ```ts
61
+ import { errorMessage } from "@mark1russell7/text";
62
+
63
+ errorMessage(new Error("boom")); // "boom"
64
+ errorMessage("timeout"); // "timeout"
65
+ errorMessage(Object.create(null)); // "[object Object]"
66
+ ```
67
+
68
+ | Value | Text |
69
+ | --- | --- |
70
+ | An object with a `message` text, also an `Error` from a `vm` context or an `iframe` | The message |
71
+ | An `Error` of any realm with a `message` that is not a text | `String(message)` |
72
+ | Each other value | `String(value)` |
73
+ | A value that makes `String` throw | The tag of `Object.prototype.toString`, for example `[object Object]` |
74
+ | A revoked `Proxy` | `[object]` |
75
+
56
76
  ## The removal of secrets
57
77
 
58
78
  `redactSecrets(text, secrets, options?)` puts a mark in place of each secret in a text. Use it on an error message or a log line before it goes out. The function is in the entry `@mark1russell7/text/redact`, so the root entry stays small.
@@ -91,4 +111,5 @@ The tests run 58 copies of these functions from other repositories of the owner.
91
111
  - `escapeMarkdown` does not change spaces. Four spaces at the start of a line make a code block.
92
112
  - `wordSearch` keeps accents, so "cafe" does not find "café".
93
113
  - `countOf` knows no irregular plural. Give the plural noun for each noun that does not add "s".
114
+ - `errorMessage` gives only the message. It does not add the name of the error, its `cause` or its stack.
94
115
  - `redactSecrets` removes only the forms in its table. A secret in a JSON string with escapes, or in a different encoding, stays.
package/build/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * The entry of `@mark1russell7/text`: plain text helpers. The groups are the escapes for HTML, attributes, regular
3
- * expressions, Markdown and GitHub Actions workflow commands, the English word forms and a word search. The entry
4
- * `@mark1russell7/text/redact` removes secrets from a text.
2
+ * The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes, regular
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.
5
5
  */
6
6
  /**
7
7
  * This module has the English word forms.
@@ -17,6 +17,24 @@
17
17
  * @returns The count, a space and the noun.
18
18
  */
19
19
  export declare function countOf(count: number, noun: string, plural?: string): string;
20
+ /**
21
+ * This module has the text of a thrown value.
22
+ */
23
+ /**
24
+ * This function gives the text of a thrown value, for a log line or a message on a screen. It does not throw.
25
+ *
26
+ * - An object with a `message` text gives that text. The check reads the field, not `instanceof Error`, so an error
27
+ * from another realm (a `vm` context or an `iframe`) also gives its message.
28
+ * - An `Error` with a `message` that is not a text gives `String(message)`. The brand check of
29
+ * `Object.prototype.toString` finds an `Error` of each realm.
30
+ * - Each other value gives `String(value)`.
31
+ * - When `String` throws, the function gives the tag of `Object.prototype.toString`, for example `[object Object]`.
32
+ * - A revoked `Proxy` gives its `typeof` in brackets: `[object]`.
33
+ *
34
+ * @param error - The thrown value. It can be any value.
35
+ * @returns The text of the value.
36
+ */
37
+ export declare function errorMessage(error: unknown): string;
20
38
  /**
21
39
  * This function escapes a text for the content of an HTML element. It changes `&`, `<` and `>` to named character
22
40
  * references. The quotes stay, because they have no meaning in element content.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/english.ts","../src/escape.ts","../src/search.ts"],"mappings":";;;;;;;;;;;;;;;;;;wBAcgB,QAAQ,eAAe,cAAc;;;;;;;;wBCCrC,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe;;;;;;;;wBAWf,mBAAmB;;;;;;;;wBAWnB,uBAAuB;;;;;;;;;;;;;;;;;wBCnDvB,UAAU;;;;;;;;;;;wBAcV,WAAW,iBAAiB"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/english.ts","../src/error.ts","../src/escape.ts","../src/search.ts"],"mappings":";;;;;;;;;;;;;;;;;;wBAcgB,QAAQ,eAAe,cAAc;;;;;;;;;;;;;;;;;;wBCIrC,aAAa;;;;;;;;wBCHb,WAAW;;;;;;;;wBAWX,gBAAgB;;;;;;;;;wBAYhB,aAAa;;;;;;;;;;;;wBAeb,eAAe;;;;;;;;wBAWf,mBAAmB;;;;;;;;wBAWnB,uBAAuB;;;;;;;;;;;;;;;;;wBCnDvB,UAAU;;;;;;;;;;;wBAcV,WAAW,iBAAiB"}
package/build/index.js CHANGED
@@ -16,6 +16,40 @@ import { a as escapeWorkflowData, i as escapeRegExp, n as escapeHtml, o as escap
16
16
  function countOf(count, noun, plural = `${noun}s`) {
17
17
  return `${count.toLocaleString("en")} ${count === 1 ? noun : plural}`;
18
18
  }
19
+ // src/error.ts
20
+ /**
21
+ * This module has the text of a thrown value.
22
+ */
23
+ /**
24
+ * This function gives the text of a thrown value, for a log line or a message on a screen. It does not throw.
25
+ *
26
+ * - An object with a `message` text gives that text. The check reads the field, not `instanceof Error`, so an error
27
+ * from another realm (a `vm` context or an `iframe`) also gives its message.
28
+ * - An `Error` with a `message` that is not a text gives `String(message)`. The brand check of
29
+ * `Object.prototype.toString` finds an `Error` of each realm.
30
+ * - Each other value gives `String(value)`.
31
+ * - When `String` throws, the function gives the tag of `Object.prototype.toString`, for example `[object Object]`.
32
+ * - A revoked `Proxy` gives its `typeof` in brackets: `[object]`.
33
+ *
34
+ * @param error - The thrown value. It can be any value.
35
+ * @returns The text of the value.
36
+ */
37
+ function errorMessage(error) {
38
+ try {
39
+ if (typeof error === "object" && error !== null && "message" in error) {
40
+ const message = error.message;
41
+ if (typeof message === "string") return message;
42
+ if (Object.prototype.toString.call(error) === "[object Error]") return String(message);
43
+ }
44
+ return String(error);
45
+ } catch {
46
+ try {
47
+ return Object.prototype.toString.call(error);
48
+ } catch {
49
+ return `[${typeof error}]`;
50
+ }
51
+ }
52
+ }
19
53
  // src/search.ts
20
54
  /**
21
55
  * This module has a word search of plain text. A text agrees with a search when the text has each word of the search,
@@ -56,6 +90,6 @@ function wordSearch(query) {
56
90
  const words = searchKey(query).split(/\s+/u).filter((word) => word !== "");
57
91
  return (text) => words.length === 0 || words.every((word) => searchKey(text).includes(word));
58
92
  }
59
- export { countOf, escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty, searchKey, wordSearch };
93
+ export { countOf, errorMessage, escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty, searchKey, wordSearch };
60
94
 
61
95
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/english.ts","../src/search.ts"],"sourcesContent":["/**\n * This module has the English word forms.\n */\n\n/**\n * This function gives a count with a noun, for example \"1 site\" or \"1,024 sites\". Only the count 1 takes the singular\n * noun. Each other count takes the plural noun, also 0 and -1. The count has the digit groups of English and at most\n * three decimal places.\n *\n * @param count - The number of things.\n * @param noun - The singular noun.\n * @param plural - The plural noun. The default is the singular noun with the letter \"s\" at the end.\n * @returns The count, a space and the noun.\n */\nexport function countOf(count: number, noun: string, plural = `${noun}s`): string {\n return `${count.toLocaleString(\"en\")} ${count === 1 ? noun : plural}`;\n}\n","/**\n * This module has a word search of plain text. A text agrees with a search when the text has each word of the search,\n * in any order. The case of the letters is not significant.\n */\n\n/** The small final sigma. Unicode case folding changes it to the small sigma, so a word agrees in each position. */\nconst FINAL_SIGMA = /ς/gu;\n\n/**\n * This function gives the search key of a text. `wordSearch` compares the keys of texts. The function does these steps:\n *\n * 1. It applies the Unicode normalization NFKC. Thus the ligature \"fi\" agrees with \"fi\", and full-width letters agree\n * with ASCII letters.\n * 2. It applies the full lower case, then the full upper case, then the full lower case. Thus \"ß\", \"ẞ\" and \"SS\" give\n * \"ss\".\n * 3. It changes the small final sigma \"ς\" to \"σ\", as Unicode case folding does.\n * 4. It applies NFKC again.\n *\n * The steps use no rules of a locale. Thus the Turkish \"İ\" gives \"i\" with a combining dot above (U+0307), and the\n * Turkish \"ı\" gives \"i\". The key of a key is the same key. Accents stay, so \"é\" does not agree with \"e\".\n *\n * @param text - The plain text.\n * @returns The search key of the text.\n */\nexport function searchKey(text: string): string {\n return text.normalize(\"NFKC\").toLowerCase().toUpperCase().toLowerCase().replace(FINAL_SIGMA, \"σ\").normalize(\"NFKC\");\n}\n\n/**\n * This function gives the test of a search. The function makes the key of the search with `searchKey`. Then it splits\n * the key into words at each run of white space, the class `\\s` of a regular expression.\n *\n * The test tells if the key of a text contains each word, in any order. A word can be a part of a longer word, so the\n * search \"art\" finds \"Party\". A search with no words agrees with each text.\n *\n * @param query - The search, as a person types it.\n * @returns The test of a text.\n */\nexport function wordSearch(query: string): (text: string) => boolean {\n const words = searchKey(query)\n .split(/\\s+/u)\n .filter((word) => word !== \"\");\n return (text) => words.length === 0 || words.every((word) => searchKey(text).includes(word));\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,SAAgB,QAAQ,OAAe,MAAc,SAAS,GAAG,KAAK,IAAY;CAChF,OAAO,GAAG,MAAM,eAAe,IAAI,EAAE,GAAG,UAAU,IAAI,OAAO;AAC/D;;;;;;;ACVA,MAAM,cAAc;;;;;;;;;;;;;;;;;AAkBpB,SAAgB,UAAU,MAAsB;CAC9C,OAAO,KAAK,UAAU,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC,CAAC,QAAQ,aAAa,GAAG,CAAC,CAAC,UAAU,MAAM;AACpH;;;;;;;;;;;AAYA,SAAgB,WAAW,OAA0C;CACnE,MAAM,QAAQ,UAAU,KAAK,CAAC,CAC3B,MAAM,MAAM,CAAC,CACb,QAAQ,SAAS,SAAS,EAAE;CAC/B,QAAQ,SAAS,MAAM,WAAW,KAAK,MAAM,OAAO,SAAS,UAAU,IAAI,CAAC,CAAC,SAAS,IAAI,CAAC;AAC7F"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/english.ts","../src/error.ts","../src/search.ts"],"sourcesContent":["/**\n * This module has the English word forms.\n */\n\n/**\n * This function gives a count with a noun, for example \"1 site\" or \"1,024 sites\". Only the count 1 takes the singular\n * noun. Each other count takes the plural noun, also 0 and -1. The count has the digit groups of English and at most\n * three decimal places.\n *\n * @param count - The number of things.\n * @param noun - The singular noun.\n * @param plural - The plural noun. The default is the singular noun with the letter \"s\" at the end.\n * @returns The count, a space and the noun.\n */\nexport function countOf(count: number, noun: string, plural = `${noun}s`): string {\n return `${count.toLocaleString(\"en\")} ${count === 1 ? noun : plural}`;\n}\n","/**\n * This module has the text of a thrown value.\n */\n\n/**\n * This function gives the text of a thrown value, for a log line or a message on a screen. It does not throw.\n *\n * - An object with a `message` text gives that text. The check reads the field, not `instanceof Error`, so an error\n * from another realm (a `vm` context or an `iframe`) also gives its message.\n * - An `Error` with a `message` that is not a text gives `String(message)`. The brand check of\n * `Object.prototype.toString` finds an `Error` of each realm.\n * - Each other value gives `String(value)`.\n * - When `String` throws, the function gives the tag of `Object.prototype.toString`, for example `[object Object]`.\n * - A revoked `Proxy` gives its `typeof` in brackets: `[object]`.\n *\n * @param error - The thrown value. It can be any value.\n * @returns The text of the value.\n */\nexport function errorMessage(error: unknown): string {\n try {\n if (typeof error === \"object\" && error !== null && \"message\" in error) {\n const message: unknown = error.message;\n if (typeof message === \"string\") return message;\n if (Object.prototype.toString.call(error) === \"[object Error]\") return String(message);\n }\n return String(error);\n } catch {\n // String() throws for an object with no prototype, for a toString that throws and for a message getter that throws.\n try {\n return Object.prototype.toString.call(error);\n } catch {\n // A revoked Proxy throws also here. The operator typeof never throws.\n return `[${typeof error}]`;\n }\n }\n}\n","/**\n * This module has a word search of plain text. A text agrees with a search when the text has each word of the search,\n * in any order. The case of the letters is not significant.\n */\n\n/** The small final sigma. Unicode case folding changes it to the small sigma, so a word agrees in each position. */\nconst FINAL_SIGMA = /ς/gu;\n\n/**\n * This function gives the search key of a text. `wordSearch` compares the keys of texts. The function does these steps:\n *\n * 1. It applies the Unicode normalization NFKC. Thus the ligature \"fi\" agrees with \"fi\", and full-width letters agree\n * with ASCII letters.\n * 2. It applies the full lower case, then the full upper case, then the full lower case. Thus \"ß\", \"ẞ\" and \"SS\" give\n * \"ss\".\n * 3. It changes the small final sigma \"ς\" to \"σ\", as Unicode case folding does.\n * 4. It applies NFKC again.\n *\n * The steps use no rules of a locale. Thus the Turkish \"İ\" gives \"i\" with a combining dot above (U+0307), and the\n * Turkish \"ı\" gives \"i\". The key of a key is the same key. Accents stay, so \"é\" does not agree with \"e\".\n *\n * @param text - The plain text.\n * @returns The search key of the text.\n */\nexport function searchKey(text: string): string {\n return text.normalize(\"NFKC\").toLowerCase().toUpperCase().toLowerCase().replace(FINAL_SIGMA, \"σ\").normalize(\"NFKC\");\n}\n\n/**\n * This function gives the test of a search. The function makes the key of the search with `searchKey`. Then it splits\n * the key into words at each run of white space, the class `\\s` of a regular expression.\n *\n * The test tells if the key of a text contains each word, in any order. A word can be a part of a longer word, so the\n * search \"art\" finds \"Party\". A search with no words agrees with each text.\n *\n * @param query - The search, as a person types it.\n * @returns The test of a text.\n */\nexport function wordSearch(query: string): (text: string) => boolean {\n const words = searchKey(query)\n .split(/\\s+/u)\n .filter((word) => word !== \"\");\n return (text) => words.length === 0 || words.every((word) => searchKey(text).includes(word));\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,SAAgB,QAAQ,OAAe,MAAc,SAAS,GAAG,KAAK,IAAY;CAChF,OAAO,GAAG,MAAM,eAAe,IAAI,EAAE,GAAG,UAAU,IAAI,OAAO;AAC/D;;;;;;;;;;;;;;;;;;;ACEA,SAAgB,aAAa,OAAwB;CACnD,IAAI;EACF,IAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,aAAa,OAAO;GACrE,MAAM,UAAmB,MAAM;GAC/B,IAAI,OAAO,YAAY,UAAU,OAAO;GACxC,IAAI,OAAO,UAAU,SAAS,KAAK,KAAK,MAAM,kBAAkB,OAAO,OAAO,OAAO;EACvF;EACA,OAAO,OAAO,KAAK;CACrB,QAAQ;EAEN,IAAI;GACF,OAAO,OAAO,UAAU,SAAS,KAAK,KAAK;EAC7C,QAAQ;GAEN,OAAO,IAAI,OAAO,MAAM;EAC1B;CACF;AACF;;;;;;;AC7BA,MAAM,cAAc;;;;;;;;;;;;;;;;;AAkBpB,SAAgB,UAAU,MAAsB;CAC9C,OAAO,KAAK,UAAU,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC,CAAC,YAAY,CAAC,CAAC,QAAQ,aAAa,GAAG,CAAC,CAAC,UAAU,MAAM;AACpH;;;;;;;;;;;AAYA,SAAgB,WAAW,OAA0C;CACnE,MAAM,QAAQ,UAAU,KAAK,CAAC,CAC3B,MAAM,MAAM,CAAC,CACb,QAAQ,SAAS,SAAS,EAAE;CAC/B,QAAQ,SAAS,MAAM,WAAW,KAAK,MAAM,OAAO,SAAS,UAAU,IAAI,CAAC,CAAC,SAAS,IAAI,CAAC;AAC7F"}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/text",
4
- "version": "0.0.4",
5
- "description": "Plain text helpers: the escapes for HTML, attributes, patterns, Markdown and workflow commands, English word forms (countOf), a word search (wordSearch) and the removal of secrets (redactSecrets).",
4
+ "version": "0.0.5",
5
+ "description": "Plain text helpers: the escapes for HTML, attributes, patterns, Markdown and workflow commands, English word forms (countOf), a word search (wordSearch), the text of a thrown value (errorMessage) and the removal of secrets (redactSecrets).",
6
6
  "keywords": [
7
7
  "text",
8
8
  "escape",
@@ -13,6 +13,7 @@
13
13
  "plural",
14
14
  "redact",
15
15
  "secrets",
16
+ "error",
16
17
  "typescript"
17
18
  ],
18
19
  "homepage": "https://github.com/mark1russell7/text#readme",
package/src/error.ts ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * This module has the text of a thrown value.
3
+ */
4
+
5
+ /**
6
+ * This function gives the text of a thrown value, for a log line or a message on a screen. It does not throw.
7
+ *
8
+ * - An object with a `message` text gives that text. The check reads the field, not `instanceof Error`, so an error
9
+ * from another realm (a `vm` context or an `iframe`) also gives its message.
10
+ * - An `Error` with a `message` that is not a text gives `String(message)`. The brand check of
11
+ * `Object.prototype.toString` finds an `Error` of each realm.
12
+ * - Each other value gives `String(value)`.
13
+ * - When `String` throws, the function gives the tag of `Object.prototype.toString`, for example `[object Object]`.
14
+ * - A revoked `Proxy` gives its `typeof` in brackets: `[object]`.
15
+ *
16
+ * @param error - The thrown value. It can be any value.
17
+ * @returns The text of the value.
18
+ */
19
+ export function errorMessage(error: unknown): string {
20
+ try {
21
+ if (typeof error === "object" && error !== null && "message" in error) {
22
+ const message: unknown = error.message;
23
+ if (typeof message === "string") return message;
24
+ if (Object.prototype.toString.call(error) === "[object Error]") return String(message);
25
+ }
26
+ return String(error);
27
+ } catch {
28
+ // String() throws for an object with no prototype, for a toString that throws and for a message getter that throws.
29
+ try {
30
+ return Object.prototype.toString.call(error);
31
+ } catch {
32
+ // A revoked Proxy throws also here. The operator typeof never throws.
33
+ return `[${typeof error}]`;
34
+ }
35
+ }
36
+ }
package/src/index.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  /**
2
- * The entry of `@mark1russell7/text`: plain text helpers. The groups are the escapes for HTML, attributes, regular
3
- * expressions, Markdown and GitHub Actions workflow commands, the English word forms and a word search. The entry
4
- * `@mark1russell7/text/redact` removes secrets from a text.
2
+ * The entry of `@mark1russell7/text`: plain text helpers. The first group is the escapes for HTML, attributes, regular
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.
5
5
  */
6
6
  export { countOf } from "./english.ts";
7
+ export { errorMessage } from "./error.ts";
7
8
  export { escapeAttribute, escapeHtml, escapeMarkdown, escapeRegExp, escapeWorkflowData, escapeWorkflowProperty } from "./escape.ts";
8
9
  export { searchKey, wordSearch } from "./search.ts";