alltheutils 0.2.0 → 0.2.1
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/dist/index.cjs +68 -0
- package/dist/index.mjs +66 -1
- package/package.json +39 -40
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,70 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Splits the given string by grapheme rather than character.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} str
|
|
7
|
+
* @returns {string[]}
|
|
8
|
+
*/
|
|
9
|
+
function graphemes(str) {
|
|
10
|
+
return [...new Intl.Segmenter().segment(str)].map(({ segment }) => segment)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Converts the given value into a hash table (`Object`) using the given
|
|
15
|
+
* value as the value to each key (default: `true`).
|
|
16
|
+
*
|
|
17
|
+
* @param {any} data The data to derive the hash from
|
|
18
|
+
* @param {any} [value] The value to set each key derived from the data argument (default: `true`)
|
|
19
|
+
* @returns {object}
|
|
20
|
+
*/
|
|
21
|
+
function toHashTable(data, value = true) {
|
|
22
|
+
const hash = {};
|
|
23
|
+
|
|
24
|
+
if (Object.prototype.toString.call(data) === "[object Object]") {
|
|
25
|
+
for (const key of Object.keys(data)) {
|
|
26
|
+
hash[key] = value;
|
|
27
|
+
}
|
|
28
|
+
} else if (typeof data === "string") {
|
|
29
|
+
for (const char of graphemes(data)) {
|
|
30
|
+
hash[char] = value;
|
|
31
|
+
}
|
|
32
|
+
} else if (Array.isArray(data)) {
|
|
33
|
+
for (const item of data) {
|
|
34
|
+
hash[item] = value;
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
hash[data] = value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return hash
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Checks if the provided string **only** contains characters from the provided
|
|
45
|
+
* string of allowed characters.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} str
|
|
48
|
+
* @param {string} allowedChars
|
|
49
|
+
* @returns {boolean}
|
|
50
|
+
*/
|
|
51
|
+
function containsOnly(str, allowedChars) {
|
|
52
|
+
if (typeof str !== "string") throw new TypeError(`Expected str to be a string, instead got ${typeof str}`)
|
|
53
|
+
if (typeof allowedChars !== "string")
|
|
54
|
+
throw new TypeError(`Expected allowedChars to be a string, instead got ${typeof allowedChars}`)
|
|
55
|
+
if (!allowedChars.length)
|
|
56
|
+
throw new Error(`Expected allowedChars to have at least one character, instead got an empty string`)
|
|
57
|
+
|
|
58
|
+
str = graphemes(str);
|
|
59
|
+
allowedChars = toHashTable(allowedChars);
|
|
60
|
+
|
|
61
|
+
for (const char of str) {
|
|
62
|
+
if (!allowedChars[char]) return false
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true
|
|
66
|
+
}
|
|
67
|
+
|
|
3
68
|
const WHITESPACE = {
|
|
4
69
|
"\u0009": true, // CHARACTER_TABULATION
|
|
5
70
|
"\u000A": true, // LINE_FEED
|
|
@@ -74,5 +139,8 @@ function truncateWhitespace(str) {
|
|
|
74
139
|
return n
|
|
75
140
|
}
|
|
76
141
|
|
|
142
|
+
exports.containsOnly = containsOnly;
|
|
143
|
+
exports.graphemes = graphemes;
|
|
77
144
|
exports.isWhitespace = isWhitespace;
|
|
145
|
+
exports.toHashTable = toHashTable;
|
|
78
146
|
exports.truncateWhitespace = truncateWhitespace;
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits the given string by grapheme rather than character.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} str
|
|
5
|
+
* @returns {string[]}
|
|
6
|
+
*/
|
|
7
|
+
function graphemes(str) {
|
|
8
|
+
return [...new Intl.Segmenter().segment(str)].map(({ segment }) => segment)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Converts the given value into a hash table (`Object`) using the given
|
|
13
|
+
* value as the value to each key (default: `true`).
|
|
14
|
+
*
|
|
15
|
+
* @param {any} data The data to derive the hash from
|
|
16
|
+
* @param {any} [value] The value to set each key derived from the data argument (default: `true`)
|
|
17
|
+
* @returns {object}
|
|
18
|
+
*/
|
|
19
|
+
function toHashTable(data, value = true) {
|
|
20
|
+
const hash = {};
|
|
21
|
+
|
|
22
|
+
if (Object.prototype.toString.call(data) === "[object Object]") {
|
|
23
|
+
for (const key of Object.keys(data)) {
|
|
24
|
+
hash[key] = value;
|
|
25
|
+
}
|
|
26
|
+
} else if (typeof data === "string") {
|
|
27
|
+
for (const char of graphemes(data)) {
|
|
28
|
+
hash[char] = value;
|
|
29
|
+
}
|
|
30
|
+
} else if (Array.isArray(data)) {
|
|
31
|
+
for (const item of data) {
|
|
32
|
+
hash[item] = value;
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
hash[data] = value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return hash
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Checks if the provided string **only** contains characters from the provided
|
|
43
|
+
* string of allowed characters.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} str
|
|
46
|
+
* @param {string} allowedChars
|
|
47
|
+
* @returns {boolean}
|
|
48
|
+
*/
|
|
49
|
+
function containsOnly(str, allowedChars) {
|
|
50
|
+
if (typeof str !== "string") throw new TypeError(`Expected str to be a string, instead got ${typeof str}`)
|
|
51
|
+
if (typeof allowedChars !== "string")
|
|
52
|
+
throw new TypeError(`Expected allowedChars to be a string, instead got ${typeof allowedChars}`)
|
|
53
|
+
if (!allowedChars.length)
|
|
54
|
+
throw new Error(`Expected allowedChars to have at least one character, instead got an empty string`)
|
|
55
|
+
|
|
56
|
+
str = graphemes(str);
|
|
57
|
+
allowedChars = toHashTable(allowedChars);
|
|
58
|
+
|
|
59
|
+
for (const char of str) {
|
|
60
|
+
if (!allowedChars[char]) return false
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return true
|
|
64
|
+
}
|
|
65
|
+
|
|
1
66
|
const WHITESPACE = {
|
|
2
67
|
"\u0009": true, // CHARACTER_TABULATION
|
|
3
68
|
"\u000A": true, // LINE_FEED
|
|
@@ -72,4 +137,4 @@ function truncateWhitespace(str) {
|
|
|
72
137
|
return n
|
|
73
138
|
}
|
|
74
139
|
|
|
75
|
-
export { isWhitespace, truncateWhitespace };
|
|
140
|
+
export { containsOnly, graphemes, isWhitespace, toHashTable, truncateWhitespace };
|
package/package.json
CHANGED
|
@@ -1,41 +1,40 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
2
|
+
"name": "alltheutils",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "A utility library with functions I'd rather not recreate if I can help it.",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
"require": "./dist/index.cjs",
|
|
9
|
+
"import": "./dist/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"utility",
|
|
14
|
+
"utilities",
|
|
15
|
+
"util",
|
|
16
|
+
"utils",
|
|
17
|
+
"tools",
|
|
18
|
+
"lodash",
|
|
19
|
+
"underscore"
|
|
20
|
+
],
|
|
21
|
+
"author": "Jacob Lockett",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/jacoblockett/alltheutils.git"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist/",
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
34
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
35
|
+
"rollup": "^4.54.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "rollup -c"
|
|
39
|
+
}
|
|
40
|
+
}
|