@ibodr/utils 0.1.1 → 0.1.2
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.d.ts +4 -19
- package/dist/index.mjs +2 -142
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2392,7 +2392,7 @@ declare function getIndices(n: number, start?: IndexKey): IndexKey[];
|
|
|
2392
2392
|
*/
|
|
2393
2393
|
declare function sortByIndex<T extends {
|
|
2394
2394
|
index: IndexKey;
|
|
2395
|
-
}>(a: T, b: T):
|
|
2395
|
+
}>(a: T, b: T): 0 | 1 | -1;
|
|
2396
2396
|
/**
|
|
2397
2397
|
* Sort by index, or null.
|
|
2398
2398
|
* @param a - An object with an index property.
|
|
@@ -2401,7 +2401,7 @@ declare function sortByIndex<T extends {
|
|
|
2401
2401
|
*/
|
|
2402
2402
|
declare function sortByMaybeIndex<T extends {
|
|
2403
2403
|
index?: IndexKey | null;
|
|
2404
|
-
}>(a: T, b: T):
|
|
2404
|
+
}>(a: T, b: T): 0 | 1 | -1;
|
|
2405
2405
|
|
|
2406
2406
|
/**
|
|
2407
2407
|
* Retries an async operation with configurable attempt count, wait duration, and error filtering.
|
|
@@ -3028,23 +3028,8 @@ declare const isNativeStructuredClone: boolean;
|
|
|
3028
3028
|
declare const STRUCTURED_CLONE_OBJECT_PROTOTYPE: any;
|
|
3029
3029
|
|
|
3030
3030
|
/**
|
|
3031
|
-
* Registers a
|
|
3032
|
-
*
|
|
3033
|
-
* when multiple versions are loaded simultaneously.
|
|
3034
|
-
* @param name - The name of the tldraw library package (e.g., '\@ibodr/editor').
|
|
3035
|
-
* @param version - The semantic version string (e.g., '2.0.0').
|
|
3036
|
-
* @param modules - The module system being used ('esm' or 'cjs').
|
|
3037
|
-
* @returns void
|
|
3038
|
-
* @example
|
|
3039
|
-
* ```ts
|
|
3040
|
-
* // Register a library version during package initialization
|
|
3041
|
-
* registerDrawLibraryVersion('@ibodr/editor', '2.0.0', 'esm')
|
|
3042
|
-
* registerDrawLibraryVersion('@ibodr/tldraw', '2.0.0', 'esm')
|
|
3043
|
-
*
|
|
3044
|
-
* // If conflicting versions are detected, warnings will be logged:
|
|
3045
|
-
* registerDrawLibraryVersion('@ibodr/editor', '1.9.0', 'cjs')
|
|
3046
|
-
* // Console warning about version mismatch will appear
|
|
3047
|
-
* ```
|
|
3031
|
+
* Registers a draw library version.
|
|
3032
|
+
* Kept for package entry-point compatibility; version conflict warnings are disabled.
|
|
3048
3033
|
* @internal
|
|
3049
3034
|
*/
|
|
3050
3035
|
declare function registerDrawLibraryVersion(name?: string, version?: string, modules?: string): void;
|
package/dist/index.mjs
CHANGED
|
@@ -6,152 +6,12 @@ export { default as uniq } from 'lodash.uniq';
|
|
|
6
6
|
import { generateKeyBetween, generateNKeysBetween } from 'jittered-fractional-indexing';
|
|
7
7
|
|
|
8
8
|
// src/lib/version.ts
|
|
9
|
-
var DRAW_LIBRARY_VERSION_KEY = "__TLDRAW_LIBRARY_VERSIONS__";
|
|
10
|
-
function getLibraryVersions() {
|
|
11
|
-
if (globalThis[DRAW_LIBRARY_VERSION_KEY]) {
|
|
12
|
-
return globalThis[DRAW_LIBRARY_VERSION_KEY];
|
|
13
|
-
}
|
|
14
|
-
const info = {
|
|
15
|
-
versions: [],
|
|
16
|
-
didWarn: false,
|
|
17
|
-
scheduledNotice: null
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(globalThis, DRAW_LIBRARY_VERSION_KEY, {
|
|
20
|
-
value: info,
|
|
21
|
-
writable: false,
|
|
22
|
-
configurable: false,
|
|
23
|
-
enumerable: false
|
|
24
|
-
});
|
|
25
|
-
return info;
|
|
26
|
-
}
|
|
27
9
|
function registerDrawLibraryVersion(name, version, modules) {
|
|
28
10
|
if (!name || !version || !modules) {
|
|
29
11
|
{
|
|
30
12
|
throw new Error("Missing name/version/module system in built version of tldraw library");
|
|
31
13
|
}
|
|
32
14
|
}
|
|
33
|
-
const info = getLibraryVersions();
|
|
34
|
-
if (isNextjsDev()) {
|
|
35
|
-
const isDuplicate = info.versions.some(
|
|
36
|
-
(v) => v.name === name && v.version === version && v.modules === modules
|
|
37
|
-
);
|
|
38
|
-
if (isDuplicate) return;
|
|
39
|
-
}
|
|
40
|
-
info.versions.push({ name, version, modules });
|
|
41
|
-
if (!info.scheduledNotice) {
|
|
42
|
-
try {
|
|
43
|
-
info.scheduledNotice = setTimeout(() => {
|
|
44
|
-
info.scheduledNotice = null;
|
|
45
|
-
checkLibraryVersions(info);
|
|
46
|
-
}, 100);
|
|
47
|
-
} catch {
|
|
48
|
-
checkLibraryVersions(info);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function checkLibraryVersions(info) {
|
|
53
|
-
if (!info.versions.length) return;
|
|
54
|
-
if (info.didWarn) return;
|
|
55
|
-
const sorted = info.versions.sort((a, b) => compareVersions(a.version, b.version));
|
|
56
|
-
const latestVersion = sorted[sorted.length - 1].version;
|
|
57
|
-
const matchingVersions = /* @__PURE__ */ new Set();
|
|
58
|
-
const nonMatchingVersions = /* @__PURE__ */ new Map();
|
|
59
|
-
for (const lib of sorted) {
|
|
60
|
-
if (nonMatchingVersions.has(lib.name)) {
|
|
61
|
-
matchingVersions.delete(lib.name);
|
|
62
|
-
entry(nonMatchingVersions, lib.name, /* @__PURE__ */ new Set()).add(lib.version);
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
if (lib.version === latestVersion) {
|
|
66
|
-
matchingVersions.add(lib.name);
|
|
67
|
-
} else {
|
|
68
|
-
matchingVersions.delete(lib.name);
|
|
69
|
-
entry(nonMatchingVersions, lib.name, /* @__PURE__ */ new Set()).add(lib.version);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (nonMatchingVersions.size > 0) {
|
|
73
|
-
const message = [
|
|
74
|
-
`${format("[tldraw]", ["bold", "bgRed", "textWhite"])} ${format("You have multiple versions of tldraw libraries installed. This can lead to bugs and unexpected behavior.", ["textRed", "bold"])}`,
|
|
75
|
-
"",
|
|
76
|
-
`The latest version you have installed is ${format(`v${latestVersion}`, ["bold", "textBlue"])}. The following libraries are on the latest version:`,
|
|
77
|
-
...Array.from(matchingVersions, (name) => ` \u2022 \u2705 ${format(name, ["bold"])}`),
|
|
78
|
-
"",
|
|
79
|
-
`The following libraries are not on the latest version, or have multiple versions installed:`,
|
|
80
|
-
...Array.from(nonMatchingVersions, ([name, versions]) => {
|
|
81
|
-
const sortedVersions = Array.from(versions).sort(compareVersions).map((v) => format(`v${v}`, v === latestVersion ? ["textGreen"] : ["textRed"]));
|
|
82
|
-
return ` \u2022 \u274C ${format(name, ["bold"])} (${sortedVersions.join(", ")})`;
|
|
83
|
-
})
|
|
84
|
-
];
|
|
85
|
-
console.log(message.join("\n"));
|
|
86
|
-
info.didWarn = true;
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const potentialDuplicates = /* @__PURE__ */ new Map();
|
|
90
|
-
for (const lib of sorted) {
|
|
91
|
-
entry(potentialDuplicates, lib.name, { version: lib.version, modules: [] }).modules.push(
|
|
92
|
-
lib.modules
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
const duplicates = /* @__PURE__ */ new Map();
|
|
96
|
-
for (const [name, lib] of potentialDuplicates) {
|
|
97
|
-
if (lib.modules.length > 1) duplicates.set(name, lib);
|
|
98
|
-
}
|
|
99
|
-
if (duplicates.size > 0) {
|
|
100
|
-
const message = [
|
|
101
|
-
`${format("[tldraw]", ["bold", "bgRed", "textWhite"])} ${format("You have multiple instances of some tldraw libraries active. This can lead to bugs and unexpected behavior. ", ["textRed", "bold"])}`,
|
|
102
|
-
"",
|
|
103
|
-
"This usually means that your bundler is misconfigured, and is importing the same library multiple times - usually once as an ES Module, and once as a CommonJS module.",
|
|
104
|
-
"",
|
|
105
|
-
"The following libraries have been imported multiple times:",
|
|
106
|
-
...Array.from(duplicates, ([name, lib]) => {
|
|
107
|
-
const modules = lib.modules.map((m, i) => m === "esm" ? ` ${i + 1}. ES Modules` : ` ${i + 1}. CommonJS`).join("\n");
|
|
108
|
-
return ` \u2022 \u274C ${format(name, ["bold"])} v${lib.version}:
|
|
109
|
-
${modules}`;
|
|
110
|
-
}),
|
|
111
|
-
"",
|
|
112
|
-
"You should configure your bundler to only import one version of each library."
|
|
113
|
-
];
|
|
114
|
-
console.log(message.join("\n"));
|
|
115
|
-
info.didWarn = true;
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
function compareVersions(a, b) {
|
|
120
|
-
const aMatch = a.match(/^(\d+)\.(\d+)\.(\d+)(?:-(\w+))?$/);
|
|
121
|
-
const bMatch = b.match(/^(\d+)\.(\d+)\.(\d+)(?:-(\w+))?$/);
|
|
122
|
-
if (!aMatch || !bMatch) return a.localeCompare(b);
|
|
123
|
-
if (aMatch[1] !== bMatch[1]) return Number(aMatch[1]) - Number(bMatch[1]);
|
|
124
|
-
if (aMatch[2] !== bMatch[2]) return Number(aMatch[2]) - Number(bMatch[2]);
|
|
125
|
-
if (aMatch[3] !== bMatch[3]) return Number(aMatch[3]) - Number(bMatch[3]);
|
|
126
|
-
if (aMatch[4] && bMatch[4]) return aMatch[4].localeCompare(bMatch[4]);
|
|
127
|
-
if (aMatch[4]) return 1;
|
|
128
|
-
if (bMatch[4]) return -1;
|
|
129
|
-
return 0;
|
|
130
|
-
}
|
|
131
|
-
var formats = {
|
|
132
|
-
bold: "1",
|
|
133
|
-
textBlue: "94",
|
|
134
|
-
textRed: "31",
|
|
135
|
-
textGreen: "32",
|
|
136
|
-
bgRed: "41",
|
|
137
|
-
textWhite: "97"
|
|
138
|
-
};
|
|
139
|
-
function format(value, formatters = []) {
|
|
140
|
-
return `\x1B[${formatters.map((f) => formats[f]).join(";")}m${value}\x1B[m`;
|
|
141
|
-
}
|
|
142
|
-
function isNextjsDev() {
|
|
143
|
-
try {
|
|
144
|
-
return "__NEXT_DATA__" in globalThis;
|
|
145
|
-
} catch {
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
function entry(map, key, defaultValue) {
|
|
150
|
-
if (map.has(key)) {
|
|
151
|
-
return map.get(key);
|
|
152
|
-
}
|
|
153
|
-
map.set(key, defaultValue);
|
|
154
|
-
return defaultValue;
|
|
155
15
|
}
|
|
156
16
|
|
|
157
17
|
// src/lib/array.ts
|
|
@@ -225,7 +85,7 @@ function areArraysShallowEqual(arr1, arr2) {
|
|
|
225
85
|
return true;
|
|
226
86
|
}
|
|
227
87
|
function mergeArraysAndReplaceDefaults(key, customEntries, defaults) {
|
|
228
|
-
const overrideTypes = new Set(customEntries.map((
|
|
88
|
+
const overrideTypes = new Set(customEntries.map((entry) => entry[key]));
|
|
229
89
|
const result = [];
|
|
230
90
|
for (const defaultEntry of defaults) {
|
|
231
91
|
if (overrideTypes.has(defaultEntry[key])) continue;
|
|
@@ -2532,7 +2392,7 @@ function warnOnce(message) {
|
|
|
2532
2392
|
// src/index.ts
|
|
2533
2393
|
registerDrawLibraryVersion(
|
|
2534
2394
|
"@ibodr/utils",
|
|
2535
|
-
"0.1.
|
|
2395
|
+
"0.1.2",
|
|
2536
2396
|
"esm"
|
|
2537
2397
|
);
|
|
2538
2398
|
/*!
|