@bhsd/common 0.4.6 → 0.5.0

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.js CHANGED
@@ -30,7 +30,6 @@ __export(src_exports, {
30
30
  });
31
31
  module.exports = __toCommonJS(src_exports);
32
32
  const CDN = "https://testingcf.jsdelivr.net";
33
- const hexColor = String.raw`#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\d_])`, rgbValue = String.raw`(?:\d*\.)?\d+%?`, hue = String.raw`(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw`rgba?\(\s*(?:${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw`hsla?\(\s*(?:${String.raw`${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${hue}${String.raw`\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`, reFull = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor}|${hslColor})`, "giu"), reRGB = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor})`, "giu");
34
33
  let span;
35
34
  if (typeof document === "object") {
36
35
  span = document.createElement("span");
@@ -46,7 +45,7 @@ const normalizeTitle = (title) => {
46
45
  };
47
46
  const numToHex = (d) => Math.round(d * 255).toString(16).padStart(2, "0");
48
47
  const splitColors = (str, hsl = true) => {
49
- const pieces = [], re = hsl ? reFull : reRGB;
48
+ const hexColor = String.raw`#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\d_])`, rgbValue = String.raw`(?:\d*\.)?\d+%?`, hue = String.raw`(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw`rgba?\(\s*(?:${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw`hsla?\(\s*(?:${String.raw`${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${hue}${String.raw`\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`, reFull = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor}|${hslColor})`, "giu"), reRGB = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor})`, "giu"), pieces = [], re = hsl ? reFull : reRGB;
50
49
  re.lastIndex = 0;
51
50
  let mt = re.exec(str), lastIndex = 0;
52
51
  while (mt) {
package/dist/index.mjs ADDED
@@ -0,0 +1,73 @@
1
+ const CDN = "https://testingcf.jsdelivr.net";
2
+ let span;
3
+ if (typeof document === "object") {
4
+ span = document.createElement("span");
5
+ }
6
+ const rawurldecode = (str) => decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, "%25"));
7
+ const normalizeTitle = (title) => {
8
+ const decoded = rawurldecode(title);
9
+ if (/[<>[\]|{}]/u.test(decoded)) {
10
+ return decoded;
11
+ }
12
+ span.innerHTML = decoded;
13
+ return span.textContent;
14
+ };
15
+ const numToHex = (d) => Math.round(d * 255).toString(16).padStart(2, "0");
16
+ const splitColors = (str, hsl = true) => {
17
+ const hexColor = String.raw`#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\d_])`, rgbValue = String.raw`(?:\d*\.)?\d+%?`, hue = String.raw`(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw`rgba?\(\s*(?:${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw`hsla?\(\s*(?:${String.raw`${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${hue}${String.raw`\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`, reFull = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor}|${hslColor})`, "giu"), reRGB = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor})`, "giu"), pieces = [], re = hsl ? reFull : reRGB;
18
+ re.lastIndex = 0;
19
+ let mt = re.exec(str), lastIndex = 0;
20
+ while (mt) {
21
+ const index = mt.index + mt[1].length;
22
+ if (index > lastIndex) {
23
+ pieces.push([str.slice(lastIndex, index), lastIndex, index, false]);
24
+ }
25
+ ({ lastIndex } = re);
26
+ pieces.push([mt[2], index, lastIndex, true]);
27
+ mt = re.exec(str);
28
+ }
29
+ if (str.length > lastIndex) {
30
+ pieces.push([str.slice(lastIndex), lastIndex, str.length, false]);
31
+ }
32
+ return pieces;
33
+ };
34
+ const loadScript = (src, globalConst, amd) => new Promise((resolve) => {
35
+ const path = `${CDN}/${src}`;
36
+ let obj = globalThis;
37
+ for (const prop of globalConst.split(".")) {
38
+ obj = obj?.[prop];
39
+ }
40
+ if (obj) {
41
+ resolve();
42
+ } else if (amd && typeof define === "function" && "amd" in define) {
43
+ const requirejs = globalThis.require;
44
+ requirejs.config({ paths: { [globalConst]: path } });
45
+ requirejs([globalConst], (exports) => {
46
+ Object.assign(globalThis, { [globalConst]: exports });
47
+ resolve();
48
+ });
49
+ } else {
50
+ const script = document.createElement("script");
51
+ script.src = path;
52
+ script.onload = () => {
53
+ resolve();
54
+ };
55
+ document.head.append(script);
56
+ }
57
+ });
58
+ const getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
59
+ const setObject = (key, value) => {
60
+ localStorage.setItem(key, JSON.stringify(value));
61
+ };
62
+ const parseVersion = (v) => v.split(".", 3).map(Number);
63
+ export {
64
+ CDN,
65
+ getObject,
66
+ loadScript,
67
+ normalizeTitle,
68
+ numToHex,
69
+ parseVersion,
70
+ rawurldecode,
71
+ setObject,
72
+ splitColors
73
+ };
package/package.json CHANGED
@@ -1,18 +1,27 @@
1
1
  {
2
2
  "name": "@bhsd/common",
3
- "version": "0.4.6",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
+ "author": "Bhsd",
5
6
  "files": [
6
7
  "/dist/",
7
8
  "*.cjs"
8
9
  ],
9
- "main": "./dist/index.js",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js"
15
+ },
16
+ "./*.cjs": "./*.cjs"
17
+ },
10
18
  "types": "./dist/index.d.ts",
11
19
  "bin": {
12
20
  "copy-dev": "bin/dev.cjs"
13
21
  },
14
22
  "scripts": {
15
- "build": "tsc --emitDeclarationOnly && esbuild src/index.ts --charset=utf8 --target=es2023 --format=cjs --outfile=dist/index.js",
23
+ "prepublishOnly": "npm run build",
24
+ "build": "tsc --emitDeclarationOnly && esbuild src/index.ts --charset=utf8 --target=es2023 --format=cjs --outfile=dist/index.js && esbuild src/index.ts --charset=utf8 --target=es2023 --format=esm --outfile=dist/index.mjs",
16
25
  "lint:ts": "tsc --noEmit && eslint --cache .",
17
26
  "lint": "npm run lint:ts"
18
27
  },