]> Lady’s Gitweb - Pisces/blob - string.js
Add string functions
[Pisces] / string.js
1 // ♓🌟 Piscēs ∷ string.js
2 // ====================================================================
3 //
4 // Copyright © 2022 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 /**
11 * Returns the result of converting the provided value to A·S·C·I·I
12 * lowercase.
13 */
14 export const asciiLowercase = ($) =>
15 `${$}`.replaceAll(
16 /[A-Z]/gu,
17 Function.prototype.call.bind(String.prototype.toLowerCase),
18 );
19
20 /**
21 * Returns the result of converting the provided value to A·S·C·I·I
22 * uppercase.
23 */
24 export const asciiUppercase = ($) =>
25 `${$}`.replaceAll(
26 /[a-z]/gu,
27 Function.prototype.call.bind(String.prototype.toUpperCase),
28 );
29
30 /**
31 * Returns the result of converting the provided value to a string of
32 * scalar values by replacing (unpaired) surrogate values with U+FFFD.
33 */
34 export const scalarValueString = ($) =>
35 String.fromCodePoint(
36 ...function* () {
37 for (const char of `${$}`) {
38 const scalar = char.codePointAt(0);
39 yield scalar >= 0xD800 && scalar <= 0xDFFF ? 0xFFFD : scalar;
40 }
41 }(),
42 );
43
44 /**
45 * Returns the result of splitting the provided value on A·S·C·I·I
46 * whitespace.
47 */
48 export const splitOnASCIIWhitespace = ($) =>
49 stripAndCollapseASCIIWhitespace($).split(" ");
50
51 /**
52 * Returns the result of splitting the provided value on commas,
53 * trimming A·S·C·I·I whitespace from the resulting tokens.
54 */
55 export const splitOnCommas = ($) =>
56 stripLeadingAndTrailingASCIIWhitespace(
57 `${$}`.replaceAll(
58 /[\n\r\t\f ]*,[\n\r\t\f ]*/gu,
59 ",",
60 ),
61 ).split(",");
62
63 /**
64 * Returns the result of stripping leading and trailing A·S·C·I·I
65 * whitespace from the provided value.
66 */
67 export const stripLeadingAndTrailingASCIIWhitespace = ($) =>
68 /^[\n\r\t\f ]*([^]*?)[\n\r\t\f ]*$/u.exec($)[1];
69
70 /**
71 * Returns the result of stripping leading and trailing A·S·C·I·I
72 * whitespace from the provided value and collapsing other A·S·C·I·I
73 * whitespace in the provided value.
74 */
75 export const stripAndCollapseASCIIWhitespace = ($) =>
76 stripLeadingAndTrailingASCIIWhitespace(
77 `${$}`.replaceAll(
78 /[\n\r\t\f ]+/gu,
79 " ",
80 ),
81 );
This page took 0.137807 seconds and 5 git commands to generate.