@mark1russell7/text 0.0.8 → 0.0.9
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 +17 -0
- package/build/index.d.ts +2 -1
- package/build/index.d.ts.map +1 -1
- package/build/order.d.ts +18 -0
- package/build/order.d.ts.map +1 -0
- package/build/order.js +34 -0
- package/build/order.js.map +1 -0
- package/package.json +6 -2
- package/src/index.ts +2 -1
- package/src/order.ts +34 -0
package/README.md
CHANGED
|
@@ -53,6 +53,23 @@ wordSearch("istanbul")("İSTANBUL"); // false: "İ" keeps its dot as U+0307
|
|
|
53
53
|
searchKey("file"); // "file"
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
+
## The order of texts
|
|
57
|
+
|
|
58
|
+
The entry `@mark1russell7/text/order` has two orders of texts. Each order is the same in each locale and in each copy of a program. The name of each function tells its unit.
|
|
59
|
+
|
|
60
|
+
- `compareCodeUnits(a, b)` compares the UTF-16 code units. The operator `<`, the default sort and canonical JSON (RFC 8785) use this order.
|
|
61
|
+
- `compareCodePoints(a, b)` compares the Unicode code points. A surrogate pair is one code point above U+FFFF.
|
|
62
|
+
|
|
63
|
+
The two orders are different only in one case. At the first code point that is different, one text has a code point above U+FFFF. The other text has a lone surrogate or a code unit from U+E000 to U+FFFF at that place.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { compareCodePoints, compareCodeUnits } from "@mark1russell7/text/order";
|
|
67
|
+
|
|
68
|
+
["b", "a"].sort(compareCodeUnits); // ["a", "b"]
|
|
69
|
+
compareCodeUnits("😀", "~"); // -1: the high surrogate U+D83D is below U+FF5E
|
|
70
|
+
compareCodePoints("😀", "~"); // 1: U+1F600 is above U+FF5E
|
|
71
|
+
```
|
|
72
|
+
|
|
56
73
|
## The text of a thrown value
|
|
57
74
|
|
|
58
75
|
`errorMessage(error)` gives the text of a thrown value, for a log line or a message on a screen. It does not throw.
|
package/build/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
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
|
|
4
|
+
* search and the text of a thrown value. The entry `@mark1russell7/text/redact` removes secrets from a text, and the entry
|
|
5
|
+
* `@mark1russell7/text/order` has two orders of texts.
|
|
5
6
|
*/
|
|
6
7
|
/**
|
|
7
8
|
* This module has the English word forms.
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/english.ts","../src/error.ts","../src/escape.ts","../src/search.ts"],"mappings":"
|
|
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/order.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module has two orders of texts. Each order is the same in each locale and in each copy of a program. The name of
|
|
3
|
+
* each function tells its unit, with the unit names of `TextUnit`. A grapheme has no order of its own, so no function
|
|
4
|
+
* compares graphemes.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* This function compares two texts by their UTF-16 code units. It gives -1, 0 or 1. The operator `<` and the default
|
|
8
|
+
* `Array.prototype.sort` use this order. Canonical JSON (RFC 8785) sorts the keys of an object in this order.
|
|
9
|
+
*/
|
|
10
|
+
export declare function compareCodeUnits(a: string, b: string): number;
|
|
11
|
+
/**
|
|
12
|
+
* This function compares two texts by their Unicode code points. It gives -1, 0 or 1. A surrogate pair is one code point
|
|
13
|
+
* above U+FFFF, and a lone surrogate is its own code point. The two orders are different only in one case. At the first code
|
|
14
|
+
* point that is different, one text has a code point above U+FFFF. The other text has a lone surrogate or a code unit from
|
|
15
|
+
* U+E000 to U+FFFF at that place.
|
|
16
|
+
*/
|
|
17
|
+
export declare function compareCodePoints(a: string, b: string): number;
|
|
18
|
+
//# sourceMappingURL=order.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order.d.ts","names":[],"sources":["../src/order.ts"],"mappings":";;;;;;;;;wBAUgB,iBAAiB,WAAW;;;;;;;wBAa5B,kBAAkB,WAAW"}
|
package/build/order.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/order.ts
|
|
2
|
+
/**
|
|
3
|
+
* This module has two orders of texts. Each order is the same in each locale and in each copy of a program. The name of
|
|
4
|
+
* each function tells its unit, with the unit names of `TextUnit`. A grapheme has no order of its own, so no function
|
|
5
|
+
* compares graphemes.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* This function compares two texts by their UTF-16 code units. It gives -1, 0 or 1. The operator `<` and the default
|
|
9
|
+
* `Array.prototype.sort` use this order. Canonical JSON (RFC 8785) sorts the keys of an object in this order.
|
|
10
|
+
*/
|
|
11
|
+
function compareCodeUnits(a, b) {
|
|
12
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
13
|
+
}
|
|
14
|
+
const isHigh = (unit) => unit >= 55296 && unit <= 56319;
|
|
15
|
+
const isLow = (unit) => unit >= 56320 && unit <= 57343;
|
|
16
|
+
/**
|
|
17
|
+
* This function compares two texts by their Unicode code points. It gives -1, 0 or 1. A surrogate pair is one code point
|
|
18
|
+
* above U+FFFF, and a lone surrogate is its own code point. The two orders are different only in one case. At the first code
|
|
19
|
+
* point that is different, one text has a code point above U+FFFF. The other text has a lone surrogate or a code unit from
|
|
20
|
+
* U+E000 to U+FFFF at that place.
|
|
21
|
+
*/
|
|
22
|
+
function compareCodePoints(a, b) {
|
|
23
|
+
const end = Math.min(a.length, b.length);
|
|
24
|
+
let index = 0;
|
|
25
|
+
while (index < end && a.charCodeAt(index) === b.charCodeAt(index)) index += 1;
|
|
26
|
+
if (index === end) return a.length < b.length ? -1 : a.length > b.length ? 1 : 0;
|
|
27
|
+
if (index > 0 && isHigh(a.charCodeAt(index - 1)) && (isLow(a.charCodeAt(index)) || isLow(b.charCodeAt(index)))) index -= 1;
|
|
28
|
+
const x = a.codePointAt(index);
|
|
29
|
+
const y = b.codePointAt(index);
|
|
30
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
31
|
+
}
|
|
32
|
+
export { compareCodePoints, compareCodeUnits };
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=order.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order.js","names":[],"sources":["../src/order.ts"],"sourcesContent":["/**\n * This module has two orders of texts. Each order is the same in each locale and in each copy of a program. The name of\n * each function tells its unit, with the unit names of `TextUnit`. A grapheme has no order of its own, so no function\n * compares graphemes.\n */\n\n/**\n * This function compares two texts by their UTF-16 code units. It gives -1, 0 or 1. The operator `<` and the default\n * `Array.prototype.sort` use this order. Canonical JSON (RFC 8785) sorts the keys of an object in this order.\n */\nexport function compareCodeUnits(a: string, b: string): number {\n return a < b ? -1 : a > b ? 1 : 0;\n}\n\nconst isHigh = (unit: number): boolean => unit >= 0xd800 && unit <= 0xdbff;\nconst isLow = (unit: number): boolean => unit >= 0xdc00 && unit <= 0xdfff;\n\n/**\n * This function compares two texts by their Unicode code points. It gives -1, 0 or 1. A surrogate pair is one code point\n * above U+FFFF, and a lone surrogate is its own code point. The two orders are different only in one case. At the first code\n * point that is different, one text has a code point above U+FFFF. The other text has a lone surrogate or a code unit from\n * U+E000 to U+FFFF at that place.\n */\nexport function compareCodePoints(a: string, b: string): number {\n const end = Math.min(a.length, b.length);\n let index = 0;\n while (index < end && a.charCodeAt(index) === b.charCodeAt(index)) index += 1;\n if (index === end) return a.length < b.length ? -1 : a.length > b.length ? 1 : 0;\n // The first difference can be the low surrogate of a pair. Then the code point starts at the high surrogate before it.\n if (index > 0 && isHigh(a.charCodeAt(index - 1)) && (isLow(a.charCodeAt(index)) || isLow(b.charCodeAt(index)))) index -= 1;\n const x = a.codePointAt(index)!;\n const y = b.codePointAt(index)!;\n return x < y ? -1 : x > y ? 1 : 0;\n}\n"],"mappings":";;;;;;;;;;AAUA,SAAgB,iBAAiB,GAAW,GAAmB;CAC7D,OAAO,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;AAClC;AAEA,MAAM,UAAU,SAA0B,QAAQ,SAAU,QAAQ;AACpE,MAAM,SAAS,SAA0B,QAAQ,SAAU,QAAQ;;;;;;;AAQnE,SAAgB,kBAAkB,GAAW,GAAmB;CAC9D,MAAM,MAAM,KAAK,IAAI,EAAE,QAAQ,EAAE,MAAM;CACvC,IAAI,QAAQ;CACZ,OAAO,QAAQ,OAAO,EAAE,WAAW,KAAK,MAAM,EAAE,WAAW,KAAK,GAAG,SAAS;CAC5E,IAAI,UAAU,KAAK,OAAO,EAAE,SAAS,EAAE,SAAS,KAAK,EAAE,SAAS,EAAE,SAAS,IAAI;CAE/E,IAAI,QAAQ,KAAK,OAAO,EAAE,WAAW,QAAQ,CAAC,CAAC,MAAM,MAAM,EAAE,WAAW,KAAK,CAAC,KAAK,MAAM,EAAE,WAAW,KAAK,CAAC,IAAI,SAAS;CACzH,MAAM,IAAI,EAAE,YAAY,KAAK;CAC7B,MAAM,IAAI,EAAE,YAAY,KAAK;CAC7B,OAAO,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;AAClC"}
|
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.
|
|
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), the removal of secrets (redactSecrets) and the shapes of tokens (findSecretShapes, redactSecretShapes, findSecretShapesInValue), and lengths in code units, code points and graphemes with truncation and padding (textLength, truncate, pad), and the marked parts of a text (partsAt, partsOf).",
|
|
4
|
+
"version": "0.0.9",
|
|
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), the orders of texts by code units and by code points (compareCodeUnits, compareCodePoints), the removal of secrets (redactSecrets) and the shapes of tokens (findSecretShapes, redactSecretShapes, findSecretShapesInValue), and lengths in code units, code points and graphemes with truncation and padding (textLength, truncate, pad), and the marked parts of a text (partsAt, partsOf).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"text",
|
|
8
8
|
"escape",
|
|
@@ -52,6 +52,10 @@
|
|
|
52
52
|
"types": "./build/parts.d.ts",
|
|
53
53
|
"default": "./build/parts.js"
|
|
54
54
|
},
|
|
55
|
+
"./order": {
|
|
56
|
+
"types": "./build/order.d.ts",
|
|
57
|
+
"default": "./build/order.js"
|
|
58
|
+
},
|
|
55
59
|
"./package.json": "./package.json"
|
|
56
60
|
},
|
|
57
61
|
"files": [
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
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
|
|
4
|
+
* search and the text of a thrown value. The entry `@mark1russell7/text/redact` removes secrets from a text, and the entry
|
|
5
|
+
* `@mark1russell7/text/order` has two orders of texts.
|
|
5
6
|
*/
|
|
6
7
|
export { countOf } from "./english.ts";
|
|
7
8
|
export { errorMessage } from "./error.ts";
|
package/src/order.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module has two orders of texts. Each order is the same in each locale and in each copy of a program. The name of
|
|
3
|
+
* each function tells its unit, with the unit names of `TextUnit`. A grapheme has no order of its own, so no function
|
|
4
|
+
* compares graphemes.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* This function compares two texts by their UTF-16 code units. It gives -1, 0 or 1. The operator `<` and the default
|
|
9
|
+
* `Array.prototype.sort` use this order. Canonical JSON (RFC 8785) sorts the keys of an object in this order.
|
|
10
|
+
*/
|
|
11
|
+
export function compareCodeUnits(a: string, b: string): number {
|
|
12
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const isHigh = (unit: number): boolean => unit >= 0xd800 && unit <= 0xdbff;
|
|
16
|
+
const isLow = (unit: number): boolean => unit >= 0xdc00 && unit <= 0xdfff;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* This function compares two texts by their Unicode code points. It gives -1, 0 or 1. A surrogate pair is one code point
|
|
20
|
+
* above U+FFFF, and a lone surrogate is its own code point. The two orders are different only in one case. At the first code
|
|
21
|
+
* point that is different, one text has a code point above U+FFFF. The other text has a lone surrogate or a code unit from
|
|
22
|
+
* U+E000 to U+FFFF at that place.
|
|
23
|
+
*/
|
|
24
|
+
export function compareCodePoints(a: string, b: string): number {
|
|
25
|
+
const end = Math.min(a.length, b.length);
|
|
26
|
+
let index = 0;
|
|
27
|
+
while (index < end && a.charCodeAt(index) === b.charCodeAt(index)) index += 1;
|
|
28
|
+
if (index === end) return a.length < b.length ? -1 : a.length > b.length ? 1 : 0;
|
|
29
|
+
// The first difference can be the low surrogate of a pair. Then the code point starts at the high surrogate before it.
|
|
30
|
+
if (index > 0 && isHigh(a.charCodeAt(index - 1)) && (isLow(a.charCodeAt(index)) || isLow(b.charCodeAt(index)))) index -= 1;
|
|
31
|
+
const x = a.codePointAt(index)!;
|
|
32
|
+
const y = b.codePointAt(index)!;
|
|
33
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
34
|
+
}
|