@rokkit/core 1.0.0-next.119 → 1.0.0-next.120
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/constants.d.ts +1 -0
- package/dist/utils.d.ts +7 -0
- package/package.json +4 -3
- package/src/constants.js +1 -1
- package/src/utils.js +44 -0
package/dist/constants.d.ts
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -73,4 +73,11 @@ export function getSnippet(obj: Object, key: string, defaultSnippet?: null | Fun
|
|
|
73
73
|
* @return {string}
|
|
74
74
|
*/
|
|
75
75
|
export function hex2rgb(hex: string): string;
|
|
76
|
+
/**
|
|
77
|
+
* A utility function that detects if a string is an image URL or image data (base64)
|
|
78
|
+
*
|
|
79
|
+
* @param {string} str - The string to check
|
|
80
|
+
* @returns {string|null} - Returns the original string if it's an image URL or image data, otherwise null
|
|
81
|
+
*/
|
|
82
|
+
export function getImage(str: string): string | null;
|
|
76
83
|
export function importIcons(icons: Object): Object;
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokkit/core",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.120",
|
|
4
4
|
"description": "Contains core utility functions and classes that can be used in various components.",
|
|
5
5
|
"author": "Jerry Thomas <me@jerrythomas.name>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
8
|
-
"module": "src/index.js",
|
|
9
8
|
"publishConfig": {
|
|
10
9
|
"access": "public"
|
|
11
10
|
},
|
|
@@ -17,7 +16,9 @@
|
|
|
17
16
|
"files": [
|
|
18
17
|
"src/**/*.js",
|
|
19
18
|
"src/**/*.json",
|
|
20
|
-
"dist/**/*.d.ts"
|
|
19
|
+
"dist/**/*.d.ts",
|
|
20
|
+
"README.md",
|
|
21
|
+
"package.json"
|
|
21
22
|
],
|
|
22
23
|
"exports": {
|
|
23
24
|
"./src": "./src",
|
package/src/constants.js
CHANGED
package/src/utils.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { has, isNil } from 'ramda'
|
|
2
|
+
import { URL } from 'url'
|
|
3
|
+
import { DATA_IMAGE_REGEX } from './constants'
|
|
4
|
+
|
|
2
5
|
let idCounter = 0
|
|
3
6
|
/**
|
|
4
7
|
* Finds the closest ancestor of the given element that has the given attribute.
|
|
@@ -145,3 +148,44 @@ export function hex2rgb(hex) {
|
|
|
145
148
|
const [r, g, b] = hex.match(/\w\w/g).map((x) => parseInt(x, 16))
|
|
146
149
|
return `${r} ${g} ${b}`
|
|
147
150
|
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Checks if a string is a valid image URL
|
|
154
|
+
*
|
|
155
|
+
* @param {string} str - The string to check
|
|
156
|
+
* @returns {boolean} - Returns true if the string is an image URL
|
|
157
|
+
*/
|
|
158
|
+
function isImageUrl(str) {
|
|
159
|
+
// Check if the string is a URL
|
|
160
|
+
try {
|
|
161
|
+
const url = new URL(str)
|
|
162
|
+
|
|
163
|
+
// Check common image extensions
|
|
164
|
+
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg', '.tiff']
|
|
165
|
+
const path = url.pathname.toLowerCase()
|
|
166
|
+
|
|
167
|
+
if (imageExtensions.some((ext) => path.endsWith(ext))) {
|
|
168
|
+
return true
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return false
|
|
172
|
+
// eslint-disable-next-line no-unused-vars
|
|
173
|
+
} catch (e) {
|
|
174
|
+
// Not a valid URL
|
|
175
|
+
return false
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* A utility function that detects if a string is an image URL or image data (base64)
|
|
180
|
+
*
|
|
181
|
+
* @param {string} str - The string to check
|
|
182
|
+
* @returns {string|null} - Returns the original string if it's an image URL or image data, otherwise null
|
|
183
|
+
*/
|
|
184
|
+
export function getImage(str) {
|
|
185
|
+
if (DATA_IMAGE_REGEX.test(str)) return str
|
|
186
|
+
// Check if it's a URL
|
|
187
|
+
|
|
188
|
+
if (isImageUrl(str)) return str
|
|
189
|
+
|
|
190
|
+
return null
|
|
191
|
+
}
|