necessary 13.0.6 → 13.0.8
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 +13 -11
- package/lib/constants.js +5 -1
- package/lib/utilities/string.js +11 -4
- package/package.json +1 -1
- package/src/constants.js +1 -0
- package/src/utilities/string.js +22 -8
package/README.md
CHANGED
|
@@ -911,11 +911,22 @@ Ideally the `host` argument should not include a trailing forward slash whereas
|
|
|
911
911
|
|
|
912
912
|
## String utilities
|
|
913
913
|
|
|
914
|
-
- `strcmp()`
|
|
915
914
|
- `strlen()`
|
|
915
|
+
- `strcmp()`
|
|
916
916
|
- `substring()`
|
|
917
917
|
|
|
918
|
-
|
|
918
|
+
String functions with support for Unicode. Specifically, characters in Unicode astral plains are counted twice in native string functions and methods whereas these functions effectively count astral Unicode characters only once.
|
|
919
|
+
|
|
920
|
+
Note that although the `strlen()` function uses a regular expression to count the number of astral Unicode characters and is therefore relatively fast, the other two functions convert strings to arrays and if necessary back again. Be warned that for this reason they are nowhere near as fast as their native counterparts.
|
|
921
|
+
|
|
922
|
+
* The `strlen()` function takes a single `string` argument. It works in much the same way as the `length` property of the `String` prototype, however it is Unicode safe:
|
|
923
|
+
|
|
924
|
+
```
|
|
925
|
+
"𝔸𝔹C".length = 5 // The 𝔹 and C characters are in an astral plane and count as two each.
|
|
926
|
+
|
|
927
|
+
strlen("𝔸𝔹C") = 3 // The string is converted to an array whose length is 3.
|
|
928
|
+
|
|
929
|
+
```
|
|
919
930
|
|
|
920
931
|
* The `strcmp` function takes `stringA` and `stringB` arguments. It compares them character by character in order to find the lexicographically lesser of the two. Its return value is the difference between the code points of the first differing characters, with the code point of either string given as zero if it is empty. Some examples should clarify:
|
|
921
932
|
|
|
@@ -939,15 +950,6 @@ Note that, conceptually speaking, the first argument is taken away from the seco
|
|
|
939
950
|
|
|
940
951
|
Note also that the double-struck `C` is in the basic multilingual plane and has code point `0x02102` whereas the double-struck `𝔸` is in an astral plane and has code point `0x1D538`, therefore their difference is positive.
|
|
941
952
|
|
|
942
|
-
* The `strlen()` function takes a single `string` argument. It works in much the same way as the `length` property of the `String` prototype, however it is Unicode safe:
|
|
943
|
-
|
|
944
|
-
```
|
|
945
|
-
"𝔸𝔹C".length = 5 // The 𝔹 and C characters are in an astral plane and count as two each.
|
|
946
|
-
|
|
947
|
-
strlen("𝔸𝔹C") = 3 // The string is converted to an array whose length is 3.
|
|
948
|
-
|
|
949
|
-
```
|
|
950
|
-
|
|
951
953
|
* The `substring()` function takes `string` and `start` arguments and an optional `end` argument. It works in much the same way as the `substring()` method of the `String` prototype, however it is Unicode safe:
|
|
952
954
|
|
|
953
955
|
```
|
package/lib/constants.js
CHANGED
|
@@ -36,10 +36,14 @@ _export(exports, {
|
|
|
36
36
|
STRING: function() {
|
|
37
37
|
return STRING;
|
|
38
38
|
},
|
|
39
|
+
UG: function() {
|
|
40
|
+
return UG;
|
|
41
|
+
},
|
|
39
42
|
ZERO: function() {
|
|
40
43
|
return ZERO;
|
|
41
44
|
}
|
|
42
45
|
});
|
|
46
|
+
var UG = "ug";
|
|
43
47
|
var ZERO = "0";
|
|
44
48
|
var DATA = "data";
|
|
45
49
|
var ERROR = "error";
|
|
@@ -51,4 +55,4 @@ var ENVIRONMENT = "ENVIRONMENT";
|
|
|
51
55
|
var EMPTY_STRING = "";
|
|
52
56
|
var PACKAGE_JSON = "package.json";
|
|
53
57
|
|
|
54
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
58
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uL3NyYy9jb25zdGFudHMuanMiXSwic291cmNlc0NvbnRlbnQiOlsiXCJ1c2Ugc3RyaWN0XCI7XG5cbmV4cG9ydCBjb25zdCBVRyA9IFwidWdcIjtcbmV4cG9ydCBjb25zdCBaRVJPID0gXCIwXCI7XG5leHBvcnQgY29uc3QgREFUQSA9IFwiZGF0YVwiO1xuZXhwb3J0IGNvbnN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ZUFBZTtBQUNyQixJQUFNSSxlQUFlIn0=
|
package/lib/utilities/string.js
CHANGED
|
@@ -48,6 +48,16 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
48
48
|
if (n === "Map" || n === "Set") return Array.from(n);
|
|
49
49
|
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
50
50
|
}
|
|
51
|
+
var astralCharactersRegExp = new RegExp("[\\u{10000}-\\u{10ffff}]", _constants.UG);
|
|
52
|
+
function strlen(string) {
|
|
53
|
+
var length = string.length;
|
|
54
|
+
var matches = string.match(astralCharactersRegExp);
|
|
55
|
+
if (matches !== null) {
|
|
56
|
+
var astralCharactersLength = matches.length;
|
|
57
|
+
length -= astralCharactersLength;
|
|
58
|
+
}
|
|
59
|
+
return length;
|
|
60
|
+
}
|
|
51
61
|
function strcmp(stringA, stringB) {
|
|
52
62
|
var difference;
|
|
53
63
|
var codePointA = stringA === _constants.EMPTY_STRING ? 0 : stringA.codePointAt(0), codePointB = stringB === _constants.EMPTY_STRING ? 0 : stringB.codePointAt(0);
|
|
@@ -59,9 +69,6 @@ function strcmp(stringA, stringB) {
|
|
|
59
69
|
}
|
|
60
70
|
return difference;
|
|
61
71
|
}
|
|
62
|
-
function strlen(string) {
|
|
63
|
-
return _to_consumable_array(string).length;
|
|
64
|
-
} ///
|
|
65
72
|
function substring(string, start, end) {
|
|
66
73
|
return _to_consumable_array(string).slice(start, end).join("");
|
|
67
74
|
} ///
|
|
@@ -71,4 +78,4 @@ var _default = {
|
|
|
71
78
|
substring: substring
|
|
72
79
|
};
|
|
73
80
|
|
|
74
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
81
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlsaXRpZXMvc3RyaW5nLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIlwidXNlIHN0c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FBU0QsT0FBT0MsTUFBTTtJQUUxQixJQUFNQyxVQUFVRixPQUFPRyxLQUFLLENBQUNOO0lBRTdCLElBQUlLLFlBQVksTUFBTTtRQUNwQixJQUFRRCxBQUFRRyx5QkFBMkJGLFFBQW5DRDtRQUVSQSxVQUFVRztJQUNaO0lBRUEsT0FBT0g7QUFDVDtBQUVPLFNBQVNQLE9BQU9XLE9BQU8sRUFBRUMsT0FBTztJQUNyQyxJQUFJQztJQUVKLElBQU1DLGFBQWEsQUFBQ0gsWUFBWUksdUJBQVksR0FDeEMsSUFDQUosUUFBUUssV0FBVyxDQUFDLElBQ3RCQyxhQUFhLEFBQUNMLFlBQVlHLHVCQUFZLEdBQ3BDLElBQ0FILFFBQVFJLFdBQVcsQ0FBQztJQUV4QkgsYUFBYUksYUFBYUg7SUFFMUIsSUFBSSxBQUFDRCxlQUFlLEtBQU9DLGVBQWMsS0FBT0csZUFBYyxHQUFJO1FBQ2hFTixVQUFVVCxVQUFVUyxTQUFTLElBQUksR0FBRztRQUVwQ0MsVUFBVVYsVUFBVVUsU0FBUyxJQUFJLEdBQUc7UUFFcENDLGFBQWFiLE9BQU9XLFNBQVNDO0lBQy9CO0lBRUEsT0FBT0M7QUFDVDtBQUVPLFNBQVNYLFVBQVVJLE1BQU0sRUFBRVksS0FBSyxFQUFFQyxHQUFHO0lBQUksT0FBTyxBQUFDLHFCQUFHYixRQUFRYyxLQUFLLENBQUNGLE9BQU9DLEtBQUtFLElBQUksQ0FBQztBQUFLLEVBQUcsR0FBRztJQUVyRyxXQUFlO0lBQ2JyQixRQUFBQTtJQUNBQyxRQUFBQTtJQUNBQyxXQUFBQTtBQUNGIn0=
|
package/package.json
CHANGED
package/src/constants.js
CHANGED
package/src/utilities/string.js
CHANGED
|
@@ -1,16 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import { EMPTY_STRING } from "../constants";
|
|
3
|
+
import { EMPTY_STRING, UG } from "../constants";
|
|
4
|
+
|
|
5
|
+
const astralCharactersRegExp = new RegExp("[\\u{10000}-\\u{10ffff}]", UG);
|
|
6
|
+
|
|
7
|
+
export function strlen(string) {
|
|
8
|
+
let length = string.length;
|
|
9
|
+
|
|
10
|
+
const matches = string.match(astralCharactersRegExp);
|
|
11
|
+
|
|
12
|
+
if (matches !== null) {
|
|
13
|
+
const { length: astralCharactersLength } = matches;
|
|
14
|
+
|
|
15
|
+
length -= astralCharactersLength;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return length;
|
|
19
|
+
}
|
|
4
20
|
|
|
5
21
|
export function strcmp(stringA, stringB) {
|
|
6
22
|
let difference;
|
|
7
23
|
|
|
8
24
|
const codePointA = (stringA === EMPTY_STRING) ?
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
25
|
+
0 :
|
|
26
|
+
stringA.codePointAt(0),
|
|
27
|
+
codePointB = (stringB === EMPTY_STRING) ?
|
|
28
|
+
0 :
|
|
29
|
+
stringB.codePointAt(0);
|
|
14
30
|
|
|
15
31
|
difference = codePointB - codePointA;
|
|
16
32
|
|
|
@@ -25,8 +41,6 @@ export function strcmp(stringA, stringB) {
|
|
|
25
41
|
return difference;
|
|
26
42
|
}
|
|
27
43
|
|
|
28
|
-
export function strlen(string) { return [...string].length; } ///
|
|
29
|
-
|
|
30
44
|
export function substring(string, start, end) { return [...string].slice(start, end).join(""); } ///
|
|
31
45
|
|
|
32
46
|
export default {
|